From 6dd3696bf6f3b7acdaad9dab7902e4ca7d1f69f1 Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Sun, 21 May 2017 22:53:45 +1000 Subject: [PATCH] Fix: Add exponentiation operators (fixes #280) (#281) --- lib/node-utils.js | 2 + tests/fixtures/ast/Assignment-Operators.json | 74 +++- .../ast/Multiplicative-Operators.json | 73 +++- .../exponential-operators.result.js | 403 ++++++++++++++++++ .../exponential-operators.src.js | 2 + 5 files changed, 552 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/ecma-features/exponentiationOperators/exponential-operators.result.js create mode 100644 tests/fixtures/ecma-features/exponentiationOperators/exponential-operators.src.js diff --git a/lib/node-utils.js b/lib/node-utils.js index aafa359..5550270 100644 --- a/lib/node-utils.js +++ b/lib/node-utils.js @@ -64,6 +64,7 @@ TOKEN_TO_TEXT[SyntaxKind.EqualsGreaterThanToken] = "=>"; TOKEN_TO_TEXT[SyntaxKind.PlusToken] = "+"; TOKEN_TO_TEXT[SyntaxKind.MinusToken] = "-"; TOKEN_TO_TEXT[SyntaxKind.AsteriskToken] = "*"; +TOKEN_TO_TEXT[SyntaxKind.AsteriskAsteriskToken] = "**"; TOKEN_TO_TEXT[SyntaxKind.SlashToken] = "/"; TOKEN_TO_TEXT[SyntaxKind.PercentToken] = "%"; TOKEN_TO_TEXT[SyntaxKind.PlusPlusToken] = "++"; @@ -85,6 +86,7 @@ TOKEN_TO_TEXT[SyntaxKind.EqualsToken] = "="; TOKEN_TO_TEXT[SyntaxKind.PlusEqualsToken] = "+="; TOKEN_TO_TEXT[SyntaxKind.MinusEqualsToken] = "-="; TOKEN_TO_TEXT[SyntaxKind.AsteriskEqualsToken] = "*="; +TOKEN_TO_TEXT[SyntaxKind.AsteriskAsteriskEqualsToken] = "**="; TOKEN_TO_TEXT[SyntaxKind.SlashEqualsToken] = "/="; TOKEN_TO_TEXT[SyntaxKind.PercentEqualsToken] = "%="; TOKEN_TO_TEXT[SyntaxKind.LessThanLessThanEqualsToken] = "<<="; diff --git a/tests/fixtures/ast/Assignment-Operators.json b/tests/fixtures/ast/Assignment-Operators.json index b03cfde..9281067 100644 --- a/tests/fixtures/ast/Assignment-Operators.json +++ b/tests/fixtures/ast/Assignment-Operators.json @@ -575,6 +575,78 @@ } } }, + "x **= 42": { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "**=", + "left": { + "type": "Identifier", + "name": "x", + "range": [ + 0, + 1 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "Literal", + "value": 42, + "raw": "42", + "range": [ + 6, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + "range": [ + 0, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + "range": [ + 0, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, "x <<= 42": { "type": "ExpressionStatement", "expression": { @@ -1007,4 +1079,4 @@ } } } -} \ No newline at end of file +} diff --git a/tests/fixtures/ast/Multiplicative-Operators.json b/tests/fixtures/ast/Multiplicative-Operators.json index 418f1f3..7b64264 100644 --- a/tests/fixtures/ast/Multiplicative-Operators.json +++ b/tests/fixtures/ast/Multiplicative-Operators.json @@ -211,5 +211,76 @@ "column": 5 } } + }, + "x ** y": { + "type": "ExpressionStatement", + "expression": { + "type": "BinaryExpression", + "operator": "**", + "left": { + "type": "Identifier", + "name": "x", + "range": [ + 0, + 1 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "Identifier", + "name": "y", + "range": [ + 5, + 6 + ], + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + "range": [ + 0, + 6 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + "range": [ + 0, + 6 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + } } -} \ No newline at end of file +} diff --git a/tests/fixtures/ecma-features/exponentiationOperators/exponential-operators.result.js b/tests/fixtures/ecma-features/exponentiationOperators/exponential-operators.result.js new file mode 100644 index 0000000..083e46e --- /dev/null +++ b/tests/fixtures/ecma-features/exponentiationOperators/exponential-operators.result.js @@ -0,0 +1,403 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "range": [ + 0, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "range": [ + 4, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "range": [ + 4, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "name": "x" + }, + "init": { + "type": "BinaryExpression", + "range": [ + 8, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "operator": "**", + "left": { + "type": "Literal", + "range": [ + 8, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "value": 2, + "raw": "2" + }, + "right": { + "type": "Literal", + "range": [ + 13, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "value": 3, + "raw": "3" + } + } + } + ], + "kind": "var" + }, + { + "type": "ExpressionStatement", + "range": [ + 16, + 24 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "expression": { + "type": "BinaryExpression", + "range": [ + 16, + 23 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "operator": "**=", + "left": { + "type": "Identifier", + "range": [ + 16, + 17 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 1 + } + }, + "name": "x" + }, + "right": { + "type": "Literal", + "range": [ + 22, + 23 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "value": 4, + "raw": "4" + } + } + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "var", + "range": [ + 0, + 3 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "Identifier", + "value": "x", + "range": [ + 4, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 6, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + { + "type": "Numeric", + "value": "2", + "range": [ + 8, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + } + }, + { + "type": "Punctuator", + "value": "**", + "range": [ + 10, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + { + "type": "Numeric", + "value": "3", + "range": [ + 13, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 14, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + } + } + }, + { + "type": "Identifier", + "value": "x", + "range": [ + 16, + 17 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 1 + } + } + }, + { + "type": "Punctuator", + "value": "**=", + "range": [ + 18, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 5 + } + } + }, + { + "type": "Numeric", + "value": "4", + "range": [ + 22, + 23 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + } + } + } + ] +} diff --git a/tests/fixtures/ecma-features/exponentiationOperators/exponential-operators.src.js b/tests/fixtures/ecma-features/exponentiationOperators/exponential-operators.src.js new file mode 100644 index 0000000..b04a6f3 --- /dev/null +++ b/tests/fixtures/ecma-features/exponentiationOperators/exponential-operators.src.js @@ -0,0 +1,2 @@ +var x = 2 ** 3; +x **= 4;