Skip to content

Commit

Permalink
Fix parsing error in exponent expressions with unary left-hand sides.
Browse files Browse the repository at this point in the history
Esprima (correctly) rejects expressions like -1**2.
However, expressions like (-1)**2 are valid but still rejected.
This commit fixes this issue by identifying when the left operand
is parenthesized.

Fixes jquery#1981
  • Loading branch information
luiscubal committed Mar 8, 2021
1 parent 70c0159 commit b04ee63
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 1 deletion.
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
}
}
}
]
}

0 comments on commit b04ee63

Please sign in to comment.