Skip to content

Commit 3046a82

Browse files
nunomarksveritem
andauthored
feat: Option to disable autofix of prefer-import-in-mock (#846)
* feat: Option to disable autofix of prefer-import-in-mock * chore: apply formatting and update docs --------- Co-authored-by: Verite Mugabo <mugaboverite@gmail.com>
1 parent 2f31996 commit 3046a82

File tree

3 files changed

+101
-4
lines changed

3 files changed

+101
-4
lines changed

docs/rules/prefer-import-in-mock.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,25 @@ The following pattern is not considered a warning:
2121
```js
2222
vi.mock(import('./path/to/module'))
2323
```
24+
25+
### Options
26+
27+
This rule has a `fixable` option that tells the plugin to automatically fix the imports for you.
28+
The option is enabled by default. You can disable it in your `eslint.config.js` file using the following configuration.
29+
30+
```ts
31+
import vitest from 'eslint-plugin-vitest'
32+
33+
export default [
34+
{
35+
files: ['**/*.ts', '**/*.js'], // or any other pattern
36+
plugins: {
37+
vitest,
38+
},
39+
rules: {
40+
...vitest.configs.recommended.all,
41+
'vitest/prefer-import-in-mock': ['error', { fixable: false }],
42+
},
43+
},
44+
]
45+
```

src/rules/prefer-import-in-mock.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import { parseVitestFnCall } from '../utils/parse-vitest-fn-call'
55
export const RULE_NAME = 'prefer-import-in-mock'
66

77
type MESSAGE_ID = 'preferImport'
8+
type Options = [
9+
Partial<{
10+
fixable: boolean
11+
}>,
12+
]
813

9-
export default createEslintRule<[], MESSAGE_ID>({
14+
export default createEslintRule<Options, MESSAGE_ID>({
1015
name: RULE_NAME,
1116
meta: {
1217
fixable: 'code',
@@ -17,10 +22,23 @@ export default createEslintRule<[], MESSAGE_ID>({
1722
messages: {
1823
preferImport: "Replace '{{path}}' with import('{{path}}')",
1924
},
20-
schema: [],
25+
schema: [
26+
{
27+
type: 'object',
28+
properties: {
29+
fixable: {
30+
type: 'boolean',
31+
default: true,
32+
},
33+
},
34+
additionalProperties: false,
35+
},
36+
],
2137
},
22-
defaultOptions: [],
23-
create(context) {
38+
defaultOptions: [{ fixable: true }],
39+
create(context, options) {
40+
const fixable = options[0].fixable!
41+
2442
return {
2543
CallExpression(node) {
2644
// Only consider vi.mock() calls
@@ -50,6 +68,8 @@ export default createEslintRule<[], MESSAGE_ID>({
5068
},
5169
node: node,
5270
fix(fixer) {
71+
if (!fixable) return null
72+
5373
return fixer.replaceText(pathArg, `import('${pathArg.value}')`)
5474
},
5575
})

tests/prefer-import-in-mock.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,61 @@ import rule, { RULE_NAME } from '../src/rules/prefer-import-in-mock'
22
import { ruleTester } from './ruleTester'
33

44
describe(RULE_NAME, () => {
5+
6+
ruleTester.run(RULE_NAME, rule, {
7+
valid: [
8+
'vi.mock(import("foo"))',
9+
'vi.mock(import("node:fs/promises"))',
10+
'vi.mock(import("./foo.js"), () => ({ Foo: vi.fn() }))',
11+
'vi.mock(import("./foo.js"), { spy: true });',
12+
],
13+
invalid: [
14+
{
15+
options: [
16+
{
17+
fixable: false,
18+
},
19+
],
20+
code: `vi.mock('foo', () => {})`,
21+
errors: [
22+
{
23+
messageId: 'preferImport',
24+
},
25+
],
26+
},
27+
{
28+
options: [
29+
{
30+
fixable: false,
31+
},
32+
],
33+
code: 'vi.mock("node:fs/promises")',
34+
errors: [{ messageId: 'preferImport', column: 1, line: 1 }],
35+
},
36+
{
37+
options: [
38+
{
39+
fixable: false,
40+
},
41+
],
42+
code: 'vi.mock("./foo.js", () => ({ Foo: vi.fn() }))',
43+
errors: [{ messageId: 'preferImport', column: 1, line: 1 }],
44+
},
45+
{
46+
options: [
47+
{
48+
fixable: false,
49+
},
50+
],
51+
code: `
52+
import { vi as renamedVi } from 'vitest';
53+
renamedVi.mock('./foo.js', () => ({ Foo: vi.fn() }))
54+
`,
55+
errors: [{ messageId: 'preferImport', column: 9, line: 3 }],
56+
},
57+
],
58+
})
59+
560
ruleTester.run(RULE_NAME, rule, {
661
valid: [
762
'vi.mock(import("foo"))',

0 commit comments

Comments
 (0)