lib: ensure --check flag works with --require

PR-URL: https://github.com/nodejs/node/pull/19600
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
This commit is contained in:
John-David Dalton 2018-03-25 11:02:49 -07:00 committed by James M Snell
parent ae2b5bcb7c
commit 0876a0314d
4 changed files with 34 additions and 6 deletions

View File

@ -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.

View File

@ -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');
@ -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);

1
test/fixtures/no-wrapper.js vendored Normal file
View File

@ -0,0 +1 @@
require('module').wrapper = ['', ''];

View File

@ -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}`);
}));
});
});