-
-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(biome_js_analyze): noExclusiveTests
- Loading branch information
Showing
13 changed files
with
277 additions
and
78 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
117 changes: 117 additions & 0 deletions
117
crates/biome_js_analyze/src/semantic_analyzers/nursery/no_exclusive_tests.rs
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,117 @@ | ||
use biome_analyze::{ | ||
context::RuleContext, declare_rule, Ast, FixKind, Rule, RuleDiagnostic, RuleSource, | ||
RuleSourceKind, | ||
}; | ||
use biome_console::markup; | ||
use biome_diagnostics::Applicability; | ||
use biome_js_syntax::{JsCallExpression, TextRange}; | ||
use biome_rowan::BatchMutationExt; | ||
|
||
use crate::JsRuleAction; | ||
|
||
declare_rule! { | ||
/// Disallow exclusive tests. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// describe.only("foo", function () {}); | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// it.only("foo", function () {}); | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// test.only("foo", function () {}); | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// ```js | ||
/// test("foo", function () {}); | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// it("foo", function () {}); | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// test("foo", function () {}); | ||
/// ``` | ||
pub NoExclusiveTests { | ||
version: "next", | ||
name: "noExclusiveTests", | ||
recommended: true, | ||
source: RuleSource::EslintJest("no-exclusive-tests"), | ||
source_kind: RuleSourceKind::Inspired, | ||
fix_kind: FixKind::Unsafe, | ||
} | ||
} | ||
|
||
const FUNCTION_NAMES: [&str; 1] = ["only"]; | ||
const CALEE_NAMES: [&str; 3] = ["describe", "it", "test"]; | ||
|
||
impl Rule for NoExclusiveTests { | ||
type Query = Ast<JsCallExpression>; | ||
type State = TextRange; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
let callee = node.callee().ok()?; | ||
|
||
if node.is_test_call_expression().ok()? { | ||
if callee.contains_a_test_pattern().ok()? { | ||
let function_name = callee.get_callee_member_name()?; | ||
let callee_name = callee.get_callee_member_name()?; | ||
|
||
if FUNCTION_NAMES.contains(&function_name.text_trimmed()) | ||
&& CALEE_NAMES.contains(&callee_name.text_trimmed()) | ||
{ | ||
return Some(function_name.text_trimmed_range()); | ||
} | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
fn diagnostic(_: &RuleContext<Self>, range: &Self::State) -> Option<RuleDiagnostic> { | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
range, | ||
markup! { | ||
"Don't exclusive the test." | ||
}, | ||
) | ||
.note("The '.only' method is often used for debugging or during implementation. It should be removed before deploying to production.") | ||
.note("Consider removing '.only' to ensure all tests are executed.") | ||
) | ||
} | ||
|
||
fn action(ctx: &RuleContext<Self>, _: &Self::State) -> Option<JsRuleAction> { | ||
let node = ctx.query(); | ||
let callee = node.callee().ok()?; | ||
let mut mutation = ctx.root().begin(); | ||
|
||
if let Some(expression) = callee.as_js_static_member_expression() { | ||
let member_name = expression.member().ok()?; | ||
let operator_token = expression.operator_token().ok()?; | ||
|
||
mutation.remove_element(member_name.into()); | ||
mutation.remove_element(operator_token.into()); | ||
} | ||
|
||
Some(JsRuleAction { | ||
category: biome_analyze::ActionCategory::QuickFix, | ||
applicability: Applicability::MaybeIncorrect, | ||
message: markup! { "Remove the '.only' method to ensure all tests are executed." } | ||
.to_owned(), | ||
mutation, | ||
}) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
crates/biome_js_analyze/tests/specs/nursery/noExclusiveTests/invalid.js
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,8 @@ | ||
describe.only("bar", function () {}); | ||
it.only("bar", function () {}); | ||
test.only("bar", function () {}); | ||
|
||
describe.only("bar", () => {}); | ||
it.only("bar", () => {}); | ||
test.only("bar", () => {}); | ||
|
18 changes: 18 additions & 0 deletions
18
crates/biome_js_analyze/tests/specs/nursery/noExclusiveTests/invalid.js.snap
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,18 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```jsx | ||
describe.only("bar", function () {}); | ||
it.only("bar", function () {}); | ||
test.only("bar", function () {}); | ||
|
||
describe.only("bar", () => {}); | ||
it.only("bar", () => {}); | ||
test.only("bar", () => {}); | ||
|
||
|
||
``` | ||
|
||
|
4 changes: 4 additions & 0 deletions
4
crates/biome_js_analyze/tests/specs/nursery/noExclusiveTests/valid.js
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,4 @@ | ||
describe("bar", function () {}); | ||
it("bar", function () {}); | ||
suite("bar", function () {}); | ||
test("bar", function () {}); |
14 changes: 14 additions & 0 deletions
14
crates/biome_js_analyze/tests/specs/nursery/noExclusiveTests/valid.js.snap
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,14 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: valid.js | ||
--- | ||
# Input | ||
```jsx | ||
describe("bar", function () {}); | ||
it("bar", function () {}); | ||
suite("bar", function () {}); | ||
test("bar", function () {}); | ||
|
||
``` | ||
|
||
|
Oops, something went wrong.