Skip to content

Commit

Permalink
make build work (#618)
Browse files Browse the repository at this point in the history
* make build work

* improve eslint

* feat: add custom plugin type
  • Loading branch information
veritem authored Dec 26, 2024
1 parent cb50e0a commit 5f1aac9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 66 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@typescript-eslint/parser": "8.18.2",
"@typescript-eslint/rule-tester": "8.18.1",
"@vitest/eslint-plugin": "^1.1.20",
"bumpp": "^9.9.1",
"bumpp": "^9.9.2",
"concurrently": "^8.2.2",
"eslint": "^9.17.0",
"eslint-doc-generator": "^2.0.2",
Expand Down
14 changes: 2 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
18 changes: 11 additions & 7 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ import {
KnownMemberExpression,
ParsedExpectVitestFnCall
} from './parse-vitest-fn-call'
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)
}
// 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 5f1aac9

Please sign in to comment.