Skip to content

Commit

Permalink
Merge pull request #25590 from storybookjs/shilman/fix-configfile-exp…
Browse files Browse the repository at this point in the history
…ort-specifier

ConfigFile: Fix export specifiers
(cherry picked from commit cb97367)
  • Loading branch information
shilman authored and storybook-bot committed Jan 16, 2024
1 parent 678515f commit e5dc58b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
29 changes: 29 additions & 0 deletions code/lib/csf-tools/src/ConfigFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,17 @@ describe('ConfigFile', () => {
)
).toEqual([{ directory: '../src', titlePrefix: 'Demo' }]);
});
it('export specfier', () => {
expect(
getField(
['foo'],
dedent`
const foo = 'bar';
export { foo };
`
)
).toEqual('bar');
});
});
});

Expand Down Expand Up @@ -436,6 +447,24 @@ describe('ConfigFile', () => {
`);
});
});

describe('export specifiers', () => {
it('found object', () => {
expect(
setField(
['core', 'builder'],
'webpack5',
dedent`
const core = { builder: 'webpack4' };
export { core };
`
)
).toMatchInlineSnapshot(`
const core = { builder: 'webpack5' };
export { core };
`);
});
});
});

describe('appendToArray', () => {
Expand Down
30 changes: 24 additions & 6 deletions code/lib/csf-tools/src/ConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ const _getPathProperties = (path: string[], node: t.Node): t.ObjectProperty[] |
}
return undefined;
};

// eslint-disable-next-line @typescript-eslint/naming-convention
const _findVarInitialization = (identifier: string, program: t.Program) => {
let init: t.Expression | null | undefined = null;
const _findVarDeclarator = (
identifier: string,
program: t.Program
): t.VariableDeclarator | null | undefined => {
let declarator: t.VariableDeclarator | null | undefined = null;
let declarations: t.VariableDeclarator[] | null = null;
program.body.find((node: t.Node) => {
if (t.isVariableDeclaration(node)) {
Expand All @@ -92,20 +94,26 @@ const _findVarInitialization = (identifier: string, program: t.Program) => {

return (
declarations &&
declarations.find((decl: t.Node) => {
declarations.find((decl: t.VariableDeclarator) => {
if (
t.isVariableDeclarator(decl) &&
t.isIdentifier(decl.id) &&
decl.id.name === identifier
) {
init = decl.init;
declarator = decl;
return true; // stop looking
}
return false;
})
);
});
return init;
return declarator;
};

// eslint-disable-next-line @typescript-eslint/naming-convention
const _findVarInitialization = (identifier: string, program: t.Program) => {
const declarator = _findVarDeclarator(identifier, program);
return declarator?.init;
};

// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -213,6 +221,16 @@ export class ConfigFile {
self._exportDecls[exportName] = decl;
}
});
} else if (node.specifiers) {
// export { X };
node.specifiers.forEach((spec) => {
if (t.isExportSpecifier(spec) && t.isIdentifier(spec.exported)) {
const { name: exportName } = spec.exported;
const decl = _findVarDeclarator(exportName, parent as t.Program) as any;
self._exports[exportName] = decl.init;
self._exportDecls[exportName] = decl;
}
});
} else {
logger.warn(
getCsfParsingErrorMessage({
Expand Down
3 changes: 3 additions & 0 deletions code/lib/csf-tools/src/getStorySortParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ const parseDefault = (defaultExpr: t.Expression, program: t.Program): t.Expressi
};

export const getStorySortParameter = (previewCode: string) => {
// don't even try to process the file
if (!previewCode.includes('storySort')) return undefined;

let storySort: t.Expression | undefined;
const ast = babelParse(previewCode);
traverse.default(ast, {
Expand Down

0 comments on commit e5dc58b

Please sign in to comment.