Better comments for vendor/constants.js.
Ben Newman committed
Nov 13, 2013 at 15:28 UTC
da717977edc3120ddb5c88766f8564fa09eb7bb0
1 file changed
+18
-2
vendor/constants.js
+18
-2
@@ -30,12 +30,17 @@ function transform(ast, constants) {
30
31
return types.traverse(ast, function(node, traverse) {
32
if (namedTypes.Identifier.check(node)) {
33
+ // If the identifier is the property of a member expression
34
+ // (e.g. object.property), then it definitely is not a constant
35
+ // expression that we want to replace.
36
if (namedTypes.MemberExpression.check(this.parent.node) &&
37
this.name === 'property' &&
38
!this.parent.node.computed) {
39
return false;
40
}
41
42
+ // There could in principle be a constant called "hasOwnProperty",
43
+ // so be careful always to use Object.prototype.hasOwnProperty.
44
if (hasOwn.call(constants, node.name)) {
45
this.replace(builders.literal(constants[node.name]));
46
return false;
@@ -45,6 +50,8 @@ function transform(ast, constants) {
50
if (!constants.__DEV__) {
51
if (namedTypes.Identifier.check(node.callee) &&
52
node.callee.name === 'invariant') {
53
+ // Truncate the arguments of invariant(condition, ...)
54
+ // statements to just the condition.
55
node.arguments.length = 1;
56
}
57
}
@@ -52,12 +59,21 @@ function transform(ast, constants) {
59
} else if (namedTypes.IfStatement.check(node) &&
60
namedTypes.Literal.check(node.test)) {
61
if (node.test.value) {
55
- node.alternate = null;
62
+ // If the alternate (then) branch is dead code, remove it.
63
+ this.get("alternate").replace();
64
+
65
+ // This is what happens when you replace a node with nothing and
66
+ // it can't be removed from a list of statements.
67
+ assert.strictEqual(node.alternate, null);
68
+
69
} else if (node.alternate) {
70
+ // Replace the whole if-statement with just the alternate clause.
71
this.replace(node.alternate);
72
return false;
73
+
74
} else {
60
- this.replace(); // Remove the if-statement.
75
+ // Remove the entire if-statement.
76
+ this.replace();
77
return false;
78
}
79
}