Skip to content

Commit

Permalink
ConfigFile: Fix export specifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed Jan 13, 2024
1 parent ee06c80 commit 4e2aa4c
Show file tree
Hide file tree
Showing 2 changed files with 53 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 @@ -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

0 comments on commit 4e2aa4c

Please sign in to comment.