diff --git a/src/parser/expression.js b/src/parser/expression.js index 3bb8d1aecb..16e297147b 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -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); } diff --git a/test/fixtures/experimental/exponentiation-operator/basic/actual.js b/test/fixtures/experimental/exponentiation-operator/basic/actual.js new file mode 100644 index 0000000000..e340e8a676 --- /dev/null +++ b/test/fixtures/experimental/exponentiation-operator/basic/actual.js @@ -0,0 +1 @@ +3 ** 2; diff --git a/test/fixtures/experimental/exponentiation-operator/basic/expected.json b/test/fixtures/experimental/exponentiation-operator/basic/expected.json new file mode 100644 index 0000000000..b376ee6a68 --- /dev/null +++ b/test/fixtures/experimental/exponentiation-operator/basic/expected.json @@ -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": [] + } +} \ No newline at end of file diff --git a/test/fixtures/experimental/exponentiation-operator/parenthesized-all/actual.js b/test/fixtures/experimental/exponentiation-operator/parenthesized-all/actual.js new file mode 100644 index 0000000000..a9b52db6a0 --- /dev/null +++ b/test/fixtures/experimental/exponentiation-operator/parenthesized-all/actual.js @@ -0,0 +1 @@ +-(5 ** 6); diff --git a/test/fixtures/experimental/exponentiation-operator/parenthesized-all/expected.json b/test/fixtures/experimental/exponentiation-operator/parenthesized-all/expected.json new file mode 100644 index 0000000000..7908853db6 --- /dev/null +++ b/test/fixtures/experimental/exponentiation-operator/parenthesized-all/expected.json @@ -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": [] + } +} diff --git a/test/fixtures/experimental/exponentiation-operator/parenthesized-left/actual.js b/test/fixtures/experimental/exponentiation-operator/parenthesized-left/actual.js new file mode 100644 index 0000000000..28e2b045a0 --- /dev/null +++ b/test/fixtures/experimental/exponentiation-operator/parenthesized-left/actual.js @@ -0,0 +1 @@ +(-5) ** 6; diff --git a/test/fixtures/experimental/exponentiation-operator/parenthesized-left/expected.json b/test/fixtures/experimental/exponentiation-operator/parenthesized-left/expected.json new file mode 100644 index 0000000000..53c11f64ee --- /dev/null +++ b/test/fixtures/experimental/exponentiation-operator/parenthesized-left/expected.json @@ -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": [] + } +} \ No newline at end of file diff --git a/test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-all/actual.js b/test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-all/actual.js new file mode 100644 index 0000000000..63fce35a8a --- /dev/null +++ b/test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-all/actual.js @@ -0,0 +1 @@ +-(5) ** 6; diff --git a/test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-all/options.json b/test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-all/options.json new file mode 100644 index 0000000000..f04c5c035a --- /dev/null +++ b/test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-all/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Illegal expression. Wrap left hand side or entire exponentiation in parentheses. (1:2)" +} diff --git a/test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-left/actual.js b/test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-left/actual.js new file mode 100644 index 0000000000..47b91d35a8 --- /dev/null +++ b/test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-left/actual.js @@ -0,0 +1 @@ +(-5 ** 6); diff --git a/test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-left/options.json b/test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-left/options.json new file mode 100644 index 0000000000..f04c5c035a --- /dev/null +++ b/test/fixtures/experimental/exponentiation-operator/wrong-parenthesized-left/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Illegal expression. Wrap left hand side or entire exponentiation in parentheses. (1:2)" +}