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 |
| --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| 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.

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 |
| ---------------------------- | ----- | ------- | -------------- | ------ | ------ | ----------- |
| Abstract references | ✓ | | | | | |
| Array comprehension | ✓ | ✓ | ✓ | | | |
| Arrow functions | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Async functions | ✓ | ✓ | | ✓ | | |

View File

@ -1,5 +1,13 @@
# 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))
```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
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)
- [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)

View File

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

View File

@ -27,8 +27,7 @@ Node.prototype.isUserWhitespacable = function () {
var parent = this.parent;
var node = this.node;
if (t.isUserWhitespacable(node) ||
t.isSequenceExpression(parent)) {
if (t.isUserWhitespacable(node)) {
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") {
require("es6-symbol/implement");
}
require("es6-shim");
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"),
forOf: require("./transformers/es6-for-of"),
unicodeRegex: require("./transformers/es6-unicode-regex"),
abstractReferences: require("./transformers/es7-abstract-references"),
react: require("./transformers/react"),
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"],
"ArrayPattern": ["elements"],
"ArrowFunctionExpression": ["params", "defaults", "rest", "body"],
"AssignmentExpression": ["left", "right"],
"AwaitExpression": ["argument"],
"BinaryExpression": ["left", "right"],
"BlockStatement": ["body"],
"BreakStatement": ["label"],
"CallExpression": ["callee", "arguments"],
"CatchClause": ["param", "body"],
"ClassBody": ["body"],
"ClassDeclaration": ["id", "body", "superClass"],
"ClassExpression": ["id", "body", "superClass"],
"ComprehensionBlock": ["left", "right", "body"],
"ComprehensionExpression": ["filter", "blocks", "body"],
"ConditionalExpression": ["test", "consequent", "alternate"],
"ContinueStatement": ["label"],
"DebuggerStatement": [],
"DoWhileStatement": ["body", "test"],
"EmptyStatement": [],
"ExportBatchSpecifier": [],
"ExportDeclaration": ["declaration", "specifiers", "source"],
"ExportSpecifier": ["id", "name"],
"ExpressionStatement": ["expression"],
"File": ["program"],
"ForInStatement": ["left", "right", "body"],
"ForOfStatement": ["left", "right", "body"],
"ForStatement": ["init", "test", "update", "body"],
"FunctionDeclaration": ["id", "params", "defaults", "rest", "body"],
"FunctionExpression": ["id", "params", "defaults", "rest", "body"],
"Identifier": [],
"IfStatement": ["test", "consequent", "alternate"],
"ImportBatchSpecifier": ["id"],
"ImportDeclaration": ["specifiers", "source"],
"ImportSpecifier": ["id", "name"],
"LabeledStatement": ["label", "body"],
"Literal": [],
"LogicalExpression": ["left", "right"],
"MemberExpression": ["object", "property"],
"MethodDefinition": ["key", "value"],
"NewExpression": ["callee", "arguments"],
"ObjectExpression": ["properties"],
"ObjectPattern": ["properties"],
"ParenthesizedExpression": ["expression"],
"Program": ["body"],
"Property": ["key", "value"],
"ReturnStatement": ["argument"],
"SequenceExpression": ["expressions"],
"SpreadElement": ["argument"],
"SwitchCase": ["test", "consequent"],
"SwitchStatement": ["discriminant", "cases"],
"TaggedTemplateExpression": ["tag", "quasi"],
"TemplateElement": [],
"TemplateLiteral": ["quasis", "expressions"],
"ThisExpression": [],
"ThrowStatement": ["argument"],
"TryStatement": ["block", "handlers", "handler", "guardedHandlers", "finalizer"],
"UnaryExpression": ["argument"],
"UpdateExpression": ["argument"],
"VariableDeclaration": ["declarations"],
"VariableDeclarator": ["id", "init"],
"WhileStatement": ["test", "body"],
"WithStatement": ["object", "body"],
"XJSAttribute": ["name", "value"],
"XJSClosingElement": ["name"],
"XJSElement": ["openingElement", "closingElement", "children"],
"XJSEmptyExpression": [],
"XJSExpressionContainer": ["expression"],
"XJSIdentifier": [],
"XJSMemberExpression": ["object", "property"],
"XJSNamespacedName": ["namespace", "name"],
"XJSOpeningElement": ["name", "attributes"],
"XJSSpreadAttribute": ["argument"],
"YieldExpression": ["argument"]
"ArrayExpression": ["elements"],
"ArrayPattern": ["elements"],
"ArrowFunctionExpression": ["params", "defaults", "rest", "body"],
"AssignmentExpression": ["left", "right"],
"AwaitExpression": ["argument"],
"BinaryExpression": ["left", "right"],
"BlockStatement": ["body"],
"BreakStatement": ["label"],
"CallExpression": ["callee", "arguments"],
"CatchClause": ["param", "body"],
"ClassBody": ["body"],
"ClassDeclaration": ["id", "body", "superClass"],
"ClassExpression": ["id", "body", "superClass"],
"ComprehensionBlock": ["left", "right", "body"],
"ComprehensionExpression": ["filter", "blocks", "body"],
"ConditionalExpression": ["test", "consequent", "alternate"],
"ContinueStatement": ["label"],
"DebuggerStatement": [],
"DoWhileStatement": ["body", "test"],
"EmptyStatement": [],
"ExportBatchSpecifier": [],
"ExportDeclaration": ["declaration", "specifiers", "source"],
"ExportSpecifier": ["id", "name"],
"ExpressionStatement": ["expression"],
"File": ["program"],
"ForInStatement": ["left", "right", "body"],
"ForOfStatement": ["left", "right", "body"],
"ForStatement": ["init", "test", "update", "body"],
"FunctionDeclaration": ["id", "params", "defaults", "rest", "body"],
"FunctionExpression": ["id", "params", "defaults", "rest", "body"],
"Identifier": [],
"IfStatement": ["test", "consequent", "alternate"],
"ImportBatchSpecifier": ["id"],
"ImportDeclaration": ["specifiers", "source"],
"ImportSpecifier": ["id", "name"],
"LabeledStatement": ["label", "body"],
"Literal": [],
"LogicalExpression": ["left", "right"],
"MemberExpression": ["object", "property"],
"MethodDefinition": ["key", "value"],
"NewExpression": ["callee", "arguments"],
"ObjectExpression": ["properties"],
"ObjectPattern": ["properties"],
"ParenthesizedExpression": ["expression"],
"Program": ["body"],
"Property": ["key", "value"],
"ReturnStatement": ["argument"],
"SequenceExpression": ["expressions"],
"SpreadElement": ["argument"],
"SwitchCase": ["test", "consequent"],
"SwitchStatement": ["discriminant", "cases"],
"TaggedTemplateExpression": ["tag", "quasi"],
"TemplateElement": [],
"TemplateLiteral": ["quasis", "expressions"],
"ThisExpression": [],
"ThrowStatement": ["argument"],
"TryStatement": ["block", "handlers", "handler", "guardedHandlers", "finalizer"],
"UnaryExpression": ["argument"],
"UpdateExpression": ["argument"],
"VariableDeclaration": ["declarations"],
"VariableDeclarator": ["id", "init"],
"VirtualPropertyExpression": ["left", "right"],
"WhileStatement": ["test", "body"],
"WithStatement": ["object", "body"],
"XJSAttribute": ["name", "value"],
"XJSClosingElement": ["name"],
"XJSElement": ["openingElement", "closingElement", "children"],
"XJSEmptyExpression": [],
"XJSExpressionContainer": ["expression"],
"XJSIdentifier": [],
"XJSMemberExpression": ["object", "property"],
"XJSNamespacedName": ["namespace", "name"],
"XJSOpeningElement": ["name", "attributes"],
"XJSSpreadAttribute": ["argument"],
"YieldExpression": ["argument"]
}

View File

@ -47,7 +47,7 @@
"chokidar": "0.11.1",
"source-map-support": "0.2.8",
"esutils": "1.1.6",
"acorn-6to5": "0.9.1-4",
"acorn-6to5": "0.9.1-5",
"estraverse": "1.8.0",
"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)) ;