Skip to content

Commit

Permalink
module: fix strip-types interaction with detect-module
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Aug 1, 2024
1 parent 8e1e3a8 commit 8565004
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/get_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
const { tsParse } = require('internal/modules/helpers');
const parsedSource = tsParse(source);
const detectedFormat = detectModuleFormat(parsedSource, url);
const format = detectedFormat ? `${detectedFormat}-typescript` : 'commonjs-typescript';
const format = detectedFormat ? `${detectedFormat}-typescript` : 'module-typescript';
if (format === 'module-typescript' && foundPackageJson) {
// This module has a .js extension, a package.json with no `type` field, and ESM syntax.
// Warn about the missing `type` field so that the user can avoid the performance penalty of detection.
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ function lazyLoadTSParser() {
}

function tsParse(source) {
if (!source || typeof source !== 'string') { return; }
if (!source || typeof source !== 'string') { return ''; }
const transformSync = lazyLoadTSParser();
const { code } = transformSync(source);
return code;
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/run_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function shouldUseESMLoader(mainPath) {
if (getOptionValue('--experimental-strip-types')) {
// This ensures that --experimental-default-type=commonjs and .mts files are treated as commonjs
if (getOptionValue('--experimental-default-type') === 'commonjs') { return false; }
if (mainPath && StringPrototypeEndsWith(mainPath, '.cts')) { return false; }
if (!mainPath || StringPrototypeEndsWith(mainPath, '.cts')) { return false; }
// This will likely change in the future to start with commonjs loader by default
if (mainPath && StringPrototypeEndsWith(mainPath, '.mts')) { return true; }
}
Expand Down
2 changes: 1 addition & 1 deletion test/es-module/test-typescript-commonjs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ test('execute a .cts file importing a .mts file export', async () => {
strictEqual(result.code, 0);
});

test('expect failure of a .cts file with default type module', async () => {
test('Runs a .cts file with default type module', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-default-type=module', // Keeps working with commonjs
Expand Down
2 changes: 0 additions & 2 deletions test/es-module/test-typescript-eval.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { test } from 'node:test';

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

test('eval TypeScript CommonJS syntax', async () => {
const result = await spawnPromisified(process.execPath, [
'--input-type=commonjs',
'--experimental-strip-types',
'--eval',
`const util = require('node:util');
Expand Down
26 changes: 24 additions & 2 deletions test/es-module/test-typescript-module.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ test('execute an .mts file importing an .mts file', async () => {
test('execute an .mts file importing a .ts file', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-default-type=module', // this should fail
'--no-warnings',
fixtures.path('typescript/mts/test-import-ts-file.mts'),
]);
Expand All @@ -42,7 +41,6 @@ test('execute an .mts file importing a .cts file', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--no-warnings',
'--no-warnings',
fixtures.path('typescript/mts/test-import-commonjs.mts'),
]);

Expand Down Expand Up @@ -95,3 +93,27 @@ test('execute a .ts file from node_modules', async () => {
strictEqual(result.stdout, '');
strictEqual(result.code, 1);
});

test('execute an empty .ts file', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--no-warnings',
fixtures.path('typescript/ts/test-empty-file.ts'),
]);

strictEqual(result.stderr, '');
strictEqual(result.stdout, '');
strictEqual(result.code, 0);
});

test('execute .ts file importing a module', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--no-warnings',
fixtures.path('typescript/ts/test-import-fs.ts'),
]);

strictEqual(result.stderr, '');
strictEqual(result.stdout, 'Hello, TypeScript!\n');
strictEqual(result.code, 0);
});
7 changes: 0 additions & 7 deletions test/es-module/test-typescript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ test('execute a TypeScript file', async () => {
test('execute a TypeScript file with imports', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-default-type=module',
'--no-warnings',
fixtures.path('typescript/ts/test-import-foo.ts'),
]);
Expand All @@ -30,7 +29,6 @@ test('execute a TypeScript file with imports', async () => {
test('execute a TypeScript file with node_modules', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-default-type=module',
'--no-warnings',
fixtures.path('typescript/ts/test-typescript-node-modules.ts'),
]);
Expand All @@ -43,7 +41,6 @@ test('execute a TypeScript file with node_modules', async () => {
test('expect error when executing a TypeScript file with imports with no extensions', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-default-type=module',
fixtures.path('typescript/ts/test-import-no-extension.ts'),
]);

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

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

test('execute CommonJS TypeScript file from node_modules with require-module', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-default-type=module',
'--experimental-strip-types',
fixtures.path('typescript/ts/test-import-ts-node-modules.ts'),
]);
Expand Down Expand Up @@ -218,7 +212,6 @@ test('execute a TypeScript file with CommonJS syntax requiring .mts with require
test('execute a TypeScript file with CommonJS syntax requiring .mts with require-module', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-default-type=commonjs',
'--no-warnings',
fixtures.path('typescript/ts/test-require-cts.ts'),
]);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/typescript/ts/test-empty-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 2 additions & 0 deletions test/fixtures/typescript/ts/test-import-fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import fs from 'fs';
console.log('Hello, TypeScript!');

0 comments on commit 8565004

Please sign in to comment.