Skip to content

Commit 7c09272

Browse files
fix: ast for offsetlookup in encapsed
1 parent 74ee26f commit 7c09272

File tree

7 files changed

+120
-60
lines changed

7 files changed

+120
-60
lines changed

src/parser/scalar.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ module.exports = {
221221
text
222222
);
223223
} else if (this.token === this.tok.T_DOLLAR_OPEN_CURLY_BRACES) {
224-
syntax = 'simple';
224+
syntax = "simple";
225225
curly = true;
226226
// dynamic variable name
227227
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1239
@@ -237,24 +237,23 @@ module.exports = {
237237
node = this.node("offsetlookup");
238238
offset = this.next().read_expr();
239239
this.expect("]") && this.next();
240-
name = node(name, offset);
240+
result = node(name, offset);
241241
} else {
242-
name = varName;
242+
result = name(varName, false);
243243
}
244244
} else {
245-
name = this.read_expr();
245+
result = result("variable", this.read_expr(), false);
246246
}
247247
this.expect("}") && this.next();
248-
result = result("variable", name);
249248
} else if (this.token === this.tok.T_CURLY_OPEN) {
250249
// expression
251250
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1246
252-
syntax = 'complex';
251+
syntax = "complex";
253252
result.destroy();
254253
result = this.next().read_variable(false, false);
255254
this.expect("}") && this.next();
256255
} else if (this.token === this.tok.T_VARIABLE) {
257-
syntax = 'simple';
256+
syntax = "simple";
258257
// plain variable
259258
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1231
260259
result.destroy();

src/parser/variable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ module.exports = {
153153
what = this.node("encapsedpart");
154154
name = this.next().read_expr();
155155
this.expect("}") && this.next();
156-
what = what(name, true);
156+
what = what(name, "complex", false);
157157
break;
158158
default:
159159
this.error([this.tok.T_STRING, this.tok.T_VARIABLE, "$", "{"]);

test/snapshot/__snapshots__/encapsed.test.js.snap

+83-17
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@ Program {
193193
"expression": Variable {
194194
"curly": false,
195195
"kind": "variable",
196-
"name": "juice",
196+
"name": Variable {
197+
"curly": false,
198+
"kind": "variable",
199+
"name": "juice",
200+
},
197201
},
198202
"kind": "encapsedpart",
199203
"syntax": "simple",
@@ -244,12 +248,16 @@ Program {
244248
EncapsedPart {
245249
"curly": true,
246250
"expression": Variable {
247-
"curly": true,
251+
"curly": false,
248252
"kind": "variable",
249253
"name": Variable {
250-
"curly": false,
254+
"curly": true,
251255
"kind": "variable",
252-
"name": "juice",
256+
"name": Variable {
257+
"curly": false,
258+
"kind": "variable",
259+
"name": "juice",
260+
},
253261
},
254262
},
255263
"kind": "encapsedpart",
@@ -300,13 +308,17 @@ Program {
300308
},
301309
EncapsedPart {
302310
"curly": true,
303-
"expression": Call {
304-
"arguments": Array [],
305-
"kind": "call",
306-
"what": ClassReference {
307-
"kind": "classreference",
308-
"name": "call",
309-
"resolution": "uqn",
311+
"expression": Variable {
312+
"curly": false,
313+
"kind": "variable",
314+
"name": Call {
315+
"arguments": Array [],
316+
"kind": "call",
317+
"what": ClassReference {
318+
"kind": "classreference",
319+
"name": "call",
320+
"resolution": "uqn",
321+
},
310322
},
311323
},
312324
"kind": "encapsedpart",
@@ -604,9 +616,9 @@ Program {
604616
EncapsedPart {
605617
"curly": true,
606618
"expression": Variable {
607-
"curly": "juice",
619+
"curly": false,
608620
"kind": "variable",
609-
"name": "variable",
621+
"name": "juice",
610622
},
611623
"kind": "encapsedpart",
612624
"syntax": "simple",
@@ -1447,6 +1459,60 @@ Program {
14471459
}
14481460
`;
14491461

1462+
exports[`encapsed propertylookup 1`] = `
1463+
Program {
1464+
"children": Array [
1465+
ExpressionStatement {
1466+
"expression": PropertyLookup {
1467+
"kind": "propertylookup",
1468+
"offset": EncapsedPart {
1469+
"curly": false,
1470+
"expression": Encapsed {
1471+
"kind": "encapsed",
1472+
"raw": "\\"set{$type}\\"",
1473+
"type": "string",
1474+
"value": Array [
1475+
EncapsedPart {
1476+
"curly": false,
1477+
"expression": String {
1478+
"isDoubleQuote": false,
1479+
"kind": "string",
1480+
"raw": "set",
1481+
"unicode": false,
1482+
"value": "set",
1483+
},
1484+
"kind": "encapsedpart",
1485+
"syntax": null,
1486+
},
1487+
EncapsedPart {
1488+
"curly": false,
1489+
"expression": Variable {
1490+
"curly": false,
1491+
"kind": "variable",
1492+
"name": "type",
1493+
},
1494+
"kind": "encapsedpart",
1495+
"syntax": "complex",
1496+
},
1497+
],
1498+
},
1499+
"kind": "encapsedpart",
1500+
"syntax": "complex",
1501+
},
1502+
"what": Variable {
1503+
"curly": false,
1504+
"kind": "variable",
1505+
"name": "this",
1506+
},
1507+
},
1508+
"kind": "expressionstatement",
1509+
},
1510+
],
1511+
"errors": Array [],
1512+
"kind": "program",
1513+
}
1514+
`;
1515+
14501516
exports[`encapsed propertylookup and offsetlookup (complex syntax) 1`] = `
14511517
Program {
14521518
"children": Array [
@@ -1547,7 +1613,7 @@ Program {
15471613
"expression": PropertyLookup {
15481614
"kind": "propertylookup",
15491615
"offset": EncapsedPart {
1550-
"curly": null,
1616+
"curly": false,
15511617
"expression": OffsetLookup {
15521618
"kind": "offsetlookup",
15531619
"offset": Number {
@@ -1561,7 +1627,7 @@ Program {
15611627
},
15621628
},
15631629
"kind": "encapsedpart",
1564-
"syntax": true,
1630+
"syntax": "complex",
15651631
},
15661632
"what": Variable {
15671633
"curly": false,
@@ -2854,9 +2920,9 @@ Program {
28542920
EncapsedPart {
28552921
"curly": true,
28562922
"expression": Variable {
2857-
"curly": "var",
2923+
"curly": false,
28582924
"kind": "variable",
2859-
"name": "variable",
2925+
"name": "var",
28602926
},
28612927
"kind": "encapsedpart",
28622928
"syntax": "simple",

test/snapshot/__snapshots__/propertylookup.test.js.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ Program {
3535
"expression": PropertyLookup {
3636
"kind": "propertylookup",
3737
"offset": EncapsedPart {
38-
"curly": null,
38+
"curly": false,
3939
"expression": ClassReference {
4040
"kind": "classreference",
4141
"name": "foo",
4242
"resolution": "uqn",
4343
},
4444
"kind": "encapsedpart",
45-
"syntax": true,
45+
"syntax": "complex",
4646
},
4747
"what": Variable {
4848
"curly": false,

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

+23-31
Original file line numberDiff line numberDiff line change
@@ -1662,23 +1662,19 @@ Program {
16621662
},
16631663
EncapsedPart {
16641664
"curly": true,
1665-
"expression": Variable {
1666-
"curly": false,
1667-
"kind": "variable",
1668-
"name": OffsetLookup {
1669-
"kind": "offsetlookup",
1670-
"offset": String {
1671-
"isDoubleQuote": false,
1672-
"kind": "string",
1673-
"raw": "'FOO'",
1674-
"unicode": false,
1675-
"value": "FOO",
1676-
},
1677-
"what": Variable {
1678-
"curly": false,
1679-
"kind": "variable",
1680-
"name": "juices",
1681-
},
1665+
"expression": OffsetLookup {
1666+
"kind": "offsetlookup",
1667+
"offset": String {
1668+
"isDoubleQuote": false,
1669+
"kind": "string",
1670+
"raw": "'FOO'",
1671+
"unicode": false,
1672+
"value": "FOO",
1673+
},
1674+
"what": Variable {
1675+
"curly": false,
1676+
"kind": "variable",
1677+
"name": "juices",
16821678
},
16831679
},
16841680
"kind": "encapsedpart",
@@ -2866,20 +2862,16 @@ EOFX",
28662862
},
28672863
EncapsedPart {
28682864
"curly": true,
2869-
"expression": Variable {
2870-
"curly": false,
2871-
"kind": "variable",
2872-
"name": OffsetLookup {
2873-
"kind": "offsetlookup",
2874-
"offset": Number {
2875-
"kind": "number",
2876-
"value": "1",
2877-
},
2878-
"what": Variable {
2879-
"curly": false,
2880-
"kind": "variable",
2881-
"name": "targetDirs",
2882-
},
2865+
"expression": OffsetLookup {
2866+
"kind": "offsetlookup",
2867+
"offset": Number {
2868+
"kind": "number",
2869+
"value": "1",
2870+
},
2871+
"what": Variable {
2872+
"curly": false,
2873+
"kind": "variable",
2874+
"name": "targetDirs",
28832875
},
28842876
},
28852877
"kind": "encapsedpart",

test/snapshot/__snapshots__/variable.test.js.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ Program {
12351235
"expression": PropertyLookup {
12361236
"kind": "propertylookup",
12371237
"offset": EncapsedPart {
1238-
"curly": null,
1238+
"curly": false,
12391239
"expression": PropertyLookup {
12401240
"kind": "propertylookup",
12411241
"offset": Identifier {
@@ -1249,7 +1249,7 @@ Program {
12491249
},
12501250
},
12511251
"kind": "encapsedpart",
1252-
"syntax": true,
1252+
"syntax": "complex",
12531253
},
12541254
"what": Variable {
12551255
"curly": false,

test/snapshot/encapsed.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,7 @@ describe("encapsed", function() {
188188
it("no curly", () => {
189189
expect(parser.parseEval('"string $$juice string";')).toMatchSnapshot();
190190
});
191+
it("propertylookup", () => {
192+
expect(parser.parseEval('$this->{"set{$type}"};')).toMatchSnapshot();
193+
});
191194
});

0 commit comments

Comments
 (0)