From 49341d0315b98001a615aa0707c4bafd841fd865 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 19 Feb 2022 02:00:21 -0500 Subject: [PATCH] Implement #1510: add test to catch when TS adds new `ModuleKind`s (#1650) * fix * fix * fix * fix --- src/index.ts | 9 ++++++--- src/test/index.spec.ts | 35 +++++++++++++++++++++++++++++++++++ src/transpilers/swc.ts | 2 +- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0544b2d98..bca7d3bde 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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(); diff --git a/src/test/index.spec.ts b/src/test/index.spec.ts index 8eef6bc0f..19c0913b2 100644 --- a/src/test/index.spec.ts +++ b/src/test/index.spec.ts @@ -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); +}); diff --git a/src/transpilers/swc.ts b/src/transpilers/swc.ts index fedc6a3af..23949595d 100644 --- a/src/transpilers/swc.ts +++ b/src/transpilers/swc.ts @@ -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'