Skip to content

Commit

Permalink
feat(components-json): update parser to support StoryObj from storybo…
Browse files Browse the repository at this point in the history
…ok (#4748)

Co-authored-by: Josh Black <[email protected]>
  • Loading branch information
joshblack and joshblack authored Jul 18, 2024
1 parent 2ec6d14 commit c744a8e
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions packages/react/script/components-json/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,35 @@ function getStorySourceCode(filepath: string) {
ExportNamedDeclaration(path) {
const varDeclaration = path.node.declaration

let id: Identifier
let func: ArrowFunctionExpression | FunctionDeclaration
let id: Identifier | null = null
let func: ArrowFunctionExpression | FunctionDeclaration | null = null

if (varDeclaration?.type === 'VariableDeclaration') {
id = varDeclaration.declarations[0].id as Identifier
const init = varDeclaration.declarations[0].init
if (init?.type === 'ArrowFunctionExpression') func = init
else return // not a function = not story
if (init?.type === 'ArrowFunctionExpression') {
func = init
} else if (init?.type === 'ObjectExpression') {
const renderProperty = init.properties.find(property => {
if (property.type === 'ObjectProperty') {
if (property.key.type === 'Identifier') {
return property.key.name === 'render'
}
}
return false
})

if (renderProperty?.type === 'ObjectProperty' && renderProperty.value.type === 'ArrowFunctionExpression') {
func = renderProperty.value
}
}
} else if (varDeclaration?.type === 'FunctionDeclaration') {
id = varDeclaration.id as Identifier
func = varDeclaration
} else {
return // not a function = not story
}

if (!id || !func) {
return
}

const code = prettier
Expand Down

0 comments on commit c744a8e

Please sign in to comment.