Skip to content

Commit

Permalink
Warn about missing ESLint plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer committed Jan 23, 2024
1 parent dd12589 commit 5cfe2a3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/lib/options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ import { describe, expect, it, vi } from 'vitest';

import { Environment, Framework, Language, Plugin } from '../types/shared';

import * as logger from './logger';
import {
pickConfigOrDetect,
hasDependency,
detectLanguage,
detectEnvironments,
detectFrameworks,
detectOpenSource,
detectPlugins,
warnAboutMissingPlugins,
NODE_LIBRARIES,
BROWSER_LIBRARIES,
detectPlugins,
} from './options';

const basePackageJson = {
Expand All @@ -36,6 +38,8 @@ const basePackageJson = {
_id: 'id',
};

vi.mock('./logger.ts');

describe('options', () => {
describe('pickConfigOrDetect', () => {
it('should return the config value when defined', () => {
Expand Down Expand Up @@ -235,4 +239,21 @@ describe('options', () => {
expect(actual).toBe(false);
});
});

describe('warnAboutMissingPlugins', () => {
it('should log a warning if a framework is installed but not its corresponding ESLint plugin', () => {
const packageJson = {
...basePackageJson,
license: 'MIT',
dependencies: { next: '^1.0.0' },
};

warnAboutMissingPlugins(packageJson);

expect(logger.warn).toHaveBeenCalledOnce();
expect(logger.warn).toHaveBeenCalledWith(
'"next" is installed but not the corresponding ESLint plugin. Please install "eslint-config-next".',
);
});
});
});
56 changes: 56 additions & 0 deletions src/lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '../types/shared';

import { readPackageJson } from './files';
import * as logger from './logger';

// These lists are not exhaustive and should be expanded if necessary.
export const NODE_LIBRARIES = [
Expand All @@ -34,10 +35,51 @@ export const NODE_LIBRARIES = [
];
export const BROWSER_LIBRARIES = ['next', 'react', 'preact', 'svelte', 'vue'];

const FRAMEWORK_PLUGINS = [
{
packages: ['next'],
plugin: 'eslint-config-next',
},
{
packages: ['@emotion/react', '@emotion/styled'],
plugin: 'eslint-config-next',
},
{
packages: ['@emotion/react', '@emotion/styled'],
plugin: '@emotion/eslint-plugin',
},
{
packages: ['jest'],
plugin: 'eslint-plugin-jest',
},
{
packages: [
'@testing-library/dom',
'@testing-library/jest-dom',
'@testing-library/react',
],
plugin: 'eslint-plugin-testing-library',
},
{
packages: ['cypress'],
plugin: 'eslint-plugin-cypress',
},
{
packages: ['@playwright/test'],
plugin: 'eslint-plugin-playwright',
},
{
packages: ['storybook', '@storybook/react'],
plugin: 'eslint-plugin-storybook',
},
];

export function getOptions(): Required<Options> {
const packageJson = readPackageJson();
const config = (packageJson.foundry || {}) as Options;

warnAboutMissingPlugins(packageJson);

Check warning on line 82 in src/lib/options.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/options.ts#L81-L82

Added lines #L81 - L82 were not covered by tests
const pick = pickConfigOrDetect(packageJson);

return {
Expand Down Expand Up @@ -111,6 +153,20 @@ export function detectFrameworks(packageJson: PackageJson): Framework[] {
return frameworks;
}

export function warnAboutMissingPlugins(packageJson: PackageJson): void {
FRAMEWORK_PLUGINS.forEach(({ packages, plugin }) => {
const installedPackage = packages.find((pkg) =>
hasDependency(packageJson, pkg),
);

if (installedPackage && !hasDependency(packageJson, plugin)) {
logger.warn(
`"${installedPackage}" is installed but not the corresponding ESLint plugin. Please install "${plugin}".`,
);
}
});
}

export function detectPlugins(packageJson: PackageJson): Plugin[] {
const plugins: Plugin[] = [];

Expand Down

0 comments on commit 5cfe2a3

Please sign in to comment.