This commit is contained in:
Alex
2024-08-02 15:51:10 -04:00
parent 3ac5f91988
commit ba20685f2e
13 changed files with 825 additions and 969 deletions

View File

@ -6,10 +6,10 @@
"subject": "[meta] add `repository.directory` field"
},
"eslint-import-resolver-typescript": {
"hash": "c9b5626ee69bd529c7e391e40928a4fb28dce179",
"date": "2024-07-23T20:40:14+08:00",
"hash": "3dfad602a05b4b3812a4d3fc681051932f86e838",
"date": "2024-08-01T01:15:59+00:00",
"committer": "GitHub",
"subject": "chore: release eslint-import-resolver-typescript (#302)"
"subject": "chore(deps): update dependency node to v18.20.4 (#309)"
},
"eslint-plugin-jsx-a11y": {
"hash": "cca288b73a39fa0932a57c02a7a88de68fc971fc",

View File

@ -4,8 +4,45 @@ import { Middleware, storybook } from './index';
import { react, reactRefresh } from './presets/react';
import { tailwind } from './presets/tailwind';
import { reactQuery } from './presets/misc';
import { testingLibrary } from './presets/testing-library';
export function* checkEnv(): Generator<Middleware> {
export const envs: {
dependency: string;
eslintPlugin?: string;
middleware: Middleware;
}[] = [
{
dependency: 'react',
middleware: react,
},
{
dependency: '@vitejs/plugin-react',
eslintPlugin: 'eslint-plugin-react-refresh',
middleware: reactRefresh,
},
{
dependency: 'tailwindcss',
eslintPlugin: 'eslint-plugin-tailwindcss',
middleware: tailwind,
},
{
dependency: 'storybook',
eslintPlugin: 'eslint-plugin-storybook',
middleware: storybook,
},
{
dependency: '@tanstack/react-query',
eslintPlugin: '@tanstack/eslint-plugin-query',
middleware: reactQuery,
},
{
dependency: '@testing-library/react',
eslintPlugin: 'eslint-plugin-testing-library',
middleware: testingLibrary,
},
];
export function getProjectDependencies() {
const rootDir = process.cwd();
const pkgJsonPath = resolve(rootDir, 'package.json');
@ -13,27 +50,21 @@ export function* checkEnv(): Generator<Middleware> {
? JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8'))
: {};
const deps = new Set(
return new Set(
Object.keys({
...pkgJson.dependencies,
...pkgJson.devDependencies,
...pkgJson.peerDependencies,
}),
);
}
if (deps.has('react')) {
yield react;
}
if (deps.has('@vitejs/plugin-react') && deps.has('eslint-plugin-react-refresh')) {
yield reactRefresh;
}
if (deps.has('tailwindcss') && deps.has('eslint-plugin-tailwindcss')) {
yield tailwind;
}
if (deps.has('storybook') && deps.has('eslint-plugin-storybook')) {
yield storybook;
}
if (deps.has('@tanstack/react-query') && deps.has('@tanstack/eslint-plugin-query')) {
yield reactQuery;
export function* checkEnv(): Generator<Middleware> {
const deps = getProjectDependencies();
for (const { dependency, eslintPlugin, middleware } of envs) {
if (deps.has(dependency) && (!eslintPlugin || deps.has(eslintPlugin))) {
yield middleware;
}
}
}

20
src/install.ts Normal file
View File

@ -0,0 +1,20 @@
import { installPackage } from '@antfu/install-pkg';
import { uniq } from 'lodash';
import { getProjectDependencies, envs } from './env';
const deps = getProjectDependencies();
const packages = uniq(
envs
.filter(_ => deps.has(_.dependency) && _.eslintPlugin && !deps.has(_.eslintPlugin))
.map(_ => _.eslintPlugin!),
);
console.log('Installing missing ESLint plugins:');
for (const pkg of packages) {
console.log(`- ${pkg}`);
}
console.log();
void installPackage(packages, {
silent: false,
});

View File

@ -0,0 +1,12 @@
import type { Middleware } from '../index';
import { TestingLibraryRulesObject } from '@aet/eslint-define-config/src/rules/testing-library';
const testingLibraryRules: Partial<TestingLibraryRulesObject> = {};
export const testingLibrary: Middleware = (config, { addRules }) => {
config.overrides.push({
files: ['**/*.(spec|test).{ts,tsx}'],
plugins: ['plugin:testing-library/react'],
});
addRules(testingLibraryRules);
};

View File

@ -19,7 +19,6 @@ const typescriptRules: Partial<TypeScriptRulesObject> = {
'ts-nocheck': 'allow-with-description',
},
],
'@typescript-eslint/ban-types': [error, { extendDefaults: true }],
'@typescript-eslint/consistent-type-imports': [
error,
{ disallowTypeAnnotations: false, fixStyle: 'inline-type-imports' },