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

Implement #1510: add test to catch when TS adds new ModuleKinds #1650

Merged
merged 4 commits into from Feb 19, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/index.ts
Expand Up @@ -1307,13 +1307,16 @@ export function create(rawOptions: CreateOptions = {}): Service {
config.options.module === ts.ModuleKind.CommonJS
? undefined
: createTranspileOnlyGetOutputFunction(ts.ModuleKind.CommonJS);
// [MUST_UPDATE_FOR_NEW_MODULEKIND]
const getOutputForceESM =
config.options.module === ts.ModuleKind.ES2015 ||
config.options.module === ts.ModuleKind.ES2020 ||
(ts.ModuleKind.ES2020 && config.options.module === ts.ModuleKind.ES2020) ||
(ts.ModuleKind.ES2022 && config.options.module === ts.ModuleKind.ES2022) ||
config.options.module === ts.ModuleKind.ESNext
? undefined
: createTranspileOnlyGetOutputFunction(
ts.ModuleKind.ES2020 || ts.ModuleKind.ES2015
: // [MUST_UPDATE_FOR_NEW_MODULEKIND]
createTranspileOnlyGetOutputFunction(
ts.ModuleKind.ES2022 || ts.ModuleKind.ES2020 || ts.ModuleKind.ES2015
);
const getOutputTranspileOnly = createTranspileOnlyGetOutputFunction();

Expand Down
35 changes: 35 additions & 0 deletions src/test/index.spec.ts
Expand Up @@ -1267,3 +1267,38 @@ test('Falls back to transpileOnly when ts compiler returns emitSkipped', async (
expect(err).toBe(null);
expect(stdout).toBe('foo\n');
});

test('Detect when typescript adds new ModuleKind values; flag as a failure so we can update our code flagged [MUST_UPDATE_FOR_NEW_MODULEKIND]', async () => {
// We have marked a few places in our code with MUST_UPDATE_FOR_NEW_MODULEKIND to make it easier to update them when TS adds new ModuleKinds
const foundKeys: string[] = [];
function check(value: number, name: string, required: boolean) {
if (required) expect(ts.ModuleKind[name]).toBe(value);
if (ts.ModuleKind[value] === undefined) {
expect(ts.ModuleKind[name]).toBeUndefined();
} else {
expect(ts.ModuleKind[value]).toBe(name);
foundKeys.push(name, `${value}`);
}
}
check(0, 'None', true);
check(1, 'CommonJS', true);
check(2, 'AMD', true);
check(3, 'UMD', true);
check(4, 'System', true);
check(5, 'ES2015', true);
try {
check(6, 'ES2020', false);
check(99, 'ESNext', true);
} catch {
// the value changed: is `99` now, but was `6` in TS 2.7
check(6, 'ESNext', true);
expect(ts.ModuleKind[99]).toBeUndefined();
}
check(7, 'ES2022', false);
check(100, 'Node12', false);
check(199, 'NodeNext', false);
const actualKeys = Object.keys(ts.ModuleKind);
actualKeys.sort();
foundKeys.sort();
expect(actualKeys).toEqual(foundKeys);
});
2 changes: 1 addition & 1 deletion src/transpilers/swc.ts
Expand Up @@ -77,7 +77,7 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
}
swcTarget = swcTargets[swcTargetIndex];
const keepClassNames = target! >= /* ts.ScriptTarget.ES2016 */ 3;
// swc only supports these 4x module options
// swc only supports these 4x module options [MUST_UPDATE_FOR_NEW_MODULEKIND]
const moduleType =
module === ModuleKind.CommonJS
? 'commonjs'
Expand Down