Skip to content

Commit 797f795

Browse files
committed
Make remainingTargetNames consistent, avoid php notice
Add a test of parsing an empty `insteadof` clause Make remainingTargetNames consistently be an empty array instead of null
1 parent f297043 commit 797f795

11 files changed

+194
-11
lines changed

src/Parser.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2950,15 +2950,15 @@ private function parseTraitSelectOrAliasClauseFn() {
29502950
if ($traitSelectAndAliasClause->asOrInsteadOfKeyword->kind === TokenKind::InsteadOfKeyword) {
29512951
// https://github.com/Microsoft/tolerant-php-parser/issues/190
29522952
// TODO: In the next backwards incompatible release, convert targetName to a list?
2953-
$interfaceNameList = $this->parseQualifiedNameList($traitSelectAndAliasClause)->children;
2954-
$traitSelectAndAliasClause->targetName = $interfaceNameList[0] ?? null;
2953+
$interfaceNameList = $this->parseQualifiedNameList($traitSelectAndAliasClause)->children ?? [];
2954+
$traitSelectAndAliasClause->targetName = $interfaceNameList[0] ?? new MissingToken(TokenKind::BarToken, $this->token->fullStart);
29552955
$traitSelectAndAliasClause->remainingTargetNames = array_slice($interfaceNameList, 1);
29562956
} else {
29572957
$traitSelectAndAliasClause->targetName =
29582958
$this->parseQualifiedNameOrScopedPropertyAccessExpression($traitSelectAndAliasClause);
2959+
$traitSelectAndAliasClause->remainingTargetNames = [];
29592960
}
29602961

2961-
29622962
// TODO errors for insteadof/as
29632963
return $traitSelectAndAliasClause;
29642964
};

tests/cases/parser/traits10.php.tree

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
]
9191
}
9292
},
93-
"remainingTargetNames": null
93+
"remainingTargetNames": []
9494
}
9595
}
9696
]

tests/cases/parser/traits12.php.tree

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
]
129129
}
130130
},
131-
"remainingTargetNames": null
131+
"remainingTargetNames": []
132132
}
133133
},
134134
{

tests/cases/parser/traits19.php.tree

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
]
9696
}
9797
},
98-
"remainingTargetNames": null
98+
"remainingTargetNames": []
9999
}
100100
},
101101
{

tests/cases/parser/traits20.php.tree

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
]
100100
}
101101
},
102-
"remainingTargetNames": null
102+
"remainingTargetNames": []
103103
}
104104
},
105105
{

tests/cases/parser/traits21.php.tree

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
]
9797
}
9898
},
99-
"remainingTargetNames": null
99+
"remainingTargetNames": []
100100
}
101101
},
102102
{
@@ -157,7 +157,7 @@
157157
]
158158
}
159159
},
160-
"remainingTargetNames": null
160+
"remainingTargetNames": []
161161
}
162162
},
163163
{

tests/cases/parser/traits22.php.tree

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
]
150150
}
151151
},
152-
"remainingTargetNames": null
152+
"remainingTargetNames": []
153153
}
154154
},
155155
{

tests/cases/parser/traits25.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
// https://github.com/Microsoft/tolerant-php-parser/issues/98
3+
4+
class A {
5+
use X, Y, Z {
6+
\X::b insteadof;
7+
}
8+
}

tests/cases/parser/traits25.php.diag

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"kind": 0,
4+
"message": "'|' expected.",
5+
"start": 121,
6+
"length": 0
7+
}
8+
]

