Skip to content

Commit 6e372ba

Browse files
committed
fix: remove trailing newline in flexible heredoc/nowdoc value
1 parent c589b2a commit 6e372ba

File tree

8 files changed

+57
-62
lines changed

8 files changed

+57
-62
lines changed

src/parser/scalar.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ module.exports = {
223223
node = this.node("nowdoc");
224224
value = this.next().text();
225225
// strip the last line return char
226+
if (this.lexer.heredoc_label.indentation > 0) {
227+
value = value.substring(
228+
0,
229+
value.length - this.lexer.heredoc_label.indentation
230+
);
231+
}
226232
const lastCh = value[value.length - 1];
227233
if (lastCh === "\n") {
228234
if (value[value.length - 2] === "\r") {
@@ -432,6 +438,26 @@ module.exports = {
432438
while (this.token !== expect && this.token !== this.EOF) {
433439
value.push(this.read_encapsed_string_item(true));
434440
}
441+
if (
442+
value.length > 0 &&
443+
value[value.length - 1].kind === "encapsedpart" &&
444+
value[value.length - 1].expression.kind === "string"
445+
) {
446+
const node = value[value.length - 1].expression;
447+
const lastCh = node.value[node.value.length - 1];
448+
if (lastCh === "\n") {
449+
if (node.value[node.value.length - 2] === "\r") {
450+
// windows style
451+
node.value = node.value.substring(0, node.value.length - 2);
452+
} else {
453+
// linux style
454+
node.value = node.value.substring(0, node.value.length - 1);
455+
}
456+
} else if (lastCh === "\r") {
457+
// mac style
458+
node.value = node.value.substring(0, node.value.length - 1);
459+
}
460+
}
435461
this.expect(expect) && this.next();
436462
const raw = this.lexer._input.substring(
437463
type === "heredoc" ? labelStart : start - 1,

test/snapshot/__snapshots__/acid.test.js.snap

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7747,8 +7747,7 @@ BAZ
77477747
"raw": " Hello world
77487748
",
77497749
"unicode": false,
7750-
"value": " Hello world
7751-
",
7750+
"value": " Hello world",
77527751
},
77537752
"kind": "encapsedpart",
77547753
"loc": Location {
@@ -7972,8 +7971,7 @@ FOO
79727971
"raw": " return 'This is madness!';
79737972
",
79747973
"unicode": false,
7975-
"value": " return 'This is madness!';
7976-
",
7974+
"value": " return 'This is madness!';",
79777975
},
79787976
"kind": "encapsedpart",
79797977
"loc": Location {

test/snapshot/__snapshots__/heredoc.test.js.snap

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ Program {
2727
"unicode": false,
2828
"value": " a
2929
b
30-
c
31-
",
30+
c",
3231
},
3332
"kind": "encapsedpart",
3433
"syntax": null,
@@ -72,8 +71,7 @@ c
7271
"unicode": false,
7372
"value": " a
7473
b
75-
c
76-
",
74+
c",
7775
},
7876
"kind": "encapsedpart",
7977
"syntax": null,
@@ -126,8 +124,7 @@ Program {
126124
"raw": " a
127125
",
128126
"unicode": false,
129-
"value": " a
130-
",
127+
"value": " a",
131128
},
132129
"kind": "encapsedpart",
133130
"syntax": null,
@@ -201,8 +198,7 @@ Program {
201198
"raw": " a
202199
",
203200
"unicode": false,
204-
"value": " a
205-
",
201+
"value": " a",
206202
},
207203
"kind": "encapsedpart",
208204
"syntax": null,
@@ -321,8 +317,7 @@ Program {
321317
"unicode": false,
322318
"value": "
323319
b
324-
c
325-
",
320+
c",
326321
},
327322
"kind": "encapsedpart",
328323
"syntax": null,
@@ -392,8 +387,7 @@ EOD
392387
"raw": "foobar!
393388
",
394389
"unicode": false,
395-
"value": "foobar!
396-
",
390+
"value": "foobar!",
397391
},
398392
"kind": "encapsedpart",
399393
"syntax": null,
@@ -450,8 +444,7 @@ FOOBAR",
450444
"raw": "Constant example
451445
",
452446
"unicode": false,
453-
"value": "Constant example
454-
",
447+
"value": "Constant example",
455448
},
456449
"kind": "encapsedpart",
457450
"syntax": null,
@@ -491,8 +484,7 @@ FOOBAR",
491484
"raw": "Property example
492485
",
493486
"unicode": false,
494-
"value": "Property example
495-
",
487+
"value": "Property example",
496488
},
497489
"kind": "encapsedpart",
498490
"syntax": null,
@@ -548,8 +540,7 @@ LABEL",
548540
"raw": "Nothing in here...
549541
",
550542
"unicode": false,
551-
"value": "Nothing in here...
552-
",
543+
"value": "Nothing in here...",
553544
},
554545
"kind": "encapsedpart",
555546
"syntax": null,
@@ -604,8 +595,7 @@ TEST",
604595
"raw": "
605596
",
606597
"unicode": false,
607-
"value": "
608-
",
598+
"value": "",
609599
},
610600
"kind": "encapsedpart",
611601
"syntax": null,
@@ -649,8 +639,7 @@ using heredoc syntax.
649639
"unicode": false,
650640
"value": "Example of string
651641
spanning multiple lines
652-
using heredoc syntax.
653-
",
642+
using heredoc syntax.",
654643
},
655644
"kind": "encapsedpart",
656645
"syntax": null,
@@ -694,8 +683,7 @@ c
694683
"unicode": false,
695684
"value": " a
696685
b
697-
c
698-
",
686+
c",
699687
},
700688
"kind": "encapsedpart",
701689
"syntax": null,
@@ -739,8 +727,7 @@ c
739727
"unicode": false,
740728
"value": " a
741729
b
742-
c
743-
",
730+
c",
744731
},
745732
"kind": "encapsedpart",
746733
"syntax": null,
@@ -784,8 +771,7 @@ using heredoc syntax.
784771
"unicode": false,
785772
"value": "Example of string
786773
spanning multiple lines
787-
using heredoc syntax.
788-
",
774+
using heredoc syntax.",
789775
},
790776
"kind": "encapsedpart",
791777
"syntax": null,
@@ -829,8 +815,7 @@ using heredoc syntax.
829815
"unicode": false,
830816
"value": "Example of string
831817
spanning multiple lines
832-
using heredoc syntax.
833-
",
818+
using heredoc syntax.",
834819
},
835820
"kind": "encapsedpart",
836821
"syntax": null,
@@ -961,8 +946,7 @@ This should print a capital 'A': A
961946
",
962947
"unicode": false,
963948
"value": ".
964-
This should print a capital 'A': A
965-
",
949+
This should print a capital 'A': A",
966950
},
967951
"kind": "encapsedpart",
968952
"syntax": null,

