From fafdb1a18a3a14993bc9ed0dbe906a680e3819df Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 23 Nov 2014 22:55:20 +1100 Subject: [PATCH] add exponentiation operator --- doc/differences.md | 1 + doc/features.md | 9 +++++++++ doc/index.md | 1 + lib/6to5/transformation/transform.js | 1 + .../transformers/es7-exponentiation-operator.js | 16 ++++++++++++++++ package.json | 2 +- .../assignment/actual.js | 1 + .../assignment/expected.js | 3 +++ .../es7-exponentian-operator/binary/actual.js | 1 + .../es7-exponentian-operator/binary/expected.js | 3 +++ .../es7-exponentian-operator/options.json | 3 +++ 11 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 lib/6to5/transformation/transformers/es7-exponentiation-operator.js create mode 100644 test/fixtures/transformation/es7-exponentian-operator/assignment/actual.js create mode 100644 test/fixtures/transformation/es7-exponentian-operator/assignment/expected.js create mode 100644 test/fixtures/transformation/es7-exponentian-operator/binary/actual.js create mode 100644 test/fixtures/transformation/es7-exponentian-operator/binary/expected.js create mode 100644 test/fixtures/transformation/es7-exponentian-operator/options.json diff --git a/doc/differences.md b/doc/differences.md index 8698103818..0d853a6513 100644 --- a/doc/differences.md +++ b/doc/differences.md @@ -78,6 +78,7 @@ better suited if you'd like a full ES6 environment with polyfills and all. | Constants | ✓ | ✓ | ✓ | | | | | Default parameters | ✓ | ✓ | ✓ | ✓ | ✓ | | | Destructuring | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | +| Exponentiation operator | ✓ | ✓ | | | | | | For-of | ✓ | ✓ | ✓ | ✓ | ✓ | | | Generators | ✓ | ✓ | | ✓ | | | | Generator comprehension | ✓ | ✓ | | | | | diff --git a/doc/features.md b/doc/features.md index ab33d0333c..e503a2ab54 100644 --- a/doc/features.md +++ b/doc/features.md @@ -130,6 +130,15 @@ var [a] = []; a === undefined; ``` +## Exponentiation operator ([experimental](usage.md#experimental)) ([spec](https://github.com/rwaldron/exponentiation-operator)) + +```javascript +var a = 2; +a **= 2; + +var squared = 2 ** 2; +``` + ## For-of ```javascript diff --git a/doc/index.md b/doc/index.md index 3a0ef4d384..e4a47ad72a 100644 --- a/doc/index.md +++ b/doc/index.md @@ -40,6 +40,7 @@ And it doesn't end here! To see all the ways you can use 6to5, check out the - [Constants](features.md#constants) - [Default parameters](features.md#default-parameters) - [Destructuring](features.md#destructuring) + - [Exponentiation operator](features.md#exponentiation-operator) ([experimental](usage.md#experimental)) - [For-of](features.md#for-of) - [Generators](features.md#generators) - [Generator comprehension](features.md#generator-comprehension) ([experimental](usage.md#experimental)) diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index f799391d9d..f3df8d952c 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -39,6 +39,7 @@ _.each({ computedPropertyNames: require("./transformers/es6-computed-property-names"), objectSpread: require("./transformers/es7-object-spread"), + exponentiationOperator: require("./transformers/es7-exponentiation-operator"), spread: require("./transformers/es6-spread"), templateLiterals: require("./transformers/es6-template-literals"), propertyMethodAssignment: require("./transformers/es5-property-method-assignment"), diff --git a/lib/6to5/transformation/transformers/es7-exponentiation-operator.js b/lib/6to5/transformation/transformers/es7-exponentiation-operator.js new file mode 100644 index 0000000000..af5858cdd8 --- /dev/null +++ b/lib/6to5/transformation/transformers/es7-exponentiation-operator.js @@ -0,0 +1,16 @@ +// https://github.com/rwaldron/exponentiation-operator + +var t = require("../../types"); +var pow = t.memberExpression(t.identifier("Math"), t.identifier("pow")); + +exports.AssignmentExpression = function (node) { + if (node.operator !== "**=") return; + node.operator = "="; + node.right = t.callExpression(pow, [node.left, node.right]); +}; + +exports.BinaryExpression = function (node) { + if (node.operator !== "**") return; + + return t.callExpression(pow, [node.left, node.right]); +}; diff --git a/package.json b/package.json index a62b040db2..6dd27ef857 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "chokidar": "0.11.1", "source-map-support": "0.2.8", "esutils": "1.1.6", - "acorn-6to5": "0.9.1-6", + "acorn-6to5": "0.9.1-7", "estraverse": "1.8.0", "private": "0.1.6" }, diff --git a/test/fixtures/transformation/es7-exponentian-operator/assignment/actual.js b/test/fixtures/transformation/es7-exponentian-operator/assignment/actual.js new file mode 100644 index 0000000000..86a33753a9 --- /dev/null +++ b/test/fixtures/transformation/es7-exponentian-operator/assignment/actual.js @@ -0,0 +1 @@ +num **= 2; diff --git a/test/fixtures/transformation/es7-exponentian-operator/assignment/expected.js b/test/fixtures/transformation/es7-exponentian-operator/assignment/expected.js new file mode 100644 index 0000000000..c1803c3164 --- /dev/null +++ b/test/fixtures/transformation/es7-exponentian-operator/assignment/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +num = Math.pow(num, 2); diff --git a/test/fixtures/transformation/es7-exponentian-operator/binary/actual.js b/test/fixtures/transformation/es7-exponentian-operator/binary/actual.js new file mode 100644 index 0000000000..ae4150c205 --- /dev/null +++ b/test/fixtures/transformation/es7-exponentian-operator/binary/actual.js @@ -0,0 +1 @@ +2 ** 2; diff --git a/test/fixtures/transformation/es7-exponentian-operator/binary/expected.js b/test/fixtures/transformation/es7-exponentian-operator/binary/expected.js new file mode 100644 index 0000000000..ea7fbea158 --- /dev/null +++ b/test/fixtures/transformation/es7-exponentian-operator/binary/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +Math.pow(2, 2); diff --git a/test/fixtures/transformation/es7-exponentian-operator/options.json b/test/fixtures/transformation/es7-exponentian-operator/options.json new file mode 100644 index 0000000000..252f473a73 --- /dev/null +++ b/test/fixtures/transformation/es7-exponentian-operator/options.json @@ -0,0 +1,3 @@ +{ + "experimental": true +}