From 4691ac0f76a0e3fce09bf338ef699e063f5a3fa5 Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Wed, 25 Jan 2023 15:23:28 +0100 Subject: [PATCH] csf-tools: fix getNameFromPaths in string literal obj property --- code/lib/csf-tools/src/ConfigFile.test.ts | 9 ++++++++- code/lib/csf-tools/src/ConfigFile.ts | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/code/lib/csf-tools/src/ConfigFile.test.ts b/code/lib/csf-tools/src/ConfigFile.test.ts index 2c3fa24c5101..9369eb7b38cb 100644 --- a/code/lib/csf-tools/src/ConfigFile.test.ts +++ b/code/lib/csf-tools/src/ConfigFile.test.ts @@ -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`, () => { @@ -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']); }); }); diff --git a/code/lib/csf-tools/src/ConfigFile.ts b/code/lib/csf-tools/src/ConfigFile.ts index 3464bdfcabf1..d0ac7daf02be 100644 --- a/code/lib/csf-tools/src/ConfigFile.ts +++ b/code/lib/csf-tools/src/ConfigFile.ts @@ -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) && @@ -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; + } }); }