1717
1818var recast = require ( 'recast' ) ;
1919var types = recast . types ;
20- var namedTypes = types . namedTypes ;
2120var builders = types . builders ;
22- var hasOwn = Object . prototype . hasOwnProperty ;
2321
2422function 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
10796exports . propagate = propagate ;
0 commit comments