From cca7a1c088eaf28ceb167e8f81d6ff39a689b331 Mon Sep 17 00:00:00 2001 From: "asamuzaK (Kazz)" Date: Sat, 4 Dec 2021 13:03:13 +0900 Subject: [PATCH] Fix error when parsing .gyp file with a backslash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Regarding [Unify node.gyp code styling · Issue #41072 · nodejs/node](https://github.com/nodejs/node/issues/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. --- index.js | 15 ++++++++++++++- test/multiline_cr.in | 1 + test/multiline_cr.out | 1 + test/multiline_crlf.in | 6 ++++++ test/multiline_crlf.out | 4 ++++ test/multiline_lf.in | 6 ++++++ test/multiline_lf.out | 4 ++++ 7 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/multiline_cr.in create mode 100644 test/multiline_cr.out create mode 100644 test/multiline_crlf.in create mode 100644 test/multiline_crlf.out create mode 100644 test/multiline_lf.in create mode 100644 test/multiline_lf.out diff --git a/index.js b/index.js index a90c3cc..07c5750 100644 --- a/index.js +++ b/index.js @@ -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 "'": @@ -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 { diff --git a/test/multiline_cr.in b/test/multiline_cr.in new file mode 100644 index 0000000..0d3975a --- /dev/null +++ b/test/multiline_cr.in @@ -0,0 +1 @@ +{ 'foo': ['bar ' 'baz'], 'qux \ quux': 'corge', } \ No newline at end of file diff --git a/test/multiline_cr.out b/test/multiline_cr.out new file mode 100644 index 0000000..06ec905 --- /dev/null +++ b/test/multiline_cr.out @@ -0,0 +1 @@ +{ "foo": ["bar baz"], "qux quux": "corge" } \ No newline at end of file diff --git a/test/multiline_crlf.in b/test/multiline_crlf.in new file mode 100644 index 0000000..0f41ab0 --- /dev/null +++ b/test/multiline_crlf.in @@ -0,0 +1,6 @@ +{ + 'foo': ['bar ' + 'baz'], + 'qux \ +quux': 'corge', +} diff --git a/test/multiline_crlf.out b/test/multiline_crlf.out new file mode 100644 index 0000000..92236ac --- /dev/null +++ b/test/multiline_crlf.out @@ -0,0 +1,4 @@ +{ + "foo": ["bar baz"], + "qux quux": "corge" +} diff --git a/test/multiline_lf.in b/test/multiline_lf.in new file mode 100644 index 0000000..0f41ab0 --- /dev/null +++ b/test/multiline_lf.in @@ -0,0 +1,6 @@ +{ + 'foo': ['bar ' + 'baz'], + 'qux \ +quux': 'corge', +} diff --git a/test/multiline_lf.out b/test/multiline_lf.out new file mode 100644 index 0000000..92236ac --- /dev/null +++ b/test/multiline_lf.out @@ -0,0 +1,4 @@ +{ + "foo": ["bar baz"], + "qux quux": "corge" +}