Skip to content
This repository was archived by the owner on Mar 8, 2019. It is now read-only.

Commit ab57dfa

Browse files
authored
feat: resolve body of default function arrow functions (#82)
closes #80
1 parent abdbcd1 commit ab57dfa

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/script-handlers/__tests__/propHandler.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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', () => {

src/script-handlers/propHandler.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)