Skip to content

Commit

Permalink
plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress committed Feb 21, 2024
1 parent d102da7 commit 03a420d
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export default function (
let packageVersion: string | null;

function visit(node: Node): Node {
if (ts.isIdentifier(node) && node.getText() === identifier) {
if (
ts.isIdentifier(node) &&
!ts.isPropertyAccessExpression(node.parent) &&
node.getText() === identifier
) {
if (packageVersion === undefined) {
packageVersion = getPackageVersion(sourceFile.fileName);
}
Expand Down
3 changes: 3 additions & 0 deletions modules/dev-tools/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import './lib/utils.spec';
import './lib/configuration.spec';

import './ts-plugins/ts-transform-version-inline.spec';
import './ts-plugins/ts-transform-append-extension.spec';
51 changes: 51 additions & 0 deletions modules/dev-tools/test/ts-plugins/test-transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {Project, ts} from 'ts-morph';
import type {PluginConfig} from 'ts-patch';

/**
* Transpile ts code with TypeScript compiler API
*/
export function transpile({
source,
transformer,
config = {},
outputType = 'js'
}: {
source: string;
transformer: Function;
config?: PluginConfig;
outputType?: 'js' | 'd.ts';
}): string {
const dts = outputType === 'd.ts';

const project = new Project({
compilerOptions: {
target: ts.ScriptTarget.ESNext,
module: ts.ModuleKind.ESNext,
declaration: dts
}
});

project.createSourceFile('test.ts', source);

const customTransformers: ts.CustomTransformers = {};
const transform: ts.TransformerFactory<ts.SourceFile> = transformer(
project.getProgram(),
config,
{ts}
);
if (config.after) {
customTransformers.after = [transform];
} else if (config.afterDeclarations) {
// @ts-ignore
customTransformers.afterDeclarations = [transform];
} else {
customTransformers.before = [transform];
}

const result = project.emitToMemory({
customTransformers,
emitOnlyDtsFiles: dts
});

return result.getFiles()[0].text;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import test from 'tape-promise/tape';
import {transpile} from './test-transformer.js';
// @ts-expect-error Aliased import, remapped to valid path in esm-loader
import appendExtension from 'ocular-dev-tools/ts-plugins/ts-transform-append-extension';

const input = `\
export type { TypedArray } from "./types";
export { add } from "./math/add";
import vs from "../shaders/vs.glsl";
import { Shader } from "@luma.gl/core";
export { vs, Shader };`;

const testCases = [
{
title: 'add default extension to js imports',
config: {after: true},
output: `\
export { add } from "./math/add.js";
import vs from "../shaders/vs.glsl";
import { Shader } from "@luma.gl/core";
export { vs, Shader };`
},
{
title: 'add default extension to d.ts imports',
config: {afterDeclarations: true},
output: `\
export type { TypedArray } from "./types.js";
export { add } from "./math/add.js";
import vs from "../shaders/vs.glsl";
import { Shader } from "@luma.gl/core";
export { vs, Shader };`
},
{
title: 'add custom extension to js imports',
config: {after: true, extensions: ['.mjs', '.glsl.mjs']},
output: `\
export { add } from "./math/add.mjs";
import vs from "../shaders/vs.glsl.mjs";
import { Shader } from "@luma.gl/core";
export { vs, Shader };`
}
];

test('ts-transform-append-extension', (t) => {
for (const testCase of testCases) {
const result = transpile({
source: input,
transformer: appendExtension,
config: testCase.config,
outputType: testCase.config.afterDeclarations ? 'd.ts' : 'js'
});

t.is(result.trim(), testCase.output, testCase.title);
}

t.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import test from 'tape-promise/tape';
import {transpile} from './test-transformer.js';
// @ts-expect-error Aliased import, remapped to valid path in esm-loader
import versionInline from 'ocular-dev-tools/ts-plugins/ts-transform-version-inline';

const testCases = [
{
title: 'default identifier replaced',
config: {},
input: 'const version: string = __VERSION__;',
output: 'const version = "0.0.0";'
},
{
title: 'no matching identifier',
config: {},
input: 'const version: string = deck.__VERSION__;',
output: 'const version = deck.__VERSION__;'
},
{
title: 'custom identifier replaced',
config: {identifier: '__package_version'},
input: 'const version: string = __package_version;',
output: 'const version = "0.0.0";'
}
];

test('ts-transform-version-inline', (t) => {
for (const testCase of testCases) {
const result = transpile({
source: testCase.input,
transformer: versionInline,
config: testCase.config
});

t.is(result.trim(), testCase.output, testCase.title);
}

t.end();
});
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
"devDependencies": {
"@babel/plugin-syntax-import-assertions": "^7.20.0",
"pre-commit": "^1.2.2",
"pre-push": "^0.1.1"
},
"resolutions": {
"istanbul-lib-instrument": "4.0.3"
"pre-push": "^0.1.1",
"ts-morph": "^21.0.0"
},
"pre-commit": "pre-commit",
"pre-push": "pre-push",
Expand Down
48 changes: 47 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2394,6 +2394,16 @@
resolved "https://registry.yarnpkg.com/@tootallnate/quickjs-emscripten/-/quickjs-emscripten-0.23.0.tgz#db4ecfd499a9765ab24002c3b696d02e6d32a12c"
integrity sha512-C5Mc6rdnsaJDjO3UpGW/CQTHtCKaYlScZTly4JIu97Jxo/odCiH0ITnDXSJPTOrEKk/ycSZ0AOgTmkDtkOsvIA==

"@ts-morph/common@~0.22.0":
version "0.22.0"
resolved "https://registry.yarnpkg.com/@ts-morph/common/-/common-0.22.0.tgz#8951d451622a26472fbc3a227d6c3a90e687a683"
integrity sha512-HqNBuV/oIlMKdkLshXd1zKBqNQCsuPEsgQOkfFQ/eUKjRlwndXW1AjN9LVkBEIukm00gGXSRmfkl0Wv5VXLnlw==
dependencies:
fast-glob "^3.3.2"
minimatch "^9.0.3"
mkdirp "^3.0.1"
path-browserify "^1.0.1"

"@tsconfig/node10@^1.0.7":
version "1.0.9"
resolved "https://registry.yarnpkg.com/@tsconfig/node10/-/node10-1.0.9.tgz#df4907fc07a886922637b15e02d4cebc4c0021b2"
Expand Down Expand Up @@ -3445,6 +3455,11 @@ clone@^1.0.2:
resolved "https://registry.yarnpkg.com/clone/-/clone-1.0.4.tgz#da309cc263df15994c688ca902179ca3c7cd7c7e"
integrity sha512-JQHZ2QMW6l3aH/j6xCqQThY/9OH4D/9ls34cgkUBiEeocRTU04tHfKPBsUK1PqZCUQM7GiA0IIXJSuXHI64Kbg==

code-block-writer@^12.0.0:
version "12.0.0"
resolved "https://registry.yarnpkg.com/code-block-writer/-/code-block-writer-12.0.0.tgz#4dd58946eb4234105aff7f0035977b2afdc2a770"
integrity sha512-q4dMFMlXtKR3XNBHyMHt/3pwYNA69EDk00lloMOaaUMKPUXBw6lpXtbu3MMVG6/uOihGnRDOlkyqsONEUj60+w==

code-point-at@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"
Expand Down Expand Up @@ -4770,6 +4785,17 @@ fast-glob@^3.2.11, fast-glob@^3.2.9:
merge2 "^1.3.0"
micromatch "^4.0.4"

fast-glob@^3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.3.2.tgz#a904501e57cfdd2ffcded45e99a54fef55e46129"
integrity sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow==
dependencies:
"@nodelib/fs.stat" "^2.0.2"
"@nodelib/fs.walk" "^1.2.3"
glob-parent "^5.1.2"
merge2 "^1.3.0"
micromatch "^4.0.4"

fast-json-stable-stringify@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
Expand Down Expand Up @@ -6726,6 +6752,13 @@ minimatch@^5.0.1:
dependencies:
brace-expansion "^2.0.1"

minimatch@^9.0.3:
version "9.0.3"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.3.tgz#a6e00c3de44c3a542bfaae70abfc22420a6da825"
integrity sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==
dependencies:
brace-expansion "^2.0.1"

[email protected]:
version "4.1.0"
resolved "https://registry.yarnpkg.com/minimist-options/-/minimist-options-4.1.0.tgz#c0655713c53a8a2ebd77ffa247d342c40f010619"
Expand Down Expand Up @@ -6804,7 +6837,7 @@ mkdirp-promise@^5.0.1:
dependencies:
mkdirp "*"

mkdirp@*:
mkdirp@*, mkdirp@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-3.0.1.tgz#e44e4c5607fb279c168241713cc6e0fea9adcb50"
integrity sha512-+NsyUUAZDmo6YVHzL/stxSu3t9YS1iljliy3BSDrXJ/dkn1KYdmtZODGGjLcc9XLgVVpH4KshHB8XmZgMhaBXg==
Expand Down Expand Up @@ -7512,6 +7545,11 @@ pascalcase@^0.1.1:
resolved "https://registry.yarnpkg.com/pascalcase/-/pascalcase-0.1.1.tgz#b363e55e8006ca6fe21784d2db22bd15d7917f14"
integrity sha512-XHXfu/yOQRy9vYOtUDVMN60OEJjW013GoObG1o+xwQTpB9eYJX/BjXMsdW13ZDPruFhYYn0AG22w0xgQMwl3Nw==

path-browserify@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-1.0.1.tgz#d98454a9c3753d5790860f16f68867b9e46be1fd"
integrity sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==

path-dirname@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/path-dirname/-/path-dirname-1.0.2.tgz#cc33d24d525e099a5388c0336c6e32b9160609e0"
Expand Down Expand Up @@ -9130,6 +9168,14 @@ ts-api-utils@^1.0.1:
resolved "https://registry.yarnpkg.com/ts-api-utils/-/ts-api-utils-1.0.3.tgz#f12c1c781d04427313dbac808f453f050e54a331"
integrity sha512-wNMeqtMz5NtwpT/UZGY5alT+VoKdSsOOP/kqHFcUW1P/VRhH2wJ48+DN2WwUliNbQ976ETwDL0Ifd2VVvgonvg==

ts-morph@^21.0.0:
version "21.0.1"
resolved "https://registry.yarnpkg.com/ts-morph/-/ts-morph-21.0.1.tgz#712302a0f6e9dbf1aa8d9cf33a4386c4b18c2006"
integrity sha512-dbDtVdEAncKctzrVZ+Nr7kHpHkv+0JDJb2MjjpBaj8bFeCkePU9rHfMklmhuLFnpeq/EJZk2IhStY6NzqgjOkg==
dependencies:
"@ts-morph/common" "~0.22.0"
code-block-writer "^12.0.0"

ts-node@~10.9.0:
version "10.9.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.9.1.tgz#e73de9102958af9e1f0b168a6ff320e25adcff4b"
Expand Down

0 comments on commit 03a420d

Please sign in to comment.