Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConfigFile: Fix export specifiers #25590

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -216,6 +216,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 @@ -435,6 +446,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 @@ -98,6 +98,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