Skip to content

Commit

Permalink
Merge pull request microsoft#243 from TysonAndre/fix-trait-list
Browse files Browse the repository at this point in the history
Fixes microsoft#190: `insteadof` should be able to accept a name list
  • Loading branch information
roblourens authored May 12, 2018
2 parents 31cffdb + 797f795 commit 7647b60
Show file tree
Hide file tree
Showing 21 changed files with 524 additions and 45 deletions.
18 changes: 17 additions & 1 deletion src/Node/TraitSelectOrAliasClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,26 @@ class TraitSelectOrAliasClause extends Node {
/** @var QualifiedName|Node\Expression\ScopedPropertyAccessExpression */
public $targetName;

/**
* @var Token[]|QualifiedName[]|null
*
* This is set if $asOrInsteadOfKeyword is an insteadof keyword.
* (E.g. for parsing `use T1, T2, T3{T1::foo insteadof T2, T3}`
*
* NOTE: This was added as a separate property to minimize
* backwards compatibility breaks in applications using this file.
*
* TODO: Use a more consistent design such as either of the following:
* 1. Combine targetName and remainingTargetNames into a DelimitedList
* 2. Use two distinct properties for the targets of `as` and `insteadof`
*/
public $remainingTargetNames;

const CHILD_NAMES = [
'name',
'asOrInsteadOfKeyword',
'modifiers',
'targetName'
'targetName',
'remainingTargetNames',
];
}
17 changes: 15 additions & 2 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,10 @@ private function parsePropertyDeclaration($parentNode, $modifiers) {
return $propertyDeclaration;
}

