Skip to content

Commit

Permalink
feat: 解决ts类型错误,把规则放到单独文件中
Browse files Browse the repository at this point in the history
  • Loading branch information
dxhuii committed Oct 10, 2023
1 parent b885391 commit 9ce5888
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 139 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@
},
"dependencies": {
"@antfu/eslint-config": "1.0.0-beta.24",
"@typescript-eslint/parser": "^6.7.4",
"@typescript-eslint/parser": "^6.7.5",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0"
},
"devDependencies": {
"@dxhuii/eslint-config": "workspace:*",
"@types/node": "^20.8.3",
"@types/eslint": "^8.44.3",
"@types/node": "^20.8.4",
"bumpp": "^9.2.0",
"eslint": "^8.51.0",
"lint-staged": "^14.0.1",
Expand Down
170 changes: 98 additions & 72 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions src/configs/all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { ConfigItem } from '@antfu/eslint-config'

export const all: ConfigItem[] = [
{
name: 'dxhuii:all',
rules: {
'antfu/no-cjs-exports': 'off', // 允许使用 commonjs 的 exports
'antfu/consistent-list-newline': 'off', // 允许在数组元素之间换行
'arrow-parens': ['error', 'as-needed'], // 箭头函数参数只有一个时不需要括号

'import/prefer-default-export': 'off', // 允许使用 export
'import/extensions': 'off', // 允许不写文件后缀

'no-multi-str': 'off', // 允许多行字符串
'no-restricted-globals': 'off', // 允许使用全局变量
'no-console': 'off', // 允许使用 console

'node/prefer-global/process': 'off', // 允许使用 process

// 删除未尾逗号
'jsonc/comma-dangle': ['error', 'never'],
'style/comma-dangle': ['error', 'never'],
'style/jsx-quotes': ['error', 'prefer-single'], // jsx 使用单引号
'style/jsx-closing-bracket-location': ['error', 'after-props'],
'style/jsx-wrap-multilines': ['error', {
declaration: 'parens-new-line',
assignment: 'parens-new-line',
return: 'parens-new-line',
arrow: 'parens-new-line',
condition: 'parens-new-line',
logical: 'parens-new-line',
prop: 'parens-new-line'
}],
// 删除未使用的依赖
'unused-imports/no-unused-imports': 'error',
'unused-imports/no-unused-vars': [
'warn',
{
vars: 'all',
varsIgnorePattern: '^_',
args: 'after-used',
argsIgnorePattern: '^_'
}
]
}
}
]
2 changes: 2 additions & 0 deletions src/configs/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './react'
export * from './vue'
export * from './all'
1 change: 1 addition & 0 deletions src/configs/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const react: ConfigItem[] = [
}
}
},
name: 'dxhuii:react',
plugins: {
'react': pluginReact,
'react-hooks': pluginReactHooks
Expand Down
20 changes: 20 additions & 0 deletions src/configs/vue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { ConfigItem } from '@antfu/eslint-config'
import { GLOB_VUE } from '../globs'

export const vue: ConfigItem[] = [
{
files: [GLOB_VUE],
name: 'dxhuii:vue',
rules: {
'vue/v-on-event-hyphenation': ['error', 'never'], // 事件名不允许使用连字符
'vue/valid-attribute-name': 'off', // 属性名不允许使用连字符
// 关闭组件命名规则
'vue/multi-word-component-names': 'off', // 组件名不允许使用连字符
'vue/custom-event-name-casing': ['error', 'camelCase', { // 事件名不允许使用连字符
ignores: [
'/^(click):[a-z]+[a-zA-Z]+$/'
]
}]
}
}
]
76 changes: 12 additions & 64 deletions src/factory.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,27 @@
import type { ConfigItem, OptionsConfig } from '@antfu/eslint-config'
import { react } from './configs'
import { all, react, vue } from './configs'
import { combine } from './utils'

interface CustomConfig { react?: boolean; all?: boolean }

/**
* Construct an array of ESLint flat config items.
* 构建一个 ESLint 平面配置项数组。
*/
export function dxhuii(options: OptionsConfig & { react?: boolean } & ConfigItem = {}, ...userConfigs: (ConfigItem | ConfigItem[])[]) {
export function dxhuii(options: OptionsConfig & CustomConfig & ConfigItem = {}, ...userConfigs: (ConfigItem | ConfigItem[])[]) {
const configs: ConfigItem[][] = []

if (options.react ?? true)
configs.push(react)

const merged = combine(
...configs,
...userConfigs,
{
// Remember to specify the file glob here, otherwise it might cause the vue plugin to handle non-vue files
files: ['**/*.vue'],
rules: {
'vue/v-on-event-hyphenation': ['error', 'never'], // 事件名不允许使用连字符
'vue/valid-attribute-name': 'off', // 属性名不允许使用连字符
// 关闭组件命名规则
'vue/multi-word-component-names': 'off', // 组件名不允许使用连字符
'vue/custom-event-name-casing': ['error', 'camelCase', { // 事件名不允许使用连字符
ignores: [
'/^(click):[a-z]+[a-zA-Z]+$/'
]
}]
}
},
{
// 所有文件
rules: {
'no-multi-str': 'off', // 允许多行字符串
'no-restricted-globals': 'off', // 允许使用全局变量
'antfu/no-cjs-exports': 'off', // 允许使用 commonjs 的 exports
'node/prefer-global/process': 'off', // 允许使用 process

'antfu/consistent-list-newline': 'off', // 允许在数组元素之间换行

'arrow-parens': ['error', 'as-needed'], // 箭头函数参数只有一个时不需要括号
if (options.vue ?? true)
configs.push(vue)

'import/prefer-default-export': 'off', // 允许使用 export
'import/extensions': 'off', // 允许不写文件后缀
if (options.all ?? true)
configs.push(all)

'no-console': 'off', // 允许使用 console

// 删除未尾逗号
'style/comma-dangle': ['error', 'never'],
'jsonc/comma-dangle': ['error', 'never'],

'style/jsx-quotes': ['error', 'prefer-single'], // jsx 使用单引号
'style/jsx-closing-bracket-location': ['error', 'after-props'],
'style/jsx-wrap-multilines': ['error', {
declaration: 'parens-new-line',
assignment: 'parens-new-line',
return: 'parens-new-line',
arrow: 'parens-new-line',
condition: 'parens-new-line',
logical: 'parens-new-line',
prop: 'parens-new-line'
}],
// 删除未使用的依赖
'unused-imports/no-unused-imports': 'error',
'unused-imports/no-unused-vars': [
'warn',
{
vars: 'all',
varsIgnorePattern: '^_',
args: 'after-used',
argsIgnorePattern: '^_'
}
]
} as any
}
const merged = combine(
...configs,
...userConfigs
)

return merged
Expand Down
2 changes: 2 additions & 0 deletions src/globs.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export const GLOB_JSX = '**/*.?([cm])jsx'
export const GLOB_TSX = '**/*.?([cm])tsx'

export const GLOB_VUE = '**/*.vue'
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ConfigItem } from '@antfu/eslint-config'

/**
* Combine array and non-array configs into a single array.
* 将阵列和非阵列配置合并为一个阵列。
*/
export function combine(...configs: (ConfigItem | ConfigItem[])[]): ConfigItem[] {
return configs.flatMap(config => Array.isArray(config) ? config : [config])
Expand Down

0 comments on commit 9ce5888

Please sign in to comment.