Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
squash: ensure the filename is still used
  • Loading branch information
not-an-aardvark committed Apr 1, 2017
commit bc820d5636c452634817abeb5000d303d0e67b3c
8 changes: 4 additions & 4 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
// read the source
const filename = Module._resolveFilename(process.argv[1]);
var source = fs.readFileSync(filename, 'utf-8');
checkScriptSyntax(source);
checkScriptSyntax(source, filename);
process.exit(0);
}

Expand Down Expand Up @@ -178,7 +178,7 @@

process.stdin.on('end', function() {
if (process._syntax_check_only != null) {
checkScriptSyntax(code);
checkScriptSyntax(code, '[stdin]');
} else {
process._eval = code;
evalScript('[stdin]');
Expand Down Expand Up @@ -442,7 +442,7 @@
}
}

function checkScriptSyntax(source) {
function checkScriptSyntax(source, filename) {
const Module = NativeModule.require('module');
const vm = NativeModule.require('vm');
const internalModule = NativeModule.require('internal/module');
Expand All @@ -452,7 +452,7 @@
// wrap it
source = Module.wrap(source);
// compile the script, this will throw if it fails
new vm.Script(source, {displayErrors: true});
new vm.Script(source, {displayErrors: true, filename});
}

// Below you find a minimal module system, which is used to load the node
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-cli-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ const syntaxArgs = [
// no stdout should be produced
assert.strictEqual(c.stdout, '', 'stdout produced');

// stderr should include the filename
assert(c.stderr.startsWith(file), "stderr doesn't start with the filename");

// stderr should have a syntax error message
const match = c.stderr.match(/^SyntaxError: Unexpected identifier$/m);
assert(match, 'stderr incorrect');
Expand Down Expand Up @@ -95,3 +98,22 @@ syntaxArgs.forEach(function(args) {

assert.strictEqual(c.status, 0, 'code == ' + c.status);
});

// should should throw if code piped from stdin with --check has bad syntax
// loop each possible option, `-c` or `--check`
syntaxArgs.forEach(function(args) {
const stdin = 'var foo bar;';
const c = spawnSync(node, args, {encoding: 'utf8', input: stdin});

// stderr should include '[stdin]' as the filename
assert(c.stderr.startsWith('[stdin]'), "stderr doesn't start with [stdin]");

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

// stderr should have a syntax error message
const match = c.stderr.match(/^SyntaxError: Unexpected identifier$/m);
assert(match, 'stderr incorrect');

assert.strictEqual(c.status, 1, 'code == ' + c.status);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All ==s here should probably be ===, but that can be fixed while landing

});