Skip to content

Commit 1b068c0

Browse files
committed
Fixed a bug with heredoc+indenting+variables
1 parent 61c9043 commit 1b068c0

File tree

4 files changed

+204
-1
lines changed

4 files changed

+204
-1
lines changed

src/lexer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ lexer.prototype.setInput = function(input) {
164164
length: 0,
165165
indentation: 0,
166166
indentation_uses_spaces: false,
167+
finished: false,
167168
/**
168169
* this used for parser to detemine the if current node segment is first encaps node.
169170
* if ture, the indentation will remove from the begining. and if false, the prev node

src/parser/scalar.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ module.exports = {
339339
result = result(
340340
"string",
341341
false,
342-
this.version >= 703
342+
this.version >= 703 && !this.lexer.heredoc_label.finished
343343
? this.remove_heredoc_leading_whitespace_chars(
344344
this.resolve_special_chars(text, isDoubleQuote),
345345
this.lexer.heredoc_label.indentation,
@@ -473,6 +473,7 @@ module.exports = {
473473

474474
if (expect === this.tok.T_END_HEREDOC) {
475475
node.label = this.lexer.heredoc_label.label;
476+
this.lexer.heredoc_label.finished = true;
476477
}
477478
return node;
478479
},

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

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,181 @@ a
723723
}
724724
`;
725725

726+
exports[`heredoc Flexible heredoc syntax: indentation bracket bug 1`] = `
727+
Program {
728+
"children": Array [
729+
ExpressionStatement {
730+
"expression": Assign {
731+
"kind": "assign",
732+
"left": Variable {
733+
"curly": false,
734+
"kind": "variable",
735+
"name": "a",
736+
},
737+
"operator": "=",
738+
"right": Encapsed {
739+
"kind": "encapsed",
740+
"label": "EOT",
741+
"raw": "<<<EOT
742+
EOT",
743+
"type": "heredoc",
744+
"value": Array [],
745+
},
746+
},
747+
"kind": "expressionstatement",
748+
},
749+
ExpressionStatement {
750+
"expression": Assign {
751+
"kind": "assign",
752+
"left": Variable {
753+
"curly": false,
754+
"kind": "variable",
755+
"name": "b",
756+
},
757+
"operator": "=",
758+
"right": Encapsed {
759+
"kind": "encapsed",
760+
"raw": "\\"{$x}
761+
test\\"",
762+
"type": "string",
763+
"value": Array [
764+
EncapsedPart {
765+
"curly": false,
766+
"expression": Variable {
767+
"curly": false,
768+
"kind": "variable",
769+
"name": "x",
770+
},
771+
"kind": "encapsedpart",
772+
"syntax": "complex",
773+
},
774+
EncapsedPart {
775+
"curly": false,
776+
"expression": String {
777+
"isDoubleQuote": false,
778+
"kind": "string",
779+
"raw": "
780+
test",
781+
"unicode": false,
782+
"value": "
783+
test",
784+
},
785+
"kind": "encapsedpart",
786+
"syntax": null,
787+
},
788+
],
789+
},
790+
},
791+
"kind": "expressionstatement",
792+
},
793+
],
794+
"errors": Array [],
795+
"kind": "program",
796+
}
797+
`;
798+
799+
exports[`heredoc Flexible heredoc syntax: indentation bracket bug 2`] = `
800+
Program {
801+
"children": Array [
802+
ExpressionStatement {
803+
"expression": Assign {
804+
"kind": "assign",
805+
"left": Variable {
806+
"curly": false,
807+
"kind": "variable",
808+
"name": "a",
809+
},
810+
"operator": "=",
811+
"right": Encapsed {
812+
"kind": "encapsed",
813+
"label": "EOT",
814+
"raw": "<<<EOT
815+
EOT;
816+
$b = \\"{$x}
817+
test\\";
818+
",
819+
"type": "heredoc",
820+
"value": Array [
821+
EncapsedPart {
822+
"curly": false,
823+
"expression": String {
824+
"isDoubleQuote": false,
825+
"kind": "string",
826+
"raw": " EOT;
827+
",
828+
"unicode": false,
829+
"value": " EOT;
830+
",
831+
},
832+
"kind": "encapsedpart",
833+
"syntax": null,
834+
},
835+
EncapsedPart {
836+
"curly": false,
837+
"expression": Variable {
838+
"curly": false,
839+
"kind": "variable",
840+
"name": "b",
841+
},
842+
"kind": "encapsedpart",
843+
"syntax": "simple",
844+
},
845+
EncapsedPart {
846+
"curly": false,
847+
"expression": String {
848+
"isDoubleQuote": false,
849+
"kind": "string",
850+
"raw": " = \\"",
851+
"unicode": false,
852+
"value": " = \\"",
853+
},
854+
"kind": "encapsedpart",
855+
"syntax": null,
856+
},
857+
EncapsedPart {
858+
"curly": false,
859+
"expression": Variable {
860+
"curly": false,
861+
"kind": "variable",
862+
"name": "x",
863+
},
864+
"kind": "encapsedpart",
865+
"syntax": "complex",
866+
},
867+
EncapsedPart {
868+
"curly": false,
869+
"expression": String {
870+
"isDoubleQuote": false,
871+
"kind": "string",
872+
"raw": "
873+
test\\";
874+
",
875+
"unicode": false,
876+
"value": "
877+
test\\";",
878+
},
879+
"kind": "encapsedpart",
880+
"syntax": null,
881+
},
882+
],
883+
},
884+
},
885+
"kind": "expressionstatement",
886+
},
887+
],
888+
"errors": Array [
889+
Error {
890+
"expected": 220,
891+
"kind": "error",
892+
"line": 6,
893+
"message": "Parse Error : syntax error, expecting T_END_HEREDOC on line 6",
894+
"token": "the end of file (EOF)",
895+
},
896+
],
897+
"kind": "program",
898+
}
899+
`;
900+
726901
exports[`heredoc Flexible heredoc syntax: mixing spaces and tabs in body 1`] = `
727902
Program {
728903
"children": Array [

test/snapshot/heredoc.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,32 @@ $values = <<<END
448448
c
449449
450450
END;
451+
`,
452+
{
453+
parser: { version: 702, suppressErrors: true, debug: false },
454+
lexer: { debug: false }
455+
}
456+
)
457+
).toMatchSnapshot();
458+
});
459+
460+
it("Flexible heredoc syntax: indentation bracket bug", () => {
461+
expect(
462+
parser.parseEval(
463+
`
464+
$a = <<<EOT
465+
EOT;
466+
$b = "{$x}\ntest";
467+
`
468+
)
469+
).toMatchSnapshot();
470+
471+
expect(
472+
parser.parseEval(
473+
`
474+
$a = <<<EOT
475+
EOT;
476+
$b = "{$x}\ntest";
451477
`,
452478
{
453479
parser: { version: 702, suppressErrors: true, debug: false },

0 commit comments

Comments
 (0)