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

Csf-tools: Add satisfies support to ConfigFile #20936

Merged
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
85 changes: 85 additions & 0 deletions code/lib/csf-tools/src/ConfigFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,48 @@ describe('ConfigFile', () => {
expect(config.getNameFromPath(['framework'])).toEqual('foo');
});

describe('satisfies', () => {
it(`supports string literal node`, () => {
const source = dedent`
import type { StorybookConfig } from '@storybook/react-webpack5';

const config = {
framework: 'foo',
} satisfies StorybookConfig
export default config;
`;
const config = loadConfig(source).parse();
expect(config.getNameFromPath(['framework'])).toEqual('foo');
});

it(`supports string literal node without variables`, () => {
const source = dedent`
import type { StorybookConfig } from '@storybook/react-webpack5';

export default {
framework: 'foo',
} satisfies StorybookConfig;
`;
const config = loadConfig(source).parse();
expect(config.getNameFromPath(['framework'])).toEqual('foo');
});

it(`supports object expression node with name property`, () => {
const source = dedent`
import type { StorybookConfig } from '@storybook/react-webpack5';

const config = {
framework: { name: 'foo', options: { bar: require('baz') } },
"otherField": { "name": 'foo', options: { bar: require('baz') } },
} satisfies StorybookConfig
export default config;
`;
const config = loadConfig(source).parse();
expect(config.getNameFromPath(['framework'])).toEqual('foo');
expect(config.getNameFromPath(['otherField'])).toEqual('foo');
});
});

it(`supports object expression node with name property`, () => {
const source = dedent`
import type { StorybookConfig } from '@storybook/react-webpack5';
Expand Down Expand Up @@ -826,6 +868,49 @@ describe('ConfigFile', () => {
expect(config.getNamesFromPath(['addons'])).toEqual(['foo', 'bar']);
expect(config.getNamesFromPath(['otherField'])).toEqual(['foo', 'bar']);
});

describe('satisfies', () => {
it(`supports an array with string literal and object expression with name property`, () => {
const source = dedent`
import type { StorybookConfig } from '@storybook/react-webpack5';

const config = {
addons: [
'foo',
{ name: 'bar', options: {} },
],
"otherField": [
"foo",
{ "name": 'bar', options: {} },
],
} satisfies StorybookConfig
export default config;
`;
const config = loadConfig(source).parse();
expect(config.getNamesFromPath(['addons'])).toEqual(['foo', 'bar']);
expect(config.getNamesFromPath(['otherField'])).toEqual(['foo', 'bar']);
});

it(`supports an array with string literal and object expression with name property without variable`, () => {
const source = dedent`
import type { StorybookConfig } from '@storybook/react-webpack5';

export default {
addons: [
'foo',
{ name: 'bar', options: {} },
],
"otherField": [
"foo",
{ "name": 'bar', options: {} },
],
} satisfies StorybookConfig;
`;
const config = loadConfig(source).parse();
expect(config.getNamesFromPath(['addons'])).toEqual(['foo', 'bar']);
expect(config.getNamesFromPath(['otherField'])).toEqual(['foo', 'bar']);
});
});
});

it(`returns undefined when accessing a field that does not exist`, () => {
Expand Down
5 changes: 4 additions & 1 deletion code/lib/csf-tools/src/ConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,13 @@ export class ConfigFile {
ExportDefaultDeclaration: {
enter({ node, parent }) {
self.hasDefaultExport = true;
const decl =
let decl =
t.isIdentifier(node.declaration) && t.isProgram(parent)
? _findVarInitialization(node.declaration.name, parent)
: node.declaration;
if (t.isTSAsExpression(decl) || t.isTSSatisfiesExpression(decl)) {
decl = decl.expression;
}

if (t.isObjectExpression(decl)) {
self._exportsObject = decl;
Expand Down