more efficient constants collision checking

This commit is contained in:
Sebastian McKenzie 2014-11-07 12:26:19 +11:00
parent 26428cde41
commit bf632ca20b

View File

@ -9,17 +9,19 @@ exports.ForOfStatement =
exports.ForStatement = function (node, parent, file) {
var constants = [];
var check = function (node, name) {
if (constants.indexOf(name) >= 0) {
throw file.errorWithNode(node, name + " is read-only");
}
var check = function (node, names) {
_.each(names, function (name) {
if (constants.indexOf(name) >= 0) {
throw file.errorWithNode(node, name + " is read-only");
}
});
};
_.each(node.body, function (child) {
if (child && t.isVariableDeclaration(child) && child.kind === "const") {
_.each(child.declarations, function (declar) {
_.each(t.getIds(declar.id), function (name) {
check(declar, name);
check(declar, [name]);
constants.push(name);
});
@ -34,10 +36,8 @@ exports.ForStatement = function (node, parent, file) {
traverse(node, function (child) {
if (child._ignoreConstant) return;
if (t.isVariableDeclarator(child) || t.isFunctionDeclaration(child) || t.isClassDeclaration(child)) {
check(child, child.id.name);
} else if (t.isAssignmentExpression(child)) {
check(child, child.left.name);
if (t.isVariableDeclarator(child) || t.isFunctionDeclaration(child) || t.isClassDeclaration(child) || t.isAssignmentExpression(child)) {
check(child, t.getIds(child));
}
});
};