Skip to content

Commit 2b321c6

Browse files
committed
feat: add custom plugin type
1 parent dd61ac8 commit 2b321c6

File tree

2 files changed

+66
-54
lines changed

2 files changed

+66
-54
lines changed

src/index.ts

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Linter } from 'eslint'
1+
import type { Linter, RuleModule } from '@typescript-eslint/utils/ts-eslint'
22
import { version } from '../package.json'
33
import lowerCaseTitle, { RULE_NAME as lowerCaseTitleName } from './rules/prefer-lowercase-title'
44
import maxNestedDescribe, { RULE_NAME as maxNestedDescribeName } from './rules/max-nested-describe'
@@ -70,8 +70,8 @@ const createConfig = <R extends Linter.RulesRecord>(rules: R) => (
7070
[`vitest/${ruleName}`]: rules[ruleName]
7171
}
7272
}, {})) as {
73-
[K in keyof R as `vitest/${Extract<K, string>}`]: R[K]
74-
}
73+
[K in keyof R as `vitest/${Extract<K, string>}`]: R[K]
74+
}
7575

7676
const createConfigLegacy = (rules: Record<string, string>) => ({
7777
plugins: ['@vitest'],
@@ -159,7 +159,18 @@ const recommended = {
159159
[noImportNodeTestName]: 'error'
160160
} as const
161161

162-
const plugin = {
162+
interface VitestPLugin extends Linter.Plugin {
163+
meta: {
164+
name: string
165+
version: string
166+
}
167+
rules: Record<string, RuleModule<any, any>>
168+
//TODO: use classic type for config
169+
configs?: Record<string, any>
170+
environments?: Record<string, any>
171+
}
172+
173+
const plugin: VitestPLugin = {
163174
meta: {
164175
name: 'vitest',
165176
version
@@ -228,48 +239,6 @@ const plugin = {
228239
[paddingAroundTestBlocksName]: paddingAroundTestBlocks,
229240
[validExpectInPromiseName]: validExpectInPromise
230241
},
231-
configs: {
232-
'legacy-recommended': createConfigLegacy(recommended),
233-
'legacy-all': createConfigLegacy(allRules),
234-
'recommended': {
235-
plugins: {
236-
get vitest() {
237-
return plugin
238-
}
239-
},
240-
rules: createConfig(recommended)
241-
},
242-
'all': {
243-
plugins: {
244-
get vitest() {
245-
return plugin
246-
}
247-
},
248-
rules: createConfig(allRules)
249-
},
250-
'env': {
251-
languageOptions: {
252-
globals: {
253-
suite: 'writable',
254-
test: 'writable',
255-
describe: 'writable',
256-
it: 'writable',
257-
expectTypeOf: 'writable',
258-
assertType: 'writable',
259-
expect: 'writable',
260-
assert: 'writable',
261-
vitest: 'writable',
262-
vi: 'writable',
263-
beforeAll: 'writable',
264-
afterAll: 'writable',
265-
beforeEach: 'writable',
266-
afterEach: 'writable',
267-
onTestFailed: 'writable',
268-
onTestFinished: 'writable'
269-
}
270-
}
271-
}
272-
},
273242
environments: {
274243
env: {
275244
globals: {
@@ -294,4 +263,44 @@ const plugin = {
294263
}
295264
}
296265

266+
plugin.configs = {
267+
'legacy-recommended': createConfigLegacy(recommended),
268+
'legacy-all': createConfigLegacy(allRules),
269+
'recommended': {
270+
plugins: {
271+
["vitest"]: plugin
272+
},
273+
rules: createConfig(recommended)
274+
},
275+
'all': {
276+
plugins: {
277+
["vitest"]: plugin
278+
},
279+
rules: createConfig(allRules)
280+
},
281+
'env': {
282+
languageOptions: {
283+
globals: {
284+
suite: 'writable',
285+
test: 'writable',
286+
describe: 'writable',
287+
it: 'writable',
288+
expectTypeOf: 'writable',
289+
assertType: 'writable',
290+
expect: 'writable',
291+
assert: 'writable',
292+
vitest: 'writable',
293+
vi: 'writable',
294+
beforeAll: 'writable',
295+
afterAll: 'writable',
296+
beforeEach: 'writable',
297+
afterEach: 'writable',
298+
onTestFailed: 'writable',
299+
onTestFinished: 'writable'
300+
}
301+
}
302+
}
303+
}
304+
305+
297306
export default plugin

src/utils/index.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@ import {
1010
KnownMemberExpression,
1111
ParsedExpectVitestFnCall
1212
} from './parse-vitest-fn-call'
13-
import { Rule } from 'eslint'
13+
import { RuleRecommendation, RuleRecommendationAcrossConfigs } from '@typescript-eslint/utils/ts-eslint'
1414

1515
interface PluginDocs {
1616
recommended?: boolean
1717
requiresTypeChecking?: boolean
18+
extendsBaseRule?: boolean | string;
1819
}
1920

20-
export function createEslintRule<TOptions extends readonly unknown[], TMessageIds extends string>(rule: Readonly<ESLintUtils.RuleWithMetaAndName<TOptions, TMessageIds, PluginDocs>>) {
21-
const createRule = ESLintUtils.RuleCreator<PluginDocs>(
22-
ruleName =>
23-
`https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/${ruleName}.md`
24-
)
2521

26-
return createRule(rule) as unknown as Rule.RuleModule
27-
}
22+
// export function createEslintRule<TOptions extends readonly unknown[], TMessageIds extends string>(rule: Readonly<ESLintUtils.RuleWithMetaAndName<TOptions, TMessageIds, PluginDocs>>) {
23+
// const createRule = ESLintUtils.RuleCreator<PluginDocs>(
24+
// ruleName =>
25+
// `https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/${ruleName}.md`
26+
// )
27+
//
28+
// return createRule(rule) as unknown as Rule.RuleModule
29+
// }
30+
export const createEslintRule = ESLintUtils.RuleCreator<PluginDocs>(name => `https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/${name}.md`)
2831

2932
export const joinNames = (a: string | null, b: string | null): string | null =>
3033
a && b ? `${a}.${b}` : null

0 commit comments

Comments
 (0)