Skip to content

Commit

Permalink
Fix error when parsing .gyp file with a backslash
Browse files Browse the repository at this point in the history
Regarding [Unify node.gyp code styling · Issue #41072 · nodejs/node](nodejs/node#41072), in `node.gyp`, multi-line character strings are combined by inserting `\` before a line break.
That causes 'Unknown escape character' error in `gyp-parser`.

This PR is a bug fix on the `gyp-parser` side.
  • Loading branch information
asamuzaK committed Dec 4, 2021
1 parent 8de2e5e commit cca7a1c
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 1 deletion.
15 changes: 14 additions & 1 deletion index.js
Expand Up @@ -179,6 +179,8 @@ function parseString(input, at) {
let value = '';
while (input[at] !== type) {
if (input[at] === '\\') {
const cr = String.fromCodePoint(0x0D);
const lf = String.fromCodePoint(0x0A);
switch (input[++at]) {
case '"':
case "'":
Expand Down Expand Up @@ -208,7 +210,18 @@ function parseString(input, at) {
value += String.fromCodePoint(parseInt(hexString, 16));
at += 2;
break;
} default:
}
case cr:
if (input[at + 1] === lf) {
at += 2;
} else {
at++;
}
break;
case lf:
at++;
break;
default:
return [ new ParseError(input, at, 'Unknown escape character') ];
}
} else {
Expand Down
1 change: 1 addition & 0 deletions test/multiline_cr.in
@@ -0,0 +1 @@
{ 'foo': ['bar ' 'baz'], 'qux \quux': 'corge',}
Expand Down
1 change: 1 addition & 0 deletions test/multiline_cr.out
@@ -0,0 +1 @@
{ "foo": ["bar baz"], "qux quux": "corge"}
Expand Down
6 changes: 6 additions & 0 deletions test/multiline_crlf.in
@@ -0,0 +1,6 @@
{
'foo': ['bar '
'baz'],
'qux \
quux': 'corge',
}
4 changes: 4 additions & 0 deletions test/multiline_crlf.out
@@ -0,0 +1,4 @@
{
"foo": ["bar baz"],
"qux quux": "corge"
}
6 changes: 6 additions & 0 deletions test/multiline_lf.in
@@ -0,0 +1,6 @@
{
'foo': ['bar '
'baz'],
'qux \
quux': 'corge',
}
4 changes: 4 additions & 0 deletions test/multiline_lf.out
@@ -0,0 +1,4 @@
{
"foo": ["bar baz"],
"qux quux": "corge"
}

0 comments on commit cca7a1c

Please sign in to comment.