Skip to content

Commit 8565004

Browse files
module: fix strip-types interaction with detect-module
1 parent 8e1e3a8 commit 8565004

File tree

9 files changed

+31
-15
lines changed

9 files changed

+31
-15
lines changed

lib/internal/modules/esm/get_format.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
164164
const { tsParse } = require('internal/modules/helpers');
165165
const parsedSource = tsParse(source);
166166
const detectedFormat = detectModuleFormat(parsedSource, url);
167-
const format = detectedFormat ? `${detectedFormat}-typescript` : 'commonjs-typescript';
167+
const format = detectedFormat ? `${detectedFormat}-typescript` : 'module-typescript';
168168
if (format === 'module-typescript' && foundPackageJson) {
169169
// This module has a .js extension, a package.json with no `type` field, and ESM syntax.
170170
// Warn about the missing `type` field so that the user can avoid the performance penalty of detection.

lib/internal/modules/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ function lazyLoadTSParser() {
308308
}
309309

310310
function tsParse(source) {
311-
if (!source || typeof source !== 'string') { return; }
311+
if (!source || typeof source !== 'string') { return ''; }
312312
const transformSync = lazyLoadTSParser();
313313
const { code } = transformSync(source);
314314
return code;

lib/internal/modules/run_main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function shouldUseESMLoader(mainPath) {
8484
if (getOptionValue('--experimental-strip-types')) {
8585
// This ensures that --experimental-default-type=commonjs and .mts files are treated as commonjs
8686
if (getOptionValue('--experimental-default-type') === 'commonjs') { return false; }
87-
if (mainPath && StringPrototypeEndsWith(mainPath, '.cts')) { return false; }
87+
if (!mainPath || StringPrototypeEndsWith(mainPath, '.cts')) { return false; }
8888
// This will likely change in the future to start with commonjs loader by default
8989
if (mainPath && StringPrototypeEndsWith(mainPath, '.mts')) { return true; }
9090
}

test/es-module/test-typescript-commonjs.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ test('execute a .cts file importing a .mts file export', async () => {
118118
strictEqual(result.code, 0);
119119
});
120120

121-
test('expect failure of a .cts file with default type module', async () => {
121+
test('Runs a .cts file with default type module', async () => {
122122
const result = await spawnPromisified(process.execPath, [
123123
'--experimental-strip-types',
124124
'--experimental-default-type=module', // Keeps working with commonjs

test/es-module/test-typescript-eval.mjs

-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { test } from 'node:test';
44

55
test('eval TypeScript ESM syntax', async () => {
66
const result = await spawnPromisified(process.execPath, [
7-
'--input-type=module',
87
'--experimental-strip-types',
98
'--eval',
109
`import util from 'node:util'
@@ -18,7 +17,6 @@ test('eval TypeScript ESM syntax', async () => {
1817

1918
test('eval TypeScript CommonJS syntax', async () => {
2019
const result = await spawnPromisified(process.execPath, [
21-
'--input-type=commonjs',
2220
'--experimental-strip-types',
2321
'--eval',
2422
`const util = require('node:util');

test/es-module/test-typescript-module.mjs

+24-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ test('execute an .mts file importing an .mts file', async () => {
2828
test('execute an .mts file importing a .ts file', async () => {
2929
const result = await spawnPromisified(process.execPath, [
3030
'--experimental-strip-types',
31-
'--experimental-default-type=module', // this should fail
3231
'--no-warnings',
3332
fixtures.path('typescript/mts/test-import-ts-file.mts'),
3433
]);
@@ -42,7 +41,6 @@ test('execute an .mts file importing a .cts file', async () => {
4241
const result = await spawnPromisified(process.execPath, [
4342
'--experimental-strip-types',
4443
'--no-warnings',
45-
'--no-warnings',
4644
fixtures.path('typescript/mts/test-import-commonjs.mts'),
4745
]);
4846

@@ -95,3 +93,27 @@ test('execute a .ts file from node_modules', async () => {
9593
strictEqual(result.stdout, '');
9694
strictEqual(result.code, 1);
9795
});
96+
97+
test('execute an empty .ts file', async () => {
98+
const result = await spawnPromisified(process.execPath, [
99+
'--experimental-strip-types',
100+
'--no-warnings',
101+
fixtures.path('typescript/ts/test-empty-file.ts'),
102+
]);
103+
104+
strictEqual(result.stderr, '');
105+
strictEqual(result.stdout, '');
106+
strictEqual(result.code, 0);
107+
});
108+
109+
test('execute .ts file importing a module', async () => {
110+
const result = await spawnPromisified(process.execPath, [
111+
'--experimental-strip-types',
112+
'--no-warnings',
113+
fixtures.path('typescript/ts/test-import-fs.ts'),
114+
]);
115+
116+
strictEqual(result.stderr, '');
117+
strictEqual(result.stdout, 'Hello, TypeScript!\n');
118+
strictEqual(result.code, 0);
119+
});

test/es-module/test-typescript.mjs

-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ test('execute a TypeScript file', async () => {
1717
test('execute a TypeScript file with imports', async () => {
1818
const result = await spawnPromisified(process.execPath, [
1919
'--experimental-strip-types',
20-
'--experimental-default-type=module',
2120
'--no-warnings',
2221
fixtures.path('typescript/ts/test-import-foo.ts'),
2322
]);
@@ -30,7 +29,6 @@ test('execute a TypeScript file with imports', async () => {
3029
test('execute a TypeScript file with node_modules', async () => {
3130
const result = await spawnPromisified(process.execPath, [
3231
'--experimental-strip-types',
33-
'--experimental-default-type=module',
3432
'--no-warnings',
3533
fixtures.path('typescript/ts/test-typescript-node-modules.ts'),
3634
]);
@@ -43,7 +41,6 @@ test('execute a TypeScript file with node_modules', async () => {
4341
test('expect error when executing a TypeScript file with imports with no extensions', async () => {
4442
const result = await spawnPromisified(process.execPath, [
4543
'--experimental-strip-types',
46-
'--experimental-default-type=module',
4744
fixtures.path('typescript/ts/test-import-no-extension.ts'),
4845
]);
4946

@@ -101,7 +98,6 @@ test('execute a TypeScript file with type definition', async () => {
10198
test('execute a TypeScript file with type definition but no type keyword', async () => {
10299
const result = await spawnPromisified(process.execPath, [
103100
'--experimental-strip-types',
104-
'--experimental-default-type=module',
105101
fixtures.path('typescript/ts/test-import-no-type-keyword.ts'),
106102
]);
107103

@@ -124,7 +120,6 @@ test('execute a TypeScript file with CommonJS syntax', async () => {
124120
test('execute a TypeScript file with ES module syntax', async () => {
125121
const result = await spawnPromisified(process.execPath, [
126122
'--experimental-strip-types',
127-
'--experimental-default-type=module',
128123
'--no-warnings',
129124
fixtures.path('typescript/ts/test-module-typescript.ts'),
130125
]);
@@ -159,7 +154,6 @@ test('expect stack trace of a TypeScript file to be correct', async () => {
159154

160155
test('execute CommonJS TypeScript file from node_modules with require-module', async () => {
161156
const result = await spawnPromisified(process.execPath, [
162-
'--experimental-default-type=module',
163157
'--experimental-strip-types',
164158
fixtures.path('typescript/ts/test-import-ts-node-modules.ts'),
165159
]);
@@ -218,7 +212,6 @@ test('execute a TypeScript file with CommonJS syntax requiring .mts with require
218212
test('execute a TypeScript file with CommonJS syntax requiring .mts with require-module', async () => {
219213
const result = await spawnPromisified(process.execPath, [
220214
'--experimental-strip-types',
221-
'--experimental-default-type=commonjs',
222215
'--no-warnings',
223216
fixtures.path('typescript/ts/test-require-cts.ts'),
224217
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import fs from 'fs';
2+
console.log('Hello, TypeScript!');

0 commit comments

Comments
 (0)