Skip to content

Commit d7f21d7

Browse files
committed
Update recast to 0.6, update constants.js
I dropped the part of constants.js that we weren't using (namely the part where we insert constants) but left it open to do that. It should be trivial, we just aren't using it. Fixes facebook#1824
1 parent 5ca9e19 commit d7f21d7

File tree

2 files changed

+48
-59
lines changed

2 files changed

+48
-59
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"phantomjs": "~1.9",
6060
"platform": "^1.1.0",
6161
"populist": "~0.1.6",
62-
"recast": "~0.5.6",
62+
"recast": "^0.6.10",
6363
"sauce-tunnel": "~1.1.0",
6464
"semver": "^2.3.0",
6565
"tmp": "~0.0.18",

vendor/constants.js

Lines changed: 47 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
var recast = require('recast');
1919
var types = recast.types;
20-
var namedTypes = types.namedTypes;
2120
var builders = types.builders;
22-
var hasOwn = Object.prototype.hasOwnProperty;
2321

2422
function propagate(constants, source) {
2523
return recast.print(transform(recast.parse(source), constants)).code;
@@ -39,69 +37,60 @@ var DEV_EXPRESSION = builders.binaryExpression(
3937
)
4038
);
4139

42-
function transform(ast, constants) {
43-
constants = constants || {};
40+
var visitors = {
41+
visitIdentifier: function(nodePath) {
42+
// If the identifier is the property of a member expression
43+
// (e.g. object.property), then it definitely is not a constant
44+
// expression that we want to replace.
45+
if (nodePath.parentPath.value.type === 'MemberExpression') {
46+
return false;
47+
}
4448

45-
return types.traverse(ast, function(node, traverse) {
46-
if (namedTypes.Identifier.check(node)) {
47-
// If the identifier is the property of a member expression
48-
// (e.g. object.property), then it definitely is not a constant
49-
// expression that we want to replace.
50-
if (namedTypes.MemberExpression.check(this.parent.node) &&
51-
this.name === 'property' &&
52-
!this.parent.node.computed) {
53-
return false;
54-
}
49+
// replace __DEV__ with process.env.NODE_ENV !== 'production'
50+
if (nodePath.value.name === '__DEV__') {
51+
nodePath.replace(DEV_EXPRESSION);
52+
}
53+
// TODO: bring back constant replacement if we decide we need it
5554

56-
// There could in principle be a constant called "hasOwnProperty",
57-
// so be careful always to use Object.prototype.hasOwnProperty.
58-
if (node.name === '__DEV__') {
59-
// replace __DEV__ with process.env.NODE_ENV !== 'production'
60-
this.replace(DEV_EXPRESSION);
61-
return false;
62-
} else if (hasOwn.call(constants, node.name)) {
63-
this.replace(builders.literal(constants[node.name]));
64-
return false;
65-
}
55+
this.traverse(nodePath);
56+
},
6657

67-
} else if (namedTypes.CallExpression.check(node)) {
68-
if (namedTypes.Identifier.check(node.callee) &&
69-
node.callee.name === 'invariant') {
70-
// Truncate the arguments of invariant(condition, ...)
71-
// statements to just the condition based on NODE_ENV
72-
// (dead code removal will remove the extra bytes).
73-
this.replace(
74-
builders.conditionalExpression(
75-
DEV_EXPRESSION,
76-
node,
77-
builders.callExpression(
78-
node.callee,
79-
[node.arguments[0]]
80-
)
58+
visitCallExpression: function(nodePath) {
59+
var node = nodePath.value;
60+
if (node.callee.name === 'invariant') {
61+
// Truncate the arguments of invariant(condition, ...)
62+
// statements to just the condition based on NODE_ENV
63+
// (dead code removal will remove the extra bytes).
64+
nodePath.replace(
65+
builders.conditionalExpression(
66+
DEV_EXPRESSION,
67+
node,
68+
builders.callExpression(
69+
node.callee,
70+
[node.arguments[0]]
8171
)
82-
);
83-
return false;
84-
} else if (namedTypes.Identifier.check(node.callee) &&
85-
node.callee.name === 'warning') {
86-
// Eliminate warning(condition, ...) statements based on NODE_ENV
87-
// (dead code removal will remove the extra bytes).
88-
this.replace(
89-
builders.conditionalExpression(
90-
DEV_EXPRESSION,
91-
node,
92-
builders.literal(null)
93-
)
94-
);
95-
}
72+
)
73+
);
74+
return false;
75+
} else if (node.callee.name === 'warning') {
76+
// Eliminate warning(condition, ...) statements based on NODE_ENV
77+
// (dead code removal will remove the extra bytes).
78+
nodePath.replace(
79+
builders.conditionalExpression(
80+
DEV_EXPRESSION,
81+
node,
82+
builders.literal(null)
83+
)
84+
);
85+
return false;
9686
}
97-
});
87+
this.traverse(nodePath);
88+
}
9889
}
9990

100-
if (!module.parent) {
101-
var constants = JSON.parse(process.argv[3]);
102-
recast.run(function(ast, callback) {
103-
callback(transform(ast, constants));
104-
});
91+
function transform(ast, constants) {
92+
// TODO constants
93+
return recast.visit(ast, visitors);
10594
}
10695

10796
exports.propagate = propagate;

0 commit comments

Comments
 (0)