Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for TypeScript plugins #452

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 2 additions & 25 deletions modules/dev-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,9 @@
"ocular-test": "./scripts/test.js"
},
"scripts": {
"bootstrap": "yarn install-fast && ocular-bootstrap",
"clean": "echo No build needed",
"build": "(cd ../.. && ocular-build)",
"lint": "npm run lint-yarn",
"lint-yarn": "!(grep -q unpm.u yarn.lock) || (echo 'Please rebuild yarn file using public npmrc' && false)",
"publish-prod": "npm run build && npm run test && npm run test dist && npm publish",
"publish-beta": "npm run build && npm run test && npm run test dist && npm publish --tag beta",
"test": "echo Please help add tests"
"publish-prod": "npm run build && npm publish",
"publish-beta": "npm run build && npm publish --tag beta"
},
"dependencies": {
"@babel/cli": "^7.14.5",
Expand All @@ -75,15 +70,11 @@
"@probe.gl/test-utils": "^4.0.6",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"babel-loader": "8.2.2",
"babel-plugin-add-import-extension": "^1.6.0",
"babel-plugin-istanbul": "^6.0.0",
"babel-plugin-version-inline": "^1.0.0",
"c8": "^7.12.0",
"coveralls": "^3.0.3",
"deepmerge": "^4.2.2",
"esbuild": "^0.16.7",
"esbuild-plugin-babel": "git+https://github.com/Pessimistress/esbuild-plugin-babel.git#patch-1",
"esbuild-plugin-external-global": "^1.0.1",
"eslint": "^8.52.0",
"eslint-config-prettier": "^6.7.0",
Expand All @@ -107,23 +98,9 @@
"vite": "^4.0.1",
"vite-plugin-html": "^3.2.0"
},
"dependencies_pin_comments": [
"Note: tape 4.12 and higher no longer compares 0 and -0 equally",
"Note: prettier >2.3.0 breaks on typescript import type"
],
"dependencies-webpack5-todo": {
"webpack-bundle-analyzer": "^4.4.1",
"webpack": "5.38.1",
"webpack-cli": "4.7.0",
"webpack-dev-server": "4.0.0-beta.3"
},
"devDependencies": {
"math.gl": "^3.0.0",
"puppeteer": "^22.0.0"
},
"peerDependencies": {
"math.gl": "^3.0.0"
},
"engines": {
"node": ">= 18"
}
Expand Down
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
Loading
Loading