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 as const satisfies modifiers #29000

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 satsifies as variable', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

expect(
removeField(
['globals'],
dedent`
const preview = {
globals: { a: 1 },
bar: { a: 1 }
} as const satisfies Foo;
export default preview;
Comment on lines +804 to +808
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Typo in test name: 'satsifies' should be 'satisfies'

`
)
).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
Loading