Skip to content

Commit

Permalink
fix(docgen): resolve exported displayname
Browse files Browse the repository at this point in the history
closes #1220
  • Loading branch information
elevatebart committed Nov 21, 2021
1 parent 2f2fd17 commit d414c4a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,21 @@ describe('displayNameHandler', () => {
}
expect(documentation.set).toHaveBeenCalledWith('displayName', 'name-123')
})

it('should return the right component name as a constant when it is exported', () => {
const src = `
export const NAME = 'name-123';
export default {
name: NAME,
components: {
testComp: {}
}
}
`
const def = parse(src)
if (def) {
displayNameHandler(documentation, def)
}
expect(documentation.set).toHaveBeenCalledWith('displayName', 'name-123')
})
})
17 changes: 12 additions & 5 deletions packages/vue-docgen-api/src/script-handlers/displayNameHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,18 @@ function getDeclaredConstantValue(prog: NodePath<bt.Program>, nameConstId: strin
const globalVariableDeclarations = body.filter((node: bt.Node) =>
bt.isVariableDeclaration(node)
) as bt.VariableDeclaration[]
const declarators = globalVariableDeclarations.reduce(
(a: bt.VariableDeclarator[], declPath) => a.concat(declPath.declarations),
[]
)
const nodeDeclaratorArray = declarators.filter(

const globalVariableExports = body
.filter(
(node: bt.Node) =>
bt.isExportNamedDeclaration(node) && bt.isVariableDeclaration(node.declaration)
)
.map((node: bt.ExportNamedDeclaration) => node.declaration) as bt.VariableDeclaration[]

const declarations = globalVariableDeclarations
.concat(globalVariableExports)
.reduce((a: bt.VariableDeclarator[], declPath) => a.concat(declPath.declarations), [])
const nodeDeclaratorArray = declarations.filter(
d => bt.isIdentifier(d.id) && d.id.name === nameConstId
)
const nodeDeclarator = nodeDeclaratorArray.length ? nodeDeclaratorArray[0] : undefined
Expand Down

0 comments on commit d414c4a

Please sign in to comment.