This repository was archived by the owner on Mar 8, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +31
-5
lines changed Expand file tree Collapse file tree 2 files changed +31
-5
lines changed Original file line number Diff line number Diff line change @@ -198,6 +198,21 @@ describe('propHandler', () => {
198
198
defaultValue : { value : `"normal"` } ,
199
199
} )
200
200
} )
201
+
202
+ it ( 'should return the body of the function as default value without parenthesis' , ( ) => {
203
+ const src = `
204
+ export default {
205
+ props: {
206
+ test: {
207
+ default: () => ({})
208
+ }
209
+ }
210
+ }
211
+ `
212
+ tester ( src , {
213
+ defaultValue : { value : `{}` } ,
214
+ } )
215
+ } )
201
216
} )
202
217
203
218
describe ( 'description' , ( ) => {
Original file line number Diff line number Diff line change @@ -136,13 +136,24 @@ export function describeDefault(
136
136
) {
137
137
const defaultArray = propPropertiesPath . filter ( p => p . node . key . name === 'default' )
138
138
if ( defaultArray . length ) {
139
- const defaultPath = defaultArray [ 0 ] . get ( 'value' )
139
+ let defaultPath = defaultArray [ 0 ] . get ( 'value' )
140
+
141
+ let parenthesized = false
142
+ if (
143
+ bt . isArrowFunctionExpression ( defaultPath . node ) &&
144
+ bt . isObjectExpression ( defaultPath . node . body ) // if () => ({})
145
+ ) {
146
+ defaultPath = defaultPath . get ( 'body' )
147
+ const extra = ( defaultPath . node as any ) . extra
148
+ if ( extra && extra . parenthesized ) {
149
+ parenthesized = true
150
+ }
151
+ }
140
152
141
- const func =
142
- bt . isArrowFunctionExpression ( defaultPath . node ) || bt . isFunctionExpression ( defaultPath . node )
153
+ const rawValue = recast . print ( defaultPath ) . code
143
154
propDescriptor . defaultValue = {
144
- func,
145
- value : recast . print ( defaultPath ) . code ,
155
+ func : bt . isFunction ( defaultPath . node ) ,
156
+ value : parenthesized ? rawValue . slice ( 1 , rawValue . length - 1 ) : rawValue ,
146
157
}
147
158
}
148
159
}
You can’t perform that action at this time.
0 commit comments