/**
* @param Node $parentNode
* @return DelimitedList\QualifiedNameList
*/
private function parseQualifiedNameList($parentNode) {
return $this->parseDelimitedList(
DelimitedList\QualifiedNameList::class,
Expand Down Expand Up @@ -2943,8 +2947,17 @@ private function parseTraitSelectOrAliasClauseFn() {
$traitSelectAndAliasClause->asOrInsteadOfKeyword = $this->eat(TokenKind::AsKeyword, TokenKind::InsteadOfKeyword);
$traitSelectAndAliasClause->modifiers = $this->parseModifiers(); // TODO accept all modifiers, verify later

$traitSelectAndAliasClause->targetName =
$this->parseQualifiedNameOrScopedPropertyAccessExpression($traitSelectAndAliasClause);
if ($traitSelectAndAliasClause->asOrInsteadOfKeyword->kind === TokenKind::InsteadOfKeyword) {
// https://github.com/Microsoft/tolerant-php-parser/issues/190
// TODO: In the next backwards incompatible release, convert targetName to a list?
$interfaceNameList = $this->parseQualifiedNameList($traitSelectAndAliasClause)->children ?? [];
$traitSelectAndAliasClause->targetName = $interfaceNameList[0] ?? new MissingToken(TokenKind::BarToken, $this->token->fullStart);
$traitSelectAndAliasClause->remainingTargetNames = array_slice($interfaceNameList, 1);
} else {
$traitSelectAndAliasClause->targetName =
$this->parseQualifiedNameOrScopedPropertyAccessExpression($traitSelectAndAliasClause);
$traitSelectAndAliasClause->remainingTargetNames = [];
}

// TODO errors for insteadof/as
return $traitSelectAndAliasClause;
Expand Down
3 changes: 2 additions & 1 deletion tests/cases/parser/traits10.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
}
]
}
}
},
"remainingTargetNames": []
}
}
]
Expand Down
6 changes: 4 additions & 2 deletions tests/cases/parser/traits12.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
}
]
}
}
},
"remainingTargetNames": []
}
},
{
Expand Down Expand Up @@ -126,7 +127,8 @@
}
]
}
}
},
"remainingTargetNames": []
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion tests/cases/parser/traits13.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
}
]
}
}
},
"remainingTargetNames": []
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion tests/cases/parser/traits14.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
}
]
}
}
},
"remainingTargetNames": []
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion tests/cases/parser/traits15.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
}
]
}
}
},
"remainingTargetNames": []
}
},
{
Expand Down
33 changes: 32 additions & 1 deletion tests/cases/parser/traits16.php.diag
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
[]
[
{
"kind": 0,
"message": "'}' expected.",
"start": 41,
"length": 0
},
{
"kind": 0,
"message": "Unexpected '::'",
"start": 41,
"length": 2
},
{
"kind": 0,
"message": "'}' expected.",
"start": 43,
"length": 0
},
{
"kind": 0,
"message": "Unexpected '}'",
"start": 46,
"length": 1
},
{
"kind": 0,
"message": "Unexpected '}'",
"start": 48,
"length": 1
}
]
76 changes: 49 additions & 27 deletions tests/cases/parser/traits16.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -79,52 +79,74 @@
},
"modifiers": [],
"targetName": {
"ScopedPropertyAccessExpression": {
"scopeResolutionQualifier": {
"QualifiedName": {
"globalSpecifier": null,
"relativeSpecifier": null,
"nameParts": [
{
"kind": "Name",
"textLength": 1
}
]
"QualifiedName": {
"globalSpecifier": null,
"relativeSpecifier": null,
"nameParts": [
{
"kind": "Name",
"textLength": 1
}
},
"doubleColon": {
"kind": "ColonColonToken",
"textLength": 2
},
"memberName": {
"kind": "Name",
"textLength": 1
}
]
}
}
},
"remainingTargetNames": []
}
},
{
"kind": "SemicolonToken",
"textLength": 1
}
]
}
},
"closeBrace": {
"error": "MissingToken",
"kind": "CloseBraceToken",
"textLength": 1
"textLength": 0
}
}
},
{
"error": "SkippedToken",
"kind": "ColonColonToken",
"textLength": 2
}
],
"closeBrace": {
"error": "MissingToken",
"kind": "CloseBraceToken",
"textLength": 1
"textLength": 0
}
}
}
}
},
{
"ExpressionStatement": {
"expression": {
"QualifiedName": {
"globalSpecifier": null,
"relativeSpecifier": null,
"nameParts": [
{
"kind": "Name",
"textLength": 1
}
]
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
},
{
"error": "SkippedToken",
"kind": "CloseBraceToken",
"textLength": 1
},
{
"error": "SkippedToken",
"kind": "CloseBraceToken",
"textLength": 1
}
],
"endOfFileToken": {
Expand Down
3 changes: 2 additions & 1 deletion tests/cases/parser/traits17.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@
}
]
}
}
},
"remainingTargetNames": []
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion tests/cases/parser/traits19.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@
}
]
}
}
},
"remainingTargetNames": []
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion tests/cases/parser/traits20.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
}
]
}
}
},
"remainingTargetNames": []
}
},
{
Expand Down
6 changes: 4 additions & 2 deletions tests/cases/parser/traits21.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
}
]
}
}
},
"remainingTargetNames": []
}
},
{
Expand Down Expand Up @@ -155,7 +156,8 @@
}
]
}
}
},
"remainingTargetNames": []
}
},
{
Expand Down
6 changes: 4 additions & 2 deletions tests/cases/parser/traits22.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@
}
]
}
}
},
"remainingTargetNames": []
}
},
{
Expand Down Expand Up @@ -147,7 +148,8 @@
}
]
}
}
},
"remainingTargetNames": []
}
},
{
Expand Down
8 changes: 8 additions & 0 deletions tests/cases/parser/traits24.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
// https://github.com/Microsoft/tolerant-php-parser/issues/98

class A {
use X, Y, Z {
\X::b insteadof Y, Z;
}
}
1 change: 1 addition & 0 deletions tests/cases/parser/traits24.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Loading

0 comments on commit 7647b60

Please sign in to comment.