From d45df099083c3388679092eed4ae75bce26b695e Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 19 Feb 2022 01:12:22 -0500 Subject: [PATCH 1/4] fix --- src/index.ts | 9 ++++++--- src/test/index.spec.ts | 34 ++++++++++++++++++++++++++++++++++ src/transpilers/swc.ts | 2 +- 3 files changed, 41 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..22b6b9465 100644 --- a/src/test/index.spec.ts +++ b/src/test/index.spec.ts @@ -1267,3 +1267,37 @@ 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: string, 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); + check('6', 'ES2020', false); + check('7', 'ES2022', false); + try { + check('99', 'ESNext', true); + } catch { + // the value changed: is `99` now, but was `6` in TS 2.7 + check('6', 'ESNext', true); + } + 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' From f4069c10f29ba094bba8fea58f2f75602f7dfe22 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 19 Feb 2022 01:20:56 -0500 Subject: [PATCH 2/4] fix --- src/test/index.spec.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/test/index.spec.ts b/src/test/index.spec.ts index 22b6b9465..a998211fb 100644 --- a/src/test/index.spec.ts +++ b/src/test/index.spec.ts @@ -1271,31 +1271,31 @@ test('Falls back to transpileOnly when ts compiler returns emitSkipped', async ( 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: string, name: string, required: boolean) { + 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); + 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); - check('6', 'ES2020', false); - check('7', 'ES2022', false); + check(0, 'None', true); + check(1, 'CommonJS', true); + check(2, 'AMD', true); + check(3, 'UMD', true); + check(4, 'System', true); + check(5, 'ES2015', true); + check(6, 'ES2020', false); + check(7, 'ES2022', false); try { - check('99', 'ESNext', true); + check(99, 'ESNext', true); } catch { // the value changed: is `99` now, but was `6` in TS 2.7 - check('6', 'ESNext', true); + check(6, 'ESNext', true); } - check('100', 'Node12', false); - check('199', 'NodeNext', false); + check(100, 'Node12', false); + check(199, 'NodeNext', false); const actualKeys = Object.keys(ts.ModuleKind); actualKeys.sort(); foundKeys.sort(); From b8c6639f496bcfa54ef68ae6542c5da7d56fbf24 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 19 Feb 2022 01:22:34 -0500 Subject: [PATCH 3/4] fix --- src/test/index.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/index.spec.ts b/src/test/index.spec.ts index a998211fb..4f74abb59 100644 --- a/src/test/index.spec.ts +++ b/src/test/index.spec.ts @@ -1277,7 +1277,7 @@ test('Detect when typescript adds new ModuleKind values; flag as a failure so we expect(ts.ModuleKind[name]).toBeUndefined(); } else { expect(ts.ModuleKind[value]).toBe(name); - foundKeys.push(name, `${ value }`); + foundKeys.push(name, `${value}`); } } check(0, 'None', true); From 6db1dda88b9be74886c06b17bb6a20e53980f097 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 19 Feb 2022 01:33:43 -0500 Subject: [PATCH 4/4] fix --- src/test/index.spec.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/index.spec.ts b/src/test/index.spec.ts index 4f74abb59..19c0913b2 100644 --- a/src/test/index.spec.ts +++ b/src/test/index.spec.ts @@ -1286,14 +1286,15 @@ test('Detect when typescript adds new ModuleKind values; flag as a failure so we check(3, 'UMD', true); check(4, 'System', true); check(5, 'ES2015', true); - check(6, 'ES2020', false); - check(7, 'ES2022', false); 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);