From 02c42b94f56c453a0b296e364879d5a3c5cee053 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 23 Nov 2014 16:04:25 +1100 Subject: [PATCH] Add abstract references base #205 --- doc/caveats.md | 13 ++------- doc/differences.md | 1 + doc/features.md | 8 +++++ doc/index.md | 1 + lib/6to5/polyfill.js | 29 +++++++++++++++++++ lib/6to5/transformation/transform.js | 1 + .../transformers/abstract-references.js | 0 .../abstract-references/delete/actual.js | 1 + .../abstract-references/delete/expected.js | 3 ++ .../abstract-references/get/actual.js | 1 + .../abstract-references/get/expected.js | 3 ++ .../abstract-references/set/actual.js | 1 + .../abstract-references/set/expected.js | 3 ++ 13 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 lib/6to5/transformation/transformers/abstract-references.js create mode 100644 test/fixtures/transformation/abstract-references/delete/actual.js create mode 100644 test/fixtures/transformation/abstract-references/delete/expected.js create mode 100644 test/fixtures/transformation/abstract-references/get/actual.js create mode 100644 test/fixtures/transformation/abstract-references/get/expected.js create mode 100644 test/fixtures/transformation/abstract-references/set/actual.js create mode 100644 test/fixtures/transformation/abstract-references/set/expected.js diff --git a/doc/caveats.md b/doc/caveats.md index af8fb6b60f..3d70e833d0 100644 --- a/doc/caveats.md +++ b/doc/caveats.md @@ -6,9 +6,11 @@ satisfy **all** 6to5 feature requirements by using the included | Feature | Requirements | | --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | +| Abstract References | `Symbol` | | Array destructuring | `Array.isArray`, `Array.from` | | Async functions, Generators | [experimental option](usage.md#experimental), [regenerator runtime](https://github.com/facebook/regenerator/blob/master/runtime.js) | | Comprehensions | [experimental option](usage.md#experimental), `Array.isArray`, `Array.from` | +| For..Of | `Symbol`, `prototype[Symbol.iterator]` | | Spread | `Array.isArray`, `Array.from` | ## Classes @@ -36,14 +38,3 @@ class Bar extends Foo { } } ``` - -## Constructor spread - -Constructor spreads do not currently support built-ins. ie. -`new Array(...items)`. - -## For-of - -A polyfill is required for for-of functionality that implements `Symbol` and -adds `prototype[Symbol.iterator]` behaviour to built-ins. Using the polyfills -specified in [polyfill](polyfill.md) suffices. diff --git a/doc/differences.md b/doc/differences.md index 0a887a0ec0..81a07e0b27 100644 --- a/doc/differences.md +++ b/doc/differences.md @@ -69,6 +69,7 @@ better suited if you'd like a full ES6 environment with polyfills and all. | | 6to5 | Traceur | es6-transpiler | esnext | es6now | jstransform | | ---------------------------- | ----- | ------- | -------------- | ------ | ------ | ----------- | +| Abstract references | ✓ | | | | | | | Array comprehension | ✓ | ✓ | ✓ | | | | | Arrow functions | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Async functions | ✓ | ✓ | | ✓ | | | diff --git a/doc/features.md b/doc/features.md index 1d2732533d..1c0e42c215 100644 --- a/doc/features.md +++ b/doc/features.md @@ -1,5 +1,13 @@ # Features +## Abstract references ([experimental](usage.md#experimental)) + +```javascript +foo::bar; +foo::bar = baz; +delete foo::bar; +``` + ## Array comprehension ([experimental](usage.md#experimental)) ```javascript diff --git a/doc/index.md b/doc/index.md index 0e986d4869..45325bbfde 100644 --- a/doc/index.md +++ b/doc/index.md @@ -31,6 +31,7 @@ And it doesn't end here! To see all the ways you can use 6to5, check out the ## [Features](features.md) + - [Abstract references](features.md#abstract-references) ([experimental](usage.md#experimental)) - [Array comprehension](features.md#array-comprehension) ([experimental](usage.md#experimental)) - [Async functions](features.md#async-functions) ([experimental](usage.md#experimental)) - [Arrow functions](features.md#arrow-functions) diff --git a/lib/6to5/polyfill.js b/lib/6to5/polyfill.js index 343d5d3b18..332d317eee 100644 --- a/lib/6to5/polyfill.js +++ b/lib/6to5/polyfill.js @@ -1,6 +1,35 @@ +// + +var ensureSymbol = function (key) { + Symbol[key] = Symbol[key] || Symbol(); +}; + +var ensureProto = function (Constructor, key, val) { + var proto = Constructor.prototype; + proto[key] = proto[key] || val; +}; + +// + if (typeof Symbol === "undefined") { require("es6-symbol/implement"); } require("es6-shim"); require("./transformation/transformers/generators/runtime"); + +// Abstract references + +ensureSymbol("referenceGet"); +ensureSymbol("referenceSet"); +ensureSymbol("referenceDelete"); + +ensureProto(Function, Symbol.referenceGet, function () { return this }); + +ensureProto(Map, Symbol.referenceGet, Map.prototype.get); +ensureProto(Map, Symbol.referenceSet, Map.prototype.set); +ensureProto(Map, Symbol.referenceDelete, Map.prototype.delete); + +ensureProto(WeakMap, Symbol.referenceGet, WeakMap.prototype.get); +ensureProto(WeakMap, Symbol.referenceSet, WeakMap.prototype.set); +ensureProto(WeakMap, Symbol.referenceDelete, WeakMap.prototype.delete); diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index a0ba465c72..5a4b807f30 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -47,6 +47,7 @@ _.each({ forOf: require("./transformers/for-of"), unicodeRegex: require("./transformers/unicode-regex"), react: require("./transformers/react"), + abstractReferences: require("./transformers/abstract-references"), constants: require("./transformers/constants"), letScoping: require("./transformers/let-scoping"), diff --git a/lib/6to5/transformation/transformers/abstract-references.js b/lib/6to5/transformation/transformers/abstract-references.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/fixtures/transformation/abstract-references/delete/actual.js b/test/fixtures/transformation/abstract-references/delete/actual.js new file mode 100644 index 0000000000..c6e232ad4c --- /dev/null +++ b/test/fixtures/transformation/abstract-references/delete/actual.js @@ -0,0 +1 @@ +delete foo::bar; diff --git a/test/fixtures/transformation/abstract-references/delete/expected.js b/test/fixtures/transformation/abstract-references/delete/expected.js new file mode 100644 index 0000000000..f49850f41d --- /dev/null +++ b/test/fixtures/transformation/abstract-references/delete/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +bar[Symbol.referenceDelete](foo); diff --git a/test/fixtures/transformation/abstract-references/get/actual.js b/test/fixtures/transformation/abstract-references/get/actual.js new file mode 100644 index 0000000000..d6a275fb29 --- /dev/null +++ b/test/fixtures/transformation/abstract-references/get/actual.js @@ -0,0 +1 @@ +foo::bar; diff --git a/test/fixtures/transformation/abstract-references/get/expected.js b/test/fixtures/transformation/abstract-references/get/expected.js new file mode 100644 index 0000000000..70419d5d74 --- /dev/null +++ b/test/fixtures/transformation/abstract-references/get/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +bar[Symbol.referenceGet](foo); diff --git a/test/fixtures/transformation/abstract-references/set/actual.js b/test/fixtures/transformation/abstract-references/set/actual.js new file mode 100644 index 0000000000..0edb7d4dd8 --- /dev/null +++ b/test/fixtures/transformation/abstract-references/set/actual.js @@ -0,0 +1 @@ +foo::bar = baz; diff --git a/test/fixtures/transformation/abstract-references/set/expected.js b/test/fixtures/transformation/abstract-references/set/expected.js new file mode 100644 index 0000000000..03749174eb --- /dev/null +++ b/test/fixtures/transformation/abstract-references/set/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +bar[Symbol.referenceSet](foo, baz);