test/snapshot/__snapshots__/location.test.js.snap

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4416,8 +4416,7 @@ EOD",
44164416
"raw": "Text
44174417
",
44184418
"unicode": false,
4419-
"value": "Text
4420-
",
4419+
"value": "Text",
44214420
},
44224421
"kind": "encapsedpart",
44234422
"loc": Location {
@@ -4561,8 +4560,7 @@ EOD",
45614560
"raw": "Text
45624561
",
45634562
"unicode": false,
4564-
"value": "Text
4565-
",
4563+
"value": "Text",
45664564
},
45674565
"kind": "encapsedpart",
45684566
"loc": Location {
@@ -4852,8 +4850,7 @@ command;
48524850
"value": "
48534851
command;
48544852
command;
4855-
command;
4856-
",
4853+
command;",
48574854
},
48584855
"kind": "encapsedpart",
48594856
"loc": Location {

test/snapshot/__snapshots__/loop.test.js.snap

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ Program {
9090
"raw": "
9191
",
9292
"unicode": false,
93-
"value": "
94-
",
93+
"value": "",
9594
},
9695
"kind": "encapsedpart",
9796
"syntax": null,
@@ -196,8 +195,7 @@ Program {
196195
"raw": "
197196
",
198197
"unicode": false,
199-
"value": "
200-
",
198+
"value": "",
201199
},
202200
"kind": "encapsedpart",
203201
"syntax": null,
@@ -460,8 +458,7 @@ Program {
460458
"raw": "
461459
",
462460
"unicode": false,
463-
"value": "
464-
",
461+
"value": "",
465462
},
466463
"kind": "encapsedpart",
467464
"syntax": null,
@@ -538,8 +535,7 @@ Program {
538535
"raw": "
539536
",
540537
"unicode": false,
541-
"value": "
542-
",
538+
"value": "",
543539
},
544540
"kind": "encapsedpart",
545541
"syntax": null,

test/snapshot/__snapshots__/nowdoc.test.js.snap

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ Program {
1515
END",
1616
"value": " a
1717
b
18-
c
19-
",
18+
c",
2019
},
2120
],
2221
"kind": "echo",
@@ -45,8 +44,7 @@ Program {
4544
"value": " a
4645
{$foo->bar[1]}
4746
b
48-
c
49-
",
47+
c",
5048
},
5149
],
5250
"kind": "echo",

test/snapshot/__snapshots__/property.test.js.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,7 @@ EOD",
757757
"raw": "hello world
758758
",
759759
"unicode": false,
760-
"value": "hello world
761-
",
760+
"value": "hello world",
762761
},
763762
"kind": "encapsedpart",
764763
"syntax": null,

test/snapshot/__snapshots__/string.test.js.snap

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,8 +1314,7 @@ Program {
13141314
"kind": "string",
13151315
"raw": "\\\\n",
13161316
"unicode": false,
1317-
"value": "
1318-
",
1317+
"value": "",
13191318
},
13201319
"kind": "encapsedpart",
13211320
"syntax": null,
@@ -2347,8 +2346,7 @@ Program {
23472346
"kind": "string",
23482347
"raw": "<\\\\n",
23492348
"unicode": false,
2350-
"value": "<
2351-
",
2349+
"value": "<",
23522350
},
23532351
"kind": "encapsedpart",
23542352
"syntax": null,
@@ -2926,8 +2924,7 @@ EOFX",
29262924
"raw": "
29272925
",
29282926
"unicode": false,
2929-
"value": "
2930-
",
2927+
"value": "",
29312928
},
29322929
"kind": "encapsedpart",
29332930
"syntax": null,

0 commit comments

Comments
 (0)