Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
Closed
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
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 @@ -207,6 +207,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 @@ -216,12 +222,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}`);
}));
});
});