-
-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): eslint-plugin-vitest/no-import-node-test (#4440)
support [eslint-plugin-vitest/no-import-node-test](https://github.com/veritem/eslint-plugin-vitest/blob/main/docs/rules/no-import-node-test.md)
- Loading branch information
Showing
5 changed files
with
104 additions
and
1 deletion.
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
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,79 @@ | ||
use oxc_diagnostics::OxcDiagnostic; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_span::Span; | ||
|
||
use crate::{context::LintContext, rule::Rule}; | ||
|
||
fn no_import_node_test(span0: Span) -> OxcDiagnostic { | ||
OxcDiagnostic::warn("disallow importing `node:test`".to_string()) | ||
.with_help("Import from `vitest` instead of `node:test`") | ||
.with_label(span0) | ||
} | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct NoImportNodeTest; | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// | ||
/// This rule warns when `node:test` is imported (usually accidentally). With `--fix`, it will replace the import with `vitest`. | ||
/// | ||
/// ### Examples | ||
/// | ||
/// ```javascript | ||
/// // invalid | ||
/// import { test } from 'node:test' | ||
/// import { expect } from 'vitest' | ||
/// | ||
/// test('foo', () => { | ||
/// expect(1).toBe(1) | ||
/// }) | ||
/// ``` | ||
/// | ||
/// ```javascript | ||
/// // valid | ||
/// import { test, expect } from 'vitest' | ||
/// | ||
/// test('foo', () => { | ||
/// expect(1).toBe(1) | ||
/// }) | ||
/// ``` | ||
NoImportNodeTest, | ||
style, | ||
); | ||
|
||
impl Rule for NoImportNodeTest { | ||
fn run_once(&self, ctx: &LintContext<'_>) { | ||
let module_record = ctx.module_record(); | ||
|
||
if let Some(node_test_module) = module_record.requested_modules.get("node:test") { | ||
if let Some(requested_module) = node_test_module.first() { | ||
ctx.diagnostic_with_fix(no_import_node_test(requested_module.span()), |fixer| { | ||
fixer.replace(requested_module.span(), "\"vitest\"") | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec![(r#"import { test } from "vitest""#, None)]; | ||
|
||
let fail = vec![ | ||
(r#"import { test } from "node:test""#, None), | ||
("import * as foo from 'node:test'", None), | ||
]; | ||
|
||
let fix = vec![ | ||
(r#"import { test } from "node:test""#, r#"import { test } from "vitest""#, None), | ||
(r#"import * as foo from "node:test""#, r#"import * as foo from "vitest""#, None), | ||
]; | ||
|
||
Tester::new(NoImportNodeTest::NAME, pass, fail) | ||
.with_vitest_plugin(true) | ||
.expect_fix(fix) | ||
.test_and_snapshot(); | ||
} |
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,17 @@ | ||
--- | ||
source: crates/oxc_linter/src/tester.rs | ||
assertion_line: 216 | ||
--- | ||
⚠ eslint-plugin-vitest(no-import-node-test): disallow importing `node:test` | ||
╭─[no_import_node_test.tsx:1:22] | ||
1 │ import { test } from "node:test" | ||
· ─────────── | ||
╰──── | ||
help: Import from `vitest` instead of `node:test` | ||
|
||
⚠ eslint-plugin-vitest(no-import-node-test): disallow importing `node:test` | ||
╭─[no_import_node_test.tsx:1:22] | ||
1 │ import * as foo from 'node:test' | ||
· ─────────── | ||
╰──── | ||
help: Import from `vitest` instead of `node:test` |
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