Skip to content

Commit

Permalink
feat: update some rules
Browse files Browse the repository at this point in the history
  • Loading branch information
coderwyd committed Aug 30, 2024
1 parent 9fadd70 commit 6bbc864
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
8 changes: 6 additions & 2 deletions src/configs/ignores.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { GLOB_EXCLUDE } from '../constants/glob'
import type { TypedFlatConfigItem } from '../types'

export function ignores(): TypedFlatConfigItem[] {
export function ignores(userIgnores: string[] = []): TypedFlatConfigItem[] {
return [
{
ignores: GLOB_EXCLUDE,
ignores: [
...GLOB_EXCLUDE,
...userIgnores,
],
name: 'coderwyd/ignores',
},
]
}
3 changes: 0 additions & 3 deletions src/configs/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ export function javascript(
],
'no-restricted-syntax': [
'error',
'DebuggerStatement',
'LabeledStatement',
'WithStatement',
'TSEnumDeclaration[const=true]',
'TSExportAssignment',
],
Expand Down
32 changes: 22 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,29 @@ import {
vue,
} from './configs'
import {
isInEditor as defaultIsInEditor,
hasTypeScript,
hasVue,
} from './env'
import {
combine,
getOverrides,
interopDefault,
isInEditorEnv,
renamePluginInConfigs,
resolveSubOptions,
} from './shared'
import type { Awaitable, OptionsConfig, TypedFlatConfigItem } from './types'
import type { Linter } from 'eslint'

const flatConfigProps: (keyof TypedFlatConfigItem)[] = [
const flatConfigProps = [
'name',
'files',
'ignores',
'languageOptions',
'linterOptions',
'processor',
'plugins',
'rules',
'settings',
]
] satisfies (keyof TypedFlatConfigItem)[]

export const defaultPluginRenaming = {
'@eslint-react': 'react',
Expand All @@ -75,7 +73,7 @@ export const defaultPluginRenaming = {
* The merged ESLint configurations.
*/
export async function defineConfig(
options: OptionsConfig & TypedFlatConfigItem = {},
options: OptionsConfig & Omit<TypedFlatConfigItem, 'files'> = {},
...userConfigs: Awaitable<
TypedFlatConfigItem | TypedFlatConfigItem[] | Linter.Config[]
>[]
Expand All @@ -88,7 +86,6 @@ export async function defineConfig(
html: true,
},
gitignore: enableGitignore = true,
isInEditor = defaultIsInEditor,
jsx: enableJsx = true,
react: enableReact = false,
regexp: enableRegexp = true,
Expand All @@ -99,6 +96,14 @@ export async function defineConfig(
vue: enableVue = hasVue,
} = options

let isInEditor = options.isInEditor
if (isInEditor == null) {
isInEditor = isInEditorEnv()
if (isInEditor)
// eslint-disable-next-line no-console
console.log('[@coderwyd/eslint-config] Detected running in editor, some rules are disabled.')
}

const stylisticOptions
= options.stylistic === false
? false
Expand All @@ -115,13 +120,16 @@ export async function defineConfig(
if (typeof enableGitignore !== 'boolean') {
configs.push(
interopDefault(import('eslint-config-flat-gitignore')).then(r => [
r(enableGitignore),
r({
...enableGitignore,
name: 'coderwyd/gitignore',
}),
]),
)
}
else {
configs.push(interopDefault(import('eslint-config-flat-gitignore'))
.then(r => [r({ strict: false })]))
.then(r => [r({ name: 'coderwyd/gitignore', strict: false })]))
}
}

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

// Base configs
configs.push(
ignores(),
ignores(options.ignores),
javascript({
isInEditor,
overrides: getOverrides(options, 'javascript'),
Expand Down Expand Up @@ -254,6 +262,10 @@ export async function defineConfig(
)
}

if ('files' in options) {
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.')
}

// User can optionally pass a flat config item to the first argument
// We pick the known keys as ESLint would do schema validation
const fusedConfig = flatConfigProps.reduce((acc, key) => {
Expand Down
25 changes: 24 additions & 1 deletion src/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import { getPackageInfoSync, isPackageExists } from 'local-pkg'
import type {
Awaitable,
OptionsConfig,
ResolvedOptions,
TypedFlatConfigItem,
} from '../types'
import { fileURLToPath } from 'node:url'

const scopeUrl = fileURLToPath(new URL('.', import.meta.url))
const isCwdInScope = isPackageExists('@coderwyd/eslint-config')
Expand Down Expand Up @@ -174,3 +174,26 @@ export function getOverrides<K extends keyof OptionsConfig>(
...('overrides' in sub ? sub.overrides || {} : {}),
}
}

export function isInEditorEnv(): boolean {
if (process.env.CI)
return false
if (isInGitHooksOrLintStaged())
return false
return !!(process.env.VSCODE_PID
|| process.env.VSCODE_CWD
|| process.env.JETBRAINS_IDE
|| process.env.VIM
|| process.env.NVIM
|| false
)
}

export function isInGitHooksOrLintStaged(): boolean {
return !!(process.env.GIT_PARAMS
|| process.env.VSCODE_GIT_COMMAND
|| process.env.npm_lifecycle_script?.startsWith('lint-staged')
|| process.env.npm_lifecycle_script?.startsWith('nano-staged')
|| false
)
}

0 comments on commit 6bbc864

Please sign in to comment.