Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7f425d2c6e | ||
|
|
311a8e042b | ||
|
|
27d30329fd | ||
|
|
12c5a3e73b | ||
|
|
2a166a6ed1 | ||
|
|
896929378e | ||
|
|
ae439d27b9 | ||
|
|
e22798261a | ||
|
|
d0af8b8d0a | ||
|
|
beaa2fa540 | ||
|
|
b8cac9787e | ||
|
|
64f6e4a0c5 | ||
|
|
c4a7ac5a8b | ||
|
|
1ed682fa76 | ||
|
|
9987e7fe0e | ||
|
|
e74c7cb0b7 |
10
CHANGELOG.md
10
CHANGELOG.md
@@ -1,3 +1,13 @@
|
||||
# 1.12.2
|
||||
|
||||
* Upgrade `matcha` to `0.6.0` and `browserify` to `6.3.2`.
|
||||
* Add own `trimRight` helper instead of relying on the string instance method.
|
||||
* Support JSX spreads that aren't the first.
|
||||
|
||||
# 1.12.1
|
||||
|
||||
* Fix `this` and `arguments` mapping in the `_aliasFunctions` transformer.
|
||||
|
||||
# 1.12.0
|
||||
|
||||
* Combine `jsx` and `react` transformers to `react`.
|
||||
|
||||
10
FEATURES.md
10
FEATURES.md
@@ -6,6 +6,16 @@
|
||||
[for (i of [1, 2, 3]) i * i]; // [1, 4, 9]
|
||||
```
|
||||
|
||||
## Async functions
|
||||
|
||||
```javascript
|
||||
async function chainAnimationsAsync(elem, animations) {
|
||||
for (var anim of animations) {
|
||||
await anim(elem);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Arrow functions
|
||||
|
||||
```javascript
|
||||
|
||||
@@ -55,6 +55,7 @@ It's as easy as:
|
||||
## [Features](FEATURES.md)
|
||||
|
||||
- [Array comprehension](FEATURES.md#array-comprehension)
|
||||
- [Async functions](FEATURES.md#async-functions) via [regenerator](https://github.com/facebook/regenerator)
|
||||
- [Arrow functions](FEATURES.md#arrow-functions)
|
||||
- [Classes](FEATURES.md#classes)
|
||||
- [Computed property names](FEATURES.md#computed-property-names)
|
||||
|
||||
@@ -11,7 +11,7 @@ function Buffer(position, format) {
|
||||
}
|
||||
|
||||
Buffer.prototype.get = function () {
|
||||
return this.buf.trimRight();
|
||||
return util.trimRight(this.buf);
|
||||
};
|
||||
|
||||
Buffer.prototype.getIndent = function () {
|
||||
@@ -116,7 +116,7 @@ Buffer.prototype.endsWith = function (str) {
|
||||
|
||||
Buffer.prototype.isLast = function (cha, trimRight) {
|
||||
var buf = this.buf;
|
||||
if (trimRight) buf = buf.trimRight();
|
||||
if (trimRight) buf = util.trimRight(buf);
|
||||
|
||||
var chars = [].concat(cha);
|
||||
return _.contains(chars, _.last(buf));
|
||||
|
||||
@@ -46,7 +46,6 @@ _.each({
|
||||
letScoping: require("./transformers/let-scoping"),
|
||||
forOf: require("./transformers/for-of"),
|
||||
unicodeRegex: require("./transformers/unicode-regex"),
|
||||
numericLiterals: require("./transformers/numeric-literals"),
|
||||
|
||||
react: require("./transformers/react"),
|
||||
|
||||
|
||||
@@ -5,12 +5,12 @@ var _ = require("lodash");
|
||||
|
||||
exports.ClassDeclaration = function (node, parent, file, scope) {
|
||||
return t.variableDeclaration("var", [
|
||||
t.variableDeclarator(node.id, buildClass(node, file, scope))
|
||||
t.variableDeclarator(node.id, new Class(node, file, scope).run())
|
||||
]);
|
||||
};
|
||||
|
||||
exports.ClassExpression = function (node, parent, file, scope) {
|
||||
return buildClass(node, file, scope);
|
||||
return new Class(node, file, scope).run();
|
||||
};
|
||||
|
||||
var getMemberExpressionObject = function (node) {
|
||||
@@ -20,31 +20,59 @@ var getMemberExpressionObject = function (node) {
|
||||
return node;
|
||||
};
|
||||
|
||||
var buildClass = function (node, file, scope) {
|
||||
var superName = node.superClass;
|
||||
var className = node.id || t.identifier(file.generateUid("class", scope));
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Node} node
|
||||
* @param {File} file
|
||||
* @param {Scope} scope
|
||||
*/
|
||||
|
||||
var superClassArgument = node.superClass;
|
||||
var superClassCallee = node.superClass;
|
||||
function Class(node, file, scope) {
|
||||
this.scope = scope;
|
||||
this.node = node;
|
||||
this.file = file;
|
||||
|
||||
this.instanceMutatorMap = {};
|
||||
this.staticMutatorMap = {};
|
||||
this.hasConstructor = false;
|
||||
this.className = node.id || t.identifier(file.generateUid("class", scope));
|
||||
this.superName = node.superClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @returns {Array}
|
||||
*/
|
||||
|
||||
Class.prototype.run = function () {
|
||||
var superClassArgument = this.superName;
|
||||
var superClassCallee = this.superName;
|
||||
var superName = this.superName;
|
||||
var className = this.className;
|
||||
var file = this.file;
|
||||
|
||||
if (superName) {
|
||||
if (t.isMemberExpression(superName)) {
|
||||
superClassArgument = superClassCallee = getMemberExpressionObject(superName);
|
||||
} else if (!t.isIdentifier(superName)) {
|
||||
superClassArgument = superName;
|
||||
superClassCallee = superName = t.identifier(file.generateUid("ref", scope));
|
||||
superClassCallee = superName = t.identifier(file.generateUid("ref", this.scope));
|
||||
}
|
||||
}
|
||||
|
||||
this.superName = superName;
|
||||
|
||||
var container = util.template("class", {
|
||||
CLASS_NAME: className
|
||||
});
|
||||
|
||||
var block = container.callee.expression.body;
|
||||
var body = block.body;
|
||||
var constructor = body[0].declarations[0].init;
|
||||
var body = this.body = block.body;
|
||||
var constructor = this.constructor = body[0].declarations[0].init;
|
||||
|
||||
if (node.id) constructor.id = className;
|
||||
if (this.node.id) constructor.id = className;
|
||||
|
||||
var returnStatement = body.pop();
|
||||
|
||||
@@ -55,14 +83,7 @@ var buildClass = function (node, file, scope) {
|
||||
container.callee.expression.params.push(superClassCallee);
|
||||
}
|
||||
|
||||
buildClassBody({
|
||||
file: file,
|
||||
body: body,
|
||||
node: node,
|
||||
className: className,
|
||||
superName: superName,
|
||||
constructor: constructor,
|
||||
});
|
||||
this.buildBody();
|
||||
|
||||
if (body.length === 1) {
|
||||
// only a constructor so no need for a closure container
|
||||
@@ -73,49 +94,29 @@ var buildClass = function (node, file, scope) {
|
||||
}
|
||||
};
|
||||
|
||||
var buildClassBody = function (opts) {
|
||||
var file = opts.file;
|
||||
var body = opts.body;
|
||||
var node = opts.node;
|
||||
var constructor = opts.constructor;
|
||||
var className = opts.className;
|
||||
var superName = opts.superName;
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
|
||||
var instanceMutatorMap = {};
|
||||
var staticMutatorMap = {};
|
||||
var hasConstructor = false;
|
||||
|
||||
var classBody = node.body.body;
|
||||
Class.prototype.buildBody = function () {
|
||||
var constructor = this.constructor;
|
||||
var className = this.className;
|
||||
var superName = this.superName;
|
||||
var classBody = this.node.body.body;
|
||||
var body = this.body;
|
||||
var self = this;
|
||||
|
||||
_.each(classBody, function (node) {
|
||||
var methodName = node.key;
|
||||
var method = node.value;
|
||||
|
||||
replaceInstanceSuperReferences(superName, node);
|
||||
self.replaceInstanceSuperReferences(node);
|
||||
|
||||
if (node.key.name === "constructor") {
|
||||
if (node.kind === "") {
|
||||
hasConstructor = true;
|
||||
addConstructor(constructor, method);
|
||||
} else {
|
||||
throw file.errorWithNode(node, "illegal kind for constructor method");
|
||||
}
|
||||
self.pushConstructor(node);
|
||||
} else {
|
||||
var mutatorMap = instanceMutatorMap;
|
||||
if (node.static) mutatorMap = staticMutatorMap;
|
||||
|
||||
var kind = node.kind;
|
||||
|
||||
if (kind === "") {
|
||||
kind = "value";
|
||||
util.pushMutatorMap(mutatorMap, methodName, "writable", t.identifier("true"));
|
||||
}
|
||||
|
||||
util.pushMutatorMap(mutatorMap, methodName, kind, node);
|
||||
self.pushMethod(node);
|
||||
}
|
||||
});
|
||||
|
||||
if (!hasConstructor && superName) {
|
||||
if (!this.hasConstructor && superName) {
|
||||
constructor.body.body.push(util.template("class-super-constructor-call", {
|
||||
SUPER_NAME: superName
|
||||
}, true));
|
||||
@@ -124,16 +125,16 @@ var buildClassBody = function (opts) {
|
||||
var instanceProps;
|
||||
var staticProps;
|
||||
|
||||
if (!_.isEmpty(instanceMutatorMap)) {
|
||||
if (!_.isEmpty(this.instanceMutatorMap)) {
|
||||
var protoId = util.template("prototype-identifier", {
|
||||
CLASS_NAME: className
|
||||
});
|
||||
|
||||
instanceProps = util.buildDefineProperties(instanceMutatorMap, protoId);
|
||||
instanceProps = util.buildDefineProperties(this.instanceMutatorMap, protoId);
|
||||
}
|
||||
|
||||
if (!_.isEmpty(staticMutatorMap)) {
|
||||
staticProps = util.buildDefineProperties(staticMutatorMap, className);
|
||||
if (!_.isEmpty(this.staticMutatorMap)) {
|
||||
staticProps = util.buildDefineProperties(this.staticMutatorMap, className);
|
||||
}
|
||||
|
||||
if (instanceProps || staticProps) {
|
||||
@@ -143,17 +144,50 @@ var buildClassBody = function (opts) {
|
||||
if (instanceProps) args.push(instanceProps);
|
||||
|
||||
body.push(t.expressionStatement(
|
||||
t.callExpression(file.addDeclaration("class-props"), args)
|
||||
t.callExpression(this.file.addDeclaration("class-props"), args)
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
var superIdentifier = function (superName, methodNode, node, parent) {
|
||||
var methodName = methodNode.key;
|
||||
/**
|
||||
* Push a method to it's respective mutatorMap.
|
||||
*
|
||||
* @param {Node} node MethodDefinition
|
||||
*/
|
||||
|
||||
if (parent.property === node) {
|
||||
Class.prototype.pushMethod = function (node) {
|
||||
var methodName = node.key;
|
||||
|
||||
var mutatorMap = this.instanceMutatorMap;
|
||||
if (node.static) mutatorMap = this.staticMutatorMap;
|
||||
|
||||
var kind = node.kind;
|
||||
|
||||
if (kind === "") {
|
||||
kind = "value";
|
||||
util.pushMutatorMap(mutatorMap, methodName, "writable", t.identifier("true"));
|
||||
}
|
||||
|
||||
util.pushMutatorMap(mutatorMap, methodName, kind, node);
|
||||
};
|
||||
|
||||
/**
|
||||
* Given a `methodNode`, produce a `MemberExpression` super class reference.
|
||||
*
|
||||
* @param {Node} methodNode MethodDefinition
|
||||
* @param {Node} node Identifier
|
||||
* @param {Node} parent
|
||||
*
|
||||
* @returns {Node}
|
||||
*/
|
||||
|
||||
Class.prototype.superIdentifier = function (methodNode, id, parent) {
|
||||
var methodName = methodNode.key;
|
||||
var superName = this.superName || t.identifier("Function");
|
||||
|
||||
if (parent.property === id) {
|
||||
return;
|
||||
} else if (t.isCallExpression(parent, { callee: node })) {
|
||||
} else if (t.isCallExpression(parent, { callee: id })) {
|
||||
// super(); -> ClassName.prototype.MethodName.call(this);
|
||||
parent.arguments.unshift(t.thisExpression());
|
||||
|
||||
@@ -161,15 +195,15 @@ var superIdentifier = function (superName, methodNode, node, parent) {
|
||||
// constructor() { super(); }
|
||||
return t.memberExpression(superName, t.identifier("call"));
|
||||
} else {
|
||||
node = superName;
|
||||
id = superName;
|
||||
|
||||
// foo() { super(); }
|
||||
if (!methodNode.static) {
|
||||
node = t.memberExpression(node, t.identifier("prototype"));
|
||||
id = t.memberExpression(id, t.identifier("prototype"));
|
||||
}
|
||||
|
||||
node = t.memberExpression(node, methodName, methodNode.computed);
|
||||
return t.memberExpression(node, t.identifier("call"));
|
||||
id = t.memberExpression(id, methodName, methodNode.computed);
|
||||
return t.memberExpression(id, t.identifier("call"));
|
||||
}
|
||||
} else if (t.isMemberExpression(parent) && !methodNode.static) {
|
||||
// super.test -> ClassName.prototype.test
|
||||
@@ -179,31 +213,50 @@ var superIdentifier = function (superName, methodNode, node, parent) {
|
||||
}
|
||||
};
|
||||
|
||||
var replaceInstanceSuperReferences = function (superName, methodNode) {
|
||||
var method = methodNode.value;
|
||||
/**
|
||||
* Replace all `super` references with a reference to our `superClass`.
|
||||
*
|
||||
* @param {Node} methodNode MethodDefinition
|
||||
*/
|
||||
|
||||
superName = superName || t.identifier("Function");
|
||||
Class.prototype.replaceInstanceSuperReferences = function (methodNode) {
|
||||
var method = methodNode.value;
|
||||
var self = this;
|
||||
|
||||
traverse(method, function (node, parent) {
|
||||
if (t.isIdentifier(node, { name: "super" })) {
|
||||
return superIdentifier(superName, methodNode, node, parent);
|
||||
return self.superIdentifier(methodNode, node, parent);
|
||||
} else if (t.isCallExpression(node)) {
|
||||
var callee = node.callee;
|
||||
if (!t.isMemberExpression(callee)) return;
|
||||
if (callee.object.name !== "super") return;
|
||||
|
||||
// super.test(); -> ClassName.prototype.MethodName.call(this);
|
||||
callee.property.name = callee.property.name + ".call";
|
||||
// super.test(); -> ClassName.prototype.MethodName.call(this);
|
||||
callee.property = t.memberExpression(callee.property, t.identifier("call"));
|
||||
node.arguments.unshift(t.thisExpression());
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var addConstructor = function (construct, method) {
|
||||
construct.defaults = method.defaults;
|
||||
construct.params = method.params;
|
||||
construct.body = method.body;
|
||||
construct.rest = method.rest;
|
||||
/**
|
||||
* Replace the constructor body of our class.
|
||||
*
|
||||
* @param {Node} method MethodDefinition
|
||||
*/
|
||||
|
||||
t.inherits(construct, method);
|
||||
Class.prototype.pushConstructor = function (method) {
|
||||
if (method.kind !== "") {
|
||||
throw this.file.errorWithNode(method, "illegal kind for constructor method");
|
||||
}
|
||||
|
||||
var construct = this.constructor;
|
||||
var fn = method.value;
|
||||
|
||||
this.hasConstructor = true;
|
||||
t.inherits(construct, fn);
|
||||
|
||||
construct.defaults = fn.defaults;
|
||||
construct.params = fn.params;
|
||||
construct.body = fn.body;
|
||||
construct.rest = fn.rest;
|
||||
};
|
||||
|
||||
@@ -17,6 +17,12 @@ var isVar = function (node) {
|
||||
return t.isVariableDeclaration(node, { kind: "var" }) && !isLet(node);
|
||||
};
|
||||
|
||||
var standardiseLets = function (declars) {
|
||||
_.each(declars, function (declar) {
|
||||
delete declar._let;
|
||||
});
|
||||
};
|
||||
|
||||
exports.VariableDeclaration = function (node) {
|
||||
isLet(node);
|
||||
};
|
||||
@@ -33,7 +39,8 @@ exports.For = function (node, parent, file, scope) {
|
||||
node.label = parent.label;
|
||||
}
|
||||
|
||||
run(node, node.body, parent, file, scope);
|
||||
var letScoping = new LetScoping(node, node.body, parent, file, scope);
|
||||
letScoping.run();
|
||||
|
||||
if (node.label && !t.isLabeledStatement(parent)) {
|
||||
// we've been given a label so let's wrap ourselves
|
||||
@@ -43,12 +50,98 @@ exports.For = function (node, parent, file, scope) {
|
||||
|
||||
exports.BlockStatement = function (block, parent, file, scope) {
|
||||
if (!t.isFor(parent)) {
|
||||
run(false, block, parent, file, scope);
|
||||
var letScoping = new LetScoping(false, block, parent, file, scope);
|
||||
letScoping.run();
|
||||
}
|
||||
};
|
||||
|
||||
var noClosure = function (letDeclars, block, replacements) {
|
||||
standardiseLets(letDeclars);
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Boolean|Node} forParent
|
||||
* @param {Node} block
|
||||
* @param {Node} parent
|
||||
* @param {File} file
|
||||
* @param {Scope} scope
|
||||
*/
|
||||
|
||||
function LetScoping(forParent, block, parent, file, scope) {
|
||||
this.forParent = forParent;
|
||||
this.parent = parent;
|
||||
this.scope = scope;
|
||||
this.block = block;
|
||||
this.file = file;
|
||||
|
||||
this.letReferences = {};
|
||||
this.body = [];
|
||||
this.info = this.getInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the ball rolling.
|
||||
*/
|
||||
|
||||
LetScoping.prototype.run = function () {
|
||||
var block = this.block;
|
||||
|
||||
if (block._letDone) return;
|
||||
block._letDone = true;
|
||||
|
||||
// this is a block within a `Function` so we can safely leave it be
|
||||
if (t.isFunction(this.parent)) return;
|
||||
|
||||
// this block has no let references so let's clean up
|
||||
if (!this.info.keys.length) return this.noClosure();
|
||||
|
||||
// returns whether or not there are any outside let references within any
|
||||
// functions
|
||||
var referencesInClosure = this.getLetReferences();
|
||||
|
||||
// no need for a closure so let's clean up
|
||||
if (!referencesInClosure) return this.noClosure();
|
||||
|
||||
// if we're inside of a for loop then we search to see if there are any
|
||||
// `break`s, `continue`s, `return`s etc
|
||||
this.has = this.checkFor();
|
||||
|
||||
// hoist var references to retain scope
|
||||
this.hoistVarDeclarations();
|
||||
|
||||
// set let references to plain var references
|
||||
standardiseLets(this.info.declarators);
|
||||
|
||||
// turn letReferences into an array
|
||||
var letReferences = _.values(this.letReferences);
|
||||
|
||||
// build the closure that we're going to wrap the block with
|
||||
var fn = t.functionExpression(null, letReferences, t.blockStatement(block.body));
|
||||
fn._aliasFunction = true;
|
||||
|
||||
// replace the current block body with the one we're going to build
|
||||
block.body = this.body;
|
||||
|
||||
// change upper scope references with their uid if they have one
|
||||
var params = this.getParams(letReferences);
|
||||
|
||||
// build a call and a unique id that we can assign the return value to
|
||||
var call = t.callExpression(fn, params);
|
||||
var ret = t.identifier(this.file.generateUid("ret", this.scope));
|
||||
|
||||
this.build(ret, call);
|
||||
};
|
||||
|
||||
/**
|
||||
* There are no let references accessed within a closure so we can just traverse
|
||||
* through this block and replace all references that exist in a high scope to
|
||||
* their uids.
|
||||
*/
|
||||
|
||||
LetScoping.prototype.noClosure = function () {
|
||||
var replacements = this.info.duplicates;
|
||||
var declarators = this.info.declarators;
|
||||
var block = this.block;
|
||||
|
||||
standardiseLets(declarators);
|
||||
|
||||
if (_.isEmpty(replacements)) return;
|
||||
|
||||
@@ -59,13 +152,17 @@ var noClosure = function (letDeclars, block, replacements) {
|
||||
});
|
||||
};
|
||||
|
||||
var standardiseLets = function (declars) {
|
||||
_.each(declars, function (declar) {
|
||||
delete declar._let;
|
||||
});
|
||||
};
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @returns {Object}
|
||||
*/
|
||||
|
||||
LetScoping.prototype.getInfo = function () {
|
||||
var block = this.block;
|
||||
var scope = this.scope;
|
||||
var file = this.file;
|
||||
|
||||
var getInfo = function (block, file, scope) {
|
||||
var opts = {
|
||||
// array of `Identifier` names of let variables that appear lexically out of
|
||||
// this scope but should be accessible - eg. `ForOfStatement`.left
|
||||
@@ -111,15 +208,23 @@ var getInfo = function (block, file, scope) {
|
||||
return opts;
|
||||
};
|
||||
|
||||
var checkFor = function (forParent, block) {
|
||||
/**
|
||||
* If we're inside of a `For*Statement` then traverse it and check if it has one
|
||||
* of the following node types `ReturnStatement`, `BreakStatement`,
|
||||
* `ContinueStatement` and replace it with a return value we can track later on.
|
||||
*
|
||||
* @returns {Object}
|
||||
*/
|
||||
|
||||
LetScoping.prototype.checkFor = function () {
|
||||
var has = {
|
||||
hasContinue: false,
|
||||
hasReturn: false,
|
||||
hasBreak: false,
|
||||
};
|
||||
|
||||
if (forParent) {
|
||||
traverse(block, function (node) {
|
||||
if (this.forParent) {
|
||||
traverse(this.block, function (node) {
|
||||
var replace;
|
||||
|
||||
if (t.isFunction(node) || t.isFor(node)) {
|
||||
@@ -144,38 +249,59 @@ var checkFor = function (forParent, block) {
|
||||
return has;
|
||||
};
|
||||
|
||||
var hoistVarDeclarations = function (block, pushDeclar) {
|
||||
traverse(block, function (node) {
|
||||
/**
|
||||
* Hoist all var declarations in this block to before it so they retain scope
|
||||
* once we wrap everything is in a closure.
|
||||
*/
|
||||
|
||||
LetScoping.prototype.hoistVarDeclarations = function () {
|
||||
var self = this;
|
||||
traverse(this.block, function (node) {
|
||||
if (t.isForStatement(node)) {
|
||||
if (isVar(node.init)) {
|
||||
node.init = t.sequenceExpression(pushDeclar(node.init));
|
||||
node.init = t.sequenceExpression(self.pushDeclar(node.init));
|
||||
}
|
||||
} else if (t.isFor(node)) {
|
||||
if (isVar(node.left)) {
|
||||
node.left = node.left.declarations[0].id;
|
||||
}
|
||||
} else if (isVar(node)) {
|
||||
return pushDeclar(node).map(t.expressionStatement);
|
||||
return self.pushDeclar(node).map(t.expressionStatement);
|
||||
} else if (t.isFunction(node)) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var getParams = function (info, letReferences) {
|
||||
var params = _.cloneDeep(letReferences);
|
||||
/**
|
||||
* Build up a parameter list that we'll call our closure wrapper with, replacing
|
||||
* all duplicate ids with their uid.
|
||||
*
|
||||
* @param {Array} params
|
||||
* @returns {Array}
|
||||
*/
|
||||
|
||||
LetScoping.prototype.getParams = function (params) {
|
||||
var info = this.info;
|
||||
params = _.cloneDeep(params);
|
||||
_.each(params, function (param) {
|
||||
param.name = info.duplicates[param.name] || param.name;
|
||||
});
|
||||
return params;
|
||||
};
|
||||
|
||||
var getLetReferences = function (block, info, letReferences) {
|
||||
/**
|
||||
* Get all let references within this block. Stopping whenever we reach another
|
||||
* block.
|
||||
*/
|
||||
|
||||
LetScoping.prototype.getLetReferences = function () {
|
||||
var closurify = false;
|
||||
var self = this;
|
||||
|
||||
// traverse through this block, stopping on functions and checking if they
|
||||
// contain any outside let references
|
||||
traverse(block, function (node, parent, scope) {
|
||||
traverse(this.block, function (node, parent, scope) {
|
||||
if (t.isFunction(node)) {
|
||||
traverse(node, function (node, parent) {
|
||||
// not an identifier so we have no use
|
||||
@@ -191,10 +317,10 @@ var getLetReferences = function (block, info, letReferences) {
|
||||
closurify = true;
|
||||
|
||||
// this key doesn't appear just outside our scope
|
||||
if (!_.contains(info.outsideKeys, node.name)) return;
|
||||
if (!_.contains(self.info.outsideKeys, node.name)) return;
|
||||
|
||||
// push this badboy
|
||||
letReferences[node.name] = node;
|
||||
self.letReferences[node.name] = node;
|
||||
});
|
||||
|
||||
return false;
|
||||
@@ -206,125 +332,100 @@ var getLetReferences = function (block, info, letReferences) {
|
||||
return closurify;
|
||||
};
|
||||
|
||||
var buildPushDeclar = function (body) {
|
||||
return function (node) {
|
||||
body.push(t.variableDeclaration(node.kind, node.declarations.map(function (declar) {
|
||||
return t.variableDeclarator(declar.id);
|
||||
})));
|
||||
/**
|
||||
* Turn a `VariableDeclaration` into an array of `AssignmentExpressions` with
|
||||
* their declarations hoisted to before the closure wrapper.
|
||||
*
|
||||
* @param {Node} node VariableDeclaration
|
||||
* @returns {Array}
|
||||
*/
|
||||
|
||||
var replace = [];
|
||||
LetScoping.prototype.buildPushDeclar = function (node) {
|
||||
this.body.push(t.variableDeclaration(node.kind, node.declarations.map(function (declar) {
|
||||
return t.variableDeclarator(declar.id);
|
||||
})));
|
||||
|
||||
_.each(node.declarations, function (declar) {
|
||||
if (!declar.init) return;
|
||||
var replace = [];
|
||||
|
||||
var expr = t.assignmentExpression("=", declar.id, declar.init);
|
||||
replace.push(t.inherits(expr, declar));
|
||||
});
|
||||
_.each(node.declarations, function (declar) {
|
||||
if (!declar.init) return;
|
||||
|
||||
return replace;
|
||||
};
|
||||
var expr = t.assignmentExpression("=", declar.id, declar.init);
|
||||
replace.push(t.inherits(expr, declar));
|
||||
});
|
||||
|
||||
return replace;
|
||||
};
|
||||
|
||||
var run = function (forParent, block, parent, file, scope) {
|
||||
if (block._letDone) return;
|
||||
block._letDone = true;
|
||||
|
||||
var info = getInfo(block, file, scope);
|
||||
var declarators = info.declarators;
|
||||
var letKeys = info.keys;
|
||||
|
||||
// this is a block within a `Function` so we can safely leave it be
|
||||
if (t.isFunction(parent)) return;
|
||||
|
||||
// this block has no let references so let's clean up
|
||||
if (!letKeys.length) return noClosure(declarators, block, info.duplicates);
|
||||
|
||||
// outside let references that we need to wrap
|
||||
var letReferences = {};
|
||||
|
||||
// returns whether or not there are any outside let references within any
|
||||
// functions
|
||||
var closurify = getLetReferences(block, info, letReferences);
|
||||
|
||||
letReferences = _.values(letReferences);
|
||||
|
||||
// no need for a closure so let's clean up
|
||||
if (!closurify) return noClosure(declarators, block, info.duplicates);
|
||||
|
||||
// if we're inside of a for loop then we search to see if there are any
|
||||
// `break`s, `continue`s, `return`s etc
|
||||
var has = checkFor(forParent, block);
|
||||
|
||||
var body = [];
|
||||
|
||||
// hoist a `VariableDeclaration` and add `AssignmentExpression`s in it's place
|
||||
var pushDeclar = buildPushDeclar(body);
|
||||
|
||||
// hoist var references to retain scope
|
||||
hoistVarDeclarations(block, pushDeclar);
|
||||
|
||||
// set let references to plain var references
|
||||
standardiseLets(declarators);
|
||||
|
||||
// build the closure that we're going to wrap the block with
|
||||
var fn = t.functionExpression(null, letReferences, t.blockStatement(block.body));
|
||||
fn._aliasFunction = true;
|
||||
|
||||
// replace the current block body with the one we've built
|
||||
block.body = body;
|
||||
|
||||
// change duplicate let references to their uid if they have one
|
||||
var params = getParams(info, letReferences);
|
||||
|
||||
var call = t.callExpression(fn, params);
|
||||
var ret = t.identifier(file.generateUid("ret", scope));
|
||||
/**
|
||||
* Push the closure to the body.
|
||||
*
|
||||
* @param {Node} ret Identifier
|
||||
* @param {Node} call CallExpression
|
||||
*/
|
||||
|
||||
LetScoping.prototype.build = function (ret, call) {
|
||||
var has = this.has;
|
||||
if (has.hasReturn || has.hasBreak || has.hasContinue) {
|
||||
body.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(ret, call)
|
||||
]));
|
||||
|
||||
var retCheck;
|
||||
|
||||
var cases = [];
|
||||
|
||||
if (has.hasReturn) {
|
||||
// typeof ret === "object"
|
||||
retCheck = util.template("let-scoping-return", {
|
||||
RETURN: ret,
|
||||
});
|
||||
}
|
||||
|
||||
if (has.hasBreak || has.hasContinue) {
|
||||
// ensure that the parent has a label as we're building a switch and we
|
||||
// need to be able to access it
|
||||
var label = forParent.label = forParent.label || t.identifier(file.generateUid("loop", scope));
|
||||
|
||||
if (has.hasBreak) {
|
||||
cases.push(t.switchCase(t.literal("break"), [t.breakStatement(label)]));
|
||||
}
|
||||
|
||||
if (has.hasContinue) {
|
||||
cases.push(t.switchCase(t.literal("continue"), [t.continueStatement(label)]));
|
||||
}
|
||||
|
||||
if (has.hasReturn) {
|
||||
cases.push(t.switchCase(null, [retCheck]));
|
||||
}
|
||||
|
||||
if (cases.length === 1) {
|
||||
var single = cases[0];
|
||||
body.push(t.ifStatement(
|
||||
t.binaryExpression("===", ret, single.test),
|
||||
single.consequent[0]
|
||||
));
|
||||
} else {
|
||||
body.push(t.switchStatement(ret, cases));
|
||||
}
|
||||
} else {
|
||||
if (has.hasReturn) body.push(retCheck);
|
||||
}
|
||||
this.buildHas(ret, call);
|
||||
} else {
|
||||
body.push(t.expressionStatement(call));
|
||||
this.body.push(t.expressionStatement(call));
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Node} ret Identifier
|
||||
* @param {Node} call CallExpression
|
||||
*/
|
||||
|
||||
LetScoping.prototype.buildHas = function (ret, call) {
|
||||
var body = this.body;
|
||||
|
||||
body.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(ret, call)
|
||||
]));
|
||||
|
||||
var forParent = this.forParent;
|
||||
var retCheck;
|
||||
var has = this.has;
|
||||
var cases = [];
|
||||
|
||||
if (has.hasReturn) {
|
||||
// typeof ret === "object"
|
||||
retCheck = util.template("let-scoping-return", {
|
||||
RETURN: ret
|
||||
});
|
||||
}
|
||||
|
||||
if (has.hasBreak || has.hasContinue) {
|
||||
// ensure that the parent has a label as we're building a switch and we
|
||||
// need to be able to access it
|
||||
var label = forParent.label = forParent.label || t.identifier(this.file.generateUid("loop", this.scope));
|
||||
|
||||
if (has.hasBreak) {
|
||||
cases.push(t.switchCase(t.literal("break"), [t.breakStatement(label)]));
|
||||
}
|
||||
|
||||
if (has.hasContinue) {
|
||||
cases.push(t.switchCase(t.literal("continue"), [t.continueStatement(label)]));
|
||||
}
|
||||
|
||||
if (has.hasReturn) {
|
||||
cases.push(t.switchCase(null, [retCheck]));
|
||||
}
|
||||
|
||||
if (cases.length === 1) {
|
||||
var single = cases[0];
|
||||
body.push(t.ifStatement(
|
||||
t.binaryExpression("===", ret, single.test),
|
||||
single.consequent[0]
|
||||
));
|
||||
} else {
|
||||
body.push(t.switchStatement(ret, cases));
|
||||
}
|
||||
} else {
|
||||
if (has.hasReturn) body.push(retCheck);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.Literal = function (node) {
|
||||
// TODO: remove this when the new code generator is released
|
||||
if (_.isNumber(node.value)) delete node.raw;
|
||||
};
|
||||
36
lib/6to5/transformation/transformers/react.js
vendored
36
lib/6to5/transformation/transformers/react.js
vendored
@@ -57,15 +57,39 @@ exports.XJSOpeningElement = {
|
||||
|
||||
var props = node.attributes;
|
||||
if (props.length) {
|
||||
var first = props[0];
|
||||
if (t.isXJSSpreadAttribute(first)) {
|
||||
props.shift();
|
||||
var _props = [];
|
||||
var objs = [];
|
||||
|
||||
var pushProps = function () {
|
||||
if (!_props.length) return;
|
||||
|
||||
objs.push(t.objectExpression(_props));
|
||||
_props = [];
|
||||
};
|
||||
|
||||
while (props.length) {
|
||||
var prop = props.shift();
|
||||
if (t.isXJSSpreadAttribute(prop)) {
|
||||
pushProps();
|
||||
objs.push(prop.argument);
|
||||
} else {
|
||||
_props.push(prop);
|
||||
}
|
||||
}
|
||||
|
||||
pushProps();
|
||||
|
||||
if (objs.length === 1) {
|
||||
props = objs[0];
|
||||
} else {
|
||||
if (!t.isObjectExpression(objs[0])) {
|
||||
objs.unshift(t.objectExpression([]));
|
||||
}
|
||||
|
||||
props = t.callExpression(
|
||||
t.memberExpression(t.identifier("React"), t.identifier("__spread")),
|
||||
[t.objectExpression([]), first.argument, t.objectExpression(props)]
|
||||
objs
|
||||
);
|
||||
} else {
|
||||
props = t.objectExpression(props);
|
||||
}
|
||||
} else {
|
||||
props = t.literal(null);
|
||||
|
||||
@@ -25,6 +25,10 @@ exports.resolve = function (loc) {
|
||||
}
|
||||
};
|
||||
|
||||
exports.trimRight = function (str) {
|
||||
return str.replace(/[\n\s]+$/g, "");
|
||||
};
|
||||
|
||||
exports.list = function (val) {
|
||||
return val ? val.split(",") : [];
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "6to5",
|
||||
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
|
||||
"version": "1.12.1",
|
||||
"version": "1.12.2",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://github.com/6to5/6to5",
|
||||
"repository": {
|
||||
@@ -53,10 +53,10 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"istanbul": "0.3.2",
|
||||
"matcha": "0.5.0",
|
||||
"matcha": "0.6.0",
|
||||
"mocha": "2.0.1",
|
||||
"uglify-js": "2.4.15",
|
||||
"browserify": "6.2.0",
|
||||
"browserify": "6.3.2",
|
||||
"rimraf": "2.2.8",
|
||||
"jshint": "2.5.10",
|
||||
"chai": "^1.9.2"
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<Component y={2} z { ... x } />
|
||||
@@ -0,0 +1,4 @@
|
||||
React.createElement(Component, React.__spread({
|
||||
y: 2,
|
||||
z: true
|
||||
}, x));
|
||||
@@ -0,0 +1 @@
|
||||
<Component y={2} { ... x } z />
|
||||
@@ -0,0 +1,5 @@
|
||||
React.createElement(Component, React.__spread({
|
||||
y: 2
|
||||
}, x, {
|
||||
z: true
|
||||
}));
|
||||
Reference in New Issue
Block a user