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 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
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
9 changes: 6 additions & 3 deletions code/lib/csf-tools/src/ConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ export class ConfigFile {
? _findVarInitialization(node.declaration.name, parent)
: node.declaration;

if (t.isObjectExpression(decl)) {
self._exportsObject = decl;
decl.properties.forEach((p: t.ObjectProperty) => {
if (t.isObjectExpression(decl) || t.isTSSatisfiesExpression(decl)) {
const expression = t.isTSSatisfiesExpression(decl)
? (decl.expression as Extract<t.Expression, t.ObjectExpression>)
Copy link
Member

Choose a reason for hiding this comment

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

I'm suspicious of this line. I think it should be something like:

          let decl =
            t.isIdentifier(node.declaration) && t.isProgram(parent)
              ? _findVarInitialization(node.declaration.name, parent)
              : node.declaration;
          if(t.isAsExpression(decl) || t.isTSSatisfiesExpression(decl)) {
            decl = decl.expression;
          }
          if(t.isObjectExpression(decl)) {
            ... 
          } else {
            logger.warn(`Unexpected ${JSON.stringify(node)}`);
          }

The problem with the current code is that it assumes the satisfies expression is an ObjectExpression, which might not be the case. For example:

export default 'foo' satisfies Bar;

: decl;
self._exportsObject = expression;
expression.properties.forEach((p: t.ObjectProperty) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

AST comparison:
Screenshot 2023-02-05 at 18 35 03
Screenshot 2023-02-05 at 18 36 25

const exportName = propKey(p);
if (exportName) {
let exportVal = p.value;
Expand Down