Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing error in exponent expressions with unary left-hand sides. #2070

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,16 @@ export class Parser {
parseExponentiationExpression(): Node.Expression {
const startToken = this.lookahead;

// -1 ** 2 is not accepted, but (-1) ** 2
// However, the AST for both cases is identical
// We distinguish the two cases by explicitly checking for a parenthesis.

const isLeftParenthesized = this.match('(');
let expr = this.inheritCoverGrammar(this.parseUnaryExpression);
if (expr.type !== Syntax.UnaryExpression && this.match('**')) {

const exponentAllowed = expr.type !== Syntax.UnaryExpression || isLeftParenthesized;

if (exponentAllowed && this.match('**')) {
this.nextToken();
this.context.isAssignmentTarget = false;
this.context.isBindingElement = false;
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/ES2016/exponent/exp_paren.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(-2) ** 1;
240 changes: 240 additions & 0 deletions test/fixtures/ES2016/exponent/exp_paren.tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
{
"type": "Program",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "BinaryExpression",
"operator": "**",
"left": {
"type": "UnaryExpression",
"operator": "-",
"argument": {
"type": "Literal",
"value": 2,
"raw": "2",
"range": [
2,
3
],
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 3
}
}
},
"prefix": true,
"range": [
1,
3
],
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 3
}
}
},
"right": {
"type": "Literal",
"value": 1,
"raw": "1",
"range": [
8,
9
],
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 9
}
}
},
"range": [
0,
9
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 9
}
}
},
"range": [
0,
10
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
}
}
],
"sourceType": "script",
"range": [
0,
10
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"tokens": [
{
"type": "Punctuator",
"value": "(",
"range": [
0,
1
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
}
}
},
{
"type": "Punctuator",
"value": "-",
"range": [
1,
2
],
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 2
}
}
},
{
"type": "Numeric",
"value": "2",
"range": [
2,
3
],
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 3
}
}
},
{
"type": "Punctuator",
"value": ")",
"range": [
3,
4
],
"loc": {
"start": {
"line": 1,
"column": 3
},
"end": {
"line": 1,
"column": 4
}
}
},
{
"type": "Punctuator",
"value": "**",
"range": [
5,
7
],
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 7
}
}
},
{
"type": "Numeric",
"value": "1",
"range": [
8,
9
],
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 9
}
}
},
{
"type": "Punctuator",
"value": ";",
"range": [
9,
10
],
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 10
}
}
}
]
}