Skip to content

Commit

Permalink
fix: fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
veritem committed Jan 3, 2025
1 parent 06896aa commit 57cc9e5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 54 deletions.
95 changes: 44 additions & 51 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Linter, RuleModule } from '@typescript-eslint/utils/ts-eslint'
import type { Linter } from '@typescript-eslint/utils/ts-eslint'
import type { ESLint } from "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 +71,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,23 +160,12 @@ const recommended = {
[noImportNodeTestName]: 'error'
} as const

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 = {
const plugin = {
meta: {
name: 'vitest',
version
},
configs: {},
rules: {
[lowerCaseTitleName]: lowerCaseTitle,
[maxNestedDescribeName]: maxNestedDescribe,
Expand Down Expand Up @@ -261,46 +251,49 @@ const plugin: VitestPlugin = {
onTestFinished: true
}
}
}
}

plugin.configs = {
'legacy-recommended': createConfigLegacy(recommended),
'legacy-all': createConfigLegacy(allRules),
'recommended': {
plugins: {
['vitest']: plugin
},
rules: createConfig(recommended)
},
'all': {
plugins: {
['vitest']: plugin
configs: {
'legacy-recommended': createConfigLegacy(recommended),
'legacy-all': createConfigLegacy(allRules),
'recommended': {
plugins: {
get vitest(): ESLint.Plugin {
return plugin
}
},
rules: createConfig(recommended)
},
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'
'all': {
plugins: {
get vitest(): ESLint.Plugin {
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'
}
}
}
}
}
} satisfies ESLint.Plugin

export default plugin
7 changes: 4 additions & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ import {
KnownMemberExpression,
ParsedExpectVitestFnCall
} from './parse-vitest-fn-call'
import { RuleListener, RuleModule } from '@typescript-eslint/utils/ts-eslint'
import { Rule } from "eslint"

export interface PluginDocs {
recommended?: boolean
requiresTypeChecking?: boolean
}

export function createEslintRule<TOptions extends readonly unknown[], TMessageIds extends string>(rule: Readonly<ESLintUtils.RuleWithMetaAndName<TOptions, TMessageIds, PluginDocs>>): RuleModule<TMessageIds, TOptions, PluginDocs, RuleListener> {
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)

return createRule(rule) as unknown as Rule.RuleModule
}

export const joinNames = (a: string | null, b: string | null): string | null =>
Expand Down

0 comments on commit 57cc9e5

Please sign in to comment.