Fix exponential operator to behave according to spec (#75)
This commit is contained in:
parent
f5df4b9411
commit
97325592fa
@ -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);
|
||||
}
|
||||
|
||||
1
test/fixtures/experimental/exponentiation-operator/basic/actual.js
vendored
Normal file
1
test/fixtures/experimental/exponentiation-operator/basic/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
3 ** 2;
|
||||
105
test/fixtures/experimental/exponentiation-operator/basic/expected.json
vendored
Normal file
105
test/fixtures/experimental/exponentiation-operator/basic/expected.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
1
test/fixtures/experimental/exponentiation-operator/parenthesized-all/actual.js
vendored
Normal file
1
test/fixtures/experimental/exponentiation-operator/parenthesized-all/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
-(5 ** 6);
|
||||
126
test/fixtures/experimental/exponentiation-operator/parenthesized-all/expected.json
vendored
Normal file
126
test/fixtures/experimental/exponentiation-operator/parenthesized-all/expected.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
1
test/fixtures/experimental/exponentiation-operator/parenthesized-left/actual.js
vendored
Normal file
1
test/fixtures/experimental/exponentiation-operator/parenthesized-left/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
(-5) ** 6;
|
||||
127
test/fixtures/experimental/exponentiation-operator/parenthesized-left/expected.json
vendored
Normal file
127
test/fixtures/experimental/exponentiation-operator/parenthesized-left/expected.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
1
test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-all/actual.js
vendored
Normal file
1
test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-all/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
-(5) ** 6;
|
||||
3
test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-all/options.json
vendored
Normal file
3
test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-all/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "Illegal expression. Wrap left hand side or entire exponentiation in parentheses. (1:2)"
|
||||
}
|
||||
1
test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-left/actual.js
vendored
Normal file
1
test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-left/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
(-5 ** 6);
|
||||
3
test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-left/options.json
vendored
Normal file
3
test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-left/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "Illegal expression. Wrap left hand side or entire exponentiation in parentheses. (1:2)"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user