Skip to content

Commit

Permalink
fix: configs in plugin declaration file (#428)
Browse files Browse the repository at this point in the history
* fix: configs in plugin declaration file

* feat(linter): apply linter

---------

Co-authored-by: Verite Mugabo <mugaboverite@gmail.com>
  • Loading branch information
henrist and veritem committed Apr 14, 2024
1 parent 5ec4809 commit a554dd2
Showing 1 changed file with 43 additions and 46 deletions.
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

0 comments on commit a554dd2

Please sign in to comment.