Skip to content

Commit b55b9ee

Browse files
committed
Fix ${x} variable syntax
1 parent e40b820 commit b55b9ee

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

src/Node/Expression/Variable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Variable extends Expression {
2424
public function getName() {
2525
if (
2626
$this->name instanceof Token &&
27-
$name = substr($this->name->getText($this->getFileContents()), 1)
27+
$name = ltrim($this->name->getText($this->getFileContents()), '$')
2828
) {
2929
return $name;
3030
}

src/Parser.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,11 @@ private function parseStringLiteralExpression2($parentNode) {
10071007
case TokenKind::DollarOpenBraceToken:
10081008
case TokenKind::OpenBraceDollarToken:
10091009
$expression->children[] = $this->eat(TokenKind::DollarOpenBraceToken, TokenKind::OpenBraceDollarToken);
1010-
$expression->children[] = $this->parseExpression($expression);
1010+
if ($this->getCurrentToken()->kind === TokenKind::StringVarname) {
1011+
$expression->children[] = $this->parseSimpleVariable($parentNode);
1012+
} else {
1013+
$expression->children[] = $this->parseExpression($expression);
1014+
}
10111015
$expression->children[] = $this->eat(TokenKind::CloseBraceToken);
10121016
continue;
10131017
case $startQuoteKind = $expression->startQuote->kind:
@@ -2027,9 +2031,10 @@ private function parseSimpleVariableFn() {
20272031
$token->kind === TokenKind::OpenBraceToken ?
20282032
$this->parseBracedExpression($variable) :
20292033
$this->parseSimpleVariable($variable);
2030-
} elseif ($token->kind === TokenKind::VariableName) {
2031-
// TODO consider splitting into dollar and name
2032-
$variable->name = $this->eat(TokenKind::VariableName);
2034+
} elseif ($token->kind === TokenKind::VariableName || $token->kind === TokenKind::StringVarname) {
2035+
// TODO consider splitting into dollar and name.
2036+
// StringVarname is the variable name without $, used in a template string e.g. `"${foo}"`
2037+
$variable->name = $this->eat(TokenKind::VariableName, TokenKind::StringVarname);
20332038
} else {
20342039
$variable->name = new MissingToken(TokenKind::VariableName, $token->fullStart);
20352040
}

tests/cases/parser/stringLiteral3.php.tree

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,13 @@
4545
"textLength": 2
4646
},
4747
{
48-
"error": "MissingToken",
49-
"kind": "Expression",
50-
"textLength": 0
51-
},
52-
{
53-
"error": "MissingToken",
54-
"kind": "CloseBraceToken",
55-
"textLength": 0
56-
},
57-
{
58-
"kind": "StringVarname",
59-
"textLength": 3
48+
"Variable": {
49+
"dollar": null,
50+
"name": {
51+
"kind": "StringVarname",
52+
"textLength": 3
53+
}
54+
}
6055
},
6156
{
6257
"kind": "CloseBraceToken",

0 commit comments

Comments
 (0)