Skip to content

Commit 0876a03

Browse files
jdaltonjasnell
authored andcommitted
lib: ensure --check flag works with --require
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>
1 parent ae2b5bc commit 0876a03

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

doc/api/cli.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ Identical to `-e` but prints the result.
7575
added:
7676
- v5.0.0
7777
- v4.2.0
78+
changes:
79+
- version: REPLACEME
80+
pr-url: https://github.com/nodejs/node/pull/19600
81+
description: The `--require` option is now supported when checking a file.
7882
-->
7983

8084
Syntax check the script without executing.

lib/internal/bootstrap/node.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@
210210

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

213+
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_MODULE_LOAD_END);
214+
perf.markMilestone(
215+
NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_START);
216+
preloadModules();
217+
perf.markMilestone(
218+
NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_END);
213219
// check if user passed `-c` or `--check` arguments to Node.
214220
if (process._syntax_check_only != null) {
215221
const fs = NativeModule.require('fs');
@@ -219,12 +225,6 @@
219225
checkScriptSyntax(source, filename);
220226
process.exit(0);
221227
}
222-
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_MODULE_LOAD_END);
223-
perf.markMilestone(
224-
NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_START);
225-
preloadModules();
226-
perf.markMilestone(
227-
NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_END);
228228
CJSModule.runMain();
229229
} else {
230230
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_MODULE_LOAD_START);

test/fixtures/no-wrapper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('module').wrapper = ['', ''];

test/parallel/test-cli-syntax.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,26 @@ syntaxArgs.forEach(function(args) {
140140
}));
141141
});
142142
});
143+
144+
// should work with -r flags
145+
['-c', '--check'].forEach(function(checkFlag) {
146+
['-r', '--require'].forEach(function(requireFlag) {
147+
const preloadFile = fixtures.path('no-wrapper.js');
148+
const file = fixtures.path('syntax', 'illegal_if_not_wrapped.js');
149+
const args = [requireFlag, preloadFile, checkFlag, file];
150+
const cmd = [node, ...args].join(' ');
151+
exec(cmd, common.mustCall((err, stdout, stderr) => {
152+
assert.strictEqual(err instanceof Error, true);
153+
assert.strictEqual(err.code, 1);
154+
155+
// no stdout should be produced
156+
assert.strictEqual(stdout, '');
157+
158+
// stderr should have a syntax error message
159+
assert(syntaxErrorRE.test(stderr), `${syntaxErrorRE} === ${stderr}`);
160+
161+
// stderr should include the filename
162+
assert(stderr.startsWith(file), `${stderr} starts with ${file}`);
163+
}));
164+
});
165+
});

0 commit comments

Comments
 (0)