Skip to content

Commit

Permalink
feat: move overrides option inside each integrations (#371)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
Debbl and antfu committed Dec 25, 2023
1 parent 75a547b commit 8af5bfa
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 39 deletions.
26 changes: 17 additions & 9 deletions README.md
Expand Up @@ -305,7 +305,10 @@ Certain rules would only be enabled in specific files, for example, `ts/*` rules
import antfu from '@antfu/eslint-config'

export default antfu(
{ vue: true, typescript: true },
{
vue: true,
typescript: true
},
{
// Remember to specify the file glob here, otherwise it might cause the vue plugin to handle non-vue files
files: ['**/*.vue'],
Expand All @@ -322,23 +325,28 @@ export default antfu(
)
```

We also provided an `overrides` options to make it easier:
We also provided a `overrides` options in each integration to make it easier:

```js
// eslint.config.js
import antfu from '@antfu/eslint-config'

export default antfu({
overrides: {
vue: {
vue: {
overrides: {
'vue/operator-linebreak': ['error', 'before'],
},
typescript: {
},
typescript: {
overrides: {
'ts/consistent-type-definitions': ['error', 'interface'],
},
yaml: {},
// ...
}
},
yaml: {
overrides: {
// ...
},
},
})
```

Expand Down Expand Up @@ -444,7 +452,7 @@ npm i -D @unocss/eslint-plugin

### Optional Rules

This config also provides some optional plugins/rules for extended usages.
This config also provides some optional plugins/rules for extended usage.

#### `perfectionist` (sorting)

Expand Down
63 changes: 42 additions & 21 deletions src/factory.ts
Expand Up @@ -57,7 +57,6 @@ export async function antfu(
componentExts = [],
gitignore: enableGitignore = true,
isInEditor = !!((process.env.VSCODE_PID || process.env.JETBRAINS_IDE || process.env.VIM) && !process.env.CI),
overrides = {},
react: enableReact = false,
svelte: enableSvelte = false,
typescript: enableTypeScript = isPackageExists('typescript'),
Expand All @@ -70,6 +69,7 @@ export async function antfu(
: typeof options.stylistic === 'object'
? options.stylistic
: {}

if (stylisticOptions && !('jsx' in stylisticOptions))
stylisticOptions.jsx = options.jsx ?? true

Expand All @@ -90,7 +90,7 @@ export async function antfu(
ignores(),
javascript({
isInEditor,
overrides: overrides.javascript,
overrides: getOverrides(options, 'javascript'),
}),
comments(),
node(),
Expand All @@ -111,64 +111,59 @@ export async function antfu(

if (enableTypeScript) {
configs.push(typescript({
...typeof enableTypeScript !== 'boolean'
? enableTypeScript
: {},
...resolveSubOptions(options, 'typescript'),
componentExts,
overrides: overrides.typescript,
}))
}

if (stylisticOptions) {
configs.push(stylistic({
...stylisticOptions,
overrides: overrides.stylistic,
overrides: getOverrides(options, 'stylistic'),
}))
}

if (options.test ?? true) {
configs.push(test({
isInEditor,
overrides: overrides.test,
overrides: getOverrides(options, 'test'),
}))
}

if (enableVue) {
configs.push(vue({
...typeof enableVue !== 'boolean'
? enableVue
: {},
overrides: overrides.vue,
...resolveSubOptions(options, 'vue'),
stylistic: stylisticOptions,
typescript: !!enableTypeScript,
}))
}

if (enableReact) {
configs.push(react({
overrides: overrides.react,
overrides: getOverrides(options, 'react'),
typescript: !!enableTypeScript,
}))
}

if (enableSvelte) {
configs.push(svelte({
overrides: overrides.svelte,
overrides: getOverrides(options, 'svelte'),
stylistic: stylisticOptions,
typescript: !!enableTypeScript,
}))
}

if (enableUnoCSS) {
configs.push(unocss(
typeof enableUnoCSS === 'boolean' ? {} : enableUnoCSS,
))
configs.push(unocss({
...resolveSubOptions(options, 'unocss'),
overrides: getOverrides(options, 'unocss'),
}))
}

if (options.jsonc ?? true) {
configs.push(
jsonc({
overrides: overrides.jsonc,
overrides: getOverrides(options, 'jsonc'),
stylistic: stylisticOptions,
}),
sortPackageJson(),
Expand All @@ -178,14 +173,14 @@ export async function antfu(

if (options.yaml ?? true) {
configs.push(yaml({
overrides: overrides.yaml,
overrides: getOverrides(options, 'yaml'),
stylistic: stylisticOptions,
}))
}

if (options.toml ?? true) {
configs.push(toml({
overrides: overrides.toml,
overrides: getOverrides(options, 'toml'),
stylistic: stylisticOptions,
}))
}
Expand All @@ -195,7 +190,7 @@ export async function antfu(
markdown(
{
componentExts,
overrides: overrides.markdown,
overrides: getOverrides(options, 'markdown'),
},
),
)
Expand Down Expand Up @@ -225,3 +220,29 @@ export async function antfu(

return merged
}

export type ResolvedOptions<T> = T extends boolean
? never
: NonNullable<T>

export function resolveSubOptions<K extends keyof OptionsConfig>(
options: OptionsConfig,
key: K,
): ResolvedOptions<OptionsConfig[K]> {
return typeof options[key] === 'boolean'
? {} as any
: options[key] || {}
}

export function getOverrides<K extends keyof OptionsConfig>(
options: OptionsConfig,
key: K,
) {
const sub = resolveSubOptions(options, key)
return {
...(options.overrides as any)?.[key],
...'overrides' in sub
? sub.overrides
: {},
}
}
29 changes: 20 additions & 9 deletions src/types.ts
Expand Up @@ -80,7 +80,7 @@ export interface OptionsFiles {
files?: string[]
}

export interface OptionsVue {
export interface OptionsVue extends OptionsOverrides {
/**
* Create virtual files for Vue SFC blocks to enable linting.
*
Expand All @@ -97,6 +97,10 @@ export interface OptionsVue {
vueVersion?: 2 | 3
}

export type OptionsTypescript =
(OptionsTypeScriptWithTypes & OptionsOverrides)
| (OptionsTypeScriptParserOptions & OptionsOverrides)

export interface OptionsFormatters {
/**
* Enable formatting support for CSS, Less, Sass, and SCSS.
Expand Down Expand Up @@ -191,7 +195,7 @@ export interface OptionsIsInEditor {
isInEditor?: boolean
}

export interface OptionsUnoCSS {
export interface OptionsUnoCSS extends OptionsOverrides {
/**
* Enable attributify support.
* @default true
Expand All @@ -215,14 +219,19 @@ export interface OptionsConfig extends OptionsComponentExts {
*/
gitignore?: boolean | FlatGitignoreOptions

/**
* Core rules. Can't be disabled.
*/
javascript?: OptionsOverrides

/**
* Enable TypeScript support.
*
* Passing an object to enable TypeScript Language Server support.
*
* @default auto-detect based on the dependencies
*/
typescript?: boolean | OptionsTypeScriptWithTypes | OptionsTypeScriptParserOptions
typescript?: boolean | OptionsTypescript

/**
* Enable JSX related rules.
Expand All @@ -238,7 +247,7 @@ export interface OptionsConfig extends OptionsComponentExts {
*
* @default true
*/
test?: boolean
test?: boolean | OptionsOverrides

/**
* Enable Vue support.
Expand All @@ -252,21 +261,21 @@ export interface OptionsConfig extends OptionsComponentExts {
*
* @default true
*/
jsonc?: boolean
jsonc?: boolean | OptionsOverrides

/**
* Enable YAML support.
*
* @default true
*/
yaml?: boolean
yaml?: boolean | OptionsOverrides

/**
* Enable TOML support.
*
* @default true
*/
toml?: boolean
toml?: boolean | OptionsOverrides

/**
* Enable linting for **code snippets** in Markdown.
Expand All @@ -275,7 +284,7 @@ export interface OptionsConfig extends OptionsComponentExts {
*
* @default true
*/
markdown?: boolean
markdown?: boolean | OptionsOverrides

/**
* Enable stylistic rules.
Expand All @@ -294,7 +303,7 @@ export interface OptionsConfig extends OptionsComponentExts {
*
* @default false
*/
react?: boolean
react?: boolean | OptionsOverrides

/**
* Enable svelte rules.
Expand Down Expand Up @@ -336,6 +345,8 @@ export interface OptionsConfig extends OptionsComponentExts {

/**
* Provide overrides for rules for each integration.
*
* @deprecated use `overrides` option in each integration key instead
*/
overrides?: {
stylistic?: FlatConfigItem['rules']
Expand Down

0 comments on commit 8af5bfa

Please sign in to comment.