Skip to content

Commit 9648b95

Browse files
authored
fix: correct and align retrievement of rule options (#785)
* fix(no-focused-tests): apply default options * chore(consistent-test-filename): align retrievement of options * chore(consistent-vitest-vi): align retrievement of options * chore(require-hook): align retrievement of options * chore(consistent-test-it): align retrievement of options * chore(require-top-level-describe): align retrievement of options
1 parent 3492c3b commit 9648b95

9 files changed

+43
-75
lines changed

src/rules/consistent-test-filename.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const defaultTestsPattern = /.*\.(test|spec)\.[tj]sx?$/
88
export default createEslintRule<
99
[
1010
Partial<{
11-
pattern: string
12-
allTestPattern: string
11+
pattern: RegExp | string
12+
allTestPattern: RegExp | string
1313
}>,
1414
],
1515
'consistentTestFilename'
@@ -20,7 +20,7 @@ export default createEslintRule<
2020
docs: {
2121
recommended: false,
2222
requiresTypeChecking: false,
23-
description: 'require .spec test file pattern',
23+
description: 'require test file pattern',
2424
},
2525
messages: {
2626
consistentTestFilename: 'Use test file name pattern {{ pattern }}',
@@ -31,14 +31,12 @@ export default createEslintRule<
3131
additionalProperties: false,
3232
properties: {
3333
pattern: {
34-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
35-
// @ts-expect-error
34+
type: 'string',
3635
format: 'regex',
3736
default: defaultPattern.source,
3837
},
3938
allTestPattern: {
40-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
41-
// @ts-expect-error
39+
type: 'string',
4240
format: 'regex',
4341
default: defaultTestsPattern.source,
4442
},
@@ -48,23 +46,21 @@ export default createEslintRule<
4846
},
4947
defaultOptions: [
5048
{
51-
pattern: defaultTestsPattern.source,
52-
allTestPattern: defaultTestsPattern.source,
49+
pattern: defaultPattern,
50+
allTestPattern: defaultTestsPattern,
5351
},
5452
],
5553

56-
create: (context) => {
57-
const config = context.options[0] ?? {}
58-
const {
59-
pattern: patternRaw = defaultPattern,
60-
allTestPattern: allTestPatternRaw = defaultTestsPattern,
61-
} = config
54+
create: (context, options) => {
55+
const { pattern: patternRaw, allTestPattern: allTestPatternRaw } =
56+
options[0]
57+
6258
const pattern =
63-
typeof patternRaw === 'string' ? new RegExp(patternRaw) : patternRaw
59+
typeof patternRaw === 'string' ? new RegExp(patternRaw) : patternRaw!
6460
const testPattern =
6561
typeof allTestPatternRaw === 'string'
6662
? new RegExp(allTestPatternRaw)
67-
: allTestPatternRaw
63+
: allTestPatternRaw!
6864

6965
const { filename } = context
7066

src/rules/consistent-test-it.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,11 @@ export default createEslintRule<
7979
},
8080
],
8181
},
82-
defaultOptions: [{ fn: TestCaseName.test, withinDescribe: TestCaseName.it }],
83-
create(context) {
84-
const config = context.options[0] ?? {}
85-
const testFnKeyWork = config.fn || TestCaseName.test
86-
const testKeywordWithinDescribe =
87-
config?.withinDescribe || config?.fn || TestCaseName?.it
82+
defaultOptions: [{}],
83+
create(context, options) {
84+
const { fn, withinDescribe } = options[0]
85+
const testFnKeyWork = fn || TestCaseName.test
86+
const testKeywordWithinDescribe = withinDescribe || fn || TestCaseName.it
8887
const testFnDisabled =
8988
testFnKeyWork === testKeywordWithinDescribe ? testFnKeyWork : undefined
9089

src/rules/consistent-vitest-vi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ export default createEslintRule<[Partial<{ fn: UtilName }>], MESSAGE_ID>({
2929
fn: {
3030
type: 'string',
3131
enum: [UtilName.vi, UtilName.vitest],
32+
default: UtilName.vi,
3233
},
3334
},
3435
additionalProperties: false,
3536
},
3637
],
3738
},
3839
defaultOptions: [{ fn: UtilName.vi }],
39-
create(context) {
40-
const config = context.options[0] ?? {}
41-
const utilKeyword = config.fn || UtilName.vi
40+
create(context, options) {
41+
const utilKeyword = options[0].fn!
4242
const oppositeUtilKeyword = getOppositeVitestUtilKeyword(utilKeyword)
4343

4444
return {

src/rules/no-focused-tests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export default createEslintRule<Options, MessageIds>({
4545
},
4646
},
4747
defaultOptions: [{ fixable: true }],
48-
create: (context) => {
49-
const fixable = context.options[0]?.fixable
48+
create: (context, options) => {
49+
const fixable = options[0].fixable!
5050

5151
return {
5252
ExpressionStatement(node) {

src/rules/require-hook.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ export default createEslintRule<Options, MESSAGE_IDS>({
8484
allowedFunctionCalls: [],
8585
},
8686
],
87-
create(context) {
88-
const { allowedFunctionCalls } = context.options[0] ?? {}
89-
87+
create(context, options) {
9088
const checkBlockBody = (body: TSESTree.BlockStatement['body']) => {
9189
for (const statement of body) {
92-
if (shouldBeInHook(statement, context, allowedFunctionCalls)) {
90+
if (
91+
shouldBeInHook(statement, context, options[0].allowedFunctionCalls)
92+
) {
9393
context.report({
9494
node: statement,
9595
messageId: 'useHook',

src/rules/require-top-level-describe.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@ export default createEslintRule<Options, MESSAGE_IDS>({
3131
maxNumberOfTopLevelDescribes: {
3232
type: 'number',
3333
minimum: 1,
34+
default: Infinity,
3435
},
3536
},
3637
additionalProperties: false,
3738
},
3839
],
3940
},
40-
defaultOptions: [{}],
41-
create(context) {
42-
const { maxNumberOfTopLevelDescribes = Infinity } = context.options[0] ?? {}
41+
defaultOptions: [{ maxNumberOfTopLevelDescribes: Infinity }],
42+
create(context, options) {
43+
const maxNumberOfTopLevelDescribes =
44+
options[0].maxNumberOfTopLevelDescribes!
4345

4446
let numberOfTopLevelDescribeBlocks = 0
4547
let numberOfDescribeBlocks = 0

tests/consistent-test-filename.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ ruleTester.run(`file-name`, rule, {
66
{
77
code: 'export {}',
88
filename: '1.test.ts',
9-
options: [{ pattern: String.raw`.*\.test\.ts$` }],
9+
},
10+
{
11+
code: 'export {}',
12+
filename: '1.spec.ts',
13+
options: [{ pattern: String.raw`.*\.spec\.ts$` }],
1014
},
1115
],
1216
invalid: [
1317
{
1418
code: 'export {}',
1519
filename: '1.spec.ts',
1620
errors: [{ messageId: 'consistentTestFilename' }],
17-
options: [{ pattern: String.raw`.*\.test\.ts$` }],
1821
},
1922
{
2023
code: 'export {}',
@@ -23,7 +26,7 @@ ruleTester.run(`file-name`, rule, {
2326
options: [
2427
{
2528
allTestPattern: String.raw`__tests__`,
26-
pattern: String.raw`.*\.test\.ts$`,
29+
pattern: String.raw`.*\.spec\.ts$`,
2730
},
2831
],
2932
},

tests/consistent-vitest-vi.test.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,24 @@ ruleTester.run(RULE_NAME, rule, {
66
valid: [
77
{
88
code: 'import { expect, it } from "vitest";',
9-
options: [{ fn: UtilName.vi }],
109
},
1110
{
1211
code: 'import { vi } from "vitest";',
13-
options: [{ fn: UtilName.vi }],
1412
},
1513
{
1614
code: 'import { vitest } from "vitest";',
1715
options: [{ fn: UtilName.vitest }],
1816
},
1917
{
2018
code: 'import { vi } from "vitest";\nvi.stubEnv("NODE_ENV", "production");',
21-
options: [{ fn: UtilName.vi }],
2219
},
2320
{
2421
code: 'vi.stubEnv("NODE_ENV", "production");',
25-
options: [{ fn: UtilName.vi }],
2622
},
2723
],
2824
invalid: [
2925
{
3026
code: 'import { vitest } from "vitest";',
31-
options: [{ fn: UtilName.vi }],
3227
output: 'import { vi } from "vitest";',
3328
errors: [
3429
{
@@ -45,7 +40,6 @@ ruleTester.run(RULE_NAME, rule, {
4540
},
4641
{
4742
code: 'import { expect, vi, vitest } from "vitest";',
48-
options: [{ fn: UtilName.vi }],
4943
output: 'import { expect, vi } from "vitest";',
5044
errors: [
5145
{
@@ -62,7 +56,6 @@ ruleTester.run(RULE_NAME, rule, {
6256
},
6357
{
6458
code: 'import { vitest } from "vitest";\nvitest.stubEnv("NODE_ENV", "production");',
65-
options: [{ fn: UtilName.vi }],
6659
output:
6760
'import { vi } from "vitest";\nvi.stubEnv("NODE_ENV", "production");',
6861
errors: [

tests/no-focused-tests.test.ts

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ ruleTester.run(RULE_NAME, rule, {
55
valid: ['it("test", () => {});', 'describe("test group", () => {});'],
66
invalid: [
77
{
8+
options: [
9+
{
10+
fixable: false,
11+
},
12+
],
813
code: 'it.only("test", () => {});',
914
errors: [
1015
{
@@ -108,11 +113,6 @@ ruleTester.run(RULE_NAME, rule, {
108113
valid: ['it("test", () => {});', 'describe("test group", () => {});'],
109114
invalid: [
110115
{
111-
options: [
112-
{
113-
fixable: true,
114-
},
115-
],
116116
code: 'it.only("test", () => {});',
117117
errors: [
118118
{
@@ -126,11 +126,6 @@ ruleTester.run(RULE_NAME, rule, {
126126
output: 'it("test", () => {});',
127127
},
128128
{
129-
options: [
130-
{
131-
fixable: true,
132-
},
133-
],
134129
code: 'describe.only("test", () => {});',
135130
errors: [
136131
{
@@ -144,11 +139,6 @@ ruleTester.run(RULE_NAME, rule, {
144139
output: 'describe("test", () => {});',
145140
},
146141
{
147-
options: [
148-
{
149-
fixable: true,
150-
},
151-
],
152142
code: 'test.only("test", () => {});',
153143
errors: [
154144
{
@@ -162,11 +152,6 @@ ruleTester.run(RULE_NAME, rule, {
162152
output: 'test("test", () => {});',
163153
},
164154
{
165-
options: [
166-
{
167-
fixable: true,
168-
},
169-
],
170155
code: 'it.only.each([])("test", () => {});',
171156
errors: [
172157
{
@@ -180,11 +165,6 @@ ruleTester.run(RULE_NAME, rule, {
180165
output: 'it.each([])("test", () => {});',
181166
},
182167
{
183-
options: [
184-
{
185-
fixable: true,
186-
},
187-
],
188168
code: 'test.only.each``("test", () => {});',
189169
errors: [
190170
{
@@ -198,11 +178,6 @@ ruleTester.run(RULE_NAME, rule, {
198178
output: 'test.each``("test", () => {});',
199179
},
200180
{
201-
options: [
202-
{
203-
fixable: true,
204-
},
205-
],
206181
code: 'it.only.each``("test", () => {});',
207182
errors: [
208183
{

0 commit comments

Comments
 (0)