remove builders and move them to automatially generated types
This commit is contained in:
parent
718e342e86
commit
816c1d304b
@ -1,46 +0,0 @@
|
|||||||
var _ = require("lodash");
|
|
||||||
|
|
||||||
var build = function (type, keys) {
|
|
||||||
return function () {
|
|
||||||
var args = arguments;
|
|
||||||
var node = { type: type };
|
|
||||||
_.each(keys, function (key, i) {
|
|
||||||
node[key] = args[i];
|
|
||||||
});
|
|
||||||
return node;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.identifier = build("Identifier", ["name"]);
|
|
||||||
|
|
||||||
exports.literal = build("Literal", ["value"]);
|
|
||||||
|
|
||||||
exports.functionExpression = build("FunctionExpression", ["id", "params", "body"]);
|
|
||||||
|
|
||||||
exports.returnStatement = build("ReturnStatement", ["argument"]);
|
|
||||||
|
|
||||||
exports.assignmentExpression = build("AssignmentExpression", ["operator", "left", "right"]);
|
|
||||||
|
|
||||||
exports.ifStatement = build("IfStatement", ["test", "consequent", "alternate"]);
|
|
||||||
|
|
||||||
exports.callExpression = build("CallExpression", ["callee", "arguments"]);
|
|
||||||
|
|
||||||
exports.blockStatement = build("BlockStatement", ["body"]);
|
|
||||||
|
|
||||||
exports.memberExpression = build("MemberExpression", ["object", "property", "computed"]);
|
|
||||||
|
|
||||||
exports.variableDeclaration = build("VariableDeclaration", ["kind", "declarations"]);
|
|
||||||
|
|
||||||
exports.variableDeclarator = build("VariableDeclarator", ["id", "init"]);
|
|
||||||
|
|
||||||
exports.arrayExpression = build("ArrayExpression", ["elements"]);
|
|
||||||
|
|
||||||
exports.binaryExpression = build("BinaryExpression", ["operator", "left", "right"]);
|
|
||||||
|
|
||||||
exports.expressionStatement = build("ExpressionStatement", ["expression"]);
|
|
||||||
|
|
||||||
exports.thisExpression = build("ThisExpression");
|
|
||||||
|
|
||||||
exports.objectExpression = build("ObjectExpression", ["properties"]);
|
|
||||||
|
|
||||||
exports.property = build("Property", ["kind", "key", "value"]);
|
|
||||||
@ -6,7 +6,7 @@ var transform = require("./transform");
|
|||||||
var generate = require("./generator");
|
var generate = require("./generator");
|
||||||
var acorn = require("acorn-6to5");
|
var acorn = require("acorn-6to5");
|
||||||
var util = require("./util");
|
var util = require("./util");
|
||||||
var b = require("./builders");
|
var t = require("./types");
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
function File(opts) {
|
function File(opts) {
|
||||||
@ -72,7 +72,7 @@ File.prototype.addDeclaration = function (name) {
|
|||||||
var declar = this.declarations[name];
|
var declar = this.declarations[name];
|
||||||
if (declar) return declar.uid;
|
if (declar) return declar.uid;
|
||||||
|
|
||||||
var uid = b.identifier(this.generateUid(name));
|
var uid = t.identifier(this.generateUid(name));
|
||||||
this.declarations[name] = {
|
this.declarations[name] = {
|
||||||
uid: uid,
|
uid: uid,
|
||||||
node: util.template(name)
|
node: util.template(name)
|
||||||
|
|||||||
@ -78,5 +78,5 @@ exports.XJSClosingElement = function (node, print) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.XJSEmptyExpression = function () {
|
exports.XJSEmptyExpression = function () {
|
||||||
|
this.push("null");
|
||||||
};
|
};
|
||||||
|
|||||||
@ -3,7 +3,6 @@ module.exports = AMDFormatter;
|
|||||||
var CommonJSFormatter = require("./common");
|
var CommonJSFormatter = require("./common");
|
||||||
var util = require("../util");
|
var util = require("../util");
|
||||||
var t = require("../types");
|
var t = require("../types");
|
||||||
var b = require("../builders");
|
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
function AMDFormatter(file) {
|
function AMDFormatter(file) {
|
||||||
@ -19,21 +18,21 @@ AMDFormatter.prototype.transform = function (ast) {
|
|||||||
|
|
||||||
// build an array of module names
|
// build an array of module names
|
||||||
|
|
||||||
var names = [b.literal("exports")];
|
var names = [t.literal("exports")];
|
||||||
_.each(this.ids, function (id, name) {
|
_.each(this.ids, function (id, name) {
|
||||||
names.push(b.literal(name));
|
names.push(t.literal(name));
|
||||||
});
|
});
|
||||||
names = b.arrayExpression(names);
|
names = t.arrayExpression(names);
|
||||||
|
|
||||||
// build up define container
|
// build up define container
|
||||||
|
|
||||||
var params = _.values(this.ids);
|
var params = _.values(this.ids);
|
||||||
params.unshift(b.identifier("exports"));
|
params.unshift(t.identifier("exports"));
|
||||||
|
|
||||||
var container = b.functionExpression(null, params, b.blockStatement(body));
|
var container = t.functionExpression(null, params, t.blockStatement(body));
|
||||||
var call = b.callExpression(b.identifier("define"), [names, container]);
|
var call = t.callExpression(t.identifier("define"), [names, container]);
|
||||||
|
|
||||||
program.body = [b.expressionStatement(call)];
|
program.body = [t.expressionStatement(call)];
|
||||||
};
|
};
|
||||||
|
|
||||||
AMDFormatter.prototype._push = function (node) {
|
AMDFormatter.prototype._push = function (node) {
|
||||||
@ -43,7 +42,7 @@ AMDFormatter.prototype._push = function (node) {
|
|||||||
if (ids[id]) {
|
if (ids[id]) {
|
||||||
return ids[id];
|
return ids[id];
|
||||||
} else {
|
} else {
|
||||||
return this.ids[id] = b.identifier(this.file.generateUid(id));
|
return this.ids[id] = t.identifier(this.file.generateUid(id));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -57,7 +56,7 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
|||||||
|
|
||||||
// import foo from "foo";
|
// import foo from "foo";
|
||||||
if (specifier.default) {
|
if (specifier.default) {
|
||||||
id = b.identifier("default");
|
id = t.identifier("default");
|
||||||
}
|
}
|
||||||
|
|
||||||
var ref;
|
var ref;
|
||||||
@ -67,11 +66,11 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
|||||||
ref = this._push(node);
|
ref = this._push(node);
|
||||||
} else {
|
} else {
|
||||||
// import foo from "foo";
|
// import foo from "foo";
|
||||||
ref = b.memberExpression(this._push(node), id, false);
|
ref = t.memberExpression(this._push(node), id, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
nodes.push(b.variableDeclaration("var", [
|
nodes.push(t.variableDeclaration("var", [
|
||||||
b.variableDeclarator(key, ref)
|
t.variableDeclarator(key, ref)
|
||||||
]));
|
]));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@ module.exports = CommonJSFormatter;
|
|||||||
|
|
||||||
var util = require("../util");
|
var util = require("../util");
|
||||||
var t = require("../types");
|
var t = require("../types");
|
||||||
var b = require("../builders");
|
|
||||||
|
|
||||||
function CommonJSFormatter(file) {
|
function CommonJSFormatter(file) {
|
||||||
this.file = file;
|
this.file = file;
|
||||||
@ -20,7 +19,7 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes)
|
|||||||
|
|
||||||
// import foo from "foo";
|
// import foo from "foo";
|
||||||
if (specifier.default) {
|
if (specifier.default) {
|
||||||
specifier.id = b.identifier("default");
|
specifier.id = t.identifier("default");
|
||||||
}
|
}
|
||||||
|
|
||||||
var templateName = "require-assign";
|
var templateName = "require-assign";
|
||||||
@ -100,6 +99,6 @@ CommonJSFormatter.prototype._exportSpecifier = function (getRef, specifier, node
|
|||||||
|
|
||||||
CommonJSFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
|
CommonJSFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
|
||||||
return this._exportSpecifier(function () {
|
return this._exportSpecifier(function () {
|
||||||
return b.callExpression(b.identifier("require"), [node.source]);
|
return t.callExpression(t.identifier("require"), [node.source]);
|
||||||
}, specifier, node, nodes);
|
}, specifier, node, nodes);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -2,7 +2,7 @@ module.exports = UMDFormatter;
|
|||||||
|
|
||||||
var AMDFormatter = require("./amd");
|
var AMDFormatter = require("./amd");
|
||||||
var util = require("../util");
|
var util = require("../util");
|
||||||
var b = require("../builders");
|
var t = require("../types");
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
function UMDFormatter(file) {
|
function UMDFormatter(file) {
|
||||||
@ -20,28 +20,28 @@ UMDFormatter.prototype.transform = function (ast) {
|
|||||||
|
|
||||||
var names = [];
|
var names = [];
|
||||||
_.each(this.ids, function (id, name) {
|
_.each(this.ids, function (id, name) {
|
||||||
names.push(b.literal(name));
|
names.push(t.literal(name));
|
||||||
});
|
});
|
||||||
|
|
||||||
// factory
|
// factory
|
||||||
|
|
||||||
var ids = _.values(this.ids);
|
var ids = _.values(this.ids);
|
||||||
var args = [b.identifier("exports")].concat(ids);
|
var args = [t.identifier("exports")].concat(ids);
|
||||||
|
|
||||||
var factory = b.functionExpression(null, args, b.blockStatement(body));
|
var factory = t.functionExpression(null, args, t.blockStatement(body));
|
||||||
|
|
||||||
// runner
|
// runner
|
||||||
|
|
||||||
var runner = util.template("umd-runner-body", {
|
var runner = util.template("umd-runner-body", {
|
||||||
AMD_ARGUMENTS: b.arrayExpression([b.literal("exports")].concat(names)),
|
AMD_ARGUMENTS: t.arrayExpression([t.literal("exports")].concat(names)),
|
||||||
|
|
||||||
COMMON_ARGUMENTS: names.map(function (name) {
|
COMMON_ARGUMENTS: names.map(function (name) {
|
||||||
return b.callExpression(b.identifier("require"), [name]);
|
return t.callExpression(t.identifier("require"), [name]);
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
var call = b.callExpression(runner, [factory]);
|
var call = t.callExpression(runner, [factory]);
|
||||||
program.body = [b.expressionStatement(call)];
|
program.body = [t.expressionStatement(call)];
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
var b = require("../builders");
|
var t = require("../types");
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
module.exports = function (ast, file) {
|
module.exports = function (ast, file) {
|
||||||
var body = ast.program.body;
|
var body = ast.program.body;
|
||||||
|
|
||||||
_.each(file.declarations, function (declar) {
|
_.each(file.declarations, function (declar) {
|
||||||
body.unshift(b.variableDeclaration("var", [
|
body.unshift(t.variableDeclaration("var", [
|
||||||
b.variableDeclarator(declar.uid, declar.node)
|
t.variableDeclarator(declar.uid, declar.node)
|
||||||
]));
|
]));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,12 +1,11 @@
|
|||||||
var traverse = require("../traverse");
|
var traverse = require("../traverse");
|
||||||
var util = require("../util");
|
var util = require("../util");
|
||||||
var b = require("../builders");
|
|
||||||
var t = require("../types");
|
var t = require("../types");
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
exports.ClassDeclaration = function (node, parent, file) {
|
exports.ClassDeclaration = function (node, parent, file) {
|
||||||
return b.variableDeclaration("var", [
|
return t.variableDeclaration("var", [
|
||||||
b.variableDeclarator(node.id, buildClass(node, file))
|
t.variableDeclarator(node.id, buildClass(node, file))
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -23,7 +22,7 @@ var getMemberExpressionObject = function (node) {
|
|||||||
|
|
||||||
var buildClass = function (node, file) {
|
var buildClass = function (node, file) {
|
||||||
var superName = node.superClass;
|
var superName = node.superClass;
|
||||||
var className = node.id || b.identifier(file.generateUid("class"));
|
var className = node.id || t.identifier(file.generateUid("class"));
|
||||||
|
|
||||||
var superClassArgument = node.superClass;
|
var superClassArgument = node.superClass;
|
||||||
var superClassCallee = node.superClass;
|
var superClassCallee = node.superClass;
|
||||||
@ -33,7 +32,7 @@ var buildClass = function (node, file) {
|
|||||||
superClassArgument = superClassCallee = getMemberExpressionObject(superName);
|
superClassArgument = superClassCallee = getMemberExpressionObject(superName);
|
||||||
} else if (!t.isIdentifier(superName)) {
|
} else if (!t.isIdentifier(superName)) {
|
||||||
superClassArgument = superName;
|
superClassArgument = superName;
|
||||||
superClassCallee = superName = b.identifier(file.generateUid("ref"));
|
superClassCallee = superName = t.identifier(file.generateUid("ref"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,7 +49,7 @@ var buildClass = function (node, file) {
|
|||||||
var returnStatement = body.pop();
|
var returnStatement = body.pop();
|
||||||
|
|
||||||
if (superName) {
|
if (superName) {
|
||||||
body.push(b.expressionStatement(b.callExpression(file.addDeclaration("extends"), [className, superName])));
|
body.push(t.expressionStatement(t.callExpression(file.addDeclaration("extends"), [className, superName])));
|
||||||
|
|
||||||
container.arguments.push(superClassArgument);
|
container.arguments.push(superClassArgument);
|
||||||
container.callee.params.push(superClassCallee);
|
container.callee.params.push(superClassCallee);
|
||||||
@ -95,7 +94,7 @@ var buildClassBody = function (file, construct, body, className, superName, node
|
|||||||
|
|
||||||
if (kind === "") {
|
if (kind === "") {
|
||||||
kind = "value";
|
kind = "value";
|
||||||
util.pushMutatorMap(mutatorMap, methodName, "writable", b.identifier("true"));
|
util.pushMutatorMap(mutatorMap, methodName, "writable", t.identifier("true"));
|
||||||
}
|
}
|
||||||
|
|
||||||
util.pushMutatorMap(mutatorMap, methodName, kind, node);
|
util.pushMutatorMap(mutatorMap, methodName, kind, node);
|
||||||
@ -124,13 +123,13 @@ var buildClassBody = function (file, construct, body, className, superName, node
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (instanceProps || staticProps) {
|
if (instanceProps || staticProps) {
|
||||||
staticProps = staticProps || b.literal(null);
|
staticProps = staticProps || t.literal(null);
|
||||||
|
|
||||||
var args = [className, staticProps];
|
var args = [className, staticProps];
|
||||||
if (instanceProps) args.push(instanceProps);
|
if (instanceProps) args.push(instanceProps);
|
||||||
|
|
||||||
body.push(b.expressionStatement(
|
body.push(t.expressionStatement(
|
||||||
b.callExpression(file.addDeclaration("class-props"), args)
|
t.callExpression(file.addDeclaration("class-props"), args)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -140,32 +139,32 @@ var superIdentifier = function (superName, methodNode, methodName, node, parent)
|
|||||||
return;
|
return;
|
||||||
} else if (t.isCallExpression(parent) && parent.callee === node) {
|
} else if (t.isCallExpression(parent) && parent.callee === node) {
|
||||||
// super(); -> ClassName.prototype.MethodName.call(this);
|
// super(); -> ClassName.prototype.MethodName.call(this);
|
||||||
parent.arguments.unshift(b.thisExpression());
|
parent.arguments.unshift(t.thisExpression());
|
||||||
|
|
||||||
if (methodName === "constructor") {
|
if (methodName === "constructor") {
|
||||||
// constructor() { super(); }
|
// constructor() { super(); }
|
||||||
return b.memberExpression(superName, b.identifier("call"));
|
return t.memberExpression(superName, t.identifier("call"));
|
||||||
} else {
|
} else {
|
||||||
node = superName;
|
node = superName;
|
||||||
|
|
||||||
// foo() { super(); }
|
// foo() { super(); }
|
||||||
if (!methodNode.static) {
|
if (!methodNode.static) {
|
||||||
node = b.memberExpression(node, b.identifier("prototype"));
|
node = t.memberExpression(node, t.identifier("prototype"));
|
||||||
}
|
}
|
||||||
|
|
||||||
node = b.memberExpression(node, b.identifier(methodName));
|
node = t.memberExpression(node, t.identifier(methodName));
|
||||||
return b.memberExpression(node, b.identifier("call"));
|
return t.memberExpression(node, t.identifier("call"));
|
||||||
}
|
}
|
||||||
} else if (t.isMemberExpression(parent) && !methodNode.static) {
|
} else if (t.isMemberExpression(parent) && !methodNode.static) {
|
||||||
// super.test -> ClassName.prototype.test
|
// super.test -> ClassName.prototype.test
|
||||||
return b.memberExpression(superName, b.identifier("prototype"));
|
return t.memberExpression(superName, t.identifier("prototype"));
|
||||||
} else {
|
} else {
|
||||||
return superName;
|
return superName;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var replaceInstanceSuperReferences = function (superName, method, methodNode, methodName) {
|
var replaceInstanceSuperReferences = function (superName, method, methodNode, methodName) {
|
||||||
superName = superName || b.identifier("Function");
|
superName = superName || t.identifier("Function");
|
||||||
|
|
||||||
traverse(method, function (node, parent) {
|
traverse(method, function (node, parent) {
|
||||||
if (t.isIdentifier(node) && node.name === "super") {
|
if (t.isIdentifier(node) && node.name === "super") {
|
||||||
@ -177,7 +176,7 @@ var replaceInstanceSuperReferences = function (superName, method, methodNode, me
|
|||||||
|
|
||||||
// super.test(); -> ClassName.prototype.MethodName.call(this);
|
// super.test(); -> ClassName.prototype.MethodName.call(this);
|
||||||
callee.property.name = callee.property.name + ".call";
|
callee.property.name = callee.property.name + ".call";
|
||||||
node.arguments.unshift(b.thisExpression());
|
node.arguments.unshift(t.thisExpression());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
var util = require("../util");
|
var util = require("../util");
|
||||||
var b = require("../builders");
|
var t = require("../types");
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
exports.ObjectExpression = function (node, parent, file) {
|
exports.ObjectExpression = function (node, parent, file) {
|
||||||
@ -33,10 +33,10 @@ exports.ObjectExpression = function (node, parent, file) {
|
|||||||
|
|
||||||
_.each(computed, function (prop) {
|
_.each(computed, function (prop) {
|
||||||
containerBody.unshift(
|
containerBody.unshift(
|
||||||
b.expressionStatement(
|
t.expressionStatement(
|
||||||
b.assignmentExpression(
|
t.assignmentExpression(
|
||||||
"=",
|
"=",
|
||||||
b.memberExpression(objId, prop.key, true),
|
t.memberExpression(objId, prop.key, true),
|
||||||
prop.value
|
prop.value
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
var b = require("../builders");
|
|
||||||
var t = require("../types");
|
var t = require("../types");
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
var buildVariableAssign = function (kind, id, init) {
|
var buildVariableAssign = function (kind, id, init) {
|
||||||
if (kind === false) {
|
if (kind === false) {
|
||||||
return b.expressionStatement(b.assignmentExpression("=", id, init));
|
return t.expressionStatement(t.assignmentExpression("=", id, init));
|
||||||
} else {
|
} else {
|
||||||
return b.variableDeclaration(kind, [
|
return t.variableDeclaration(kind, [
|
||||||
b.variableDeclarator(id, init)
|
t.variableDeclarator(id, init)
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -27,7 +26,7 @@ var push = function (kind, nodes, elem, parentId) {
|
|||||||
var pushObjectPattern = function (kind, nodes, pattern, parentId) {
|
var pushObjectPattern = function (kind, nodes, pattern, parentId) {
|
||||||
_.each(pattern.properties, function (prop) {
|
_.each(pattern.properties, function (prop) {
|
||||||
var pattern2 = prop.value;
|
var pattern2 = prop.value;
|
||||||
var patternId2 = b.memberExpression(parentId, prop.key);
|
var patternId2 = t.memberExpression(parentId, prop.key);
|
||||||
|
|
||||||
if (t.isPattern(pattern2)) {
|
if (t.isPattern(pattern2)) {
|
||||||
push(kind, nodes, pattern2, patternId2);
|
push(kind, nodes, pattern2, patternId2);
|
||||||
@ -41,17 +40,17 @@ var pushArrayPattern = function (kind, nodes, pattern, parentId) {
|
|||||||
_.each(pattern.elements, function (elem, i) {
|
_.each(pattern.elements, function (elem, i) {
|
||||||
if (!elem) return;
|
if (!elem) return;
|
||||||
|
|
||||||
var newPatternId = b.memberExpression(parentId, b.literal(i), true);
|
var newPatternId = t.memberExpression(parentId, t.literal(i), true);
|
||||||
push(kind, nodes, elem, newPatternId);
|
push(kind, nodes, elem, newPatternId);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var pushPattern = function (kind, nodes, pattern, parentId, file) {
|
var pushPattern = function (kind, nodes, pattern, parentId, file) {
|
||||||
if (parentId.type !== "MemberExpression" && parentId.type !== "Identifier") {
|
if (parentId.type !== "MemberExpression" && parentId.type !== "Identifier") {
|
||||||
var key = b.identifier(file.generateUid("ref"));
|
var key = t.identifier(file.generateUid("ref"));
|
||||||
|
|
||||||
nodes.push(b.variableDeclaration("var", [
|
nodes.push(t.variableDeclaration("var", [
|
||||||
b.variableDeclarator(key, parentId)
|
t.variableDeclarator(key, parentId)
|
||||||
]));
|
]));
|
||||||
|
|
||||||
parentId = key;
|
parentId = key;
|
||||||
@ -68,9 +67,9 @@ exports.ForOfStatement = function (node, parent, file) {
|
|||||||
var pattern = declar.declarations[0].id;
|
var pattern = declar.declarations[0].id;
|
||||||
if (!t.isPattern(pattern)) return;
|
if (!t.isPattern(pattern)) return;
|
||||||
|
|
||||||
var key = b.identifier(file.generateUid("ref"));
|
var key = t.identifier(file.generateUid("ref"));
|
||||||
node.left = b.variableDeclaration(declar.kind, [
|
node.left = t.variableDeclaration(declar.kind, [
|
||||||
b.variableDeclarator(key, null)
|
t.variableDeclarator(key, null)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
var nodes = [];
|
var nodes = [];
|
||||||
@ -92,7 +91,7 @@ exports.Function = function (node, parent, file) {
|
|||||||
if (!t.isPattern(pattern)) return pattern;
|
if (!t.isPattern(pattern)) return pattern;
|
||||||
|
|
||||||
hasDestructuring = true;
|
hasDestructuring = true;
|
||||||
var parentId = b.identifier(file.generateUid("ref"));
|
var parentId = t.identifier(file.generateUid("ref"));
|
||||||
pushPattern("var", nodes, pattern, parentId, file);
|
pushPattern("var", nodes, pattern, parentId, file);
|
||||||
return parentId;
|
return parentId;
|
||||||
});
|
});
|
||||||
@ -113,9 +112,9 @@ exports.ExpressionStatement = function (node, parent, file) {
|
|||||||
|
|
||||||
var nodes = [];
|
var nodes = [];
|
||||||
|
|
||||||
var ref = b.identifier(file.generateUid("ref"));
|
var ref = t.identifier(file.generateUid("ref"));
|
||||||
nodes.push(b.variableDeclaration("var", [
|
nodes.push(t.variableDeclaration("var", [
|
||||||
b.variableDeclarator(ref, expr.right)
|
t.variableDeclarator(ref, expr.right)
|
||||||
]));
|
]));
|
||||||
|
|
||||||
push(false, nodes, expr.left, ref);
|
push(false, nodes, expr.left, ref);
|
||||||
@ -151,7 +150,7 @@ exports.VariableDeclaration = function (node, parent, file) {
|
|||||||
var declar;
|
var declar;
|
||||||
|
|
||||||
_.each(nodes, function (node) {
|
_.each(nodes, function (node) {
|
||||||
declar = declar || b.variableDeclaration(node.kind, []);
|
declar = declar || t.variableDeclaration(node.kind, []);
|
||||||
|
|
||||||
if (node.type !== "VariableDeclaration" && declar.kind !== node.kind) {
|
if (node.type !== "VariableDeclaration" && declar.kind !== node.kind) {
|
||||||
throw file.errorWithNode(node, "Cannot use this node within the current parent");
|
throw file.errorWithNode(node, "Cannot use this node within the current parent");
|
||||||
|
|||||||
@ -1,19 +1,18 @@
|
|||||||
var util = require("../util");
|
var util = require("../util");
|
||||||
var t = require("../types");
|
var t = require("../types");
|
||||||
var b = require("../builders");
|
|
||||||
|
|
||||||
exports.ForOfStatement = function (node, parent, file) {
|
exports.ForOfStatement = function (node, parent, file) {
|
||||||
var left = node.left;
|
var left = node.left;
|
||||||
var declar;
|
var declar;
|
||||||
|
|
||||||
var stepKey = b.identifier(file.generateUid("step"));
|
var stepKey = t.identifier(file.generateUid("step"));
|
||||||
var stepValueId = b.memberExpression(stepKey, b.identifier("value"));
|
var stepValueId = t.memberExpression(stepKey, t.identifier("value"));
|
||||||
|
|
||||||
if (t.isIdentifier(left)) {
|
if (t.isIdentifier(left)) {
|
||||||
declar = b.expressionStatement(b.assignmentExpression("=", left, stepValueId));
|
declar = t.expressionStatement(t.assignmentExpression("=", left, stepValueId));
|
||||||
} else if (t.isVariableDeclaration(left)) {
|
} else if (t.isVariableDeclaration(left)) {
|
||||||
declar = b.variableDeclaration(left.kind, [
|
declar = t.variableDeclaration(left.kind, [
|
||||||
b.variableDeclarator(left.declarations[0].id, stepValueId)
|
t.variableDeclarator(left.declarations[0].id, stepValueId)
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
// https://github.com/RReverser/jsx-transpiler
|
// https://github.com/RReverser/jsx-transpiler
|
||||||
|
|
||||||
var esutils = require("esutils");
|
var esutils = require("esutils");
|
||||||
var b = require("../builders");
|
|
||||||
var t = require("../types");
|
var t = require("../types");
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
@ -18,8 +17,8 @@ exports.Program = function (node, parent, file) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// prebuilding AST node
|
// prebuilding AST node
|
||||||
file.jsx = jsx.split(".").map(b.identifier).reduce(function (object, property) {
|
file.jsx = jsx.split(".").map(t.identifier).reduce(function (object, property) {
|
||||||
return b.memberExpression(object, property);
|
return t.memberExpression(object, property);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -27,7 +26,7 @@ exports.XJSIdentifier = function (node) {
|
|||||||
if (esutils.keyword.isIdentifierName(node.name)) {
|
if (esutils.keyword.isIdentifierName(node.name)) {
|
||||||
node.type = "Identifier";
|
node.type = "Identifier";
|
||||||
} else {
|
} else {
|
||||||
return b.literal(node.name);
|
return t.literal(node.name);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -42,19 +41,14 @@ exports.XJSMemberExpression = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.XJSEmptyExpression = function (node) {
|
|
||||||
node.value = null;
|
|
||||||
node.type = "Literal";
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.XJSExpressionContainer = function (node) {
|
exports.XJSExpressionContainer = function (node) {
|
||||||
return node.expression;
|
return node.expression;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.XJSAttribute = {
|
exports.XJSAttribute = {
|
||||||
exit: function (node) {
|
exit: function (node) {
|
||||||
var value = node.value || b.literal(true);
|
var value = node.value || t.literal(true);
|
||||||
var propNode = b.property("init", node.name, value);
|
var propNode = t.property("init", node.name, value);
|
||||||
t.inherits(propNode, node);
|
t.inherits(propNode, node);
|
||||||
return propNode;
|
return propNode;
|
||||||
}
|
}
|
||||||
@ -68,18 +62,18 @@ exports.XJSOpeningElement = {
|
|||||||
var tagName = tagExpr.name;
|
var tagName = tagExpr.name;
|
||||||
|
|
||||||
if (/[a-z]/.exec(tagName[0]) || _.contains(tagName, "-")) {
|
if (/[a-z]/.exec(tagName[0]) || _.contains(tagName, "-")) {
|
||||||
tagExpr = b.memberExpression(file.jsx, tagExpr);
|
tagExpr = t.memberExpression(file.jsx, tagExpr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var props = node.attributes;
|
var props = node.attributes;
|
||||||
if (props.length) {
|
if (props.length) {
|
||||||
props = b.objectExpression(props);
|
props = t.objectExpression(props);
|
||||||
} else {
|
} else {
|
||||||
props = b.literal(null);
|
props = t.literal(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
return b.callExpression(tagExpr, [props]);
|
return t.callExpression(tagExpr, [props]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
var traverse = require("../traverse");
|
var traverse = require("../traverse");
|
||||||
var util = require("../util");
|
var util = require("../util");
|
||||||
var t = require("../types");
|
var t = require("../types");
|
||||||
var b = require("../builders");
|
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
exports.VariableDeclaration = function (node, parent, file) {
|
exports.VariableDeclaration = function (node, parent, file) {
|
||||||
@ -12,7 +11,7 @@ exports.VariableDeclaration = function (node, parent, file) {
|
|||||||
|
|
||||||
_.each(node.declarations, function (declar) {
|
_.each(node.declarations, function (declar) {
|
||||||
_.each(util.getIds(declar.id), function (id) {
|
_.each(util.getIds(declar.id), function (id) {
|
||||||
ids[id] = b.identifier(file.generateUid(id));
|
ids[id] = t.identifier(file.generateUid(id));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -43,11 +42,11 @@ exports.VariableDeclaration = function (node, parent, file) {
|
|||||||
if (t.isFunctionDeclaration(node)) {
|
if (t.isFunctionDeclaration(node)) {
|
||||||
throw new Error("`FunctionDeclaration`s that use `let` and `constant` references aren't allowed outside of the root scope");
|
throw new Error("`FunctionDeclaration`s that use `let` and `constant` references aren't allowed outside of the root scope");
|
||||||
} else {
|
} else {
|
||||||
var func = b.functionExpression(null, letReferences, b.blockStatement([
|
var func = t.functionExpression(null, letReferences, t.blockStatement([
|
||||||
b.returnStatement(node)
|
t.returnStatement(node)
|
||||||
]));
|
]));
|
||||||
func._aliasFunction = true;
|
func._aliasFunction = true;
|
||||||
return b.callExpression(func, letReferences);
|
return t.callExpression(func, letReferences);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
3
lib/6to5/transformers/react.js
vendored
3
lib/6to5/transformers/react.js
vendored
@ -1,4 +1,3 @@
|
|||||||
var b = require("../builders");
|
|
||||||
var t = require("../types");
|
var t = require("../types");
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
@ -34,7 +33,7 @@ var addDisplayName = function (id, call) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (safe) {
|
if (safe) {
|
||||||
props.unshift(b.property("init", b.identifier("displayName"), b.literal(id)));
|
props.unshift(t.property("init", t.identifier("displayName"), t.literal(id)));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
var util = require("../util");
|
var util = require("../util");
|
||||||
var t = require("../types");
|
var t = require("../types");
|
||||||
var b = require("../builders");
|
|
||||||
|
|
||||||
exports.Function = function (node, parent, file) {
|
exports.Function = function (node, parent, file) {
|
||||||
if (!node.rest) return;
|
if (!node.rest) return;
|
||||||
@ -16,7 +15,7 @@ exports.Function = function (node, parent, file) {
|
|||||||
var template = util.template(templateName, {
|
var template = util.template(templateName, {
|
||||||
SLICE_KEY: file.addDeclaration("slice"),
|
SLICE_KEY: file.addDeclaration("slice"),
|
||||||
VARIABLE_NAME: rest,
|
VARIABLE_NAME: rest,
|
||||||
SLICE_ARG: b.literal(node.params.length)
|
SLICE_ARG: t.literal(node.params.length)
|
||||||
});
|
});
|
||||||
|
|
||||||
template.declarations[0].init.arguments[0]._ignoreAliasFunctions = true;
|
template.declarations[0].init.arguments[0]._ignoreAliasFunctions = true;
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
var util = require("../util");
|
var util = require("../util");
|
||||||
var b = require("../builders");
|
|
||||||
var t = require("../types");
|
var t = require("../types");
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
@ -32,7 +31,7 @@ var build = function (props, file) {
|
|||||||
|
|
||||||
var push = function () {
|
var push = function () {
|
||||||
if (!_props.length) return;
|
if (!_props.length) return;
|
||||||
nodes.push(b.arrayExpression(_props));
|
nodes.push(t.arrayExpression(_props));
|
||||||
_props = [];
|
_props = [];
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -59,14 +58,14 @@ exports.ArrayExpression = function (node, parent, file) {
|
|||||||
|
|
||||||
if (!nodes.length) return first;
|
if (!nodes.length) return first;
|
||||||
|
|
||||||
return b.callExpression(b.memberExpression(first, b.identifier("concat")), nodes);
|
return t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.CallExpression = function (node, parent, file) {
|
exports.CallExpression = function (node, parent, file) {
|
||||||
var args = node.arguments;
|
var args = node.arguments;
|
||||||
if (!hasSpread(args)) return;
|
if (!hasSpread(args)) return;
|
||||||
|
|
||||||
var contextLiteral = b.literal(null);
|
var contextLiteral = t.literal(null);
|
||||||
|
|
||||||
node.arguments = [];
|
node.arguments = [];
|
||||||
|
|
||||||
@ -74,7 +73,7 @@ exports.CallExpression = function (node, parent, file) {
|
|||||||
var first = nodes.shift();
|
var first = nodes.shift();
|
||||||
|
|
||||||
if (nodes.length) {
|
if (nodes.length) {
|
||||||
node.arguments.push(b.callExpression(b.memberExpression(first, b.identifier("concat")), nodes));
|
node.arguments.push(t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes));
|
||||||
} else {
|
} else {
|
||||||
node.arguments.push(first);
|
node.arguments.push(first);
|
||||||
}
|
}
|
||||||
@ -85,14 +84,14 @@ exports.CallExpression = function (node, parent, file) {
|
|||||||
contextLiteral = callee.object;
|
contextLiteral = callee.object;
|
||||||
|
|
||||||
if (callee.computed) {
|
if (callee.computed) {
|
||||||
callee.object = b.memberExpression(callee.object, callee.property, true);
|
callee.object = t.memberExpression(callee.object, callee.property, true);
|
||||||
callee.property = b.identifier("apply");
|
callee.property = t.identifier("apply");
|
||||||
callee.computed = false;
|
callee.computed = false;
|
||||||
} else {
|
} else {
|
||||||
callee.property = b.memberExpression(callee.property, b.identifier("apply"));
|
callee.property = t.memberExpression(callee.property, t.identifier("apply"));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
node.callee = b.memberExpression(node.callee, b.identifier("apply"));
|
node.callee = t.memberExpression(node.callee, t.identifier("apply"));
|
||||||
}
|
}
|
||||||
|
|
||||||
node.arguments.unshift(contextLiteral);
|
node.arguments.unshift(contextLiteral);
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
var t = require("../types");
|
var t = require("../types");
|
||||||
var b = require("../builders");
|
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
var buildBinaryExpression = function (left, right) {
|
var buildBinaryExpression = function (left, right) {
|
||||||
return b.binaryExpression("+", left, right);
|
return t.binaryExpression("+", left, right);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.TaggedTemplateExpression = function (node) {
|
exports.TaggedTemplateExpression = function (node) {
|
||||||
@ -11,22 +10,22 @@ exports.TaggedTemplateExpression = function (node) {
|
|||||||
var quasi = node.quasi;
|
var quasi = node.quasi;
|
||||||
|
|
||||||
var strings = quasi.quasis.map(function (elem) {
|
var strings = quasi.quasis.map(function (elem) {
|
||||||
return b.literal(elem.value.raw);
|
return t.literal(elem.value.raw);
|
||||||
});
|
});
|
||||||
args.push(b.arrayExpression(strings));
|
args.push(t.arrayExpression(strings));
|
||||||
|
|
||||||
_.each(quasi.expressions, function (expr) {
|
_.each(quasi.expressions, function (expr) {
|
||||||
args.push(expr);
|
args.push(expr);
|
||||||
});
|
});
|
||||||
|
|
||||||
return b.callExpression(node.tag, args);
|
return t.callExpression(node.tag, args);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.TemplateLiteral = function (node) {
|
exports.TemplateLiteral = function (node) {
|
||||||
var nodes = [];
|
var nodes = [];
|
||||||
|
|
||||||
_.each(node.quasis, function (elem) {
|
_.each(node.quasis, function (elem) {
|
||||||
nodes.push(b.literal(elem.value.raw));
|
nodes.push(t.literal(elem.value.raw));
|
||||||
|
|
||||||
var expr = node.expressions.shift();
|
var expr = node.expressions.shift();
|
||||||
if (expr) nodes.push(expr);
|
if (expr) nodes.push(expr);
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
var b = require("../builders");
|
var t = require("../types");
|
||||||
|
|
||||||
module.exports = function (ast) {
|
module.exports = function (ast) {
|
||||||
var body = ast.program.body;
|
var body = ast.program.body;
|
||||||
@ -7,6 +7,6 @@ module.exports = function (ast) {
|
|||||||
var noStrict = !first || first.type !== "ExpressionStatement" || first.expression.type !== "Literal" || first.expression.value !== "use strict";
|
var noStrict = !first || first.type !== "ExpressionStatement" || first.expression.type !== "Literal" || first.expression.value !== "use strict";
|
||||||
|
|
||||||
if (noStrict) {
|
if (noStrict) {
|
||||||
body.unshift(b.expressionStatement(b.literal("use strict")));
|
body.unshift(t.expressionStatement(t.literal("use strict")));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
22
lib/6to5/types/alias-keys.json
Normal file
22
lib/6to5/types/alias-keys.json
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"ArrowFunctionExpression": ["Function"],
|
||||||
|
"FunctionDeclaration": ["Function"],
|
||||||
|
"FunctionExpression": ["Function"],
|
||||||
|
|
||||||
|
"LogicalExpression": ["Binary"],
|
||||||
|
"BinaryExpression": ["Binary"],
|
||||||
|
|
||||||
|
"UnaryExpression": ["UnaryLike"],
|
||||||
|
"SpreadProperty": ["UnaryLike"],
|
||||||
|
"SpreadElement": ["UnaryLike"],
|
||||||
|
|
||||||
|
"ClassDeclaration": ["Class"],
|
||||||
|
"ClassExpression": ["Class"],
|
||||||
|
|
||||||
|
"ForOfStatement": ["For"],
|
||||||
|
"ForInStatement": ["For"],
|
||||||
|
"ForStatement": ["For"],
|
||||||
|
|
||||||
|
"ObjectPattern": ["Pattern"],
|
||||||
|
"ArrayPattern": ["Pattern"]
|
||||||
|
}
|
||||||
18
lib/6to5/types/builder-keys.json
Normal file
18
lib/6to5/types/builder-keys.json
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"ArrayExpression": ["elements"],
|
||||||
|
"AssignmentExpression": ["operator", "left", "right"],
|
||||||
|
"BinaryExpression": ["operator", "left", "right"],
|
||||||
|
"BlockStatement": ["body"],
|
||||||
|
"CallExpression": ["callee", "arguments"],
|
||||||
|
"ExpressionStatement": ["expression"],
|
||||||
|
"FunctionExpression": ["id", "params", "body"],
|
||||||
|
"Identifier": ["name"],
|
||||||
|
"IfStatement": ["test", "consequent", "alternate"],
|
||||||
|
"Literal": ["value"],
|
||||||
|
"MemberExpression": ["object", "property", "computed"],
|
||||||
|
"ObjectExpression": ["properties"],
|
||||||
|
"Property": ["kind", "key", "value"],
|
||||||
|
"ReturnStatement": ["argument"],
|
||||||
|
"VariableDeclaration": ["kind", "declarations"],
|
||||||
|
"VariableDeclarator": ["id", "init"]
|
||||||
|
}
|
||||||
@ -1,5 +1,4 @@
|
|||||||
var traverse = require("./traverse");
|
var traverse = require("../traverse");
|
||||||
var b = require("./builders");
|
|
||||||
var n = require("acorn-ast-types").namedTypes;
|
var n = require("acorn-ast-types").namedTypes;
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
@ -13,30 +12,41 @@ _.each(traverse.VISITOR_KEYS, function (keys, type) {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
var buildIs = function (isKey, typeKey, types) {
|
//
|
||||||
t[typeKey + "_TYPES"] = types;
|
|
||||||
|
|
||||||
t["is" + isKey] = function (node) {
|
t.BUILDER_KEYS = _.defaults(require("./builder-keys"), traverse.VISITOR_KEYS);
|
||||||
return node && _.contains(types, node.type);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
buildIs("Function", "FUNCTION", ["ArrowFunctionExpression", "FunctionDeclaration", "FunctionExpression"]);
|
_.each(t.BUILDER_KEYS, function (keys, type) {
|
||||||
buildIs("Class", "CLASS", ["ClassDeclaration", "ClassExpression"]);
|
t[type[0].toLowerCase() + type.slice(1)] = function () {
|
||||||
buildIs("Pattern", "PATTERN", ["ArrayPattern", "ObjectPattern"]);
|
var args = arguments;
|
||||||
buildIs("Binary", "BINARY", ["BinaryExpression", "LogicalExpression"]);
|
var node = { type: type };
|
||||||
buildIs("UnaryLike", "UNARY", ["UnaryExpression", "SpreadElement", "SpreadProperty"]);
|
_.each(keys, function (key, i) {
|
||||||
|
node[key] = args[i];
|
||||||
|
});
|
||||||
|
return node;
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
t.aliases = {
|
t.ALIAS_KEYS = require("./alias-keys");
|
||||||
ArrowFunctionExpression: ["Function"],
|
|
||||||
FunctionDeclaration: ["Function"],
|
|
||||||
FunctionExpression: ["Function"],
|
|
||||||
|
|
||||||
ClassDeclaration: ["Class"],
|
var _aliases = {};
|
||||||
ClassExpression: ["Class"]
|
|
||||||
|
_.each(t.ALIAS_KEYS, function (aliases, type) {
|
||||||
|
_.each(aliases, function (alias) {
|
||||||
|
var types = _aliases[alias] = _aliases[alias] || [];
|
||||||
|
types.push(type);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
_.each(_aliases, function (types, type) {
|
||||||
|
t[type.toUpperCase() + "_TYPES"] = types;
|
||||||
|
|
||||||
|
t["is" + type] = function (node) {
|
||||||
|
return node && _.contains(types, node.type);
|
||||||
};
|
};
|
||||||
|
});
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
@ -70,16 +80,16 @@ t.toBlock = function (node, parent) {
|
|||||||
if (!_.isArray(node)) {
|
if (!_.isArray(node)) {
|
||||||
if (!n.Statement.check(node)) {
|
if (!n.Statement.check(node)) {
|
||||||
if (t.isFunction(parent)) {
|
if (t.isFunction(parent)) {
|
||||||
node = b.returnStatement(node);
|
node = t.returnStatement(node);
|
||||||
} else {
|
} else {
|
||||||
node = b.expressionStatement(node);
|
node = t.expressionStatement(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
node = [node];
|
node = [node];
|
||||||
}
|
}
|
||||||
|
|
||||||
return b.blockStatement(node);
|
return t.blockStatement(node);
|
||||||
};
|
};
|
||||||
|
|
||||||
t.inherits = function (child, parent) {
|
t.inherits = function (child, parent) {
|
||||||
@ -101,7 +111,11 @@ t.needsWhitespace = function (node, expression) {
|
|||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
if (t.isFunction(node) || t.isClass(node)) {
|
if (t.isFunction(node) || t.isClass(node) || t.isFor(node) || t.isSwitchStatement(node)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (t.isIfStatement(node) && t.isBlockStatement(node.consequent)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,6 +142,10 @@ t.needsWhitespace = function (node, expression) {
|
|||||||
exprs = node.elements;
|
exprs = node.elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (t.isObjectExpression(node)) {
|
||||||
|
exprs = node.properties;
|
||||||
|
}
|
||||||
|
|
||||||
return exprs.some(function (expr) {
|
return exprs.some(function (expr) {
|
||||||
return t.needsWhitespace(expr, true);
|
return t.needsWhitespace(expr, true);
|
||||||
});
|
});
|
||||||
@ -5,7 +5,6 @@ var path = require("path");
|
|||||||
var util = require("util");
|
var util = require("util");
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var t = require("./types");
|
var t = require("./types");
|
||||||
var b = require("./builders");
|
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
_.extend(estraverse.VisitorKeys, traverse.VISITOR_KEYS);
|
_.extend(estraverse.VisitorKeys, traverse.VISITOR_KEYS);
|
||||||
@ -37,7 +36,7 @@ exports.getUid = function (parent, file) {
|
|||||||
|
|
||||||
if (t.isIdentifier(node)) id = node.name;
|
if (t.isIdentifier(node)) id = node.name;
|
||||||
|
|
||||||
return b.identifier(file.generateUid(id));
|
return t.identifier(file.generateUid(id));
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.isAbsolute = function (loc) {
|
exports.isAbsolute = function (loc) {
|
||||||
@ -100,17 +99,17 @@ exports.pushMutatorMap = function (mutatorMap, key, kind, method) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.buildDefineProperties = function (mutatorMap) {
|
exports.buildDefineProperties = function (mutatorMap) {
|
||||||
var objExpr = b.objectExpression([]);
|
var objExpr = t.objectExpression([]);
|
||||||
|
|
||||||
_.each(mutatorMap, function (map, key) {
|
_.each(mutatorMap, function (map, key) {
|
||||||
var mapNode = b.objectExpression([]);
|
var mapNode = t.objectExpression([]);
|
||||||
|
|
||||||
var propNode = b.property("init", b.identifier(key), mapNode);
|
var propNode = t.property("init", t.identifier(key), mapNode);
|
||||||
|
|
||||||
_.each(map, function (node, key) {
|
_.each(map, function (node, key) {
|
||||||
node = _.clone(node);
|
node = _.clone(node);
|
||||||
if (t.isMethodDefinition(node)) node = node.value;
|
if (t.isMethodDefinition(node)) node = node.value;
|
||||||
mapNode.properties.push(b.property("init", b.identifier(key), node));
|
mapNode.properties.push(t.property("init", t.identifier(key), node));
|
||||||
});
|
});
|
||||||
|
|
||||||
objExpr.properties.push(propNode);
|
objExpr.properties.push(propNode);
|
||||||
@ -250,7 +249,7 @@ try {
|
|||||||
if (!fs.existsSync(templatesLoc)) {
|
if (!fs.existsSync(templatesLoc)) {
|
||||||
throw new Error("no templates directory - this is most likely the result" +
|
throw new Error("no templates directory - this is most likely the result" +
|
||||||
" of a broken `npm publish`. Please report to " +
|
" of a broken `npm publish`. Please report to " +
|
||||||
"https://github.com/sebmck/6to5/issues");
|
"https://githut.com/sebmck/6to5/issues");
|
||||||
}
|
}
|
||||||
|
|
||||||
_.each(fs.readdirSync(templatesLoc), function (name) {
|
_.each(fs.readdirSync(templatesLoc), function (name) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user