Skip to content

Commit

Permalink
feat(eslint): add more rules like from antfu
Browse files Browse the repository at this point in the history
  • Loading branch information
mheob committed Jan 21, 2025
1 parent c501dc4 commit becb099
Show file tree
Hide file tree
Showing 11 changed files with 1,338 additions and 1,220 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-books-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mheob/eslint-config': minor
---

turn off `prefer-const` rule in editor
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"cz-git": "^1.11.0",
"eslint": "catalog:",
"husky": "^9.1.7",
"lint-staged": "^15.4.0",
"lint-staged": "^15.4.1",
"prettier": "^3.4.2",
"ts-node": "^10.9.2",
"turbo": "^2.3.3",
Expand Down
30 changes: 15 additions & 15 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@
"watch": "tsup --watch"
},
"dependencies": {
"@antfu/install-pkg": "^0.5.0",
"@antfu/install-pkg": "^1.0.0",
"@clack/prompts": "^0.9.1",
"@typescript-eslint/eslint-plugin": "^8.20.0",
"@typescript-eslint/parser": "^8.20.0",
"@typescript-eslint/eslint-plugin": "^8.21.0",
"@typescript-eslint/parser": "^8.21.0",
"@vitest/eslint-plugin": "^1.1.25",
"eslint-config-flat-gitignore": "^0.3.0",
"eslint-config-next": "^15.1.4",
"eslint-config-prettier": "^9.1.0",
"eslint-flat-config-utils": "^0.4.0",
"eslint-merge-processors": "^0.1.0",
"eslint-config-flat-gitignore": "^1.0.0",
"eslint-config-next": "^15.1.5",
"eslint-config-prettier": "^10.0.1",
"eslint-flat-config-utils": "^1.0.0",
"eslint-merge-processors": "^1.0.0",
"eslint-plugin-antfu": "^2.7.0",
"eslint-plugin-command": "^0.2.7",
"eslint-plugin-command": "^2.1.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-import-x": "^4.6.1",
"eslint-plugin-jsdoc": "^50.6.1",
"eslint-plugin-jsdoc": "^50.6.2",
"eslint-plugin-jsonc": "^2.18.2",
"eslint-plugin-markdown": "^5.1.0",
"eslint-plugin-n": "^17.15.1",
"eslint-plugin-no-only-tests": "^3.3.0",
"eslint-plugin-perfectionist": "^4.6.0",
"eslint-plugin-prettier": "^5.2.2",
"eslint-plugin-perfectionist": "^4.7.0",
"eslint-plugin-prettier": "^5.2.3",
"eslint-plugin-toml": "^0.12.0",
"eslint-plugin-unicorn": "^56.0.1",
"eslint-plugin-unused-imports": "^4.1.4",
Expand All @@ -66,15 +66,15 @@
"mlly": "^1.7.4",
"parse-gitignore": "^2.0.0",
"picocolors": "^1.1.1",
"svelte": "^5.18.0",
"svelte": "^5.19.1",
"svelte-eslint-parser": "^0.43.0",
"toml-eslint-parser": "^0.10.0",
"yaml-eslint-parser": "^1.2.3",
"yargs": "^17.7.2"
},
"devDependencies": {
"@eslint-react/eslint-plugin": "^1.23.2",
"@eslint/config-inspector": "^0.7.1",
"@eslint-react/eslint-plugin": "^1.24.0",
"@eslint/config-inspector": "^1.0.0",
"@mheob/tsconfig": "workspace:*",
"@types/yargs": "^17.0.33",
"astro-eslint-parser": "^1.1.0",
Expand Down
12 changes: 12 additions & 0 deletions packages/eslint-config/src/configs/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,28 @@ export async function astro(
{
files,
languageOptions: {
globals: pluginAstro.environments.astro.globals,
parser: parserAstro,
parserOptions: {
extraFileExtensions: ['.astro'],
parser: parserTs,
},
sourceType: 'module',
},
name: 'mheob/astro/rules',
processor: 'astro/client-side-ts',
rules: {
// use recommended rules
'astro/missing-client-only-directive-value': 'error',
'astro/no-conflict-set-directives': 'error',
'astro/no-deprecated-astro-canonicalurl': 'error',
'astro/no-deprecated-astro-fetchcontent': 'error',
'astro/no-deprecated-astro-resolve': 'error',
'astro/no-deprecated-getentrybyslug': 'error',
'astro/no-set-html-directive': 'off',
'astro/no-unused-define-vars-in-style': 'error',
'astro/semi': 'off',
'astro/valid-compile': 'error',

...overrides,
},
Expand Down
58 changes: 58 additions & 0 deletions packages/eslint-config/src/configs/disables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { GLOB_SRC, GLOB_SRC_EXT } from '../globs';
import type { TypedFlatConfigItem } from '../types';

export async function disables(): Promise<TypedFlatConfigItem[]> {
return [
{
files: [`**/scripts/${GLOB_SRC}`],
name: 'antfu/disables/scripts',
rules: {
'antfu/no-top-level-await': 'off',
'no-console': 'off',
'ts/explicit-function-return-type': 'off',
},
},
{
files: [`**/cli/${GLOB_SRC}`, `**/cli.${GLOB_SRC_EXT}`],
name: 'antfu/disables/cli',
rules: {
'antfu/no-top-level-await': 'off',
'no-console': 'off',
},
},
{
files: ['**/bin/**/*', `**/bin.${GLOB_SRC_EXT}`],
name: 'antfu/disables/bin',
rules: {
'antfu/no-import-dist': 'off',
'antfu/no-import-node-modules-by-path': 'off',
},
},
{
files: ['**/*.d.?([cm])ts'],
name: 'antfu/disables/dts',
rules: {
'eslint-comments/no-unlimited-disable': 'off',
'import/no-duplicates': 'off',
'no-restricted-syntax': 'off',
'unused-imports/no-unused-vars': 'off',
},
},
{
files: ['**/*.js', '**/*.cjs'],
name: 'antfu/disables/cjs',
rules: {
'ts/no-require-imports': 'off',
},
},
{
files: [`**/*.config.${GLOB_SRC_EXT}`, `**/*.config.*.${GLOB_SRC_EXT}`],
name: 'antfu/disables/config-files',
rules: {
'antfu/no-top-level-await': 'off',
'no-console': 'off',
'ts/explicit-function-return-type': 'off',
},
},
];
}
4 changes: 2 additions & 2 deletions packages/eslint-config/src/configs/ignores.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { GLOB_EXCLUDE } from '../globs';
import type { TypedFlatConfigItem } from '../types';

export async function ignores(): Promise<TypedFlatConfigItem[]> {
export async function ignores(userIgnores: string[] = []): Promise<TypedFlatConfigItem[]> {
return [
{
ignores: GLOB_EXCLUDE,
ignores: [...GLOB_EXCLUDE, ...userIgnores],
name: 'mheob/ignores',
},
];
Expand Down
7 changes: 7 additions & 0 deletions packages/eslint-config/src/configs/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export async function react(
'react-hooks-extra': plugins['@eslint-react/hooks-extra'],
'react-naming-convention': plugins['@eslint-react/naming-convention'],
'react-refresh': pluginReactRefresh,
'react-web-api': plugins['@eslint-react/web-api'],
},
},
{
Expand Down Expand Up @@ -103,6 +104,12 @@ export async function react(
},
],

// recommended rules from @eslint-react/web-api
'react-web-api/no-leaked-event-listener': 'warn',
'react-web-api/no-leaked-interval': 'warn',
'react-web-api/no-leaked-resize-observer': 'warn',
'react-web-api/no-leaked-timeout': 'warn',

// recommended rules from @eslint-react
'react/ensure-forward-ref-using-ref': 'warn',
'react/no-access-state-in-setstate': 'error',
Expand Down
11 changes: 8 additions & 3 deletions packages/eslint-config/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
unicorn,
yaml,
} from './configs';
import { disables } from './configs/disables';
import type { RuleOptions } from './typegen';
import type { Awaitable, ConfigNames, OptionsConfig, TypedFlatConfigItem } from './types';
import { existsPackage, interopDefault } from './utils/package';

Expand Down Expand Up @@ -64,7 +66,10 @@ export function resolveSubOptions<K extends keyof OptionsConfig>(
return typeof options[key] === 'boolean' ? ({} as any) : options[key] || {};
}

export function getOverrides<K extends keyof OptionsConfig>(options: OptionsConfig, key: K) {
export function getOverrides<K extends keyof OptionsConfig>(
options: OptionsConfig,
key: K,
): Partial<Linter.RulesRecord & RuleOptions> {
const sub = resolveSubOptions(options, key);
return {
...(options.overrides as any)?.[key],
Expand Down Expand Up @@ -98,7 +103,7 @@ function iniConfig(options: OptionsConfig & TypedFlatConfigItem = {}) {

// Base configs
configs.push(
ignores(),
ignores(options.ignores),
javascript({ isInEditor, overrides: getOverrides(options, 'javascript') }),
comments(),
node(),
Expand Down Expand Up @@ -215,7 +220,7 @@ export function mheob(
);
}

configs.push(prettier({ overrides: getOverrides(options, 'prettier') }));
configs.push(disables(), prettier({ overrides: getOverrides(options, 'prettier') }));

// User can optionally pass a flat config item to the first argument
// We pick the known keys as ESLint would do schema validation
Expand Down
Loading

0 comments on commit becb099

Please sign in to comment.