add "fast" option for transformers
This commit is contained in:
parent
f14f72b635
commit
0f55a66f5b
@ -18,6 +18,7 @@ commander.option("-m, --modules [modules]", "Module formatter type to use [commo
|
||||
commander.option("-l, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util.list);
|
||||
commander.option("-b, --blacklist [blacklist]", "Blacklist of transformers to NOT use", util.list);
|
||||
commander.option("-i, --optional [list]", "List of optional transformers to enable", util.list);
|
||||
commander.option("--fast [list]", "List of transformers to enable their fast mode", util.list);
|
||||
commander.option("-o, --out-file [out]", "Compile all input files into a single file");
|
||||
commander.option("-d, --out-dir [out]", "Compile an input directory of modules into an output directory");
|
||||
commander.option("-c, --remove-comments", "Remove comments from the compiled code", false);
|
||||
@ -116,6 +117,7 @@ exports.opts = {
|
||||
comments: !commander.removeComments,
|
||||
runtime: commander.runtime,
|
||||
modules: commander.modules,
|
||||
fast: commander.fast,
|
||||
format: {
|
||||
indent: {
|
||||
style: util.repeat(parseInt(commander.indent))
|
||||
|
||||
@ -63,6 +63,7 @@ File.normaliseOptions = function (opts) {
|
||||
filename: "unknown",
|
||||
modules: "common",
|
||||
runtime: false,
|
||||
fast: [],
|
||||
code: true,
|
||||
ast: true
|
||||
});
|
||||
@ -73,6 +74,18 @@ File.normaliseOptions = function (opts) {
|
||||
opts.blacklist = util.arrayify(opts.blacklist);
|
||||
opts.whitelist = util.arrayify(opts.whitelist);
|
||||
opts.optional = util.arrayify(opts.optional);
|
||||
opts.fast = util.arrayify(opts.fast);
|
||||
|
||||
// todo: remove in 3.0.0
|
||||
_.each({
|
||||
fastForOf: "forOf",
|
||||
classesFastSuper: "classes"
|
||||
}, function (newTransformer, oldTransformer) {
|
||||
if (_.contains(opts.optional, oldTransformer)) {
|
||||
_.pull(opts.optional, oldTransformer);
|
||||
opts.fast.push(newTransformer);
|
||||
}
|
||||
});
|
||||
|
||||
_.defaults(opts, {
|
||||
moduleRoot: opts.sourceRoot
|
||||
@ -102,10 +115,15 @@ File.normaliseOptions = function (opts) {
|
||||
transform._ensureTransformerNames("blacklist", opts.blacklist);
|
||||
transform._ensureTransformerNames("whitelist", opts.whitelist);
|
||||
transform._ensureTransformerNames("optional", opts.optional);
|
||||
transform._ensureTransformerNames("fast", opts.fast);
|
||||
|
||||
return opts;
|
||||
};
|
||||
|
||||
File.prototype.isFast = function (key) {
|
||||
return _.contains(this.opts.fast, key);
|
||||
};
|
||||
|
||||
File.prototype.getTransformers = function () {
|
||||
var file = this;
|
||||
var transformers = [];
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
(function () {
|
||||
if (SUPER_NAME != null) {
|
||||
SUPER_NAME.apply(this, arguments);
|
||||
}
|
||||
});
|
||||
if (SUPER_NAME != null) {
|
||||
SUPER_NAME.apply(this, arguments);
|
||||
}
|
||||
|
||||
@ -59,7 +59,6 @@ _.each({
|
||||
arrayComprehension: require("./transformers/es7-array-comprehension"),
|
||||
generatorComprehension: require("./transformers/es7-generator-comprehension"),
|
||||
arrowFunctions: require("./transformers/es6-arrow-functions"),
|
||||
classesFastSuper: require("./transformers/optional-classes-fast-super"),
|
||||
classes: require("./transformers/es6-classes"),
|
||||
|
||||
objectSpread: require("./transformers/es7-object-spread"),
|
||||
@ -70,7 +69,6 @@ _.each({
|
||||
computedPropertyNames: require("./transformers/es6-computed-property-names"),
|
||||
destructuring: require("./transformers/es6-destructuring"),
|
||||
defaultParameters: require("./transformers/es6-default-parameters"),
|
||||
forOfFast: require("./transformers/optional-for-of-fast"),
|
||||
forOf: require("./transformers/es6-for-of"),
|
||||
unicodeRegex: require("./transformers/es6-unicode-regex"),
|
||||
abstractReferences: require("./transformers/es7-abstract-references"),
|
||||
|
||||
@ -33,6 +33,7 @@ function Class(node, file, scope, isStatement) {
|
||||
this.hasConstructor = false;
|
||||
this.className = node.id || file.generateUidIdentifier("class", scope);
|
||||
this.superName = node.superClass;
|
||||
this.isFast = file.isFast("classes");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -117,27 +118,31 @@ Class.prototype.buildBody = function () {
|
||||
var superName = this.superName;
|
||||
var classBody = this.node.body.body;
|
||||
var body = this.body;
|
||||
var self = this;
|
||||
|
||||
for (var i in classBody) {
|
||||
var node = classBody[i];
|
||||
if (t.isMethodDefinition(node)) {
|
||||
self.replaceInstanceSuperReferences(node);
|
||||
this.replaceInstanceSuperReferences(node);
|
||||
|
||||
if (node.key.name === "constructor") {
|
||||
self.pushConstructor(node);
|
||||
this.pushConstructor(node);
|
||||
} else {
|
||||
self.pushMethod(node);
|
||||
this.pushMethod(node);
|
||||
}
|
||||
} else if (t.isPrivateDeclaration(node)) {
|
||||
self.closure = true;
|
||||
this.closure = true;
|
||||
body.unshift(node);
|
||||
}
|
||||
}
|
||||
|
||||
// we have no constructor, we have a super, and the super doesn't appear to be falsy
|
||||
if (!this.hasConstructor && superName && !t.isFalsyExpression(superName)) {
|
||||
constructor.body.body.push(util.template("class-super-constructor-call", {
|
||||
CLASS_NAME: className
|
||||
var defaultConstructorTemplate = "class-super-constructor-call";
|
||||
if (this.isFast) defaultConstructorTemplate += "-fast";
|
||||
|
||||
constructor.body.body.push(util.template(defaultConstructorTemplate, {
|
||||
CLASS_NAME: className,
|
||||
SUPER_NAME: this.superName
|
||||
}, true));
|
||||
}
|
||||
|
||||
@ -219,6 +224,47 @@ Class.prototype.superProperty = function (property, isStatic, isComputed, thisEx
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Object} node
|
||||
* @param {Object} id
|
||||
* @param {Object} parent
|
||||
* @returns {Object}
|
||||
*/
|
||||
|
||||
Class.prototype.fastSuperProperty = 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: id })) {
|
||||
// super(); -> ClassName.prototype.MethodName.call(this);
|
||||
parent.arguments.unshift(t.thisExpression());
|
||||
|
||||
if (methodName.name === "constructor") {
|
||||
// constructor() { super(); }
|
||||
return t.memberExpression(superName, t.identifier("call"));
|
||||
} else {
|
||||
id = superName;
|
||||
|
||||
// foo() { super(); }
|
||||
if (!methodNode.static) {
|
||||
id = t.memberExpression(id, t.identifier("prototype"));
|
||||
}
|
||||
|
||||
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
|
||||
return t.memberExpression(superName, t.identifier("prototype"));
|
||||
} else {
|
||||
return superName;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Replace all `super` references with a reference to our `superClass`.
|
||||
*
|
||||
@ -231,7 +277,7 @@ Class.prototype.replaceInstanceSuperReferences = function (methodNode) {
|
||||
|
||||
var topLevelThisReference;
|
||||
|
||||
traverse2(method, true);
|
||||
traverseLevel(method, true);
|
||||
|
||||
if (topLevelThisReference) {
|
||||
method.body.body.unshift(t.variableDeclaration("var", [
|
||||
@ -239,75 +285,100 @@ Class.prototype.replaceInstanceSuperReferences = function (methodNode) {
|
||||
]));
|
||||
}
|
||||
|
||||
function traverse2(node, topLevel) {
|
||||
function traverseLevel(node, topLevel) {
|
||||
traverse(node, {
|
||||
enter: function (node, parent) {
|
||||
if (t.isFunction(node) && !t.isArrowFunctionExpression(node)) {
|
||||
traverse2(node, false);
|
||||
// we need to call traverseLevel again so we're context aware
|
||||
traverseLevel(node, false);
|
||||
return this.skip();
|
||||
}
|
||||
|
||||
var property;
|
||||
var computed;
|
||||
var args;
|
||||
|
||||
if (t.isIdentifier(node, { name: "super" })) {
|
||||
if (!(t.isMemberExpression(parent) && !parent.computed && parent.property === node)) {
|
||||
throw self.file.errorWithNode(node, "illegal use of bare super");
|
||||
}
|
||||
} else if (t.isCallExpression(node)) {
|
||||
var callee = node.callee;
|
||||
if (t.isIdentifier(callee, { name: "super" })) {
|
||||
// super(); -> _get(Object.getPrototypeOf(ClassName), "MethodName", this).call(this);
|
||||
property = methodNode.key;
|
||||
computed = methodNode.computed;
|
||||
args = node.arguments;
|
||||
} else {
|
||||
if (!t.isMemberExpression(callee)) return;
|
||||
if (callee.object.name !== "super") return;
|
||||
|
||||
// super.test(); -> _get(Object.getPrototypeOf(ClassName.prototype), "test", this).call(this);
|
||||
property = callee.property;
|
||||
computed = callee.computed;
|
||||
args = node.arguments;
|
||||
}
|
||||
} else if (t.isMemberExpression(node)) {
|
||||
if (!t.isIdentifier(node.object, { name: "super" })) return;
|
||||
|
||||
// super.name; -> _get(Object.getPrototypeOf(ClassName.prototype), "name", this);
|
||||
property = node.property;
|
||||
computed = node.computed;
|
||||
}
|
||||
|
||||
if (property) {
|
||||
var thisReference;
|
||||
var getThisReference = function () {
|
||||
if (topLevel) {
|
||||
thisReference = t.thisExpression();
|
||||
// top level so `this` is the instance
|
||||
return t.thisExpression();
|
||||
} else {
|
||||
topLevelThisReference = thisReference = topLevelThisReference || self.file.generateUidIdentifier("this");
|
||||
// not in the top level so we need to create a reference
|
||||
return topLevelThisReference = topLevelThisReference || self.file.generateUidIdentifier("this");
|
||||
}
|
||||
};
|
||||
|
||||
var superProperty = self.superProperty(property, methodNode.static, computed, thisReference);
|
||||
if (args) {
|
||||
if (args.length === 1 && t.isSpreadElement(args[0])) {
|
||||
// super(...arguments);
|
||||
return t.callExpression(
|
||||
t.memberExpression(superProperty, t.identifier("apply"), false),
|
||||
[thisReference, args[0].argument]
|
||||
);
|
||||
} else {
|
||||
return t.callExpression(
|
||||
t.memberExpression(superProperty, t.identifier("call"), false),
|
||||
[thisReference].concat(args)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
return superProperty;
|
||||
}
|
||||
}
|
||||
var callback = specHandle;
|
||||
if (self.isFast) callback = fastHandle;
|
||||
return callback(getThisReference, node, parent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function fastHandle(getThisReference, node, parent) {
|
||||
if (t.isIdentifier(node, { name: "super" })) {
|
||||
return self.fastSuperProperty(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);
|
||||
t.appendToMemberExpression(callee, t.identifier("call"));
|
||||
node.arguments.unshift(getThisReference());
|
||||
}
|
||||
}
|
||||
|
||||
function specHandle(getThisReference, node, parent) {
|
||||
var property;
|
||||
var computed;
|
||||
var args;
|
||||
|
||||
if (t.isIdentifier(node, { name: "super" })) {
|
||||
if (!(t.isMemberExpression(parent) && !parent.computed && parent.property === node)) {
|
||||
throw self.file.errorWithNode(node, "illegal use of bare super");
|
||||
}
|
||||
} else if (t.isCallExpression(node)) {
|
||||
var callee = node.callee;
|
||||
if (t.isIdentifier(callee, { name: "super" })) {
|
||||
// super(); -> _get(Object.getPrototypeOf(ClassName), "MethodName", this).call(this);
|
||||
property = methodNode.key;
|
||||
computed = methodNode.computed;
|
||||
args = node.arguments;
|
||||
} else {
|
||||
if (!t.isMemberExpression(callee)) return;
|
||||
if (callee.object.name !== "super") return;
|
||||
|
||||
// super.test(); -> _get(Object.getPrototypeOf(ClassName.prototype), "test", this).call(this);
|
||||
property = callee.property;
|
||||
computed = callee.computed;
|
||||
args = node.arguments;
|
||||
}
|
||||
} else if (t.isMemberExpression(node)) {
|
||||
if (!t.isIdentifier(node.object, { name: "super" })) return;
|
||||
|
||||
// super.name; -> _get(Object.getPrototypeOf(ClassName.prototype), "name", this);
|
||||
property = node.property;
|
||||
computed = node.computed;
|
||||
}
|
||||
|
||||
if (!property) return;
|
||||
|
||||
var thisReference = getThisReference();
|
||||
var superProperty = self.superProperty(property, methodNode.static, computed, thisReference);
|
||||
if (args) {
|
||||
if (args.length === 1 && t.isSpreadElement(args[0])) {
|
||||
// super(...arguments);
|
||||
return t.callExpression(
|
||||
t.memberExpression(superProperty, t.identifier("apply")),
|
||||
[thisReference, args[0].argument]
|
||||
);
|
||||
} else {
|
||||
return t.callExpression(
|
||||
t.memberExpression(superProperty, t.identifier("call")),
|
||||
[thisReference].concat(args)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
return superProperty;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -2,6 +2,60 @@ var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.ForOfStatement = function (node, parent, file, scope) {
|
||||
var callback = spec;
|
||||
if (file.isFast("forOf")) callback = fast;
|
||||
|
||||
var build = callback(node, parent, file, scope);
|
||||
var loop = build.loop;
|
||||
var block = loop.body;
|
||||
|
||||
// inherit comments from the original loop
|
||||
t.inheritsComments(loop, node);
|
||||
|
||||
// ensure that it's a block so we can take all it's statemetns
|
||||
t.ensureBlock(node);
|
||||
|
||||
// push the value declaration to the new loop body
|
||||
if (build.declar) block.body.push(build.declar);
|
||||
|
||||
// push the rest of the original loop body onto our new body
|
||||
block.body = block.body.concat(node.body.body);
|
||||
|
||||
return loop;
|
||||
};
|
||||
|
||||
var fast = function (node, parent, file, scope) {
|
||||
var left = node.left;
|
||||
var declar, id;
|
||||
|
||||
if (t.isIdentifier(left) || t.isPattern(left)) {
|
||||
// for (i of test), for ({ i } of test)
|
||||
id = left;
|
||||
} else if (t.isVariableDeclaration(left)) {
|
||||
// for (var i of test)
|
||||
id = left.declarations[0].id;
|
||||
declar = t.variableDeclaration(left.kind, [
|
||||
t.variableDeclarator(id)
|
||||
]);
|
||||
} else {
|
||||
throw file.errorWithNode(left, "Unknown node type " + left.type + " in ForOfStatement");
|
||||
}
|
||||
|
||||
var loop = util.template("for-of-fast", {
|
||||
LOOP_OBJECT: file.generateUidIdentifier("loopObject", scope),
|
||||
IS_ARRAY: file.generateUidIdentifier("isArray", scope),
|
||||
OBJECT: node.right,
|
||||
INDEX: file.generateUidIdentifier("i", scope),
|
||||
ID: id
|
||||
});
|
||||
|
||||
return {
|
||||
declar: declar,
|
||||
loop: loop
|
||||
};
|
||||
};
|
||||
|
||||
var spec = function (node, parent, file, scope) {
|
||||
var left = node.left;
|
||||
var declar;
|
||||
|
||||
@ -9,8 +63,10 @@ exports.ForOfStatement = function (node, parent, file, scope) {
|
||||
var stepValue = t.memberExpression(stepKey, t.identifier("value"));
|
||||
|
||||
if (t.isIdentifier(left) || t.isPattern(left)) {
|
||||
// for (i of test), for ({ i } of test)
|
||||
declar = t.expressionStatement(t.assignmentExpression("=", left, stepValue));
|
||||
} else if (t.isVariableDeclaration(left)) {
|
||||
// for (var i of test)
|
||||
declar = t.variableDeclaration(left.kind, [
|
||||
t.variableDeclarator(left.declarations[0].id, stepValue)
|
||||
]);
|
||||
@ -18,18 +74,14 @@ exports.ForOfStatement = function (node, parent, file, scope) {
|
||||
throw file.errorWithNode(left, "Unknown node type " + left.type + " in ForOfStatement");
|
||||
}
|
||||
|
||||
var node2 = util.template("for-of", {
|
||||
var loop = util.template("for-of", {
|
||||
ITERATOR_KEY: file.generateUidIdentifier("iterator", scope),
|
||||
STEP_KEY: stepKey,
|
||||
OBJECT: node.right
|
||||
});
|
||||
|
||||
t.inheritsComments(node2, node);
|
||||
t.ensureBlock(node);
|
||||
|
||||
var block = node2.body;
|
||||
block.body.push(declar);
|
||||
block.body = block.body.concat(node.body.body);
|
||||
|
||||
return node2;
|
||||
return {
|
||||
declar: declar,
|
||||
loop: loop
|
||||
};
|
||||
};
|
||||
|
||||
@ -1,37 +0,0 @@
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.optional = true;
|
||||
|
||||
exports.ForOfStatement = function (node, parent, file, scope) {
|
||||
var left = node.left;
|
||||
var declar, id;
|
||||
|
||||
if (t.isIdentifier(left) || t.isPattern(left)) {
|
||||
id = left;
|
||||
} else if (t.isVariableDeclaration(left)) {
|
||||
id = left.declarations[0].id;
|
||||
declar = t.variableDeclaration(left.kind, [
|
||||
t.variableDeclarator(id)
|
||||
]);
|
||||
} else {
|
||||
throw file.errorWithNode(left, "Unknown node type " + left.type + " in ForOfStatement");
|
||||
}
|
||||
|
||||
var node2 = util.template("for-of-fast", {
|
||||
LOOP_OBJECT: file.generateUidIdentifier("loopObject", scope),
|
||||
IS_ARRAY: file.generateUidIdentifier("isArray", scope),
|
||||
OBJECT: node.right,
|
||||
INDEX: file.generateUidIdentifier("i", scope),
|
||||
ID: id
|
||||
});
|
||||
|
||||
t.inheritsComments(node2, node);
|
||||
t.ensureBlock(node);
|
||||
|
||||
var block = node2.body;
|
||||
if (declar) block.body.unshift(declar);
|
||||
block.body = block.body.concat(node.body.body);
|
||||
|
||||
return node2;
|
||||
};
|
||||
3
test/fixtures/transformation/es6-classes-fast/options.json
vendored
Normal file
3
test/fixtures/transformation/es6-classes-fast/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"fast": ["classes"]
|
||||
}
|
||||
@ -17,8 +17,8 @@ var _inherits = function (subClass, superClass) {
|
||||
|
||||
var BaseController = (function (_Chaplin$Controller) {
|
||||
function BaseController() {
|
||||
if (Chaplin.Controller != null) {
|
||||
Chaplin.Controller.apply(this, arguments);
|
||||
if (_Chaplin$Controller != null) {
|
||||
_Chaplin$Controller.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,8 +29,8 @@ var BaseController = (function (_Chaplin$Controller) {
|
||||
|
||||
var BaseController2 = (function (_Chaplin$Controller$Another) {
|
||||
function BaseController2() {
|
||||
if (Chaplin.Controller.Another != null) {
|
||||
Chaplin.Controller.Another.apply(this, arguments);
|
||||
if (_Chaplin$Controller$Another != null) {
|
||||
_Chaplin$Controller$Another.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"optional": ["classesFastSuper"]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user