Skip to content

Commit

Permalink
Switches to a basic json-like parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Maël Nison authored and addaleax committed Apr 17, 2019
1 parent 4025612 commit 1d3d149
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -677,14 +677,15 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv,
// [0] is expected to be the program name, fill it in from the real argv.
env_argv.push_back(argv->at(0));

bool is_in_string = false;
bool will_start_new_arg = true;
for (std::string::size_type index = 0;
index < node_options.size();
++index) {
char c = node_options.at(index);

// Backslashes escape the following character
if (c == '\\') {
if (c == '\\' && is_in_string) {
if (index + 1 == node_options.size()) {
fprintf(stderr,
"%s: invalid escaping for NODE_OPTIONS\n",
Expand All @@ -693,9 +694,12 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv,
} else {
c = node_options.at(++index);
}
} else if (c == ' ') {
} else if (c == ' ' && !is_in_string) {
will_start_new_arg = true;
continue;
} else if (c == '"') {
is_in_string = !is_in_string;
continue;
}

if (will_start_new_arg) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/print A.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('A');
console.log('A')
5 changes: 3 additions & 2 deletions test/parallel/test-cli-node-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ tmpdir.refresh();
const printA = require.resolve('../fixtures/printA.js');
const printSpaceA = require.resolve('../fixtures/print A.js');

expect(` -r ${printA} `, 'A\nB\n');
expect(`-r ${printA}`, 'A\nB\n');
expect(`-r ${printA.replace(/[a-z]/gi, '\\$&')}`, 'A\nB\n');
expect(`-r ${printSpaceA.replace(/[\\ ]/g, '\\$&')}`, 'A\nB\n');
expect(`-r ${JSON.stringify(printA)}`, 'A\nB\n');
expect(`-r ${JSON.stringify(printSpaceA)}`, 'A\nB\n');
expect(`-r ${printA} -r ${printA}`, 'A\nB\n');
expect(` -r ${printA} -r ${printA}`, 'A\nB\n');
expect(` --require ${printA} --require ${printA}`, 'A\nB\n');
Expand Down

0 comments on commit 1d3d149

Please sign in to comment.