Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/parser/scalar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions test/snapshot/__snapshots__/acid.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7747,8 +7747,7 @@ BAZ
"raw": " Hello world
",
"unicode": false,
"value": " Hello world
",
"value": " Hello world",
},
"kind": "encapsedpart",
"loc": Location {
Expand Down Expand Up @@ -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 {
Expand Down
48 changes: 16 additions & 32 deletions test/snapshot/__snapshots__/heredoc.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ Program {
"unicode": false,
"value": " a
b
c
",
c",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -72,8 +71,7 @@ c
"unicode": false,
"value": " a
b
c
",
c",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -126,8 +124,7 @@ Program {
"raw": " a
",
"unicode": false,
"value": " a
",
"value": " a",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -201,8 +198,7 @@ Program {
"raw": " a
",
"unicode": false,
"value": " a
",
"value": " a",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -321,8 +317,7 @@ Program {
"unicode": false,
"value": "
b
c
",
c",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -392,8 +387,7 @@ EOD
"raw": "foobar!
",
"unicode": false,
"value": "foobar!
",
"value": "foobar!",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -450,8 +444,7 @@ FOOBAR",
"raw": "Constant example
",
"unicode": false,
"value": "Constant example
",
"value": "Constant example",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -491,8 +484,7 @@ FOOBAR",
"raw": "Property example
",
"unicode": false,
"value": "Property example
",
"value": "Property example",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -548,8 +540,7 @@ LABEL",
"raw": "Nothing in here...
",
"unicode": false,
"value": "Nothing in here...
",
"value": "Nothing in here...",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -604,8 +595,7 @@ TEST",
"raw": "
",
"unicode": false,
"value": "
",
"value": "",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -694,8 +683,7 @@ c
"unicode": false,
"value": " a
b
c
",
c",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -739,8 +727,7 @@ c
"unicode": false,
"value": " a
b
c
",
c",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions test/snapshot/__snapshots__/location.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4416,8 +4416,7 @@ EOD",
"raw": "Text
",
"unicode": false,
"value": "Text
",
"value": "Text",
},
"kind": "encapsedpart",
"loc": Location {
Expand Down Expand Up @@ -4561,8 +4560,7 @@ EOD",
"raw": "Text
",
"unicode": false,
"value": "Text
",
"value": "Text",
},
"kind": "encapsedpart",
"loc": Location {
Expand Down Expand Up @@ -4852,8 +4850,7 @@ command;
"value": "
command;
command;
command;
",
command;",
},
"kind": "encapsedpart",
"loc": Location {
Expand Down
12 changes: 4 additions & 8 deletions test/snapshot/__snapshots__/loop.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ Program {
"raw": "
",
"unicode": false,
"value": "
",
"value": "",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -196,8 +195,7 @@ Program {
"raw": "
",
"unicode": false,
"value": "
",
"value": "",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -460,8 +458,7 @@ Program {
"raw": "
",
"unicode": false,
"value": "
",
"value": "",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -538,8 +535,7 @@ Program {
"raw": "
",
"unicode": false,
"value": "
",
"value": "",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down
6 changes: 2 additions & 4 deletions test/snapshot/__snapshots__/nowdoc.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ Program {
END",
"value": " a
b
c
",
c",
},
],
"kind": "echo",
Expand Down Expand Up @@ -45,8 +44,7 @@ Program {
"value": " a
{$foo->bar[1]}
b
c
",
c",
},
],
"kind": "echo",
Expand Down
3 changes: 1 addition & 2 deletions test/snapshot/__snapshots__/property.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -757,8 +757,7 @@ EOD",
"raw": "hello world
",
"unicode": false,
"value": "hello world
",
"value": "hello world",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down
9 changes: 3 additions & 6 deletions test/snapshot/__snapshots__/string.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1314,8 +1314,7 @@ Program {
"kind": "string",
"raw": "\\\\n",
"unicode": false,
"value": "
",
"value": "",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -2347,8 +2346,7 @@ Program {
"kind": "string",
"raw": "<\\\\n",
"unicode": false,
"value": "<
",
"value": "<",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -2926,8 +2924,7 @@ EOFX",
"raw": "
",
"unicode": false,
"value": "
",
"value": "",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down