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

Commit

Permalink
feat: resolve body of default function arrow functions (#82)
Browse files Browse the repository at this point in the history
closes #80
  • Loading branch information
elevatebart committed Jan 22, 2019
1 parent abdbcd1 commit ab57dfa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/script-handlers/__tests__/propHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ describe('propHandler', () => {
defaultValue: { value: `"normal"` },
})
})

it('should return the body of the function as default value without parenthesis', () => {
const src = `
export default {
props: {
test: {
default: () => ({})
}
}
}
`
tester(src, {
defaultValue: { value: `{}` },
})
})
})

describe('description', () => {
Expand Down
21 changes: 16 additions & 5 deletions src/script-handlers/propHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,24 @@ export function describeDefault(
) {
const defaultArray = propPropertiesPath.filter(p => p.node.key.name === 'default')
if (defaultArray.length) {
const defaultPath = defaultArray[0].get('value')
let defaultPath = defaultArray[0].get('value')

let parenthesized = false
if (
bt.isArrowFunctionExpression(defaultPath.node) &&
bt.isObjectExpression(defaultPath.node.body) // if () => ({})
) {
defaultPath = defaultPath.get('body')
const extra = (defaultPath.node as any).extra
if (extra && extra.parenthesized) {
parenthesized = true
}
}

const func =
bt.isArrowFunctionExpression(defaultPath.node) || bt.isFunctionExpression(defaultPath.node)
const rawValue = recast.print(defaultPath).code
propDescriptor.defaultValue = {
func,
value: recast.print(defaultPath).code,
func: bt.isFunction(defaultPath.node),
value: parenthesized ? rawValue.slice(1, rawValue.length - 1) : rawValue,
}
}
}

0 comments on commit ab57dfa

Please sign in to comment.