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/esprima#1981
  • Loading branch information
jogibear9988 committed Nov 1, 2021
1 parent cd1dfd4 commit 6c0abe8
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Esprima/JavascriptParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1861,10 +1861,14 @@ private Expression ParseUnaryExpression()

private Expression ParseExponentiationExpression()
{
var startToken = _lookahead;

var expr = InheritCoverGrammar(parseUnaryExpression);
if (expr.Type != Nodes.UnaryExpression && Match("**"))
var startToken = _lookahead;

var isLeftParenthesized = this.Match("(");
var expr = InheritCoverGrammar(parseUnaryExpression);

var exponentAllowed = expr.Type != Nodes.UnaryExpression || isLeftParenthesized;

if (exponentAllowed && Match("**"))
{
NextToken();
_context.IsAssignmentTarget = false;
Expand Down

0 comments on commit 6c0abe8

Please sign in to comment.