Skip to content

Commit 6bbc864

Browse files
committed
feat: update some rules
1 parent 9fadd70 commit 6bbc864

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

src/configs/ignores.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { GLOB_EXCLUDE } from '../constants/glob'
22
import type { TypedFlatConfigItem } from '../types'
33

4-
export function ignores(): TypedFlatConfigItem[] {
4+
export function ignores(userIgnores: string[] = []): TypedFlatConfigItem[] {
55
return [
66
{
7-
ignores: GLOB_EXCLUDE,
7+
ignores: [
8+
...GLOB_EXCLUDE,
9+
...userIgnores,
10+
],
11+
name: 'coderwyd/ignores',
812
},
913
]
1014
}

src/configs/javascript.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@ export function javascript(
144144
],
145145
'no-restricted-syntax': [
146146
'error',
147-
'DebuggerStatement',
148-
'LabeledStatement',
149-
'WithStatement',
150147
'TSEnumDeclaration[const=true]',
151148
'TSExportAssignment',
152149
],

src/index.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,29 @@ import {
2424
vue,
2525
} from './configs'
2626
import {
27-
isInEditor as defaultIsInEditor,
2827
hasTypeScript,
2928
hasVue,
3029
} from './env'
3130
import {
3231
combine,
3332
getOverrides,
3433
interopDefault,
34+
isInEditorEnv,
3535
renamePluginInConfigs,
3636
resolveSubOptions,
3737
} from './shared'
3838
import type { Awaitable, OptionsConfig, TypedFlatConfigItem } from './types'
3939
import type { Linter } from 'eslint'
4040

41-
const flatConfigProps: (keyof TypedFlatConfigItem)[] = [
41+
const flatConfigProps = [
4242
'name',
43-
'files',
44-
'ignores',
4543
'languageOptions',
4644
'linterOptions',
4745
'processor',
4846
'plugins',
4947
'rules',
5048
'settings',
51-
]
49+
] satisfies (keyof TypedFlatConfigItem)[]
5250

5351
export const defaultPluginRenaming = {
5452
'@eslint-react': 'react',
@@ -75,7 +73,7 @@ export const defaultPluginRenaming = {
7573
* The merged ESLint configurations.
7674
*/
7775
export async function defineConfig(
78-
options: OptionsConfig & TypedFlatConfigItem = {},
76+
options: OptionsConfig & Omit<TypedFlatConfigItem, 'files'> = {},
7977
...userConfigs: Awaitable<
8078
TypedFlatConfigItem | TypedFlatConfigItem[] | Linter.Config[]
8179
>[]
@@ -88,7 +86,6 @@ export async function defineConfig(
8886
html: true,
8987
},
9088
gitignore: enableGitignore = true,
91-
isInEditor = defaultIsInEditor,
9289
jsx: enableJsx = true,
9390
react: enableReact = false,
9491
regexp: enableRegexp = true,
@@ -99,6 +96,14 @@ export async function defineConfig(
9996
vue: enableVue = hasVue,
10097
} = options
10198

99+
let isInEditor = options.isInEditor
100+
if (isInEditor == null) {
101+
isInEditor = isInEditorEnv()
102+
if (isInEditor)
103+
// eslint-disable-next-line no-console
104+
console.log('[@coderwyd/eslint-config] Detected running in editor, some rules are disabled.')
105+
}
106+
102107
const stylisticOptions
103108
= options.stylistic === false
104109
? false
@@ -115,13 +120,16 @@ export async function defineConfig(
115120
if (typeof enableGitignore !== 'boolean') {
116121
configs.push(
117122
interopDefault(import('eslint-config-flat-gitignore')).then(r => [
118-
r(enableGitignore),
123+
r({
124+
...enableGitignore,
125+
name: 'coderwyd/gitignore',
126+
}),
119127
]),
120128
)
121129
}
122130
else {
123131
configs.push(interopDefault(import('eslint-config-flat-gitignore'))
124-
.then(r => [r({ strict: false })]))
132+
.then(r => [r({ name: 'coderwyd/gitignore', strict: false })]))
125133
}
126134
}
127135

@@ -130,7 +138,7 @@ export async function defineConfig(
130138

131139
// Base configs
132140
configs.push(
133-
ignores(),
141+
ignores(options.ignores),
134142
javascript({
135143
isInEditor,
136144
overrides: getOverrides(options, 'javascript'),
@@ -254,6 +262,10 @@ export async function defineConfig(
254262
)
255263
}
256264

265+
if ('files' in options) {
266+
throw new Error('[@coderwyd/eslint-config] The first argument should not contain the "files" property as the options are supposed to be global. Place it in the second or later config instead.')
267+
}
268+
257269
// User can optionally pass a flat config item to the first argument
258270
// We pick the known keys as ESLint would do schema validation
259271
const fusedConfig = flatConfigProps.reduce((acc, key) => {

src/shared/index.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import process from 'node:process'
2+
import { fileURLToPath } from 'node:url'
23
import { getPackageInfoSync, isPackageExists } from 'local-pkg'
34
import type {
45
Awaitable,
56
OptionsConfig,
67
ResolvedOptions,
78
TypedFlatConfigItem,
89
} from '../types'
9-
import { fileURLToPath } from 'node:url'
1010

1111
const scopeUrl = fileURLToPath(new URL('.', import.meta.url))
1212
const isCwdInScope = isPackageExists('@coderwyd/eslint-config')
@@ -174,3 +174,26 @@ export function getOverrides<K extends keyof OptionsConfig>(
174174
...('overrides' in sub ? sub.overrides || {} : {}),
175175
}
176176
}
177+
178+
export function isInEditorEnv(): boolean {
179+
if (process.env.CI)
180+
return false
181+
if (isInGitHooksOrLintStaged())
182+
return false
183+
return !!(process.env.VSCODE_PID
184+
|| process.env.VSCODE_CWD
185+
|| process.env.JETBRAINS_IDE
186+
|| process.env.VIM
187+
|| process.env.NVIM
188+
|| false
189+
)
190+
}
191+
192+
export function isInGitHooksOrLintStaged(): boolean {
193+
return !!(process.env.GIT_PARAMS
194+
|| process.env.VSCODE_GIT_COMMAND
195+
|| process.env.npm_lifecycle_script?.startsWith('lint-staged')
196+
|| process.env.npm_lifecycle_script?.startsWith('nano-staged')
197+
|| false
198+
)
199+
}

0 commit comments

Comments
 (0)