@@ -18,17 +18,12 @@ const isInMethod = (t, path, parentLimitPath) => {
18
18
}
19
19
20
20
/**
21
- * Check if it's a functional componet declarator
21
+ * Check path has JSX
22
22
* @param t
23
23
* @param path
24
24
* @returns boolean
25
25
*/
26
- const isFunctionalComponentDeclarator = ( t , path ) => {
27
- const firstCharacter = path . get ( 'id.name' ) . node [ 0 ]
28
- if ( firstCharacter < 'A' || firstCharacter > 'Z' ) {
29
- return false
30
- }
31
-
26
+ const hasJSX = ( t , path ) => {
32
27
let hasJSX = false
33
28
34
29
path . traverse ( {
@@ -39,10 +34,41 @@ const isFunctionalComponentDeclarator = (t, path) => {
39
34
} ,
40
35
} )
41
36
42
- if ( ! hasJSX ) {
37
+ return hasJSX
38
+ }
39
+
40
+ /**
41
+ * Check if it's a functional componet declarator
42
+ * @param t
43
+ * @param path
44
+ * @returns boolean
45
+ */
46
+ const isFunctionalComponentDeclarator = ( t , path ) => {
47
+ const firstCharacter = path . get ( 'id.name' ) . node [ 0 ]
48
+ if ( firstCharacter < 'A' || firstCharacter > 'Z' ) {
43
49
return false
44
50
}
45
- return true
51
+
52
+ return hasJSX ( t , path )
53
+ }
54
+
55
+ /**
56
+ * Convert arrow function to functional component
57
+ * @param t
58
+ * @param path
59
+ * @param name
60
+ */
61
+ const convertFunctionalComponent = ( t , path , name = null ) => {
62
+ const params = [ t . identifier ( 'h' ) , ...path . node . params ]
63
+ const body = path . node . body
64
+ const props = [
65
+ t . objectProperty ( t . identifier ( 'functional' ) , t . booleanLiteral ( true ) ) ,
66
+ t . objectProperty ( t . identifier ( 'render' ) , t . arrowFunctionExpression ( params , body ) ) ,
67
+ ]
68
+ if ( process . env . NODE_ENV === 'development' && name ) {
69
+ props . unshift ( t . objectProperty ( t . identifier ( 'name' ) , t . stringLiteral ( name ) ) )
70
+ }
71
+ path . replaceWith ( t . objectExpression ( props ) )
46
72
}
47
73
48
74
export default babel => {
@@ -53,6 +79,13 @@ export default babel => {
53
79
visitor : {
54
80
Program ( path ) {
55
81
path . traverse ( {
82
+ ExportDefaultDeclaration ( path ) {
83
+ if ( ! t . isArrowFunctionExpression ( path . node . declaration ) || ! hasJSX ( t , path ) ) {
84
+ return
85
+ }
86
+
87
+ convertFunctionalComponent ( t , path . get ( 'declaration' ) )
88
+ } ,
56
89
VariableDeclaration ( path ) {
57
90
if (
58
91
path . node . declarations . length !== 1 ||
@@ -69,20 +102,7 @@ export default babel => {
69
102
}
70
103
71
104
const name = path . node . declarations [ 0 ] . id . name
72
- const params = [ t . identifier ( 'h' ) , ...path . node . declarations [ 0 ] . init . params ]
73
- const body = path . node . declarations [ 0 ] . init . body
74
- const isDevEnv = process . env . NODE_ENV === 'development'
75
- const props = [
76
- t . objectProperty ( t . identifier ( 'functional' ) , t . booleanLiteral ( true ) ) ,
77
- t . objectProperty ( t . identifier ( 'render' ) , t . arrowFunctionExpression ( params , body ) ) ,
78
- ]
79
- if ( isDevEnv ) {
80
- props . unshift ( t . objectProperty ( t . identifier ( 'name' ) , t . stringLiteral ( name ) ) )
81
- }
82
- path . replaceWith (
83
- t . variableDeclaration ( 'const' , [ t . variableDeclarator ( t . identifier ( name ) , t . objectExpression ( props ) ) ] ) ,
84
- [ ] ,
85
- )
105
+ convertFunctionalComponent ( t , path . get ( 'declarations' ) [ 0 ] . get ( 'init' ) , name )
86
106
} ,
87
107
} )
88
108
} ,
0 commit comments