tests/cases/parser/traits25.php.tree

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
{
2+
"SourceFileNode": {
3+
"statementList": [
4+
{
5+
"InlineHtml": {
6+
"scriptSectionEndTag": null,
7+
"text": null,
8+
"scriptSectionStartTag": {
9+
"kind": "ScriptSectionStartTag",
10+
"textLength": 6
11+
}
12+
}
13+
},
14+
{
15+
"ClassDeclaration": {
16+
"abstractOrFinalModifier": null,
17+
"classKeyword": {
18+
"kind": "ClassKeyword",
19+
"textLength": 5
20+
},
21+
"name": {
22+
"kind": "Name",
23+
"textLength": 1
24+
},
25+
"classBaseClause": null,
26+
"classInterfaceClause": null,
27+
"classMembers": {
28+
"ClassMembersNode": {
29+
"openBrace": {
30+
"kind": "OpenBraceToken",
31+
"textLength": 1
32+
},
33+
"classMemberDeclarations": [
34+
{
35+
"TraitUseClause": {
36+
"useKeyword": {
37+
"kind": "UseKeyword",
38+
"textLength": 3
39+
},
40+
"traitNameList": {
41+
"QualifiedNameList": {
42+
"children": [
43+
{
44+
"QualifiedName": {
45+
"globalSpecifier": null,
46+
"relativeSpecifier": null,
47+
"nameParts": [
48+
{
49+
"kind": "Name",
50+
"textLength": 1
51+
}
52+
]
53+
}
54+
},
55+
{
56+
"kind": "CommaToken",
57+
"textLength": 1
58+
},
59+
{
60+
"QualifiedName": {
61+
"globalSpecifier": null,
62+
"relativeSpecifier": null,
63+
"nameParts": [
64+
{
65+
"kind": "Name",
66+
"textLength": 1
67+
}
68+
]
69+
}
70+
},
71+
{
72+
"kind": "CommaToken",
73+
"textLength": 1
74+
},
75+
{
76+
"QualifiedName": {
77+
"globalSpecifier": null,
78+
"relativeSpecifier": null,
79+
"nameParts": [
80+
{
81+
"kind": "Name",
82+
"textLength": 1
83+
}
84+
]
85+
}
86+
}
87+
]
88+
}
89+
},
90+
"semicolonOrOpenBrace": {
91+
"kind": "OpenBraceToken",
92+
"textLength": 1
93+
},
94+
"traitSelectAndAliasClauses": {
95+
"TraitSelectOrAliasClauseList": {
96+
"children": [
97+
{
98+
"TraitSelectOrAliasClause": {
99+
"name": {
100+
"ScopedPropertyAccessExpression": {
101+
"scopeResolutionQualifier": {
102+
"QualifiedName": {
103+
"globalSpecifier": {
104+
"kind": "BackslashToken",
105+
"textLength": 1
106+
},
107+
"relativeSpecifier": null,
108+
"nameParts": [
109+
{
110+
"kind": "Name",
111+
"textLength": 1
112+
}
113+
]
114+
}
115+
},
116+
"doubleColon": {
117+
"kind": "ColonColonToken",
118+
"textLength": 2
119+
},
120+
"memberName": {
121+
"kind": "Name",
122+
"textLength": 1
123+
}
124+
}
125+
},
126+
"asOrInsteadOfKeyword": {
127+
"kind": "InsteadOfKeyword",
128+
"textLength": 9
129+
},
130+
"modifiers": [],
131+
"targetName": {
132+
"error": "MissingToken",
133+
"kind": "BarToken",
134+
"textLength": 0
135+
},
136+
"remainingTargetNames": []
137+
}
138+
},
139+
{
140+
"kind": "SemicolonToken",
141+
"textLength": 1
142+
}
143+
]
144+
}
145+
},
146+
"closeBrace": {
147+
"kind": "CloseBraceToken",
148+
"textLength": 1
149+
}
150+
}
151+
}
152+
],
153+
"closeBrace": {
154+
"kind": "CloseBraceToken",
155+
"textLength": 1
156+
}
157+
}
158+
}
159+
}
160+
}
161+
],
162+
"endOfFileToken": {
163+
"kind": "EndOfFileToken",
164+
"textLength": 0
165+
}
166+
}
167+
}

tests/cases/parser/traits9.php.tree

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
]
9191
}
9292
},
93-
"remainingTargetNames": null
93+
"remainingTargetNames": []
9494
}
9595
},
9696
{

0 commit comments

Comments
 (0)