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

CLI: Fix eslint configuration for string extends #27097

Merged
merged 1 commit into from
May 12, 2024
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
21 changes: 21 additions & 0 deletions code/lib/cli/src/automigrate/helpers/eslintPlugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, it, expect } from 'vitest';
import { normalizeExtends } from './eslintPlugin';

describe('normalizeExtends', () => {
it('returns empty array when existingExtends is falsy', () => {
expect(normalizeExtends(null)).toEqual([]);
expect(normalizeExtends(undefined)).toEqual([]);
});

it('returns existingExtends when it is a string', () => {
expect(normalizeExtends('foo')).toEqual(['foo']);
});

it('returns existingExtends when it is an array', () => {
expect(normalizeExtends(['foo'])).toEqual(['foo']);
});

it('throws when existingExtends is not a string or array', () => {
expect(() => normalizeExtends(true)).toThrowError('Invalid eslint extends true');
});
});
30 changes: 14 additions & 16 deletions code/lib/cli/src/automigrate/helpers/eslintPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ export async function extractEslintInfo(packageManager: JsPackageManager): Promi
return { hasEslint, isStorybookPluginInstalled, eslintConfigFile };
}

export const normalizeExtends = (existingExtends: any): string[] => {
if (!existingExtends) return [];
if (typeof existingExtends === 'string') return [existingExtends];
if (Array.isArray(existingExtends)) return existingExtends;
throw new Error(`Invalid eslint extends ${existingExtends}`);
};

export async function configureEslintPlugin(
eslintFile: string | undefined,
packageManager: JsPackageManager
Expand All @@ -55,38 +62,29 @@ export async function configureEslintPlugin(
paddedLog(`Configuring Storybook ESLint plugin at ${eslintFile}`);
if (eslintFile.endsWith('json')) {
const eslintConfig = (await readJson(eslintFile)) as { extends?: string[] };
const existingConfigValue = Array.isArray(eslintConfig.extends)
? eslintConfig.extends
: [eslintConfig.extends].filter(Boolean);
eslintConfig.extends = [
...(existingConfigValue || []),
'plugin:storybook/recommended',
] as string[];
const existingExtends = normalizeExtends(eslintConfig.extends).filter(Boolean);
eslintConfig.extends = [...existingExtends, 'plugin:storybook/recommended'] as string[];

const eslintFileContents = await readFile(eslintFile, 'utf8');
const spaces = detectIndent(eslintFileContents).amount || 2;
await writeJson(eslintFile, eslintConfig, { spaces });
} else {
const eslint = await readConfig(eslintFile);
const extendsConfig = eslint.getFieldValue(['extends']) || [];
const existingConfigValue = Array.isArray(extendsConfig)
? extendsConfig
: [extendsConfig].filter(Boolean);
eslint.setFieldValue(
['extends'],
[...(existingConfigValue || []), 'plugin:storybook/recommended']
);
const existingExtends = normalizeExtends(eslint.getFieldValue(['extends'])).filter(Boolean);
eslint.setFieldValue(['extends'], [...existingExtends, 'plugin:storybook/recommended']);

await writeConfig(eslint);
}
} else {
paddedLog(`Configuring eslint-plugin-storybook in your package.json`);
const packageJson = await packageManager.retrievePackageJson();
const existingExtends = normalizeExtends(packageJson.eslintConfig?.extends).filter(Boolean);

await packageManager.writePackageJson({
...packageJson,
eslintConfig: {
...packageJson.eslintConfig,
extends: [...(packageJson.eslintConfig?.extends || []), 'plugin:storybook/recommended'],
extends: [...existingExtends, 'plugin:storybook/recommended'],
},
});
}
Expand Down