Skip to content

Commit 0b584b9

Browse files
benjamnzpao
authored andcommitted
Better comments for vendor/constants.js.
1 parent 1d9a0ce commit 0b584b9

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

vendor/constants.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ function transform(ast, constants) {
3030

3131
return types.traverse(ast, function(node, traverse) {
3232
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.
3336
if (namedTypes.MemberExpression.check(this.parent.node) &&
3437
this.name === 'property' &&
3538
!this.parent.node.computed) {
3639
return false;
3740
}
3841

42+
// There could in principle be a constant called "hasOwnProperty",
43+
// so be careful always to use Object.prototype.hasOwnProperty.
3944
if (hasOwn.call(constants, node.name)) {
4045
this.replace(builders.literal(constants[node.name]));
4146
return false;
@@ -45,19 +50,30 @@ function transform(ast, constants) {
4550
if (!constants.__DEV__) {
4651
if (namedTypes.Identifier.check(node.callee) &&
4752
node.callee.name === 'invariant') {
53+
// Truncate the arguments of invariant(condition, ...)
54+
// statements to just the condition.
4855
node.arguments.length = 1;
4956
}
5057
}
5158

5259
} else if (namedTypes.IfStatement.check(node) &&
5360
namedTypes.Literal.check(node.test)) {
5461
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+
5669
} else if (node.alternate) {
70+
// Replace the whole if-statement with just the alternate clause.
5771
this.replace(node.alternate);
5872
return false;
73+
5974
} else {
60-
this.replace(); // Remove the if-statement.
75+
// Remove the entire if-statement.
76+
this.replace();
6177
return false;
6278
}
6379
}

0 commit comments

Comments
 (0)