-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new rule
no-import-node-test
(#317)
* feat: new rule `no-import-node-test` * chore: docs * chore: update
- Loading branch information
Showing
5 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Disallow importing `node:test` (`vitest/no-import-node-test`) | ||
|
||
⚠️ This rule _warns_ in the 🌐 `all` config. | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
## Rule Details | ||
|
||
This rule warns when `node:test` is imported (usually accidentally). With `--fix`, it will replace the import with `vitest`. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
import { test } from 'node:test' | ||
import { expect } from 'vitest' | ||
|
||
test('foo', () => { | ||
expect(1).toBe(1) | ||
}) | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
import { test, expect } from 'vitest' | ||
|
||
test('foo', () => { | ||
expect(1).toBe(1) | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { createEslintRule } from '../utils' | ||
|
||
export const RULE_NAME = 'no-import-node-test' | ||
export type MESSAGE_IDS = 'noImportNodeTest' | ||
export type Options = [] | ||
|
||
export default createEslintRule<Options, MESSAGE_IDS>({ | ||
name: RULE_NAME, | ||
meta: { | ||
docs: { | ||
description: 'Disallow importing `node:test`', | ||
recommended: 'warn' | ||
}, | ||
type: 'suggestion', | ||
messages: { | ||
noImportNodeTest: 'Import from `vitest` instead of `node:test`' | ||
}, | ||
fixable: 'code', | ||
schema: [] | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
ImportDeclaration(node) { | ||
if (node.source.value === 'node:test') { | ||
context.report({ | ||
messageId: 'noImportNodeTest', | ||
node, | ||
fix: fixer => fixer.replaceText( | ||
node.source, | ||
node.source.raw.replace('node:test', 'vitest') | ||
) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import rule, { RULE_NAME } from '../src/rules/no-import-node-test' | ||
import { ruleTester } from './ruleTester' | ||
|
||
ruleTester.run(RULE_NAME, rule, { | ||
valid: [ | ||
'import { test } from "vitest"' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'import { test } from "node:test"', | ||
output: 'import { test } from "vitest"', | ||
errors: [{ messageId: 'noImportNodeTest' }] | ||
}, | ||
{ | ||
code: 'import * as foo from \'node:test\'', | ||
output: 'import * as foo from \'vitest\'', | ||
errors: [{ messageId: 'noImportNodeTest' }] | ||
} | ||
] | ||
}) |