Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6f0e209

Browse files
committedSep 26, 2023
Allow --input-type to be used with --experimental-type
1 parent 21ac6c3 commit 6f0e209

File tree

7 files changed

+25
-12
lines changed

7 files changed

+25
-12
lines changed
 

‎doc/api/packages.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ one module system or the other based on the value of the
9494
as CommonJS when the `package.json` file lacks a `"type"` field, regardless
9595
of `--experimental-type`, for backward compatibility.)
9696

97-
* Strings passed in as an argument to `--eval` or piped to `node` via `STDIN`.
97+
* Strings passed in as an argument to `--eval` or piped to `node` via `STDIN`, when `--input-type` is unspecified.
9898

9999
This flag currently defaults to `"commonjs"`, but it may change in the future to
100100
default to `"module"`. For this reason it is best to be explicit wherever

‎doc/node.1

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ Use this flag to enable ShadowRealm support.
179179
Enable code coverage in the test runner.
180180
.
181181
.It Fl -experimental-type Ns = Ns Ar type
182-
Interpret as either ES modules or CommonJS modules input via --eval, --print or STDIN;
182+
Interpret as either ES modules or CommonJS modules input via --eval or STDIN, when --input-type is unspecified;
183183
.js or extensionless files with no sibling or parent package.json;
184184
.js or extensionless files whose nearest parent package.json lacks a "type" field, unless under node_modules.
185185
.

‎lib/internal/main/check_syntax.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ function loadESMIfNeeded(cb) {
6060
async function checkSyntax(source, filename) {
6161
let isModule = true;
6262
if (filename === '[stdin]' || filename === '[eval]') {
63-
isModule = getOptionValue('--input-type') === 'module' || getOptionValue('--experimental-type') === 'module';
63+
isModule = getOptionValue('--input-type') === 'module' ||
64+
(getOptionValue('--experimental-type') === 'module' && getOptionValue('--input-type') !== 'commonjs');
6465
} else {
6566
const { defaultResolve } = require('internal/modules/esm/resolve');
6667
const { defaultGetFormat } = require('internal/modules/esm/get_format');

‎lib/internal/main/eval_stdin.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ readStdin((code) => {
2525

2626
const print = getOptionValue('--print');
2727
const loadESM = getOptionValue('--import').length > 0;
28-
if (getOptionValue('--input-type') === 'module' || getOptionValue('--experimental-type') === 'module')
28+
if (getOptionValue('--input-type') === 'module' ||
29+
(getOptionValue('--experimental-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) {
2930
evalModule(code, print);
30-
else
31+
} else {
3132
evalScript('[stdin]',
3233
code,
3334
getOptionValue('--inspect-brk'),
3435
print,
3536
loadESM);
37+
}
3638
});

‎lib/internal/main/eval_string.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ markBootstrapComplete();
2525
const source = getOptionValue('--eval');
2626
const print = getOptionValue('--print');
2727
const loadESM = getOptionValue('--import').length > 0 || getOptionValue('--experimental-loader').length > 0;
28-
if (getOptionValue('--input-type') === 'module' || getOptionValue('--experimental-type') === 'module')
28+
if (getOptionValue('--input-type') === 'module' ||
29+
(getOptionValue('--experimental-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) {
2930
evalModule(source, print);
30-
else {
31+
} else {
3132
// For backward compatibility, we want the identifier crypto to be the
3233
// `node:crypto` module rather than WebCrypto.
3334
const isUsingCryptoIdentifier =

‎src/node_options.cc

-5
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,6 @@ void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors,
127127
}
128128
}
129129

130-
if (!input_type.empty() && !type.empty()) {
131-
errors->push_back("--input-type and --experimental-type cannot be used "
132-
"together");
133-
}
134-
135130
if (syntax_check_only && has_eval_string) {
136131
errors->push_back("either --check or --eval can be used, not both");
137132
}

‎test/es-module/test-esm-type-flag-string-input.mjs

+14
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,18 @@ describe('the type flag should change the interpretation of string input', { con
2727

2828
match((await child.stdout.toArray()).toString(), /^function\r?\n$/);
2929
});
30+
31+
it('should be overridden by --input-type', async () => {
32+
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
33+
'--experimental-type=module',
34+
'--input-type=commonjs',
35+
'--eval',
36+
'console.log(require("process").version)',
37+
]);
38+
39+
strictEqual(stderr, '');
40+
strictEqual(stdout, `${process.version}\n`);
41+
strictEqual(code, 0);
42+
strictEqual(signal, null);
43+
});
3044
});

0 commit comments

Comments
 (0)
Please sign in to comment.