Skip to content

Commit

Permalink
Merge pull request #12099 from petermikitsh/fix-11994
Browse files Browse the repository at this point in the history
Source-loader: Fix default exports of type TSAsExpression
  • Loading branch information
shilman committed Aug 19, 2020
2 parents 2412c1e + 2ecbd85 commit 231886e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
15 changes: 14 additions & 1 deletion examples/react-ts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@ import type { StorybookConfig } from '@storybook/core/types';
module.exports = {
stories: ['./src/*.stories.*'],
logLevel: 'debug',
addons: ['@storybook/addon-essentials', '@storybook/addon-controls'],
addons: [
'@storybook/addon-essentials',
'@storybook/addon-controls',
'@storybook/addon-storysource',
{
name: '@storybook/addon-docs',
options: {
sourceLoaderOptions: {
parser: 'typescript',
injectStoryParameters: false,
},
},
},
],
typescript: {
check: true,
checkOptions: {},
Expand Down
3 changes: 2 additions & 1 deletion examples/react-ts/src/button.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import { Meta } from '@storybook/react/types-6-0';
import { Button } from './button';

export default { component: Button, title: 'Examples / Button' };
export default { component: Button, title: 'Examples / Button' } as Meta;

export const WithArgs = (args: any) => <Button {...args} />;
WithArgs.args = { label: 'With args' };
Expand Down
18 changes: 12 additions & 6 deletions lib/source-loader/src/abstract-syntax-tree/traverse-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,20 +208,26 @@ export function popParametersObjectFromDefaultExport(source, ast) {
enter: (node) => {
patchNode(node);

const isDefaultExport = node.type === 'ExportDefaultDeclaration';
const isObjectExpression = node.declaration?.type === 'ObjectExpression';
const isTsAsExpression = node.declaration?.type === 'TSAsExpression';

const targetNode = isObjectExpression ? node.declaration : node.declaration?.expression;

if (
node.type === 'ExportDefaultDeclaration' &&
node.declaration.type === 'ObjectExpression' &&
(node.declaration.properties || []).length
isDefaultExport &&
(isObjectExpression || isTsAsExpression) &&
(targetNode.properties || []).length
) {
const parametersProperty = node.declaration.properties.find(
const parametersProperty = targetNode.properties.find(
(p) => p.key.name === 'parameters' && p.value.type === 'ObjectExpression'
);

foundParametersProperty = !!parametersProperty;
if (foundParametersProperty) {
patchNode(parametersProperty.value);
} else {
patchNode(node.declaration);
patchNode(targetNode);
}

splicedSource = parametersProperty
Expand All @@ -235,7 +241,7 @@ export function popParametersObjectFromDefaultExport(source, ast) {

indexWhereToAppend = parametersProperty
? parametersProperty.value.start
: node.declaration.start + 1;
: targetNode.start + 1;
}
},
});
Expand Down

0 comments on commit 231886e

Please sign in to comment.