Fix exponential operator to behave according to spec (#75)

This commit is contained in:
Daniel Tschinder 2016-07-17 11:08:12 +02:00 committed by GitHub
parent f5df4b9411
commit 97325592fa
11 changed files with 378 additions and 2 deletions

View File

@ -188,7 +188,13 @@ pp.parseExprOp = function(left, leftStartPos, leftStartLoc, minPrec, noIn) {
node.left = left;
node.operator = this.state.value;
if (node.operator === "**" && left.type === "UnaryExpression" && left.extra && !left.extra.parenthesizedArgument) {
if (
node.operator === "**" &&
left.type === "UnaryExpression" &&
left.extra &&
!left.extra.parenthesizedArgument &&
!left.extra.parenthesized
) {
this.raise(left.argument.start, "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.");
}
@ -217,9 +223,10 @@ pp.parseMaybeUnary = function (refShorthandDefaultPos) {
this.next();
let argType = this.state.type;
this.addExtra(node, "parenthesizedArgument", argType === tt.parenL);
node.argument = this.parseMaybeUnary();
this.addExtra(node, "parenthesizedArgument", argType === tt.parenL && (!node.argument.extra || !node.argument.extra.parenthesized));
if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
this.unexpected(refShorthandDefaultPos.start);
}

View File

@ -0,0 +1 @@
3 ** 2;

View File

@ -0,0 +1,105 @@
{
"type": "File",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 7
}
},
"program": {
"type": "Program",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 7
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 7
}
},
"expression": {
"type": "BinaryExpression",
"start": 0,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
}
},
"left": {
"type": "NumericLiteral",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
}
},
"extra": {
"rawValue": 3,
"raw": "3"
},
"value": 3
},
"operator": "**",
"right": {
"type": "NumericLiteral",
"start": 5,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 6
}
},
"extra": {
"rawValue": 2,
"raw": "2"
},
"value": 2
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
-(5 ** 6);

View File

@ -0,0 +1,126 @@
{
"type": "File",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"program": {
"type": "Program",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"expression": {
"type": "UnaryExpression",
"start": 0,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 9
}
},
"operator": "-",
"prefix": true,
"argument": {
"type": "BinaryExpression",
"start": 2,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 8
}
},
"left": {
"type": "NumericLiteral",
"start": 2,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 3
}
},
"extra": {
"rawValue": 5,
"raw": "5"
},
"value": 5
},
"operator": "**",
"right": {
"type": "NumericLiteral",
"start": 7,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 8
}
},
"extra": {
"rawValue": 6,
"raw": "6"
},
"value": 6
},
"extra": {
"parenthesized": true,
"parenStart": 1
}
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
(-5) ** 6;

View File

@ -0,0 +1,127 @@
{
"type": "File",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"program": {
"type": "Program",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"expression": {
"type": "BinaryExpression",
"start": 0,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 9
}
},
"left": {
"type": "UnaryExpression",
"start": 1,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 3
}
},
"operator": "-",
"prefix": true,
"argument": {
"type": "NumericLiteral",
"start": 2,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 3
}
},
"extra": {
"rawValue": 5,
"raw": "5"
},
"value": 5
},
"extra": {
"parenthesizedArgument": false,
"parenthesized": true,
"parenStart": 0
}
},
"operator": "**",
"right": {
"type": "NumericLiteral",
"start": 8,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 9
}
},
"extra": {
"rawValue": 6,
"raw": "6"
},
"value": 6
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
-(5) ** 6;

View File

@ -0,0 +1,3 @@
{
"throws": "Illegal expression. Wrap left hand side or entire exponentiation in parentheses. (1:2)"
}

View File

@ -0,0 +1 @@
(-5 ** 6);

View File

@ -0,0 +1,3 @@
{
"throws": "Illegal expression. Wrap left hand side or entire exponentiation in parentheses. (1:2)"
}