1
- export default function ( { Plugin , types : t } ) {
1
+ export default function ( { types : t } ) {
2
2
const depthKey = '__reactTransformDepth' ;
3
3
const recordsKey = '__reactTransformRecords' ;
4
4
const wrapComponentIdKey = '__reactTransformWrapComponentId' ;
@@ -55,10 +55,7 @@ export default function ({ Plugin, types: t }) {
55
55
return false ;
56
56
}
57
57
const first = args [ 0 ] ;
58
- if ( ! t . isObjectExpression ( first ) ) {
59
- return false ;
60
- }
61
- return true ;
58
+ return t . isObjectExpression ( first ) ;
62
59
}
63
60
64
61
/**
@@ -86,26 +83,6 @@ export default function ({ Plugin, types: t }) {
86
83
Array . isArray ( options . transforms ) ;
87
84
}
88
85
89
- /**
90
- * Enforces plugin options to be defined and returns them.
91
- */
92
- function getPluginOptions ( file ) {
93
- if ( ! file . opts || ! file . opts . extra ) {
94
- return ;
95
- }
96
-
97
- let pluginOptions = file . opts . extra [ 'react-transform' ] ;
98
- if ( ! isValidOptions ( pluginOptions ) ) {
99
- throw new Error (
100
- 'babel-plugin-react-transform requires that you specify ' +
101
- 'extras["react-transform"] in .babelrc ' +
102
- 'or in your Babel Node API call options, and that it is an object with ' +
103
- 'a transforms property which is an array.'
104
- ) ;
105
- }
106
- return pluginOptions ;
107
- }
108
-
109
86
/**
110
87
* Creates a record about us having visited a valid React component.
111
88
* Such records will later be merged into a single object.
@@ -118,15 +95,15 @@ export default function ({ Plugin, types: t }) {
118
95
119
96
let props = [ ] ;
120
97
if ( typeof displayName === 'string' ) {
121
- props . push ( t . property ( 'init' ,
98
+ props . push ( t . objectProperty (
122
99
t . identifier ( 'displayName' ) ,
123
- t . literal ( displayName )
100
+ t . stringLiteral ( displayName )
124
101
) ) ;
125
102
}
126
103
if ( state [ depthKey ] > 0 ) {
127
- props . push ( t . property ( 'init' ,
104
+ props . push ( t . objectProperty (
128
105
t . identifier ( 'isInFunction' ) ,
129
- t . literal ( true )
106
+ t . booleanLiteral ( true )
130
107
) ) ;
131
108
}
132
109
@@ -140,7 +117,7 @@ export default function ({ Plugin, types: t }) {
140
117
function addComponentRecord ( node , scope , file , state ) {
141
118
const [ uniqueId , definition ] = createComponentRecord ( node , scope , file , state ) ;
142
119
state [ recordsKey ] = state [ recordsKey ] || [ ] ;
143
- state [ recordsKey ] . push ( t . property ( 'init' ,
120
+ state [ recordsKey ] . push ( t . objectProperty (
144
121
t . identifier ( uniqueId ) ,
145
122
definition
146
123
) ) ;
@@ -172,21 +149,20 @@ export default function ({ Plugin, types: t }) {
172
149
* Imports and calls a particular transformation target function.
173
150
* You may specify several such transformations, so they are handled separately.
174
151
*/
175
- function defineInitTransformCall ( scope , file , recordsId , targetOptions ) {
152
+ function defineInitTransformCall ( scope , { file } , recordsId , targetOptions ) {
176
153
const id = scope . generateUidIdentifier ( 'reactComponentWrapper' ) ;
177
154
const { transform, imports = [ ] , locals = [ ] } = targetOptions ;
178
155
const { filename } = file . opts ;
179
-
180
156
return [ id , t . variableDeclaration ( 'var' , [
181
157
t . variableDeclarator ( id ,
182
158
t . callExpression ( file . addImport ( transform ) , [
183
159
t . objectExpression ( [
184
- t . property ( 'init' , t . identifier ( 'filename' ) , t . literal ( filename ) ) ,
185
- t . property ( 'init' , t . identifier ( 'components' ) , recordsId ) ,
186
- t . property ( 'init' , t . identifier ( 'locals' ) , t . arrayExpression (
160
+ t . objectProperty ( t . identifier ( 'filename' ) , t . stringLiteral ( filename ) ) ,
161
+ t . objectProperty ( t . identifier ( 'components' ) , recordsId ) ,
162
+ t . objectProperty ( t . identifier ( 'locals' ) , t . arrayExpression (
187
163
locals . map ( local => t . identifier ( local ) )
188
164
) ) ,
189
- t . property ( 'init' , t . identifier ( 'imports' ) , t . arrayExpression (
165
+ t . objectProperty ( t . identifier ( 'imports' ) , t . arrayExpression (
190
166
imports . map ( imp => file . addImport ( imp , null , 'absolute' ) )
191
167
) )
192
168
] )
@@ -216,21 +192,21 @@ export default function ({ Plugin, types: t }) {
216
192
) ;
217
193
}
218
194
219
- return new Plugin ( 'babel-plugin-react-transform' , {
195
+ return {
220
196
visitor : {
221
197
Function : {
222
- enter ( node , parent , scope , file ) {
198
+ enter ( { node, parent, scope } , file ) {
223
199
if ( ! this . state [ depthKey ] ) {
224
200
this . state [ depthKey ] = 0 ;
225
201
}
226
202
this . state [ depthKey ] ++ ;
227
203
} ,
228
- exit ( node , parent , scope , file ) {
204
+ exit ( { node, parent, scope } , file ) {
229
205
this . state [ depthKey ] -- ;
230
206
}
231
207
} ,
232
208
233
- Class ( node , parent , scope , file ) {
209
+ Class ( { node, scope } , file ) {
234
210
if ( ! isComponentishClass ( node ) ) {
235
211
return ;
236
212
}
@@ -240,12 +216,12 @@ export default function ({ Plugin, types: t }) {
240
216
241
217
node . decorators = node . decorators || [ ] ;
242
218
node . decorators . push ( t . decorator (
243
- t . callExpression ( wrapReactComponentId , [ t . literal ( uniqueId ) ] )
219
+ t . callExpression ( wrapReactComponentId , [ t . stringLiteral ( uniqueId ) ] )
244
220
) ) ;
245
221
} ,
246
222
247
223
CallExpression : {
248
- exit ( node , parent , scope , file ) {
224
+ exit ( { node, scope } , file ) {
249
225
const { isCreateClassCallExpression } = this . state [ cacheKey ] ;
250
226
if ( ! isCreateClass ( node , isCreateClassCallExpression ) ) {
251
227
return ;
@@ -255,25 +231,34 @@ export default function ({ Plugin, types: t }) {
255
231
const uniqueId = addComponentRecord ( node , scope , file , this . state ) ;
256
232
257
233
return t . callExpression (
258
- t . callExpression ( wrapReactComponentId , [ t . literal ( uniqueId ) ] ) ,
234
+ t . callExpression ( wrapReactComponentId , [ t . stringLiteral ( uniqueId ) ] ) ,
259
235
[ node ]
260
236
) ;
261
237
}
262
238
} ,
263
239
264
240
Program : {
265
- enter ( node , parent , scope , file ) {
266
- const options = getPluginOptions ( file ) ;
267
- const factoryMethods = options . factoryMethods || [ 'React.createClass' , 'createClass' ] ;
268
- this . state [ optionsKey ] = options ;
241
+ enter ( { scope } , file ) {
242
+ const { opts } = file ;
243
+ if ( ! isValidOptions ( opts ) ) {
244
+ throw new Error (
245
+ 'babel-plugin-react-transform requires that you specify options in .babelrc ' +
246
+ 'or in your Babel Node API call options, and that it is an object with ' +
247
+ 'a transforms property which is an array.'
248
+ ) ;
249
+ }
250
+ const factoryMethods = opts . factoryMethods || [ 'React.createClass' , 'createClass' ] ;
251
+
252
+ this . state = { } ;
253
+ this . state [ optionsKey ] = opts ;
269
254
this . state [ cacheKey ] = {
270
255
isCreateClassCallExpression : buildIsCreateClassCallExpression ( factoryMethods ) ,
271
256
} ;
272
257
273
258
this . state [ wrapComponentIdKey ] = scope . generateUidIdentifier ( 'wrapComponent' ) ;
274
259
} ,
275
260
276
- exit ( node , parent , scope , file ) {
261
+ exit ( { node, scope } , file ) {
277
262
if ( ! foundComponentRecords ( this . state ) ) {
278
263
return ;
279
264
}
@@ -293,7 +278,6 @@ export default function ({ Plugin, types: t }) {
293
278
// Create one uber function calling each transformation
294
279
const wrapComponentId = this . state [ wrapComponentIdKey ] ;
295
280
const wrapComponent = defineWrapComponent ( wrapComponentId , initTransformIds ) ;
296
-
297
281
return t . program ( [
298
282
recordsVar ,
299
283
...initTransformVars ,
@@ -303,5 +287,5 @@ export default function ({ Plugin, types: t }) {
303
287
}
304
288
}
305
289
}
306
- } ) ;
290
+ } ;
307
291
}
0 commit comments