@samitouri / QOS-React-2 / commits / d7f21d760b

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 #1824

Paul O’Shannessy committed Aug 14, 2014 at 11:11 UTC d7f21d760b3a222f5a6ceb9f386d45dfabecddb7
2 files changed +48 -59
package.json
+1 -1
@@ -59,7 +59,7 @@
59 "phantomjs": "~1.9",
60 "platform": "^1.1.0",
61 "populist": "~0.1.6",
62 - "recast": "~0.5.6",
62 + "recast": "^0.6.10",
63 "sauce-tunnel": "~1.1.0",
64 "semver": "^2.3.0",
65 "tmp": "~0.0.18",
vendor/constants.js
+47 -58
@@ -17,9 +17,7 @@
17
18 var recast = require('recast');
19 var types = recast.types;
20 -var namedTypes = types.namedTypes;
20 var builders = types.builders;
22 -var hasOwn = Object.prototype.hasOwnProperty;
21
22 function propagate(constants, source) {
23 return recast.print(transform(recast.parse(source), constants)).code;
@@ -39,69 +37,60 @@ var DEV_EXPRESSION = builders.binaryExpression(
37 )
38 );
39
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 + }
48
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
54
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 + },
57
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]]
71 )
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;
86 }
97 - });
87 + this.traverse(nodePath);
88 + }
89 }
90
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);
94 }
95
96 exports.propagate = propagate;