Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/internal/main/check_syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,20 @@ function loadESMIfNeeded(cb) {
async function checkSyntax(source, filename) {
let format;
if (filename === '[stdin]' || filename === '[eval]') {
format = (getOptionValue('--input-type') === 'module') ? 'module' : 'commonjs';
const inputType = getOptionValue('--input-type');
if (inputType === 'module') {
format = 'module';
} else if (inputType === '' &&
getOptionValue('--experimental-detect-module')) {
// Without `--input-type`, code from stdin runs as ESM when it contains
// module syntax. Decide the goal the same way here. There are no
// CommonJS variables in scope for this code, as for `--eval`.
const { containsModuleSyntax } = internalBinding('contextify');
format = containsModuleSyntax(source, filename, null, 'no CJS variables') ?
'module' : 'commonjs';
} else {
format = 'commonjs';
}
} else {
const { defaultResolve } = require('internal/modules/esm/resolve');
const { defaultGetFormat } = require('internal/modules/esm/get_format');
Expand Down
46 changes: 46 additions & 0 deletions test/parallel/test-cli-syntax-piped-bad.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,49 @@ syntaxArgs.forEach(function(arg) {

assert.strictEqual(c.status, 1);
});

// Without --input-type, module syntax is detected, so the error reported is
// the one in the code and not the one for parsing it as CommonJS.
syntaxArgs.forEach(function(arg) {
const stdin = 'import "node:fs"; var foo bar;';
const c = spawnSync(
node,
['--no-warnings', arg],
{ encoding: 'utf8', input: stdin }
);

// stderr should include '[stdin]' as the filename
assert(c.stderr.startsWith('[stdin]'), `${c.stderr} starts with ${stdin}`);

// No stdout should be produced
assert.strictEqual(c.stdout, '');

// stderr should have a syntax error message
assert.match(c.stderr, syntaxErrorRE);

assert.strictEqual(c.status, 1);
});

// Module syntax is not detected with --input-type=commonjs or with
// --no-experimental-detect-module.
for (const flag of ['--input-type=commonjs', '--no-experimental-detect-module']) {
syntaxArgs.forEach(function(arg) {
const stdin = 'import "node:fs";';
const c = spawnSync(
node,
[flag, '--no-warnings', arg],
{ encoding: 'utf8', input: stdin }
);

// stderr should include '[stdin]' as the filename
assert(c.stderr.startsWith('[stdin]'), `${c.stderr} starts with ${stdin}`);

// No stdout should be produced
assert.strictEqual(c.stdout, '');

// stderr should have a syntax error message
assert.match(c.stderr, /^SyntaxError: Cannot use import statement outside a module/m);

assert.strictEqual(c.status, 1);
});
}
16 changes: 16 additions & 0 deletions test/parallel/test-cli-syntax-piped-good.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,19 @@ syntaxArgs.forEach(function(arg) {

assert.strictEqual(c.status, 0);
});

// Without --input-type, module syntax is detected as it is when the code runs.
syntaxArgs.forEach(function(arg) {
const stdin = 'import "node:fs"; throw new Error("should not get run");';
const c = spawnSync(
node,
['--no-warnings', arg],
{ encoding: 'utf8', input: stdin }
);

// No stdout or stderr should be produced
assert.strictEqual(c.stdout, '');
assert.strictEqual(c.stderr, '');

assert.strictEqual(c.status, 0);
});