Skip to content

Commit

Permalink
lib: ensure --check flag works with --require
Browse files Browse the repository at this point in the history
PR-URL: #19600
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
  • Loading branch information
jdalton authored and jasnell committed Apr 5, 2018
1 parent ae2b5bc commit 0876a03
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
4 changes: 4 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ Identical to `-e` but prints the result.
added:
- v5.0.0
- v4.2.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/19600
description: The `--require` option is now supported when checking a file.
-->

Syntax check the script without executing.
Expand Down
12 changes: 6 additions & 6 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@

const CJSModule = NativeModule.require('internal/modules/cjs/loader');

perf.markMilestone(NODE_PERFORMANCE_MILESTONE_MODULE_LOAD_END);
perf.markMilestone(
NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_START);
preloadModules();
perf.markMilestone(
NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_END);
// check if user passed `-c` or `--check` arguments to Node.
if (process._syntax_check_only != null) {
const fs = NativeModule.require('fs');
Expand All @@ -219,12 +225,6 @@
checkScriptSyntax(source, filename);
process.exit(0);
}
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_MODULE_LOAD_END);
perf.markMilestone(
NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_START);
preloadModules();
perf.markMilestone(
NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_END);
CJSModule.runMain();
} else {
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_MODULE_LOAD_START);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/no-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('module').wrapper = ['', ''];
23 changes: 23 additions & 0 deletions test/parallel/test-cli-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,26 @@ syntaxArgs.forEach(function(args) {
}));
});
});

// should work with -r flags
['-c', '--check'].forEach(function(checkFlag) {
['-r', '--require'].forEach(function(requireFlag) {
const preloadFile = fixtures.path('no-wrapper.js');
const file = fixtures.path('syntax', 'illegal_if_not_wrapped.js');
const args = [requireFlag, preloadFile, checkFlag, file];
const cmd = [node, ...args].join(' ');
exec(cmd, common.mustCall((err, stdout, stderr) => {
assert.strictEqual(err instanceof Error, true);
assert.strictEqual(err.code, 1);

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

// stderr should have a syntax error message
assert(syntaxErrorRE.test(stderr), `${syntaxErrorRE} === ${stderr}`);

// stderr should include the filename
assert(stderr.startsWith(file), `${stderr} starts with ${file}`);
}));
});
});

0 comments on commit 0876a03

Please sign in to comment.