Skip to content

Commit dbc1e76

Browse files
committed
feat: allow customize indent and quotes
1 parent 1373e0e commit dbc1e76

File tree

7 files changed

+58
-15
lines changed

7 files changed

+58
-15
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,24 @@ Certain rules would only be enabled in specific files, for example, `ts/*` rules
242242
import coderwyd from '@coderwyd/eslint-config'
243243

244244
export default coderwyd(
245-
{ vue: true, typescript: true },
245+
{
246+
// Enable stylistic formatting rules
247+
// stylistic: true,
248+
249+
// Or customize the stylistic rules
250+
stylistic: {
251+
indent: 2, // 4, or 'tab'
252+
quotes: 'single', // or 'double'
253+
},
254+
255+
// TypeScript and Vue are auto-detected, you can also explicitly enable them:
256+
typescript: true,
257+
vue: true,
258+
259+
// Disable jsonc and yaml support
260+
jsonc: false,
261+
yaml: false,
262+
},
246263
{
247264
// Remember to specify the file glob here, otherwise it might cause the vue plugin to handle non-vue files
248265
files: ['**/*.vue'],

src/configs/stylistic.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import type { FlatESLintConfigItem } from '../types'
1+
import type { FlatESLintConfigItem, StylisticConfig } from '../types'
22
import { pluginAntfu, pluginStylistic } from '../plugins'
33

4-
export function stylistic(): FlatESLintConfigItem[] {
4+
export function stylistic(options: StylisticConfig = {}): FlatESLintConfigItem[] {
5+
const {
6+
indent = 2,
7+
quotes = 'single',
8+
} = options
9+
510
return [
611
{
712
name: 'coderwyd:stylistic',
@@ -26,7 +31,7 @@ export function stylistic(): FlatESLintConfigItem[] {
2631
'style/computed-property-spacing': ['error', 'never', { enforceForClassMembers: true }],
2732
'style/dot-location': ['error', 'property'],
2833
'style/eol-last': 'error',
29-
'style/indent': ['error', 2, {
34+
'style/indent': ['error', indent, {
3035
ArrayExpression: 1,
3136
CallExpression: { arguments: 1 },
3237
FunctionDeclaration: { body: 1, parameters: 1 },
@@ -85,15 +90,15 @@ export function stylistic(): FlatESLintConfigItem[] {
8590
'style/no-mixed-spaces-and-tabs': 'error',
8691
'style/no-multi-spaces': 'error',
8792
'style/no-multiple-empty-lines': ['error', { max: 1, maxBOF: 0, maxEOF: 0 }],
88-
'style/no-tabs': 'error',
93+
'style/no-tabs': indent === 'tab' ? 'off' : 'error',
8994
'style/no-trailing-spaces': 'error',
9095
'style/no-whitespace-before-property': 'error',
9196
'style/object-curly-spacing': ['error', 'always'],
9297
'style/object-property-newline': ['error', { allowMultiplePropertiesPerLine: true }],
9398
'style/operator-linebreak': ['error', 'before'],
9499
'style/padded-blocks': ['error', { blocks: 'never', classes: 'never', switches: 'never' }],
95100
'style/quote-props': ['error', 'consistent-as-needed'],
96-
'style/quotes': ['error', 'single', { allowTemplateLiterals: true, avoidEscape: true }],
101+
'style/quotes': ['error', quotes, { allowTemplateLiterals: true, avoidEscape: true }],
97102
'style/rest-spread-spacing': ['error', 'never'],
98103
'style/semi': ['error', 'never'],
99104
'style/semi-spacing': ['error', { after: true, before: false }],

src/configs/typescript.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ export function typescript(
8181
'antfu/generic-spacing': 'error',
8282
'antfu/named-tuple-spacing': 'error',
8383
'antfu/no-cjs-exports': 'error',
84-
'antfu/no-const-enum': 'error',
85-
'antfu/no-ts-export-equal': 'error',
8684

8785
'no-dupe-class-members': OFF,
8886
'no-invalid-this': OFF,
@@ -122,6 +120,7 @@ export function typescript(
122120
rules: {
123121
'eslint-comments/no-unlimited-disable': OFF,
124122
'import/no-duplicates': OFF,
123+
'no-restricted-syntax': OFF,
125124
'unused-imports/no-unused-vars': OFF,
126125
},
127126
},

src/configs/vue.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export function vue(options: OptionsHasTypeScript & OptionsOverrides & OptionsSt
3737
overrides = {},
3838
stylistic = true,
3939
} = options
40+
41+
const {
42+
indent = 2,
43+
} = typeof stylistic === 'boolean' ? {} : stylistic
44+
4045
return [
4146
{
4247
name: 'coderwyd:vue:setup',
@@ -76,6 +81,8 @@ export function vue(options: OptionsHasTypeScript & OptionsOverrides & OptionsSt
7681
'vue/dot-location': ['error', 'property'],
7782
'vue/dot-notation': ['error', { allowKeywords: true }],
7883
'vue/eqeqeq': ['error', 'smart'],
84+
'vue/html-indent': ['error', indent],
85+
'vue/html-quotes': ['error', 'double'],
7986
'vue/max-attributes-per-line': OFF,
8087
'vue/multi-word-component-names': OFF,
8188
'vue/no-dupe-keys': OFF,

src/configs/yaml.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export function yaml(options: OptionsOverrides & OptionsStylistic = {}): FlatESL
99
stylistic = true,
1010
} = options
1111

12+
const {
13+
indent = 2,
14+
quotes = 'single',
15+
} = typeof stylistic === 'boolean' ? {} : stylistic
16+
1217
return [
1318
{
1419
name: 'coderwyd:yaml:setup',
@@ -42,10 +47,10 @@ export function yaml(options: OptionsOverrides & OptionsStylistic = {}): FlatESL
4247
'yaml/flow-mapping-curly-spacing': 'error',
4348
'yaml/flow-sequence-bracket-newline': 'error',
4449
'yaml/flow-sequence-bracket-spacing': 'error',
45-
'yaml/indent': ['error', 2],
50+
'yaml/indent': ['error', indent],
4651
'yaml/key-spacing': 'error',
47-
'yaml/no-tab-indent': 'error',
48-
'yaml/quotes': ['error', { avoidEscape: false, prefer: 'single' }],
52+
'yaml/no-tab-indent': indent === 'tab' ? 'off' : 'error',
53+
'yaml/quotes': ['error', { avoidEscape: false, prefer: quotes }],
4954
'yaml/spaced-comment': 'error',
5055
}
5156
: {},

src/factory.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,13 @@ export function coderwyd(options: OptionsConfig & FlatESLintConfigItem = {}, ...
107107
}))
108108
}
109109

110-
if (enableStylistic)
111-
configs.push(stylistic())
110+
if (enableStylistic) {
111+
configs.push(stylistic(
112+
typeof enableStylistic === 'boolean'
113+
? {}
114+
: enableStylistic,
115+
))
116+
}
112117

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

src/types.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ export interface OptionsHasTypeScript {
9999
}
100100

101101
export interface OptionsStylistic {
102-
stylistic?: boolean
102+
stylistic?: boolean | StylisticConfig
103+
}
104+
105+
export interface StylisticConfig {
106+
indent?: number | 'tab'
107+
quotes?: 'single' | 'double'
103108
}
104109

105110
export interface OptionsOverrides {
@@ -184,7 +189,7 @@ export interface OptionsConfig extends OptionsComponentExts {
184189
*
185190
* @default true
186191
*/
187-
stylistic?: boolean
192+
stylistic?: boolean | StylisticConfig
188193

189194
/**
190195
* Control to disable some rules in editors.

0 commit comments

Comments
 (0)