Merge branch 'abstract-references'

This commit is contained in:
Sebastian McKenzie 2014-11-23 18:38:24 +11:00
commit 4ed77e136b
24 changed files with 219 additions and 89 deletions

View File

@ -6,9 +6,11 @@ satisfy **all** 6to5 feature requirements by using the included
| Feature | Requirements | | Feature | Requirements |
| --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | | --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| Abstract References | `Symbol` |
| Array destructuring | `Array.isArray`, `Array.from` | | 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) | | 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` | | Comprehensions | [experimental option](usage.md#experimental), `Array.isArray`, `Array.from` |
| For..Of | `Symbol`, `prototype[Symbol.iterator]` |
| Spread | `Array.isArray`, `Array.from` | | Spread | `Array.isArray`, `Array.from` |
## Classes ## 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.

View File

@ -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 | | | 6to5 | Traceur | es6-transpiler | esnext | es6now | jstransform |
| ---------------------------- | ----- | ------- | -------------- | ------ | ------ | ----------- | | ---------------------------- | ----- | ------- | -------------- | ------ | ------ | ----------- |
| Abstract references | ✓ | | | | | |
| Array comprehension | ✓ | ✓ | ✓ | | | | | Array comprehension | ✓ | ✓ | ✓ | | | |
| Arrow functions | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Arrow functions | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Async functions | ✓ | ✓ | | ✓ | | | | Async functions | ✓ | ✓ | | ✓ | | |

View File

@ -1,5 +1,13 @@
# Features # Features
## Abstract references ([experimental](usage.md#experimental)) ([spec](https://github.com/zenparsing/es-abstract-refs))
```javascript
foo::bar;
foo::bar = baz;
delete foo::bar;
```
## Array comprehension ([experimental](usage.md#experimental)) ## Array comprehension ([experimental](usage.md#experimental))
```javascript ```javascript
@ -31,7 +39,7 @@ var bob = {
}; };
``` ```
## Async functions ([experimental](usage.md#experimental)) ## Async functions ([experimental](usage.md#experimental)) ([spec](https://github.com/lukehoban/ecmascript-asyncawait))
```javascript ```javascript
async function chainAnimationsAsync(elem, animations) { async function chainAnimationsAsync(elem, animations) {

View File

@ -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) ## [Features](features.md)
- [Abstract references](features.md#abstract-references) ([experimental](usage.md#experimental))
- [Array comprehension](features.md#array-comprehension) ([experimental](usage.md#experimental)) - [Array comprehension](features.md#array-comprehension) ([experimental](usage.md#experimental))
- [Async functions](features.md#async-functions) ([experimental](usage.md#experimental)) - [Async functions](features.md#async-functions) ([experimental](usage.md#experimental))
- [Arrow functions](features.md#arrow-functions) - [Arrow functions](features.md#arrow-functions)

View File

@ -9,6 +9,12 @@ exports.SpreadElement = function (node, print) {
print(node.argument); print(node.argument);
}; };
exports.VirtualPropertyExpression = function (node, print) {
print(node.object);
this.push("::");
print(node.property);
};
exports.ObjectExpression = exports.ObjectExpression =
exports.ObjectPattern = function (node, print) { exports.ObjectPattern = function (node, print) {
var props = node.properties; var props = node.properties;

View File

@ -27,8 +27,7 @@ Node.prototype.isUserWhitespacable = function () {
var parent = this.parent; var parent = this.parent;
var node = this.node; var node = this.node;
if (t.isUserWhitespacable(node) || if (t.isUserWhitespacable(node)) {
t.isSequenceExpression(parent)) {
return true; return true;
} }

View File

@ -1,6 +1,37 @@
//
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") { if (typeof Symbol === "undefined") {
require("es6-symbol/implement"); require("es6-symbol/implement");
} }
require("es6-shim"); require("es6-shim");
require("./transformation/transformers/es6-generators/runtime"); require("./transformation/transformers/es6-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);
if (global.WeakMap) {
ensureProto(WeakMap, Symbol.referenceGet, WeakMap.prototype.get);
ensureProto(WeakMap, Symbol.referenceSet, WeakMap.prototype.set);
ensureProto(WeakMap, Symbol.referenceDelete, WeakMap.prototype.delete);
}

View File

@ -0,0 +1 @@
PROPERTY[Symbol.referenceGet](OBJECT).call(OBJECT)

View File

@ -0,0 +1 @@
PROPERTY[Symbol.referenceDelete](OBJECT)

View File

@ -0,0 +1 @@
PROPERTY[Symbol.referenceGet](OBJECT)

View File

@ -0,0 +1 @@
PROPERTY[Symbol.referenceSet](OBJECT, VALUE)

View File

@ -46,6 +46,7 @@ _.each({
destructuring: require("./transformers/es6-destructuring"), destructuring: require("./transformers/es6-destructuring"),
forOf: require("./transformers/es6-for-of"), forOf: require("./transformers/es6-for-of"),
unicodeRegex: require("./transformers/es6-unicode-regex"), unicodeRegex: require("./transformers/es6-unicode-regex"),
abstractReferences: require("./transformers/es7-abstract-references"),
react: require("./transformers/react"), react: require("./transformers/react"),
constants: require("./transformers/es6-constants"), constants: require("./transformers/es6-constants"),

View File

@ -0,0 +1,60 @@
// https://github.com/zenparsing/es-abstract-refs
var util = require("../../util");
var t = require("../../types");
var container = function (parent, call, ret) {
if (t.isExpressionStatement(parent)) {
// we don't need to worry about return values
return call;
} else {
return t.sequenceExpression([call, ret]);
}
};
exports.AssignmentExpression = function (node, parent) {
var left = node.left;
if (!t.isVirtualPropertyExpression(left)) return;
var right = node.right;
var call = util.template("abstract-expression-set", {
PROPERTY: left.property,
OBJECT: left.object,
VALUE: right
});
return container(parent, call, right);
};
exports.UnaryExpression = function (node, parent) {
var arg = node.argument;
if (!t.isVirtualPropertyExpression(arg)) return;
if (node.operator !== "delete") return;
var call = util.template("abstract-expression-delete", {
PROPERTY: arg.property,
OBJECT: arg.object
});
return container(parent, call, t.literal(true));
};
exports.CallExpression = function (node, parent) {
var callee = node.callee;
if (!t.isVirtualPropertyExpression(callee)) return;
var call = util.template("abstract-expression-call", {
PROPERTY: callee.property,
OBJECT: callee.object
});
call.arguments = call.arguments.concat(node.arguments);
return call;
};
exports.VirtualPropertyExpression = function (node) {
return util.template("abstract-expression-get", {
PROPERTY: node.property,
OBJECT: node.object
});
};

View File

@ -1,76 +1,77 @@
{ {
"ArrayExpression": ["elements"], "ArrayExpression": ["elements"],
"ArrayPattern": ["elements"], "ArrayPattern": ["elements"],
"ArrowFunctionExpression": ["params", "defaults", "rest", "body"], "ArrowFunctionExpression": ["params", "defaults", "rest", "body"],
"AssignmentExpression": ["left", "right"], "AssignmentExpression": ["left", "right"],
"AwaitExpression": ["argument"], "AwaitExpression": ["argument"],
"BinaryExpression": ["left", "right"], "BinaryExpression": ["left", "right"],
"BlockStatement": ["body"], "BlockStatement": ["body"],
"BreakStatement": ["label"], "BreakStatement": ["label"],
"CallExpression": ["callee", "arguments"], "CallExpression": ["callee", "arguments"],
"CatchClause": ["param", "body"], "CatchClause": ["param", "body"],
"ClassBody": ["body"], "ClassBody": ["body"],
"ClassDeclaration": ["id", "body", "superClass"], "ClassDeclaration": ["id", "body", "superClass"],
"ClassExpression": ["id", "body", "superClass"], "ClassExpression": ["id", "body", "superClass"],
"ComprehensionBlock": ["left", "right", "body"], "ComprehensionBlock": ["left", "right", "body"],
"ComprehensionExpression": ["filter", "blocks", "body"], "ComprehensionExpression": ["filter", "blocks", "body"],
"ConditionalExpression": ["test", "consequent", "alternate"], "ConditionalExpression": ["test", "consequent", "alternate"],
"ContinueStatement": ["label"], "ContinueStatement": ["label"],
"DebuggerStatement": [], "DebuggerStatement": [],
"DoWhileStatement": ["body", "test"], "DoWhileStatement": ["body", "test"],
"EmptyStatement": [], "EmptyStatement": [],
"ExportBatchSpecifier": [], "ExportBatchSpecifier": [],
"ExportDeclaration": ["declaration", "specifiers", "source"], "ExportDeclaration": ["declaration", "specifiers", "source"],
"ExportSpecifier": ["id", "name"], "ExportSpecifier": ["id", "name"],
"ExpressionStatement": ["expression"], "ExpressionStatement": ["expression"],
"File": ["program"], "File": ["program"],
"ForInStatement": ["left", "right", "body"], "ForInStatement": ["left", "right", "body"],
"ForOfStatement": ["left", "right", "body"], "ForOfStatement": ["left", "right", "body"],
"ForStatement": ["init", "test", "update", "body"], "ForStatement": ["init", "test", "update", "body"],
"FunctionDeclaration": ["id", "params", "defaults", "rest", "body"], "FunctionDeclaration": ["id", "params", "defaults", "rest", "body"],
"FunctionExpression": ["id", "params", "defaults", "rest", "body"], "FunctionExpression": ["id", "params", "defaults", "rest", "body"],
"Identifier": [], "Identifier": [],
"IfStatement": ["test", "consequent", "alternate"], "IfStatement": ["test", "consequent", "alternate"],
"ImportBatchSpecifier": ["id"], "ImportBatchSpecifier": ["id"],
"ImportDeclaration": ["specifiers", "source"], "ImportDeclaration": ["specifiers", "source"],
"ImportSpecifier": ["id", "name"], "ImportSpecifier": ["id", "name"],
"LabeledStatement": ["label", "body"], "LabeledStatement": ["label", "body"],
"Literal": [], "Literal": [],
"LogicalExpression": ["left", "right"], "LogicalExpression": ["left", "right"],
"MemberExpression": ["object", "property"], "MemberExpression": ["object", "property"],
"MethodDefinition": ["key", "value"], "MethodDefinition": ["key", "value"],
"NewExpression": ["callee", "arguments"], "NewExpression": ["callee", "arguments"],
"ObjectExpression": ["properties"], "ObjectExpression": ["properties"],
"ObjectPattern": ["properties"], "ObjectPattern": ["properties"],
"ParenthesizedExpression": ["expression"], "ParenthesizedExpression": ["expression"],
"Program": ["body"], "Program": ["body"],
"Property": ["key", "value"], "Property": ["key", "value"],
"ReturnStatement": ["argument"], "ReturnStatement": ["argument"],
"SequenceExpression": ["expressions"], "SequenceExpression": ["expressions"],
"SpreadElement": ["argument"], "SpreadElement": ["argument"],
"SwitchCase": ["test", "consequent"], "SwitchCase": ["test", "consequent"],
"SwitchStatement": ["discriminant", "cases"], "SwitchStatement": ["discriminant", "cases"],
"TaggedTemplateExpression": ["tag", "quasi"], "TaggedTemplateExpression": ["tag", "quasi"],
"TemplateElement": [], "TemplateElement": [],
"TemplateLiteral": ["quasis", "expressions"], "TemplateLiteral": ["quasis", "expressions"],
"ThisExpression": [], "ThisExpression": [],
"ThrowStatement": ["argument"], "ThrowStatement": ["argument"],
"TryStatement": ["block", "handlers", "handler", "guardedHandlers", "finalizer"], "TryStatement": ["block", "handlers", "handler", "guardedHandlers", "finalizer"],
"UnaryExpression": ["argument"], "UnaryExpression": ["argument"],
"UpdateExpression": ["argument"], "UpdateExpression": ["argument"],
"VariableDeclaration": ["declarations"], "VariableDeclaration": ["declarations"],
"VariableDeclarator": ["id", "init"], "VariableDeclarator": ["id", "init"],
"WhileStatement": ["test", "body"], "VirtualPropertyExpression": ["left", "right"],
"WithStatement": ["object", "body"], "WhileStatement": ["test", "body"],
"XJSAttribute": ["name", "value"], "WithStatement": ["object", "body"],
"XJSClosingElement": ["name"], "XJSAttribute": ["name", "value"],
"XJSElement": ["openingElement", "closingElement", "children"], "XJSClosingElement": ["name"],
"XJSEmptyExpression": [], "XJSElement": ["openingElement", "closingElement", "children"],
"XJSExpressionContainer": ["expression"], "XJSEmptyExpression": [],
"XJSIdentifier": [], "XJSExpressionContainer": ["expression"],
"XJSMemberExpression": ["object", "property"], "XJSIdentifier": [],
"XJSNamespacedName": ["namespace", "name"], "XJSMemberExpression": ["object", "property"],
"XJSOpeningElement": ["name", "attributes"], "XJSNamespacedName": ["namespace", "name"],
"XJSSpreadAttribute": ["argument"], "XJSOpeningElement": ["name", "attributes"],
"YieldExpression": ["argument"] "XJSSpreadAttribute": ["argument"],
"YieldExpression": ["argument"]
} }

View File

@ -47,7 +47,7 @@
"chokidar": "0.11.1", "chokidar": "0.11.1",
"source-map-support": "0.2.8", "source-map-support": "0.2.8",
"esutils": "1.1.6", "esutils": "1.1.6",
"acorn-6to5": "0.9.1-4", "acorn-6to5": "0.9.1-5",
"estraverse": "1.8.0", "estraverse": "1.8.0",
"private": "0.1.6" "private": "0.1.6"
}, },

View File

@ -0,0 +1,2 @@
foo::bar();
foo::bar("arg");

View File

@ -0,0 +1,4 @@
"use strict";
bar[Symbol.referenceGet](foo).call(foo);
bar[Symbol.referenceGet](foo).call(foo, "arg");

View File

@ -0,0 +1,3 @@
delete foo::bar;
if (delete foo::bar);

View File

@ -0,0 +1,5 @@
"use strict";
bar[Symbol.referenceDelete](foo);
if ((bar[Symbol.referenceDelete](foo), true)) ;

View File

@ -0,0 +1 @@
foo::bar;

View File

@ -0,0 +1,3 @@
"use strict";
bar[Symbol.referenceGet](foo);

View File

@ -0,0 +1,3 @@
{
"experimental": true
}

View File

@ -0,0 +1,2 @@
foo::bar = baz;
if (foo::bar = baz);

View File

@ -0,0 +1,4 @@
"use strict";
bar[Symbol.referenceSet](foo, baz);
if ((bar[Symbol.referenceSet](foo, baz), baz)) ;