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', () => {
198198 defaultValue : { value : `"normal"` } ,
199199 } )
200200 } )
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+ } )
201216 } )
202217
203218 describe ( 'description' , ( ) => {
Original file line number Diff line number Diff line change @@ -136,13 +136,24 @@ export function describeDefault(
136136) {
137137 const defaultArray = propPropertiesPath . filter ( p => p . node . key . name === 'default' )
138138 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+ }
140152
141- const func =
142- bt . isArrowFunctionExpression ( defaultPath . node ) || bt . isFunctionExpression ( defaultPath . node )
153+ const rawValue = recast . print ( defaultPath ) . code
143154 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 ,
146157 }
147158 }
148159}
You can’t perform that action at this time.
0 commit comments