Skip to content

Commit

Permalink
feat: add custom plugin type
Browse files Browse the repository at this point in the history
  • Loading branch information
veritem committed Dec 26, 2024
1 parent dd61ac8 commit 2b321c6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 54 deletions.
101 changes: 55 additions & 46 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Linter } from 'eslint'
import type { Linter, RuleModule } from '@typescript-eslint/utils/ts-eslint'
import { version } from '../package.json'
import lowerCaseTitle, { RULE_NAME as lowerCaseTitleName } from './rules/prefer-lowercase-title'
import maxNestedDescribe, { RULE_NAME as maxNestedDescribeName } from './rules/max-nested-describe'
Expand Down Expand Up @@ -70,8 +70,8 @@ const createConfig = <R extends Linter.RulesRecord>(rules: R) => (
[`vitest/${ruleName}`]: rules[ruleName]
}
}, {})) as {
[K in keyof R as `vitest/${Extract<K, string>}`]: R[K]
}
[K in keyof R as `vitest/${Extract<K, string>}`]: R[K]
}

const createConfigLegacy = (rules: Record<string, string>) => ({
plugins: ['@vitest'],
Expand Down Expand Up @@ -159,7 +159,18 @@ const recommended = {
[noImportNodeTestName]: 'error'
} as const

const plugin = {
interface VitestPLugin extends Linter.Plugin {
meta: {
name: string
version: string
}
rules: Record<string, RuleModule<any, any>>
//TODO: use classic type for config
configs?: Record<string, any>
environments?: Record<string, any>
}

const plugin: VitestPLugin = {
meta: {
name: 'vitest',
version
Expand Down Expand Up @@ -228,48 +239,6 @@ const plugin = {
[paddingAroundTestBlocksName]: paddingAroundTestBlocks,
[validExpectInPromiseName]: validExpectInPromise
},
configs: {
'legacy-recommended': createConfigLegacy(recommended),
'legacy-all': createConfigLegacy(allRules),
'recommended': {
plugins: {
get vitest() {
return plugin
}
},
rules: createConfig(recommended)
},
'all': {
plugins: {
get vitest() {
return plugin
}
},
rules: createConfig(allRules)
},
'env': {
languageOptions: {
globals: {
suite: 'writable',
test: 'writable',
describe: 'writable',
it: 'writable',
expectTypeOf: 'writable',
assertType: 'writable',
expect: 'writable',
assert: 'writable',
vitest: 'writable',
vi: 'writable',
beforeAll: 'writable',
afterAll: 'writable',
beforeEach: 'writable',
afterEach: 'writable',
onTestFailed: 'writable',
onTestFinished: 'writable'
}
}
}
},
environments: {
env: {
globals: {
Expand All @@ -294,4 +263,44 @@ const plugin = {
}
}

plugin.configs = {
'legacy-recommended': createConfigLegacy(recommended),
'legacy-all': createConfigLegacy(allRules),
'recommended': {
plugins: {
["vitest"]: plugin
},
rules: createConfig(recommended)
},
'all': {
plugins: {
["vitest"]: plugin
},
rules: createConfig(allRules)
},
'env': {
languageOptions: {
globals: {
suite: 'writable',
test: 'writable',
describe: 'writable',
it: 'writable',
expectTypeOf: 'writable',
assertType: 'writable',
expect: 'writable',
assert: 'writable',
vitest: 'writable',
vi: 'writable',
beforeAll: 'writable',
afterAll: 'writable',
beforeEach: 'writable',
afterEach: 'writable',
onTestFailed: 'writable',
onTestFinished: 'writable'
}
}
}
}


export default plugin
19 changes: 11 additions & 8 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ import {
KnownMemberExpression,
ParsedExpectVitestFnCall
} from './parse-vitest-fn-call'
import { Rule } from 'eslint'
import { RuleRecommendation, RuleRecommendationAcrossConfigs } from '@typescript-eslint/utils/ts-eslint'

interface PluginDocs {
recommended?: boolean
requiresTypeChecking?: boolean
extendsBaseRule?: boolean | string;
}

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

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

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

0 comments on commit 2b321c6

Please sign in to comment.