Skip to content

Commit

Permalink
feat(biome_js_analyze): noExclusiveTests
Browse files Browse the repository at this point in the history
  • Loading branch information
chansuke committed Mar 5, 2024
1 parent b95f83e commit 45f6c05
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 78 deletions.
1 change: 1 addition & 0 deletions crates/biome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ define_categories! {
"lint/nursery/noEmptyBlockStatements": "https://biomejs.dev/linter/rules/no-empty-block-statements",
"lint/nursery/noEmptyTypeParameters": "https://biomejs.dev/linter/rules/no-empty-type-parameters",
"lint/nursery/noExcessiveNestedTestSuites": "https://biomejs.dev/linter/rules/no-excessive-nested-test-suites",
"lint/nursery/noExclusiveTests": "https://biomejs.dev/linter/rules/no-exclusive-tests",
"lint/nursery/noExportsInTest": "https://biomejs.dev/linter/rules/no-exports-in-test",
"lint/nursery/noFocusedTests": "https://biomejs.dev/linter/rules/no-focused-tests",
"lint/nursery/noGlobalAssign": "https://biomejs.dev/linter/rules/no-global-assign",
Expand Down
1 change: 1 addition & 0 deletions crates/biome_js_analyze/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub type NoEmptyPattern =
pub type NoEmptyTypeParameters = < analyzers :: nursery :: no_empty_type_parameters :: NoEmptyTypeParameters as biome_analyze :: Rule > :: Options ;
pub type NoExcessiveCognitiveComplexity = < analyzers :: complexity :: no_excessive_cognitive_complexity :: NoExcessiveCognitiveComplexity as biome_analyze :: Rule > :: Options ;
pub type NoExcessiveNestedTestSuites = < analyzers :: nursery :: no_excessive_nested_test_suites :: NoExcessiveNestedTestSuites as biome_analyze :: Rule > :: Options ;
pub type NoExclusiveTests = < semantic_analyzers :: nursery :: no_exclusive_tests :: NoExclusiveTests as biome_analyze :: Rule > :: Options ;
pub type NoExplicitAny =
<analyzers::suspicious::no_explicit_any::NoExplicitAny as biome_analyze::Rule>::Options;
pub type NoExportsInTest =
Expand Down
2 changes: 2 additions & 0 deletions crates/biome_js_analyze/src/semantic_analyzers/nursery.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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,
})
}
}
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", () => {});

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", () => {});


```


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 () {});
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 () {});

```


Loading

0 comments on commit 45f6c05

Please sign in to comment.