@@ -3,6 +3,7 @@ import * as _ from 'lodash';
3
3
import * as ReactDocgen from 'react-docgen' ;
4
4
import * as reactDocgenHandlers from 'react-docgen/dist/handlers' ;
5
5
import actualNameHandler from './actualNameHandler' ;
6
+ import { relativePath } from './relativePath' ;
6
7
7
8
const defaultHandlers = Object . values ( reactDocgenHandlers ) . map ( handler => handler ) ;
8
9
@@ -12,8 +13,8 @@ export default function({ types: t }) {
12
13
Program : {
13
14
exit ( path , state ) {
14
15
injectReactDocgenInfo ( path , state , this . file . code , t ) ;
15
- }
16
- }
16
+ } ,
17
+ } ,
17
18
} ,
18
19
} ;
19
20
}
@@ -28,22 +29,22 @@ function injectReactDocgenInfo(path, state, code, t) {
28
29
resolver = ReactDocgen . resolver [ state . opts . resolver ] ;
29
30
}
30
31
31
- let customHandlers = [ ]
32
+ let customHandlers = [ ] ;
32
33
if ( state . opts . handlers ) {
33
34
state . opts . handlers . forEach ( handler => {
34
- customHandlers . push ( require ( handler ) )
35
- } )
35
+ customHandlers . push ( require ( handler ) ) ;
36
+ } ) ;
36
37
}
37
38
38
- const handlers = [ ...defaultHandlers , ...customHandlers , actualNameHandler ]
39
+ const handlers = [ ...defaultHandlers , ...customHandlers , actualNameHandler ] ;
39
40
docgenResults = ReactDocgen . parse ( code , resolver , handlers ) ;
40
41
41
42
if ( state . opts . removeMethods ) {
42
43
docgenResults . forEach ( function ( docgenResult ) {
43
44
delete docgenResult . methods ;
44
- } )
45
+ } ) ;
45
46
}
46
- } catch ( e ) {
47
+ } catch ( e ) {
47
48
// this is for debugging the error only, do not ship this console log or else it pollutes the webpack output
48
49
// console.log(e);
49
50
return ;
@@ -61,12 +62,13 @@ function injectReactDocgenInfo(path, state, code, t) {
61
62
const docNode = buildObjectExpression ( docgenResult , t ) ;
62
63
const docgenInfo = t . expressionStatement (
63
64
t . assignmentExpression (
64
- "=" ,
65
+ '=' ,
65
66
t . memberExpression ( t . identifier ( exportName ) , t . identifier ( '__docgenInfo' ) ) ,
66
67
docNode
67
- ) ) ;
68
+ )
69
+ ) ;
68
70
69
- const exportPath = program . get ( 'body' ) . find ( ( node ) => isExportCurrent ( node , exportName , t ) ) ;
71
+ const exportPath = program . get ( 'body' ) . find ( node => isExportCurrent ( node , exportName , t ) ) ;
70
72
71
73
if ( exportPath ) {
72
74
exportPath . insertBefore ( docgenInfo ) ;
@@ -81,80 +83,60 @@ function injectReactDocgenInfo(path, state, code, t) {
81
83
function injectDocgenGlobal ( className , path , state , t ) {
82
84
const program = path . scope . getProgramParent ( ) . path ;
83
85
84
- if ( ! state . opts . DOC_GEN_COLLECTION_NAME ) {
86
+ if ( ! state . opts . DOC_GEN_COLLECTION_NAME ) {
85
87
return ;
86
88
}
87
89
88
90
const globalName = state . opts . DOC_GEN_COLLECTION_NAME ;
89
- const filePath = Path . relative ( './' , Path . resolve ( './' , path . hub . file . opts . filename ) ) ;
91
+ const filePath = relativePath ( path . hub . file . opts . filename ) ;
90
92
const globalNode = t . ifStatement (
91
93
t . binaryExpression (
92
94
'!==' ,
93
- t . unaryExpression (
94
- 'typeof' ,
95
- t . identifier ( globalName )
96
- ) ,
95
+ t . unaryExpression ( 'typeof' , t . identifier ( globalName ) ) ,
97
96
t . stringLiteral ( 'undefined' )
98
97
) ,
99
98
t . blockStatement ( [
100
99
t . expressionStatement (
101
100
t . assignmentExpression (
102
101
'=' ,
103
- t . memberExpression (
104
- t . identifier ( globalName ) ,
105
- t . stringLiteral ( filePath ) ,
106
- true
107
- ) ,
102
+ t . memberExpression ( t . identifier ( globalName ) , t . stringLiteral ( filePath ) , true ) ,
108
103
t . objectExpression ( [
109
- t . objectProperty (
110
- t . identifier ( 'name' ) ,
111
- t . stringLiteral ( className )
112
- ) ,
104
+ t . objectProperty ( t . identifier ( 'name' ) , t . stringLiteral ( className ) ) ,
113
105
t . objectProperty (
114
106
t . identifier ( 'docgenInfo' ) ,
115
- t . memberExpression (
116
- t . identifier ( className ) ,
117
- t . identifier ( '__docgenInfo' )
118
- )
107
+ t . memberExpression ( t . identifier ( className ) , t . identifier ( '__docgenInfo' ) )
119
108
) ,
120
- t . objectProperty (
121
- t . identifier ( 'path' ) ,
122
- t . stringLiteral ( filePath )
123
- )
109
+ t . objectProperty ( t . identifier ( 'path' ) , t . stringLiteral ( filePath ) ) ,
124
110
] )
125
111
)
126
- )
112
+ ) ,
127
113
] )
128
114
) ;
129
115
program . pushContainer ( 'body' , globalNode ) ;
130
116
}
131
117
132
- function buildObjectExpression ( obj , t ) {
133
- if ( _ . isPlainObject ( obj ) ) {
118
+ function buildObjectExpression ( obj , t ) {
119
+ if ( _ . isPlainObject ( obj ) ) {
134
120
const children = [ ] ;
135
121
for ( let key in obj ) {
136
122
if ( key === 'actualName' ) continue ;
137
- if ( ! obj . hasOwnProperty ( key ) || _ . isUndefined ( obj [ key ] ) ) continue ;
138
- children . push (
139
- t . objectProperty (
140
- t . stringLiteral ( key ) ,
141
- buildObjectExpression ( obj [ key ] , t )
142
- ) ) ;
123
+ if ( ! obj . hasOwnProperty ( key ) || _ . isUndefined ( obj [ key ] ) ) continue ;
124
+ children . push ( t . objectProperty ( t . stringLiteral ( key ) , buildObjectExpression ( obj [ key ] , t ) ) ) ;
143
125
}
144
126
return t . objectExpression ( children ) ;
145
127
} else if ( _ . isString ( obj ) ) {
146
128
return t . stringLiteral ( obj ) ;
147
129
} else if ( _ . isBoolean ( obj ) ) {
148
130
return t . booleanLiteral ( obj ) ;
149
- } else if ( _ . isNumber ( obj ) ) {
131
+ } else if ( _ . isNumber ( obj ) ) {
150
132
return t . numericLiteral ( obj ) ;
151
133
} else if ( _ . isArray ( obj ) ) {
152
134
const children = [ ] ;
153
- obj . forEach ( function ( val ) {
135
+ obj . forEach ( function ( val ) {
154
136
children . push ( buildObjectExpression ( val , t ) ) ;
155
137
} ) ;
156
138
return t . ArrayExpression ( children ) ;
157
- } else if ( _ . isNull ( obj ) ) {
139
+ } else if ( _ . isNull ( obj ) ) {
158
140
return t . nullLiteral ( ) ;
159
141
}
160
142
}
@@ -170,22 +152,18 @@ function isExportCurrent(path, exportName, t) {
170
152
if ( t . isExportDefaultDeclaration ( path ) ) {
171
153
const decl = path . get ( 'declaration' ) ;
172
154
173
- const identifier = (
174
- decl . isIdentifier ( ) // export default MyComp
175
- ? decl . node . name
176
- : getComponentFromHoC ( decl ) // export default withHoC(MyComp)
177
- ) ;
155
+ const identifier = decl . isIdentifier ( ) // export default MyComp
156
+ ? decl . node . name
157
+ : getComponentFromHoC ( decl ) ; // export default withHoC(MyComp)
178
158
179
159
if ( identifier === exportName ) {
180
- return true
160
+ return true ;
181
161
}
182
162
}
183
163
184
164
if ( t . isExportNamedDeclaration ( path ) ) {
185
- return path . get ( 'specifiers' ) . find ( ( sp ) => (
186
- sp . node . exported . name === exportName
187
- ) )
165
+ return path . get ( 'specifiers' ) . find ( sp => sp . node . exported . name === exportName ) ;
188
166
}
189
167
190
- return false
168
+ return false ;
191
169
}
0 commit comments