Skip to content

Commit

Permalink
Allow --input-type to be used with --experimental-type
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth committed Sep 26, 2023
1 parent 21ac6c3 commit bf5c677
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -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 not specified;
.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.
.
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/main/check_syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 4 additions & 2 deletions lib/internal/main/eval_stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
5 changes: 3 additions & 2 deletions lib/internal/main/eval_string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
5 changes: 0 additions & 5 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ void EnvironmentOptions::CheckOptions(std::vector<std::string>* 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");
}
Expand Down
14 changes: 14 additions & 0 deletions test/es-module/test-esm-type-flag-string-input.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit bf5c677

Please sign in to comment.