2024-10-22 09:49:56 +08:00
import {
GLOB _JSX , GLOB _TESTS , GLOB _TSX , combine , javascript , node ,
stylistic , typescript , unicorn
} from '@antfu/eslint-config'
2024-10-21 13:16:56 +08:00
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import js from '@eslint/js'
import { FlatCompat } from '@eslint/eslintrc'
2024-10-21 17:03:04 +08:00
import globals from 'globals'
2024-10-22 13:49:39 +08:00
import reactRefresh from "eslint-plugin-react-refresh"
2024-10-21 13:16:56 +08:00
const _ _filename = fileURLToPath ( import . meta . url )
const _ _dirname = path . dirname ( _ _filename )
const compat = new FlatCompat ( {
baseDirectory : _ _dirname ,
recommendedConfig : js . configs . recommended ,
allConfig : js . configs . all ,
} )
2024-10-21 16:21:12 +08:00
// storybook plugin not support v9, so add its recommended rules here
const storybook = [
{
plugins : [ 'storybook' ] ,
files : [ '*.stories.@(ts|tsx|js|jsx|mjs|cjs)' , '*.story.@(ts|tsx|js|jsx|mjs|cjs)' ] ,
rules : {
'react-hooks/rules-of-hooks' : 'off' ,
'import/no-anonymous-default-export' : 'off' ,
'storybook/await-interactions' : 'error' ,
'storybook/context-in-play-function' : 'error' ,
'storybook/default-exports' : 'error' ,
'storybook/hierarchy-separator' : 'warn' ,
'storybook/no-redundant-story-name' : 'warn' ,
'storybook/prefer-pascal-case' : 'warn' ,
'storybook/story-exports' : 'error' ,
'storybook/use-storybook-expect' : 'error' ,
'storybook/use-storybook-testing-library' : 'error' ,
}
} ,
{
plugins : [ 'storybook' ] ,
files : [ '*.stories.@(ts|tsx|js|jsx|mjs|cjs)' , '*.story.@(ts|tsx|js|jsx|mjs|cjs)' ] ,
rules : {
'storybook/no-uninstalled-addons' : 'error' ,
}
}
2024-10-21 13:16:56 +08:00
]
2024-10-21 16:21:12 +08:00
export default combine (
stylistic ( {
lessOpinionated : true ,
// original @antfu/eslint-config does not support jsx
jsx : false ,
overrides : {
2024-10-22 09:49:56 +08:00
// original config
2024-10-21 16:21:12 +08:00
"style/indent" : "off" ,
2024-10-21 13:16:56 +08:00
2024-10-21 16:21:12 +08:00
// these options does not exist in old version
2024-10-22 09:49:56 +08:00
// maybe useless
2024-10-21 16:21:12 +08:00
"style/indent-binary-ops" : "off" ,
"style/multiline-ternary" : "off" ,
2024-10-21 13:16:56 +08:00
2024-10-22 09:49:56 +08:00
// not exist in old version, and big change
2024-10-21 16:21:12 +08:00
"style/quote-props" : "off" ,
"style/member-delimiter-style" : "off" ,
"style/quotes" : "off" ,
"style/comma-dangle" : "off" ,
}
} ) ,
typescript ( {
overrides : {
// useful, but big change
"ts/no-empty-object-type" : "off" ,
}
} ) ,
2024-10-21 17:03:04 +08:00
javascript ( {
overrides : {
2024-10-22 09:49:56 +08:00
// handled by unused-imports/no-unused-vars
2024-10-21 17:03:04 +08:00
'no-unused-vars' : 'off' ,
2024-10-22 09:49:56 +08:00
// useless
'no-use-before-define' : 'warn'
2024-10-21 17:03:04 +08:00
}
} ) ,
2024-10-22 09:49:56 +08:00
unicorn ( ) ,
node ( ) ,
2024-10-22 11:36:42 +08:00
... process . env . ESLINT _CONFIG _INSPECTOR
? [ ]
// TODO: remove this when upgrade to nextjs 15
: [ compat . extends ( 'next' ) ] ,
2024-10-21 16:21:12 +08:00
{
ignores : [
'**/node_modules/*' ,
'**/node_modules/' ,
'**/dist/' ,
'**/build/' ,
'**/out/' ,
'**/.next/' ,
'**/public/*' ,
'**/*.json' ,
]
2024-10-21 13:16:56 +08:00
} ,
2024-10-21 16:21:12 +08:00
{
// orignal config
rules : {
2024-10-22 09:49:56 +08:00
// from old version of antfu/eslint-config
2024-10-22 11:43:23 +08:00
// typescript will handle this, see https://typescript-eslint.io/troubleshooting/faqs/eslint/#i-get-errors-from-the-no-undef-rule-about-global-variables-not-being-defined-even-though-there-are-no-typescript-errors
"no-undef" : "off" ,
2024-10-22 09:49:56 +08:00
2024-10-21 16:21:12 +08:00
'ts/consistent-type-definitions' : [ 'error' , 'type' ] ,
2024-10-22 09:49:56 +08:00
// orignal ts/no-var-requires
2024-10-21 16:21:12 +08:00
'ts/no-require-imports' : 'off' ,
"no-console" : 'off' ,
"react-hooks/exhaustive-deps" : "warn" ,
"react/display-name" : "off" ,
2024-10-22 09:49:56 +08:00
// orignal config, but removed in new version antfu/eslint-config
// big change
2024-10-21 16:21:12 +08:00
"curly" : "off" ,
2024-10-22 09:49:56 +08:00
// antfu use eslint-plugin-perfectionist to replace this
// will cause big change, so keep the original
// sort-imports
"sort-imports" : [
'error' ,
{
ignoreCase : false ,
ignoreDeclarationSort : true ,
ignoreMemberSort : false ,
memberSyntaxSortOrder : [ 'none' , 'all' , 'multiple' , 'single' ] ,
allowSeparatedGroups : false ,
} ,
] ,
// antfu migrate to eslint-plugin-unused-imports
2024-10-21 17:03:04 +08:00
"unused-imports/no-unused-vars" : "warn" ,
"unused-imports/no-unused-imports" : "warn" ,
2024-10-22 11:36:42 +08:00
} ,
2024-10-22 09:49:56 +08:00
2024-10-22 11:36:42 +08:00
languageOptions : {
globals : {
... globals . browser ,
... globals . es2025 ,
... globals . node ,
'React' : 'readable' ,
'JSX' : 'readable' ,
}
2024-10-21 16:21:12 +08:00
}
} ,
storybook ,
// need futher research
{
rules : {
// not exist in old version
2024-10-21 17:03:04 +08:00
"antfu/consistent-list-newline" : "off" ,
2024-10-22 09:49:56 +08:00
'node/prefer-global/process' : 'off' ,
'node/prefer-global/buffer' : 'off' ,
'node/no-callback-literal' : 'off' ,
2024-10-21 17:03:04 +08:00
// useful, but big change
2024-10-22 09:49:56 +08:00
"unicorn/prefer-number-properties" : "warn" ,
"unicorn/no-new-array" : "warn"
2024-10-21 17:03:04 +08:00
}
} ,
// suppress error for `no-undef` rule
{
files : GLOB _TESTS ,
languageOptions : {
globals : {
... globals . browser ,
... globals . es2021 ,
... globals . node ,
2024-10-22 11:36:42 +08:00
... globals . jest ,
2024-10-21 17:03:04 +08:00
} ,
} ,
} ,
2024-10-22 13:49:39 +08:00
{
files : [
GLOB _TSX ,
GLOB _JSX ,
] ,
2024-10-22 14:09:31 +08:00
ignores : [
"**/{page,layout}.tsx" ,
] ,
2024-10-22 13:49:39 +08:00
plugins : {
"react-refresh" : reactRefresh ,
} ,
rules : {
"react-refresh/only-export-components" : "warn" ,
} ,
}
2024-10-21 16:21:12 +08:00
)