Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: configs in plugin declaration file #428

Merged
merged 2 commits into from
Apr 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 43 additions & 46 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ESLint } from 'eslint'
import type { ESLint, Linter } 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 @@ -53,13 +53,15 @@ import preferComparisonMatcher, { RULE_NAME as preferComparisonMatcherName } fro
import preferToContain, { RULE_NAME as preferToContainName } from './rules/prefer-to-contain'
import preferExpectAssertions, { RULE_NAME as preferExpectAssertionsName } from './rules/prefer-expect-assertions'

const createConfig = (rules: Record<string, string>) => (
const createConfig = <R extends Linter.RulesRecord>(rules: R) => (
Object.keys(rules).reduce((acc, ruleName) => {
return {
...acc,
[`vitest/${ruleName}`]: rules[ruleName]
}
}, {}))
}, {})) as {
[K in keyof R as `vitest/${Extract<K, string>}`]: R[K]
}

const allRules = {
[lowerCaseTitleName]: 'warn',
Expand Down Expand Up @@ -106,7 +108,7 @@ const allRules = {
[preferToContainName]: 'warn',
[preferExpectAssertionsName]: 'warn',
[usePreferTobe]: 'warn'
}
} as const

const recommended = {
[expectedExpect]: 'error',
Expand All @@ -117,7 +119,7 @@ const recommended = {
[validDescribeCallbackName]: 'error',
[requireLocalTestContextForConcurrentSnapshotsName]: 'error',
[noImportNodeTestName]: 'error'
}
} as const

const plugin = {
meta: {
Expand Down Expand Up @@ -178,7 +180,42 @@ const plugin = {
[preferToContainName]: preferToContain,
[preferExpectAssertionsName]: preferExpectAssertions
},
configs: {},
configs: {
recommended: {
plugins: {
get vitest(): ESLint.Plugin {
return plugin
}
},
rules: createConfig(recommended)
},
all: {
plugins: {
get vitest(): ESLint.Plugin {
return plugin
}
},
rules: createConfig(allRules)
},
env: {
languageOptions: {
globals: {
suite: 'writable',
test: 'writable',
describe: 'writable',
it: 'writable',
expect: 'writable',
assert: 'writable',
vitest: 'writable',
vi: 'writable',
beforeAll: 'writable',
afterAll: 'writable',
beforeEach: 'writable',
afterEach: 'writable'
}
}
}
},
environments: {
env: {
globals: {
Expand All @@ -199,44 +236,4 @@ const plugin = {
}
} satisfies ESLint.Plugin

Object.assign(plugin.configs, {
recommended: {
plugins: {
vitest: plugin
},
rules: createConfig(recommended)
}
})

Object.assign(plugin.configs, {
all: {
plugins: {
vitest: plugin
},
rules: createConfig(allRules)
}
})

Object.assign(plugin.configs, {
env: {
languageOptions: {
globals: {
suite: 'writeable',
test: 'writeable',
describe: 'writeable',
it: 'writeable',
expect: 'writeable',
assert: 'writeable',
vitest: 'writeable',
vi: 'writeable',
beforeAll: 'writeable',
afterAll: 'writeable',
beforeEach: 'writeable',
afterEach: 'writeable'
}

}
}
})

export default plugin