diff --git a/src/parser/scalar.js b/src/parser/scalar.js index b7f8716f2..29d21a764 100644 --- a/src/parser/scalar.js +++ b/src/parser/scalar.js @@ -223,6 +223,12 @@ module.exports = { node = this.node("nowdoc"); value = this.next().text(); // strip the last line return char + if (this.lexer.heredoc_label.indentation > 0) { + value = value.substring( + 0, + value.length - this.lexer.heredoc_label.indentation + ); + } const lastCh = value[value.length - 1]; if (lastCh === "\n") { if (value[value.length - 2] === "\r") { @@ -432,6 +438,26 @@ module.exports = { while (this.token !== expect && this.token !== this.EOF) { value.push(this.read_encapsed_string_item(true)); } + if ( + value.length > 0 && + value[value.length - 1].kind === "encapsedpart" && + value[value.length - 1].expression.kind === "string" + ) { + const node = value[value.length - 1].expression; + const lastCh = node.value[node.value.length - 1]; + if (lastCh === "\n") { + if (node.value[node.value.length - 2] === "\r") { + // windows style + node.value = node.value.substring(0, node.value.length - 2); + } else { + // linux style + node.value = node.value.substring(0, node.value.length - 1); + } + } else if (lastCh === "\r") { + // mac style + node.value = node.value.substring(0, node.value.length - 1); + } + } this.expect(expect) && this.next(); const raw = this.lexer._input.substring( type === "heredoc" ? labelStart : start - 1, diff --git a/test/snapshot/__snapshots__/acid.test.js.snap b/test/snapshot/__snapshots__/acid.test.js.snap index 3bf92903f..a999751b5 100644 --- a/test/snapshot/__snapshots__/acid.test.js.snap +++ b/test/snapshot/__snapshots__/acid.test.js.snap @@ -7747,8 +7747,7 @@ BAZ "raw": " Hello world ", "unicode": false, - "value": " Hello world -", + "value": " Hello world", }, "kind": "encapsedpart", "loc": Location { @@ -7972,8 +7971,7 @@ FOO "raw": " return 'This is madness!'; ", "unicode": false, - "value": " return 'This is madness!'; -", + "value": " return 'This is madness!';", }, "kind": "encapsedpart", "loc": Location { diff --git a/test/snapshot/__snapshots__/heredoc.test.js.snap b/test/snapshot/__snapshots__/heredoc.test.js.snap index c984daa9e..6e1da84ff 100644 --- a/test/snapshot/__snapshots__/heredoc.test.js.snap +++ b/test/snapshot/__snapshots__/heredoc.test.js.snap @@ -27,8 +27,7 @@ Program { "unicode": false, "value": " a b -c -", +c", }, "kind": "encapsedpart", "syntax": null, @@ -72,8 +71,7 @@ c "unicode": false, "value": " a b -c -", +c", }, "kind": "encapsedpart", "syntax": null, @@ -126,8 +124,7 @@ Program { "raw": " a ", "unicode": false, - "value": " a -", + "value": " a", }, "kind": "encapsedpart", "syntax": null, @@ -201,8 +198,7 @@ Program { "raw": " a ", "unicode": false, - "value": " a -", + "value": " a", }, "kind": "encapsedpart", "syntax": null, @@ -321,8 +317,7 @@ Program { "unicode": false, "value": " b -c -", +c", }, "kind": "encapsedpart", "syntax": null, @@ -392,8 +387,7 @@ EOD "raw": "foobar! ", "unicode": false, - "value": "foobar! -", + "value": "foobar!", }, "kind": "encapsedpart", "syntax": null, @@ -450,8 +444,7 @@ FOOBAR", "raw": "Constant example ", "unicode": false, - "value": "Constant example -", + "value": "Constant example", }, "kind": "encapsedpart", "syntax": null, @@ -491,8 +484,7 @@ FOOBAR", "raw": "Property example ", "unicode": false, - "value": "Property example -", + "value": "Property example", }, "kind": "encapsedpart", "syntax": null, @@ -548,8 +540,7 @@ LABEL", "raw": "Nothing in here... ", "unicode": false, - "value": "Nothing in here... -", + "value": "Nothing in here...", }, "kind": "encapsedpart", "syntax": null, @@ -604,8 +595,7 @@ TEST", "raw": " ", "unicode": false, - "value": " -", + "value": "", }, "kind": "encapsedpart", "syntax": null, @@ -649,8 +639,7 @@ using heredoc syntax. "unicode": false, "value": "Example of string spanning multiple lines -using heredoc syntax. -", +using heredoc syntax.", }, "kind": "encapsedpart", "syntax": null, @@ -694,8 +683,7 @@ c "unicode": false, "value": " a b -c -", +c", }, "kind": "encapsedpart", "syntax": null, @@ -739,8 +727,7 @@ c "unicode": false, "value": " a b -c -", +c", }, "kind": "encapsedpart", "syntax": null, @@ -784,8 +771,7 @@ using heredoc syntax. "unicode": false, "value": "Example of string spanning multiple lines -using heredoc syntax. -", +using heredoc syntax.", }, "kind": "encapsedpart", "syntax": null, @@ -829,8 +815,7 @@ using heredoc syntax. "unicode": false, "value": "Example of string spanning multiple lines -using heredoc syntax. -", +using heredoc syntax.", }, "kind": "encapsedpart", "syntax": null, @@ -961,8 +946,7 @@ This should print a capital 'A': A ", "unicode": false, "value": ". -This should print a capital 'A': A -", +This should print a capital 'A': A", }, "kind": "encapsedpart", "syntax": null, diff --git a/test/snapshot/__snapshots__/location.test.js.snap b/test/snapshot/__snapshots__/location.test.js.snap index 96c2f0ee7..3698dbba3 100644 --- a/test/snapshot/__snapshots__/location.test.js.snap +++ b/test/snapshot/__snapshots__/location.test.js.snap @@ -4416,8 +4416,7 @@ EOD", "raw": "Text ", "unicode": false, - "value": "Text -", + "value": "Text", }, "kind": "encapsedpart", "loc": Location { @@ -4561,8 +4560,7 @@ EOD", "raw": "Text ", "unicode": false, - "value": "Text -", + "value": "Text", }, "kind": "encapsedpart", "loc": Location { @@ -4852,8 +4850,7 @@ command; "value": " command; command; -command; -", +command;", }, "kind": "encapsedpart", "loc": Location { diff --git a/test/snapshot/__snapshots__/loop.test.js.snap b/test/snapshot/__snapshots__/loop.test.js.snap index 17759a1a4..a1b44faf3 100644 --- a/test/snapshot/__snapshots__/loop.test.js.snap +++ b/test/snapshot/__snapshots__/loop.test.js.snap @@ -90,8 +90,7 @@ Program { "raw": " ", "unicode": false, - "value": " -", + "value": "", }, "kind": "encapsedpart", "syntax": null, @@ -196,8 +195,7 @@ Program { "raw": " ", "unicode": false, - "value": " -", + "value": "", }, "kind": "encapsedpart", "syntax": null, @@ -460,8 +458,7 @@ Program { "raw": " ", "unicode": false, - "value": " -", + "value": "", }, "kind": "encapsedpart", "syntax": null, @@ -538,8 +535,7 @@ Program { "raw": " ", "unicode": false, - "value": " -", + "value": "", }, "kind": "encapsedpart", "syntax": null, diff --git a/test/snapshot/__snapshots__/nowdoc.test.js.snap b/test/snapshot/__snapshots__/nowdoc.test.js.snap index a5341fa7e..4df507e4f 100644 --- a/test/snapshot/__snapshots__/nowdoc.test.js.snap +++ b/test/snapshot/__snapshots__/nowdoc.test.js.snap @@ -15,8 +15,7 @@ Program { END", "value": " a b -c -", +c", }, ], "kind": "echo", @@ -45,8 +44,7 @@ Program { "value": " a {$foo->bar[1]} b -c -", +c", }, ], "kind": "echo", diff --git a/test/snapshot/__snapshots__/property.test.js.snap b/test/snapshot/__snapshots__/property.test.js.snap index 082860b76..b6eb75c76 100644 --- a/test/snapshot/__snapshots__/property.test.js.snap +++ b/test/snapshot/__snapshots__/property.test.js.snap @@ -757,8 +757,7 @@ EOD", "raw": "hello world ", "unicode": false, - "value": "hello world -", + "value": "hello world", }, "kind": "encapsedpart", "syntax": null, diff --git a/test/snapshot/__snapshots__/string.test.js.snap b/test/snapshot/__snapshots__/string.test.js.snap index 1bfb70cdc..dc7c11e2e 100644 --- a/test/snapshot/__snapshots__/string.test.js.snap +++ b/test/snapshot/__snapshots__/string.test.js.snap @@ -1314,8 +1314,7 @@ Program { "kind": "string", "raw": "\\\\n", "unicode": false, - "value": " -", + "value": "", }, "kind": "encapsedpart", "syntax": null, @@ -2347,8 +2346,7 @@ Program { "kind": "string", "raw": "<\\\\n", "unicode": false, - "value": "< -", + "value": "<", }, "kind": "encapsedpart", "syntax": null, @@ -2926,8 +2924,7 @@ EOFX", "raw": " ", "unicode": false, - "value": " -", + "value": "", }, "kind": "encapsedpart", "syntax": null,