Skip to content

Commit

Permalink
Implement #1510: add test to catch when TS adds new ModuleKinds (#1650
Browse files Browse the repository at this point in the history
)

* fix

* fix

* fix

* fix
  • Loading branch information
cspotcode committed Feb 19, 2022
1 parent 63a2f83 commit 49341d0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
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

0 comments on commit 49341d0

Please sign in to comment.