Generates a storybook story for a "Reusable wrappers" example from the docs.
(filename, imports, code, skipImports = false)
| 326 | |
| 327 | /** Generates a storybook story for a "Reusable wrappers" example from the docs. */ |
| 328 | function generateStory(filename, imports, code, skipImports = false) { |
| 329 | // Match the JSX part of the code. |
| 330 | let example = code.match(/^(<(.|\n)*>)$/m, ''); |
| 331 | if (!example) { |
| 332 | return ''; |
| 333 | } |
| 334 | |
| 335 | let name = basename(filename, '.mdx'); |
| 336 | code = |
| 337 | imports + |
| 338 | ` |
| 339 | import type {Meta} from '@storybook/react'; |
| 340 | |
| 341 | const meta: Meta<typeof ${name}> = { |
| 342 | component: ${name}, |
| 343 | parameters: { |
| 344 | layout: 'centered', |
| 345 | }, |
| 346 | tags: ['autodocs'] |
| 347 | }; |
| 348 | |
| 349 | export default meta; |
| 350 | |
| 351 | export const Example = () => ( |
| 352 | ${example[0].replace(/\n/g, '\n ')} |
| 353 | ); |
| 354 | `; |
| 355 | |
| 356 | let ast = recast.parse(code, { |
| 357 | parser: { |
| 358 | parse() { |
| 359 | return parse(code, { |
| 360 | sourceType: 'module', |
| 361 | plugins: ['jsx', 'typescript'], |
| 362 | tokens: true, |
| 363 | errorRecovery: true |
| 364 | }); |
| 365 | } |
| 366 | } |
| 367 | }); |
| 368 | |
| 369 | removeDuplicateImports(ast); |
| 370 | |
| 371 | let imported = new Set(); |
| 372 | if (skipImports) { |
| 373 | imported.add(name); |
| 374 | } |
| 375 | |
| 376 | let args = new Map(); |
| 377 | if (name === 'ListBox' || name === 'GridList') { |
| 378 | // Avoid onAction getting a storybook control, which changes the selection behavior. |
| 379 | args.set('onAction', t.nullLiteral()); |
| 380 | } else if (name === 'Table') { |
| 381 | args.set('onRowAction', t.nullLiteral()); |
| 382 | } |
| 383 | |
| 384 | traverse.default(ast, { |
| 385 | 'ImportSpecifier|ImportDefaultSpecifier'(specifier) { |
no test coverage detected