Skip to content

Commit ad880ae

Browse files
committed
doc,test: clarify --eval usage for leading '-' scripts
Refs: #43397 Signed-off-by: jorge guerrero <grrr.jrg@gmail.com>
1 parent 2086d8c commit ad880ae

3 files changed

Lines changed: 8 additions & 47 deletions

File tree

doc/api/cli.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,9 @@ changes:
997997
Evaluate the following argument as JavaScript. The modules which are
998998
predefined in the REPL can also be used in `script`.
999999

1000+
If `script` starts with `-`, pass it using `=` (for example,
1001+
`node --print --eval=-42`) so it is parsed as the value of `--eval`.
1002+
10001003
On Windows, using `cmd.exe` a single quote will not work correctly because it
10011004
only recognizes double `"` for quoting. In Powershell or Git bash, both `'`
10021005
and `"` are usable.

src/node_options-inl.h

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -467,36 +467,7 @@ void OptionsParser<Options>::Parse(
467467

468468
value = args.pop_first();
469469

470-
bool is_eval_expression = false;
471-
if (name == "--eval" && !value.empty() && value[0] == '-') {
472-
std::string eval_value_name = value;
473-
const size_t equals_index = value.find('=');
474-
if (equals_index != std::string::npos) {
475-
eval_value_name = value.substr(0, equals_index);
476-
}
477-
478-
bool is_option_name =
479-
options_.contains(eval_value_name) ||
480-
aliases_.contains(eval_value_name) ||
481-
aliases_.contains(eval_value_name + "=") ||
482-
aliases_.contains(eval_value_name + " <arg>");
483-
484-
if (!is_option_name && eval_value_name.starts_with("--no-")) {
485-
const std::string non_negated_name =
486-
"--" + eval_value_name.substr(5);
487-
is_option_name =
488-
options_.contains(non_negated_name) ||
489-
aliases_.contains(non_negated_name) ||
490-
aliases_.contains(non_negated_name + "=") ||
491-
aliases_.contains(non_negated_name + " <arg>");
492-
}
493-
494-
is_eval_expression = !is_option_name;
495-
}
496-
497-
if (!value.empty() &&
498-
value[0] == '-' &&
499-
!is_eval_expression) {
470+
if (!value.empty() && value[0] == '-') {
500471
missing_argument();
501472
break;
502473
} else {

test/parallel/test-cli-eval.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,32 +115,19 @@ child.exec(...common.escapePOSIXShell`"${process.execPath}" -p "\\-42"`, common.
115115
assert.strictEqual(stderr, '');
116116
}));
117117

118-
// Unary-negative eval expressions should not be rejected as missing arguments.
119-
child.exec(...common.escapePOSIXShell`"${process.execPath}" -pe -42`, common.mustSucceed((stdout, stderr) => {
118+
// Eval expressions that start with '-' can be passed with '='.
119+
child.exec(...common.escapePOSIXShell`"${process.execPath}" --print --eval=-42`, common.mustSucceed((stdout, stderr) => {
120120
assert.strictEqual(stdout, '-42\n');
121121
assert.strictEqual(stderr, '');
122122
}));
123123

124124
// Edge case: negative zero should preserve its sign when printed.
125-
child.exec(...common.escapePOSIXShell`"${process.execPath}" -pe -0`, common.mustSucceed((stdout, stderr) => {
125+
child.exec(...common.escapePOSIXShell`"${process.execPath}" --print --eval=-0`, common.mustSucceed((stdout, stderr) => {
126126
assert.strictEqual(stdout, '-0\n');
127127
assert.strictEqual(stderr, '');
128128
}));
129129

130-
// Expressions like -NaN should be treated as eval input, not options.
131-
child.exec(...common.escapePOSIXShell`"${process.execPath}" -pe -NaN`, common.mustSucceed((stdout, stderr) => {
132-
assert.strictEqual(stdout, 'NaN\n');
133-
assert.strictEqual(stderr, '');
134-
}));
135-
136-
// A bare '-' should be passed to eval and fail with a syntax error.
137-
child.exec(...common.escapePOSIXShell`"${process.execPath}" -pe -`, common.mustCall((err, stdout, stderr) => {
138-
assert.notStrictEqual(err.code, 9);
139-
assert.strictEqual(stdout, '');
140-
assert.match(stderr, /SyntaxError/);
141-
}));
142-
143-
// Nearby-path safety: option-looking values should still be rejected.
130+
// Nearby-path safety: option-looking values should still be rejected with -e.
144131
child.exec(...common.escapePOSIXShell`"${process.execPath}" -e -p`, common.mustCall((err, stdout, stderr) => {
145132
assert.strictEqual(err.code, 9);
146133
assert.strictEqual(stdout, '');

0 commit comments

Comments
 (0)