Skip to content

Commit

Permalink
Merge pull request #29000 from storybookjs/shilan/fix-configfile-as-c…
Browse files Browse the repository at this point in the history
…onst-satisfies

ConfigFile: Fix `as const satisfies` modifiers
  • Loading branch information
shilman authored Aug 29, 2024
2 parents d8b6ce8 + 980f463 commit 77c37c8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
20 changes: 20 additions & 0 deletions code/core/src/csf-tools/ConfigFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,26 @@ describe('ConfigFile', () => {
export default preview;
`);
});

it('root globals as const satisfies as variable', () => {
expect(
removeField(
['globals'],
dedent`
const preview = {
globals: { a: 1 },
bar: { a: 1 }
} as const satisfies Foo;
export default preview;
`
)
).toMatchInlineSnapshot(`
const preview = {
bar: { a: 1 }
} as const satisfies Foo;
export default preview;
`);
});
});

describe('quotes', () => {
Expand Down
20 changes: 11 additions & 9 deletions code/core/src/csf-tools/ConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ const propKey = (p: t.ObjectProperty) => {
return null;
};

const unwrap = (node: t.Node | undefined | null): any => {
if (t.isTSAsExpression(node) || t.isTSSatisfiesExpression(node)) {
return unwrap(node.expression);
}
return node;
};

// eslint-disable-next-line @typescript-eslint/naming-convention
const _getPath = (path: string[], node: t.Node): t.Node | undefined => {
if (path.length === 0) {
Expand Down Expand Up @@ -191,9 +198,7 @@ export class ConfigFile {
? _findVarInitialization(node.declaration.name, parent)
: node.declaration;

if (t.isTSAsExpression(decl) || t.isTSSatisfiesExpression(decl)) {
decl = decl.expression;
}
decl = unwrap(decl);

if (t.isObjectExpression(decl)) {
self._exportsObject = decl;
Expand Down Expand Up @@ -275,9 +280,7 @@ export class ConfigFile {
exportObject = _findVarInitialization(right.name, parent as t.Program) as any;
}

if (t.isTSAsExpression(exportObject) || t.isTSSatisfiesExpression(exportObject)) {
exportObject = exportObject.expression;
}
exportObject = unwrap(exportObject);

if (t.isObjectExpression(exportObject)) {
self._exportsObject = exportObject;
Expand Down Expand Up @@ -517,9 +520,8 @@ export class ConfigFile {
if (t.isIdentifier(decl)) {
decl = _findVarInitialization(decl.name, this._ast.program);
}
if (t.isTSAsExpression(decl) || t.isTSSatisfiesExpression(decl)) {
decl = decl.expression;
}

decl = unwrap(decl);
if (t.isObjectExpression(decl)) {
const properties = decl.properties as t.ObjectProperty[];
removeProperty(properties, path[0]);
Expand Down

0 comments on commit 77c37c8

Please sign in to comment.