Skip to content

Commit 49341d0

Browse files
authored
Implement #1510: add test to catch when TS adds new ModuleKinds (#1650)
* fix * fix * fix * fix
1 parent 63a2f83 commit 49341d0

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1307,13 +1307,16 @@ export function create(rawOptions: CreateOptions = {}): Service {
13071307
config.options.module === ts.ModuleKind.CommonJS
13081308
? undefined
13091309
: createTranspileOnlyGetOutputFunction(ts.ModuleKind.CommonJS);
1310+
// [MUST_UPDATE_FOR_NEW_MODULEKIND]
13101311
const getOutputForceESM =
13111312
config.options.module === ts.ModuleKind.ES2015 ||
1312-
config.options.module === ts.ModuleKind.ES2020 ||
1313+
(ts.ModuleKind.ES2020 && config.options.module === ts.ModuleKind.ES2020) ||
1314+
(ts.ModuleKind.ES2022 && config.options.module === ts.ModuleKind.ES2022) ||
13131315
config.options.module === ts.ModuleKind.ESNext
13141316
? undefined
1315-
: createTranspileOnlyGetOutputFunction(
1316-
ts.ModuleKind.ES2020 || ts.ModuleKind.ES2015
1317+
: // [MUST_UPDATE_FOR_NEW_MODULEKIND]
1318+
createTranspileOnlyGetOutputFunction(
1319+
ts.ModuleKind.ES2022 || ts.ModuleKind.ES2020 || ts.ModuleKind.ES2015
13171320
);
13181321
const getOutputTranspileOnly = createTranspileOnlyGetOutputFunction();
13191322

src/test/index.spec.ts

+35
Original file line numberDiff line numberDiff line change
@@ -1267,3 +1267,38 @@ test('Falls back to transpileOnly when ts compiler returns emitSkipped', async (
12671267
expect(err).toBe(null);
12681268
expect(stdout).toBe('foo\n');
12691269
});
1270+
1271+
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 () => {
1272+
// 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
1273+
const foundKeys: string[] = [];
1274+
function check(value: number, name: string, required: boolean) {
1275+
if (required) expect(ts.ModuleKind[name]).toBe(value);
1276+
if (ts.ModuleKind[value] === undefined) {
1277+
expect(ts.ModuleKind[name]).toBeUndefined();
1278+
} else {
1279+
expect(ts.ModuleKind[value]).toBe(name);
1280+
foundKeys.push(name, `${value}`);
1281+
}
1282+
}
1283+
check(0, 'None', true);
1284+
check(1, 'CommonJS', true);
1285+
check(2, 'AMD', true);
1286+
check(3, 'UMD', true);
1287+
check(4, 'System', true);
1288+
check(5, 'ES2015', true);
1289+
try {
1290+
check(6, 'ES2020', false);
1291+
check(99, 'ESNext', true);
1292+
} catch {
1293+
// the value changed: is `99` now, but was `6` in TS 2.7
1294+
check(6, 'ESNext', true);
1295+
expect(ts.ModuleKind[99]).toBeUndefined();
1296+
}
1297+
check(7, 'ES2022', false);
1298+
check(100, 'Node12', false);
1299+
check(199, 'NodeNext', false);
1300+
const actualKeys = Object.keys(ts.ModuleKind);
1301+
actualKeys.sort();
1302+
foundKeys.sort();
1303+
expect(actualKeys).toEqual(foundKeys);
1304+
});

src/transpilers/swc.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
7777
}
7878
swcTarget = swcTargets[swcTargetIndex];
7979
const keepClassNames = target! >= /* ts.ScriptTarget.ES2016 */ 3;
80-
// swc only supports these 4x module options
80+
// swc only supports these 4x module options [MUST_UPDATE_FOR_NEW_MODULEKIND]
8181
const moduleType =
8282
module === ModuleKind.CommonJS
8383
? 'commonjs'

0 commit comments

Comments
 (0)