Skip to content

Commit a4625b2

Browse files
committedAug 29, 2024··
feat: add more unicorn rules, allow to configure using recommanded rules, close #595
1 parent b7c7a01 commit a4625b2

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed
 

‎src/configs/unicorn.ts

+22-28
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
1-
import type { TypedFlatConfigItem } from '../types'
1+
import type { OptionsUnicorn, TypedFlatConfigItem } from '../types'
22
import { pluginUnicorn } from '../plugins'
33

4-
export async function unicorn(): Promise<TypedFlatConfigItem[]> {
4+
export async function unicorn(options: OptionsUnicorn): Promise<TypedFlatConfigItem[]> {
55
return [
66
{
77
name: 'antfu/unicorn/rules',
88
plugins: {
99
unicorn: pluginUnicorn,
1010
},
1111
rules: {
12-
// Pass error message when throwing errors
13-
'unicorn/error-message': 'error',
14-
// Uppercase regex escapes
15-
'unicorn/escape-case': 'error',
16-
// Array.isArray instead of instanceof
17-
'unicorn/no-instanceof-array': 'error',
18-
// Ban `new Array` as `Array` constructor's params are ambiguous
19-
'unicorn/no-new-array': 'error',
20-
// Prevent deprecated `new Buffer()`
21-
'unicorn/no-new-buffer': 'error',
22-
// Lowercase number formatting for octal, hex, binary (0x1'error' instead of 0X1'error')
23-
'unicorn/number-literal-case': 'error',
24-
// textContent instead of innerText
25-
'unicorn/prefer-dom-node-text-content': 'error',
26-
// includes over indexOf when checking for existence
27-
'unicorn/prefer-includes': 'error',
28-
// Prefer using the node: protocol
29-
'unicorn/prefer-node-protocol': 'error',
30-
// Prefer using number properties like `Number.isNaN` rather than `isNaN`
31-
'unicorn/prefer-number-properties': 'error',
32-
// String methods startsWith/endsWith instead of more complicated stuff
33-
'unicorn/prefer-string-starts-ends-with': 'error',
34-
// Enforce throwing type error when throwing error while checking typeof
35-
'unicorn/prefer-type-error': 'error',
36-
// Use new when throwing error
37-
'unicorn/throw-new-error': 'error',
12+
...(options.allRecommended
13+
? pluginUnicorn.configs['flat/recommended'].rules
14+
: {
15+
'unicorn/consistent-empty-array-spread': 'error',
16+
'unicorn/consistent-function-scoping': 'error',
17+
'unicorn/error-message': 'error',
18+
'unicorn/escape-case': 'error',
19+
'unicorn/new-for-builtins': 'error',
20+
'unicorn/no-instanceof-array': 'error',
21+
'unicorn/no-new-array': 'error',
22+
'unicorn/no-new-buffer': 'error',
23+
'unicorn/number-literal-case': 'error',
24+
'unicorn/prefer-dom-node-text-content': 'error',
25+
'unicorn/prefer-includes': 'error',
26+
'unicorn/prefer-node-protocol': 'error',
27+
'unicorn/prefer-number-properties': 'error',
28+
'unicorn/prefer-string-starts-ends-with': 'error',
29+
'unicorn/prefer-type-error': 'error',
30+
'unicorn/throw-new-error': 'error',
31+
}),
3832
},
3933
},
4034
]

‎src/factory.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export function antfu(
9090
solid: enableSolid = false,
9191
svelte: enableSvelte = false,
9292
typescript: enableTypeScript = isPackageExists('typescript'),
93+
unicorn: enableUnicorn = true,
9394
unocss: enableUnoCSS = false,
9495
vue: enableVue = VuePackages.some(i => isPackageExists(i)),
9596
} = options
@@ -140,13 +141,16 @@ export function antfu(
140141
imports({
141142
stylistic: stylisticOptions,
142143
}),
143-
unicorn(),
144144
command(),
145145

146146
// Optional plugins (installed but not enabled by default)
147147
perfectionist(),
148148
)
149149

150+
if (enableUnicorn) {
151+
configs.push(unicorn(enableUnicorn === true ? {} : enableUnicorn))
152+
}
153+
150154
if (enableVue) {
151155
componentExts.push('vue')
152156
}

‎src/types.ts

+16
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ export interface OptionsComponentExts {
134134
componentExts?: string[]
135135
}
136136

137+
export interface OptionsUnicorn {
138+
/**
139+
* Include all rules recommended by `eslint-plugin-unicorn`, instead of only ones picked by Anthony.
140+
*
141+
* @default false
142+
*/
143+
allRecommended?: boolean
144+
}
145+
137146
export interface OptionsTypeScriptParserOptions {
138147
/**
139148
* Additional parser options for TypeScript.
@@ -260,6 +269,13 @@ export interface OptionsConfig extends OptionsComponentExts, OptionsProjectType
260269
*/
261270
jsx?: boolean
262271

272+
/**
273+
* Options for eslint-plugin-unicorn.
274+
*
275+
* @default true
276+
*/
277+
unicorn?: boolean | OptionsUnicorn
278+
263279
/**
264280
* Enable test support.
265281
*

0 commit comments

Comments
 (0)
Please sign in to comment.