@@ -30,12 +30,22 @@ transform.test = function (task, assert) {
|
||||
var actualAst = actualResult.ast;
|
||||
actualCode = recast.prettyPrint(actualAst).code;
|
||||
|
||||
var expectCode = expect.code.trim();
|
||||
var expectAst = util.parse(expect, expectCode);
|
||||
var expectResult = recast.prettyPrint(expectAst);
|
||||
expectCode = expectResult.code;
|
||||
if (task.options.exec) {
|
||||
try {
|
||||
var fn = new Function("assert", actualCode);
|
||||
fn(assert);
|
||||
} catch (err) {
|
||||
console.log(actualCode);
|
||||
throw err;
|
||||
}
|
||||
} else {
|
||||
var expectCode = expect.code.trim();
|
||||
var expectAst = util.parse(expect, expectCode);
|
||||
var expectResult = recast.prettyPrint(expectAst);
|
||||
expectCode = expectResult.code;
|
||||
|
||||
assert.equal(actualCode, expectCode);
|
||||
assert.equal(actualCode, expectCode);
|
||||
}
|
||||
|
||||
if (task.sourceMap) {
|
||||
assert.deepEqual(task.sourceMap, actualResult.map);
|
||||
|
||||
@@ -18,28 +18,7 @@ exports.ForStatement = function (node) {
|
||||
_.each(node.body, function (child) {
|
||||
if (child && child.type === "VariableDeclaration" && child.kind === "const") {
|
||||
_.each(child.declarations, function (declar) {
|
||||
var search = [declar.id];
|
||||
var names = [];
|
||||
|
||||
while (search.length) {
|
||||
var id = search.shift();
|
||||
|
||||
if (id.type === "Identifier") {
|
||||
names.push(id.name);
|
||||
} else if (id.type === "ArrayPattern") {
|
||||
_.each(id.elements, function (elem) {
|
||||
search.push(elem);
|
||||
});
|
||||
} else if (id.type === "ObjectPattern") {
|
||||
_.each(id.properties, function (prop) {
|
||||
search.push(prop.value);
|
||||
});
|
||||
} else {
|
||||
throw new Error("unknown node " + id.type);
|
||||
}
|
||||
}
|
||||
|
||||
_.each(names, function (name) {
|
||||
_.each(util.getIds(declar.id), function (name) {
|
||||
check(declar, name);
|
||||
constants.push(name);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
var traverse = require("../traverse");
|
||||
var util = require("../util");
|
||||
var b = require("ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.VariableDeclaration = function (node, parent, file) {
|
||||
if (node.kind !== "let") return;
|
||||
node.kind = "var";
|
||||
|
||||
var ids = {};
|
||||
|
||||
_.each(node.declarations, function (declar) {
|
||||
_.each(util.getIds(declar.id), function (id) {
|
||||
ids[id] = b.identifier(file.generateUid(id));
|
||||
});
|
||||
});
|
||||
|
||||
var replaceId = function (node, parent) {
|
||||
// not an identifier so we have no use for this node
|
||||
if (node.type !== "Identifier") return;
|
||||
|
||||
// not a let reference
|
||||
var id = ids[node.name];
|
||||
if (!id) return;
|
||||
|
||||
// we're a property key
|
||||
if (parent.type === "Property" && parent.key === node) return;
|
||||
|
||||
if (parent.type !== "MemberExpression") {
|
||||
// we aren't in a member expression
|
||||
return id;
|
||||
} else if (parent.type === "MemberExpression" && parent.property === node && parent.computed) {
|
||||
// we're in a member expression but we're a computed property so we're referenced
|
||||
return id;
|
||||
} else if (parent.type === "MemberExpression" && parent.object === node) {
|
||||
// we're in a member expression and we're the object so we're referenced
|
||||
return id;
|
||||
}
|
||||
};
|
||||
|
||||
var replace = function (node, parent) {
|
||||
if (_.contains(traverse.FUNCTION_TYPES, node.type)) {
|
||||
var letReferences = [];
|
||||
|
||||
traverse(node, function (node, parent) {
|
||||
var id = replaceId(node, parent);
|
||||
if (id) letReferences.push(id);
|
||||
return id;
|
||||
});
|
||||
|
||||
if (letReferences.length) {
|
||||
return b.callExpression(
|
||||
b.functionExpression(null, letReferences,
|
||||
b.blockStatement([
|
||||
b.returnStatement(node)
|
||||
]
|
||||
)
|
||||
), letReferences)
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return replaceId(node, parent);
|
||||
};
|
||||
|
||||
traverse(parent, replace);
|
||||
};
|
||||
|
||||
@@ -44,6 +44,31 @@ exports.removeProperties = function (tree) {
|
||||
return tree;
|
||||
};
|
||||
|
||||
exports.getIds = function (node) {
|
||||
var search = [node];
|
||||
var ids = [];
|
||||
|
||||
while (search.length) {
|
||||
var id = search.shift();
|
||||
|
||||
if (id.type === "Identifier") {
|
||||
ids.push(id.name);
|
||||
} else if (id.type === "ArrayPattern") {
|
||||
_.each(id.elements, function (elem) {
|
||||
search.push(elem);
|
||||
});
|
||||
} else if (id.type === "ObjectPattern") {
|
||||
_.each(id.properties, function (prop) {
|
||||
search.push(prop.value);
|
||||
});
|
||||
} else {
|
||||
throw new Error("unknown node " + id.type);
|
||||
}
|
||||
}
|
||||
|
||||
return ids;
|
||||
};
|
||||
|
||||
exports.errorWithNode = function (node, msg) {
|
||||
var line = node.loc.start.line;
|
||||
var col = node.loc.start.column;
|
||||
|
||||
Reference in New Issue
Block a user