Skip to content

Commit a554dd2

Browse files
henristveritem
andauthored
fix: configs in plugin declaration file (#428)
* fix: configs in plugin declaration file * feat(linter): apply linter --------- Co-authored-by: Verite Mugabo <mugaboverite@gmail.com>
1 parent 5ec4809 commit a554dd2

File tree

1 file changed

+43
-46
lines changed

1 file changed

+43
-46
lines changed

src/index.ts

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ESLint } from 'eslint'
1+
import type { ESLint, Linter } from 'eslint'
22
import { version } from '../package.json'
33
import lowerCaseTitle, { RULE_NAME as lowerCaseTitleName } from './rules/prefer-lowercase-title'
44
import maxNestedDescribe, { RULE_NAME as maxNestedDescribeName } from './rules/max-nested-describe'
@@ -53,13 +53,15 @@ import preferComparisonMatcher, { RULE_NAME as preferComparisonMatcherName } fro
5353
import preferToContain, { RULE_NAME as preferToContainName } from './rules/prefer-to-contain'
5454
import preferExpectAssertions, { RULE_NAME as preferExpectAssertionsName } from './rules/prefer-expect-assertions'
5555

56-
const createConfig = (rules: Record<string, string>) => (
56+
const createConfig = <R extends Linter.RulesRecord>(rules: R) => (
5757
Object.keys(rules).reduce((acc, ruleName) => {
5858
return {
5959
...acc,
6060
[`vitest/${ruleName}`]: rules[ruleName]
6161
}
62-
}, {}))
62+
}, {})) as {
63+
[K in keyof R as `vitest/${Extract<K, string>}`]: R[K]
64+
}
6365

6466
const allRules = {
6567
[lowerCaseTitleName]: 'warn',
@@ -106,7 +108,7 @@ const allRules = {
106108
[preferToContainName]: 'warn',
107109
[preferExpectAssertionsName]: 'warn',
108110
[usePreferTobe]: 'warn'
109-
}
111+
} as const
110112

111113
const recommended = {
112114
[expectedExpect]: 'error',
@@ -117,7 +119,7 @@ const recommended = {
117119
[validDescribeCallbackName]: 'error',
118120
[requireLocalTestContextForConcurrentSnapshotsName]: 'error',
119121
[noImportNodeTestName]: 'error'
120-
}
122+
} as const
121123

122124
const plugin = {
123125
meta: {
@@ -178,7 +180,42 @@ const plugin = {
178180
[preferToContainName]: preferToContain,
179181
[preferExpectAssertionsName]: preferExpectAssertions
180182
},
181-
configs: {},
183+
configs: {
184+
recommended: {
185+
plugins: {
186+
get vitest(): ESLint.Plugin {
187+
return plugin
188+
}
189+
},
190+
rules: createConfig(recommended)
191+
},
192+
all: {
193+
plugins: {
194+
get vitest(): ESLint.Plugin {
195+
return plugin
196+
}
197+
},
198+
rules: createConfig(allRules)
199+
},
200+
env: {
201+
languageOptions: {
202+
globals: {
203+
suite: 'writable',
204+
test: 'writable',
205+
describe: 'writable',
206+
it: 'writable',
207+
expect: 'writable',
208+
assert: 'writable',
209+
vitest: 'writable',
210+
vi: 'writable',
211+
beforeAll: 'writable',
212+
afterAll: 'writable',
213+
beforeEach: 'writable',
214+
afterEach: 'writable'
215+
}
216+
}
217+
}
218+
},
182219
environments: {
183220
env: {
184221
globals: {
@@ -199,44 +236,4 @@ const plugin = {
199236
}
200237
} satisfies ESLint.Plugin
201238

202-
Object.assign(plugin.configs, {
203-
recommended: {
204-
plugins: {
205-
vitest: plugin
206-
},
207-
rules: createConfig(recommended)
208-
}
209-
})
210-
211-
Object.assign(plugin.configs, {
212-
all: {
213-
plugins: {
214-
vitest: plugin
215-
},
216-
rules: createConfig(allRules)
217-
}
218-
})
219-
220-
Object.assign(plugin.configs, {
221-
env: {
222-
languageOptions: {
223-
globals: {
224-
suite: 'writeable',
225-
test: 'writeable',
226-
describe: 'writeable',
227-
it: 'writeable',
228-
expect: 'writeable',
229-
assert: 'writeable',
230-
vitest: 'writeable',
231-
vi: 'writeable',
232-
beforeAll: 'writeable',
233-
afterAll: 'writeable',
234-
beforeEach: 'writeable',
235-
afterEach: 'writeable'
236-
}
237-
238-
}
239-
}
240-
})
241-
242239
export default plugin

0 commit comments

Comments
 (0)