-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
constants.js
42 lines (35 loc) 路 1.04 KB
/
constants.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var traverse = require("../traverse");
var util = require("../util");
var _ = require("lodash");
exports.Program =
exports.BlockStatement =
exports.ForInStatement =
exports.ForOfStatement =
exports.ForStatement = function (node) {
var constants = [];
var check = function (node, name) {
if (constants.indexOf(name) >= 0) {
throw util.errorWithNode(node, name + " is read-only");
}
};
_.each(node.body, function (child) {
if (child.type === "VariableDeclaration" && child.kind === "const") {
_.each(child.declarations, function (declar) {
var name = declar.id.name;
check(declar, name);
declar._ignoreConstant = true;
constants.push(name);
});
child.kind = "let";
}
});
if (!constants.length) return;
traverse(node, function (child) {
if (child._ignoreConstant) return;
if (child.type === "VariableDeclarator") {
check(child, child.id.name);
} else if (child.type === "AssignmentExpression") {
check(child, child.left.name);
}
});
};