From 6f0e2091e8c476f48c2cfcc84253fc9e6107de92 Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Tue, 26 Sep 2023 09:12:16 -0700 Subject: [PATCH] Allow --input-type to be used with --experimental-type --- doc/api/packages.md | 2 +- doc/node.1 | 2 +- lib/internal/main/check_syntax.js | 3 ++- lib/internal/main/eval_stdin.js | 6 ++++-- lib/internal/main/eval_string.js | 5 +++-- src/node_options.cc | 5 ----- test/es-module/test-esm-type-flag-string-input.mjs | 14 ++++++++++++++ 7 files changed, 25 insertions(+), 12 deletions(-) diff --git a/doc/api/packages.md b/doc/api/packages.md index ef0b8bf8f1ad1d..2c3bf536970588 100644 --- a/doc/api/packages.md +++ b/doc/api/packages.md @@ -94,7 +94,7 @@ one module system or the other based on the value of the as CommonJS when the `package.json` file lacks a `"type"` field, regardless of `--experimental-type`, for backward compatibility.) -* Strings passed in as an argument to `--eval` or piped to `node` via `STDIN`. +* Strings passed in as an argument to `--eval` or piped to `node` via `STDIN`, when `--input-type` is unspecified. This flag currently defaults to `"commonjs"`, but it may change in the future to default to `"module"`. For this reason it is best to be explicit wherever diff --git a/doc/node.1 b/doc/node.1 index 0d64b62dca461d..e6e6c69b020964 100644 --- a/doc/node.1 +++ b/doc/node.1 @@ -179,7 +179,7 @@ Use this flag to enable ShadowRealm support. Enable code coverage in the test runner. . .It Fl -experimental-type Ns = Ns Ar type -Interpret as either ES modules or CommonJS modules input via --eval, --print or STDIN; +Interpret as either ES modules or CommonJS modules input via --eval or STDIN, when --input-type is unspecified; .js or extensionless files with no sibling or parent package.json; .js or extensionless files whose nearest parent package.json lacks a "type" field, unless under node_modules. . diff --git a/lib/internal/main/check_syntax.js b/lib/internal/main/check_syntax.js index dc9de45420f692..636ee049b9e922 100644 --- a/lib/internal/main/check_syntax.js +++ b/lib/internal/main/check_syntax.js @@ -60,7 +60,8 @@ function loadESMIfNeeded(cb) { async function checkSyntax(source, filename) { let isModule = true; if (filename === '[stdin]' || filename === '[eval]') { - isModule = getOptionValue('--input-type') === 'module' || getOptionValue('--experimental-type') === 'module'; + isModule = getOptionValue('--input-type') === 'module' || + (getOptionValue('--experimental-type') === 'module' && getOptionValue('--input-type') !== 'commonjs'); } else { const { defaultResolve } = require('internal/modules/esm/resolve'); const { defaultGetFormat } = require('internal/modules/esm/get_format'); diff --git a/lib/internal/main/eval_stdin.js b/lib/internal/main/eval_stdin.js index d457f4f3c1da8d..51f14be0e1afef 100644 --- a/lib/internal/main/eval_stdin.js +++ b/lib/internal/main/eval_stdin.js @@ -25,12 +25,14 @@ readStdin((code) => { const print = getOptionValue('--print'); const loadESM = getOptionValue('--import').length > 0; - if (getOptionValue('--input-type') === 'module' || getOptionValue('--experimental-type') === 'module') + if (getOptionValue('--input-type') === 'module' || + (getOptionValue('--experimental-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) { evalModule(code, print); - else + } else { evalScript('[stdin]', code, getOptionValue('--inspect-brk'), print, loadESM); + } }); diff --git a/lib/internal/main/eval_string.js b/lib/internal/main/eval_string.js index f2b89ead38db3f..90746047df6897 100644 --- a/lib/internal/main/eval_string.js +++ b/lib/internal/main/eval_string.js @@ -25,9 +25,10 @@ markBootstrapComplete(); const source = getOptionValue('--eval'); const print = getOptionValue('--print'); const loadESM = getOptionValue('--import').length > 0 || getOptionValue('--experimental-loader').length > 0; -if (getOptionValue('--input-type') === 'module' || getOptionValue('--experimental-type') === 'module') +if (getOptionValue('--input-type') === 'module' || + (getOptionValue('--experimental-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) { evalModule(source, print); -else { +} else { // For backward compatibility, we want the identifier crypto to be the // `node:crypto` module rather than WebCrypto. const isUsingCryptoIdentifier = diff --git a/src/node_options.cc b/src/node_options.cc index 77b705516af477..989d8727b19424 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -127,11 +127,6 @@ void EnvironmentOptions::CheckOptions(std::vector* errors, } } - if (!input_type.empty() && !type.empty()) { - errors->push_back("--input-type and --experimental-type cannot be used " - "together"); - } - if (syntax_check_only && has_eval_string) { errors->push_back("either --check or --eval can be used, not both"); } diff --git a/test/es-module/test-esm-type-flag-string-input.mjs b/test/es-module/test-esm-type-flag-string-input.mjs index 5895bdc549e259..7a7cb199bea74d 100644 --- a/test/es-module/test-esm-type-flag-string-input.mjs +++ b/test/es-module/test-esm-type-flag-string-input.mjs @@ -27,4 +27,18 @@ describe('the type flag should change the interpretation of string input', { con match((await child.stdout.toArray()).toString(), /^function\r?\n$/); }); + + it('should be overridden by --input-type', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ + '--experimental-type=module', + '--input-type=commonjs', + '--eval', + 'console.log(require("process").version)', + ]); + + strictEqual(stderr, ''); + strictEqual(stdout, `${process.version}\n`); + strictEqual(code, 0); + strictEqual(signal, null); + }); });