Skip to content
Open
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
18 changes: 11 additions & 7 deletions src/parser/scalar.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
return text.replace(/\\\\/g, "\\").replace(/\\'/g, "'");
}
return text
.replace(/\\"/, '"')
.replace(/\\"/g, '"')
.replace(
/\\([\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}|u{([0-9a-fA-F]+)})/g,
($match, p1, p2) => {
Expand Down Expand Up @@ -342,13 +342,16 @@ module.exports = {
"string",
false,
this.version >= 703 && !this.lexer.heredoc_label.finished
? this.remove_heredoc_leading_whitespace_chars(
this.resolve_special_chars(text, isDoubleQuote),
this.lexer.heredoc_label.indentation,
this.lexer.heredoc_label.indentation_uses_spaces,
this.lexer.heredoc_label.first_encaps_node,
? this.resolve_special_chars(
this.remove_heredoc_leading_whitespace_chars(
text,
this.lexer.heredoc_label.indentation,
this.lexer.heredoc_label.indentation_uses_spaces,
this.lexer.heredoc_label.first_encaps_node,
),
isDoubleQuote,
)
: text,
: this.resolve_special_chars(text, isDoubleQuote),
false,
text,
);
Expand Down Expand Up @@ -447,6 +450,7 @@ module.exports = {
value.push(this.read_encapsed_string_item(true));
}
if (
type === this.ast.encapsed.TYPE_HEREDOC &&
value.length > 0 &&
value[value.length - 1].kind === "encapsedpart" &&
value[value.length - 1].expression.kind === "string"
Expand Down
54 changes: 54 additions & 0 deletions test/snapshot/__snapshots__/heredoc.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`heredoc Can parse HEREDOC with escaped characters #1130 1`] = `
Program {
"children": [
If {
"alternate": null,
"body": Block {
"children": [
Echo {
"expressions": [
Encapsed {
"kind": "encapsed",
"label": "STR",
"raw": "<<<STR
\\na
STR",
"type": "heredoc",
"value": [
EncapsedPart {
"curly": false,
"expression": String {
"isDoubleQuote": false,
"kind": "string",
"raw": " \\na
",
"unicode": false,
"value": "
a",
},
"kind": "encapsedpart",
"syntax": null,
},
],
},
],
"kind": "echo",
"shortForm": false,
},
],
"kind": "block",
},
"kind": "if",
"shortForm": false,
"test": Boolean {
"kind": "boolean",
"raw": "true",
"value": true,
},
},
],
"errors": [],
"kind": "program",
}
`;

exports[`heredoc Can't parse multiple flexible nowdoc blocks with different indentation #508 1`] = `
Program {
"children": [
Expand Down
3 changes: 2 additions & 1 deletion test/snapshot/__snapshots__/location.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4860,7 +4860,8 @@ command;
"value": "
command;
command;
command;",
command;
",
},
"kind": "encapsedpart",
"loc": Location {
Expand Down
12 changes: 8 additions & 4 deletions test/snapshot/__snapshots__/loop.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ Program {
"raw": "
",
"unicode": false,
"value": "",
"value": "
",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -195,7 +196,8 @@ Program {
"raw": "
",
"unicode": false,
"value": "",
"value": "
",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -458,7 +460,8 @@ Program {
"raw": "
",
"unicode": false,
"value": "",
"value": "
",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -535,7 +538,8 @@ Program {
"raw": "
",
"unicode": false,
"value": "",
"value": "
",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down
166 changes: 135 additions & 31 deletions test/snapshot/__snapshots__/string.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,69 @@

exports[`Test strings binary cast 1`] = `
Program {
"children": Array [
"children": [
Echo {
"arguments": Array [
"expressions": [
Cast {
"kind": "cast",
"type": "string",
"what": Encapsed {
"expr": Encapsed {
"kind": "encapsed",
"raw": "\\"\\\\colors[1] contains >$colors[1]<\\\\n\\"",
"raw": ""\\colors[1] contains >$colors[1]<\\n"",
"type": "string",
"value": Array [
String {
"isDoubleQuote": false,
"kind": "string",
"raw": "\\\\colors[1] contains >",
"value": "\\\\colors[1] contains >",
},
OffsetLookup {
"kind": "offsetlookup",
"offset": Number {
"kind": "number",
"value": "1",
"value": [
EncapsedPart {
"curly": false,
"expression": String {
"isDoubleQuote": false,
"kind": "string",
"raw": "\\colors[1] contains >",
"unicode": false,
"value": "\\colors[1] contains >",
},
"what": Variable {
"byref": false,
"curly": false,
"kind": "variable",
"name": "colors",
"kind": "encapsedpart",
"syntax": null,
},
EncapsedPart {
"curly": false,
"expression": OffsetLookup {
"kind": "offsetlookup",
"offset": Number {
"kind": "number",
"value": "1",
},
"what": Variable {
"curly": false,
"kind": "variable",
"name": "colors",
},
},
"kind": "encapsedpart",
"syntax": "simple",
},
String {
"isDoubleQuote": false,
"kind": "string",
"raw": "<\\\\n",
"value": "<
EncapsedPart {
"curly": false,
"expression": String {
"isDoubleQuote": false,
"kind": "string",
"raw": "<\\n",
"unicode": false,
"value": "<
",
},
"kind": "encapsedpart",
"syntax": null,
},
],
},
"kind": "cast",
"raw": "(binary)",
"type": "binary",
},
],
"kind": "echo",
"shortForm": false,
},
],
"errors": Array [],
"errors": [],
"kind": "program",
}
`;
Expand Down Expand Up @@ -1314,7 +1331,8 @@ Program {
"kind": "string",
"raw": "\\n",
"unicode": false,
"value": "",
"value": "
",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down Expand Up @@ -1702,6 +1720,63 @@ Program {
}
`;

exports[`Test strings heredoc ... 1`] = `
Program {
"children": [
ExpressionStatement {
"expression": Assign {
"kind": "assign",
"left": Variable {
"curly": false,
"kind": "variable",
"name": "fallbackContent",
},
"operator": ".=",
"right": Call {
"arguments": [
Encapsed {
"kind": "encapsed",
"label": "EOF2",
"raw": "<<<EOF2
\\$catalogue%s = new MessageCatalogue('%s', %s);
\\$catalogue%s->addFallbackCatalogue(\\$catalogue%s);
EOF2",
"type": "heredoc",
"value": [
EncapsedPart {
"curly": false,
"expression": String {
"isDoubleQuote": false,
"kind": "string",
"raw": " \\$catalogue%s = new MessageCatalogue('%s', %s);
\\$catalogue%s->addFallbackCatalogue(\\$catalogue%s);
",
"unicode": false,
"value": "$catalogue%s = new MessageCatalogue('%s', %s);
$catalogue%s->addFallbackCatalogue($catalogue%s);",
},
"kind": "encapsedpart",
"syntax": null,
},
],
},
],
"kind": "call",
"what": Name {
"kind": "name",
"name": "sprintf",
"resolution": "uqn",
},
},
},
"kind": "expressionstatement",
},
],
"errors": [],
"kind": "program",
}
`;

exports[`Test strings implement #116 1`] = `
Program {
"children": [
Expand Down Expand Up @@ -1731,6 +1806,34 @@ bar",
}
`;

exports[`Test strings multiple escaped double quotes 1`] = `
Program {
"children": [
ExpressionStatement {
"expression": Assign {
"kind": "assign",
"left": Variable {
"curly": false,
"kind": "variable",
"name": "var",
},
"operator": "=",
"right": String {
"isDoubleQuote": true,
"kind": "string",
"raw": ""say \\"hello\\" and \\"bye\\""",
"unicode": false,
"value": "say "hello" and "bye"",
},
},
"kind": "expressionstatement",
},
],
"errors": [],
"kind": "program",
}
`;

exports[`Test strings single (2) 1`] = `
Program {
"children": [
Expand Down Expand Up @@ -2346,7 +2449,8 @@ Program {
"kind": "string",
"raw": "<\\n",
"unicode": false,
"value": "<",
"value": "<
",
},
"kind": "encapsedpart",
"syntax": null,
Expand Down
12 changes: 12 additions & 0 deletions test/snapshot/heredoc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,16 @@ $b = <<<'EOT'
),
).toMatchSnapshot();
});

it("Can parse HEREDOC with escaped characters #1130", () => {
expect(
parser.parseEval(`
if (true) {
echo <<<STR
\\na
STR;
}
`),
).toMatchSnapshot();
});
});
Loading
Loading