Skip to content

Commit 219bc6f

Browse files
committed
fix: resolve property chains in new expression class name (#1177)
1 parent c52f1c7 commit 219bc6f

3 files changed

Lines changed: 164 additions & 0 deletions

File tree

src/parser/expr.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ module.exports = {
835835
let result = this.read_namespace_name(true);
836836
if (this.token === this.tok.T_DOUBLE_COLON) {
837837
result = this.read_static_getter(result);
838+
return this.recursive_variable_chain_scan(result, true, false);
838839
}
839840
return result;
840841
} else if (this.is("VARIABLE")) {

test/snapshot/__snapshots__/new.test.js.snap

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,157 @@ Program {
3737
}
3838
`;
3939

40+
exports[`new #1177 - explicit parens equivalent 1`] = `
41+
Program {
42+
"children": [
43+
ExpressionStatement {
44+
"expression": New {
45+
"arguments": [],
46+
"kind": "new",
47+
"what": PropertyLookup {
48+
"kind": "propertylookup",
49+
"offset": Identifier {
50+
"kind": "identifier",
51+
"name": "baz",
52+
},
53+
"what": StaticLookup {
54+
"kind": "staticlookup",
55+
"offset": Variable {
56+
"curly": false,
57+
"kind": "variable",
58+
"name": "bar",
59+
},
60+
"what": Name {
61+
"kind": "name",
62+
"name": "Foo",
63+
"resolution": "uqn",
64+
},
65+
},
66+
},
67+
},
68+
"kind": "expressionstatement",
69+
},
70+
],
71+
"errors": [],
72+
"kind": "program",
73+
}
74+
`;
75+
76+
exports[`new #1177 - longer chain 1`] = `
77+
Program {
78+
"children": [
79+
ExpressionStatement {
80+
"expression": New {
81+
"arguments": [],
82+
"kind": "new",
83+
"what": PropertyLookup {
84+
"kind": "propertylookup",
85+
"offset": Identifier {
86+
"kind": "identifier",
87+
"name": "qux",
88+
},
89+
"what": PropertyLookup {
90+
"kind": "propertylookup",
91+
"offset": Identifier {
92+
"kind": "identifier",
93+
"name": "baz",
94+
},
95+
"what": StaticLookup {
96+
"kind": "staticlookup",
97+
"offset": Variable {
98+
"curly": false,
99+
"kind": "variable",
100+
"name": "bar",
101+
},
102+
"what": Name {
103+
"kind": "name",
104+
"name": "Foo",
105+
"resolution": "uqn",
106+
},
107+
},
108+
},
109+
},
110+
},
111+
"kind": "expressionstatement",
112+
},
113+
],
114+
"errors": [],
115+
"kind": "program",
116+
}
117+
`;
118+
119+
exports[`new #1177 - nullsafe operator in chain 1`] = `
120+
Program {
121+
"children": [
122+
ExpressionStatement {
123+
"expression": New {
124+
"arguments": [],
125+
"kind": "new",
126+
"what": NullSafePropertyLookup {
127+
"kind": "nullsafepropertylookup",
128+
"offset": Identifier {
129+
"kind": "identifier",
130+
"name": "baz",
131+
},
132+
"what": StaticLookup {
133+
"kind": "staticlookup",
134+
"offset": Variable {
135+
"curly": false,
136+
"kind": "variable",
137+
"name": "bar",
138+
},
139+
"what": Name {
140+
"kind": "name",
141+
"name": "Foo",
142+
"resolution": "uqn",
143+
},
144+
},
145+
},
146+
},
147+
"kind": "expressionstatement",
148+
},
149+
],
150+
"errors": [],
151+
"kind": "program",
152+
}
153+
`;
154+
155+
exports[`new #1177 - static property chain as class name 1`] = `
156+
Program {
157+
"children": [
158+
ExpressionStatement {
159+
"expression": New {
160+
"arguments": [],
161+
"kind": "new",
162+
"what": PropertyLookup {
163+
"kind": "propertylookup",
164+
"offset": Identifier {
165+
"kind": "identifier",
166+
"name": "baz",
167+
},
168+
"what": StaticLookup {
169+
"kind": "staticlookup",
170+
"offset": Variable {
171+
"curly": false,
172+
"kind": "variable",
173+
"name": "bar",
174+
},
175+
"what": Name {
176+
"kind": "name",
177+
"name": "Foo",
178+
"resolution": "uqn",
179+
},
180+
},
181+
},
182+
},
183+
"kind": "expressionstatement",
184+
},
185+
],
186+
"errors": [],
187+
"kind": "program",
188+
}
189+
`;
190+
40191
exports[`new anonymous 1`] = `
41192
Program {
42193
"children": [

test/snapshot/new.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,16 @@ describe("new", function () {
108108
it("result from function with arguments", function () {
109109
expect(parser.parseEval("$a = new (b('c')('d'))('e');")).toMatchSnapshot();
110110
});
111+
it("#1177 - static property chain as class name", function () {
112+
expect(parser.parseEval("new Foo::$bar->baz();")).toMatchSnapshot();
113+
});
114+
it("#1177 - explicit parens equivalent", function () {
115+
expect(parser.parseEval("new (Foo::$bar->baz)();")).toMatchSnapshot();
116+
});
117+
it("#1177 - nullsafe operator in chain", function () {
118+
expect(parser.parseEval("new Foo::$bar?->baz();")).toMatchSnapshot();
119+
});
120+
it("#1177 - longer chain", function () {
121+
expect(parser.parseEval("new Foo::$bar->baz->qux();")).toMatchSnapshot();
122+
});
111123
});

0 commit comments

Comments
 (0)