Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(biome_js_analyze): noExclusiveTests #1965

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ console.log('file2');
# Emitted Messages

```block
Checked 0 file(s) in <TIME>
Checked 0 files in <TIME>. No fixes needed.
```


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
2 changes: 1 addition & 1 deletion crates/biome_fs/src/fs/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Default for MemoryFileSystem {
errors: Default::default(),
allow_write: true,
on_get_changed_files: Some(Arc::new(AssertUnwindSafe(Mutex::new(Some(Box::new(
|| vec![],
Vec::new,
)))))),
}
}
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,149 @@
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::{AstNode, BatchMutationExt, NodeOrToken};

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"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no-exclusive-test doesn't exist in eslint-plugin-jest. It's the rule of the eslint-plugin-mocha.

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>;
Copy link
Contributor

@togami2864 togami2864 Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 callee_object = callee.get_callee_object_name()?;
let callee_member = callee.get_callee_member_name()?;

if FUNCTION_NAMES.contains(&callee_member.text_trimmed())
&& CALEE_NAMES.contains(&callee_object.text_trimmed())
{
return Some(callee_member.text_trimmed_range());
}
}
} else if let Some(expression) = callee.as_js_computed_member_expression() {
let value_token = expression
.as_fields()
.object
.ok()?
.as_js_identifier_expression()?
.name()
.ok()?
.value_token()
.ok()?;

if expression.l_brack_token().is_ok()
&& expression.r_brack_token().is_ok()
&& CALEE_NAMES.contains(&value_token.text_trimmed())
{
if let Some(literal) = expression.member().ok()?.as_any_js_literal_expression() {
if literal.as_js_string_literal_expression().is_some()
&& literal.to_string() == "\"only\""
{
return Some(expression.syntax().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());
} else if let Some(expression) = callee.as_js_computed_member_expression() {
let l_brack = expression.l_brack_token().ok()?;
let r_brack = expression.r_brack_token().ok()?;
let member = expression.member().ok()?;
let expression = member.as_any_js_literal_expression()?;

mutation.remove_element(NodeOrToken::Token(l_brack));
mutation.remove_element(NodeOrToken::Node(expression.syntax().clone()));
mutation.remove_element(NodeOrToken::Token(r_brack));
}

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,11 @@
describe.only("bar", function () {});
it.only("bar", function () {});
test.only("bar", function () {});

describe.only("bar", () => {});
it.only("bar", () => {});
test.only("bar", () => {});

describe["only"]("bar", function () {});
it["only"]("bar", function () {});
test["only"]("bar", function () {});
Comment on lines +1 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also handle fdescribe, fit and ftest.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@togami2864
Thank you for your review!! I will address feedbacks!

Loading
Loading