Skip to content

Commit

Permalink
csf-tools: fix getNameFromPaths in string literal obj property
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Jan 25, 2023
1 parent bdf56cd commit 4691ac0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion code/lib/csf-tools/src/ConfigFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,11 +769,13 @@ describe('ConfigFile', () => {
const config: StorybookConfig = {
framework: { name: 'foo', options: { bar: require('baz') } },
"otherField": { "name": 'foo', options: { bar: require('baz') } },
}
export default config;
`;
const config = loadConfig(source).parse();
expect(config.getNameFromPath(['framework'])).toEqual('foo');
expect(config.getNameFromPath(['otherField'])).toEqual('foo');
});

it(`returns undefined when accessing a field that does not exist`, () => {
Expand Down Expand Up @@ -812,12 +814,17 @@ describe('ConfigFile', () => {
addons: [
'foo',
{ name: 'bar', options: {} },
]
],
"otherField": [
"foo",
{ "name": 'bar', options: {} },
],
}
export default config;
`;
const config = loadConfig(source).parse();
expect(config.getNamesFromPath(['addons'])).toEqual(['foo', 'bar']);
expect(config.getNamesFromPath(['otherField'])).toEqual(['foo', 'bar']);
});
});

Expand Down
11 changes: 11 additions & 0 deletions code/lib/csf-tools/src/ConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ export class ConfigFile {
value = node.value;
} else if (t.isObjectExpression(node)) {
node.properties.forEach((prop) => {
// { framework: { name: 'value' } }
if (
t.isObjectProperty(prop) &&
t.isIdentifier(prop.key) &&
Expand All @@ -324,6 +325,16 @@ export class ConfigFile {
value = prop.value.value;
}
}

// { "framework": { "name": "value" } }
if (
t.isObjectProperty(prop) &&
t.isStringLiteral(prop.key) &&
prop.key.value === 'name' &&
t.isStringLiteral(prop.value)
) {
value = prop.value.value;
}
});
}

Expand Down

0 comments on commit 4691ac0

Please sign in to comment.