clean up constants transformer
This commit is contained in:
parent
69db46f96b
commit
d0a2bd170e
@ -9,24 +9,27 @@ exports.ForOfStatement =
|
|||||||
exports.ForStatement = function (node, parent, file) {
|
exports.ForStatement = function (node, parent, file) {
|
||||||
var constants = {};
|
var constants = {};
|
||||||
|
|
||||||
var check = function (node, names, parent) {
|
var check = function (parent, names) {
|
||||||
_.each(names, function (name) {
|
_.each(names, function (nameNode, name) {
|
||||||
if (!_.has(constants, name)) return;
|
if (!_.has(constants, name)) return;
|
||||||
if (parent && t.isBlockStatement(parent) && parent !== constants[name]) return;
|
if (parent && t.isBlockStatement(parent) && parent !== constants[name]) return;
|
||||||
|
|
||||||
throw file.errorWithNode(node, name + " is read-only");
|
throw file.errorWithNode(nameNode, name + " is read-only");
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var getIds = function (node) {
|
var getIds = function (node) {
|
||||||
return t.getIds(node, false, ["MemberExpression"]);
|
return t.getIds(node, true, ["MemberExpression"]);
|
||||||
};
|
};
|
||||||
|
|
||||||
_.each(node.body, function (child, parent) {
|
_.each(node.body, function (child, parent) {
|
||||||
if (child && t.isVariableDeclaration(child, { kind: "const" })) {
|
if (child && t.isVariableDeclaration(child, { kind: "const" })) {
|
||||||
_.each(child.declarations, function (declar) {
|
_.each(child.declarations, function (declar) {
|
||||||
_.each(getIds(declar), function (name) {
|
_.each(getIds(declar), function (nameNode, name) {
|
||||||
check(declar, [name], parent);
|
var names = {};
|
||||||
|
names[name] = nameNode;
|
||||||
|
check(parent, names);
|
||||||
|
|
||||||
constants[name] = parent;
|
constants[name] = parent;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -42,9 +45,10 @@ exports.ForStatement = function (node, parent, file) {
|
|||||||
|
|
||||||
traverse(node, function (child, parent) {
|
traverse(node, function (child, parent) {
|
||||||
if (child._ignoreConstant) return;
|
if (child._ignoreConstant) return;
|
||||||
|
if (t.isVariableDeclaration(child)) return;
|
||||||
|
|
||||||
if (t.isDeclaration(child) || t.isAssignmentExpression(child)) {
|
if (t.isVariableDeclarator(child) || t.isDeclaration(child) || t.isAssignmentExpression(child)) {
|
||||||
check(child, getIds(child), parent);
|
check(parent, getIds(child));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user