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): update the methods of noFocusedTests #1999

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
77 changes: 48 additions & 29 deletions crates/biome_js_analyze/src/analyzers/nursery/no_focused_tests.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use crate::JsRuleAction;
use biome_analyze::{
context::RuleContext, declare_rule, Ast, FixKind, Rule, RuleDiagnostic, RuleSource,
RuleSourceKind,
};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_factory::make;
use biome_js_syntax::{JsCallExpression, TextRange};
use biome_rowan::BatchMutationExt;

use crate::JsRuleAction;
use biome_rowan::{AstNode, BatchMutationExt, NodeOrToken};

declare_rule! {
/// Disallow focused tests.
Expand Down Expand Up @@ -43,7 +41,8 @@ declare_rule! {
}
}

const FUNCTION_NAMES: [&str; 3] = ["only", "fdescribe", "fit"];
Conaclos marked this conversation as resolved.
Show resolved Hide resolved
const FUNCTION_NAMES: [&str; 1] = ["only"];
const CALEE_NAMES: [&str; 3] = ["describe", "it", "test"];

impl Rule for NoFocusedTests {
type Query = Ast<JsCallExpression>;
Expand All @@ -53,6 +52,7 @@ impl Rule for NoFocusedTests {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
let callee = node.callee().ok()?;

if node.is_test_call_expression().ok()? {
let callee = node.callee().ok()?;
Expand All @@ -63,6 +63,29 @@ impl Rule for NoFocusedTests {
return Some(function_name.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
Expand All @@ -77,42 +100,38 @@ impl Rule for NoFocusedTests {
"Don't focus the test."
},
)
.note("This is likely a change done during debugging or implementation phases, but it's unlikely what you want in production.")
.note("Remove it.")
.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 function_name = callee.get_callee_member_name()?;
let replaced_function;

let mut mutation = ctx.root().begin();

match function_name.text_trimmed() {
"only" => {
let member = callee.as_js_static_member_expression()?;
let member_name = member.member().ok()?;
let operator_token = member.operator_token().ok()?;
mutation.remove_element(member_name.into());
mutation.remove_element(operator_token.into());
}
"fit" => {
replaced_function = make::js_reference_identifier(make::ident("it"));
mutation.replace_element(function_name.into(), replaced_function.into());
}
"fdescribe" => {
replaced_function = make::js_reference_identifier(make::ident("describe"));
mutation.replace_element(function_name.into(), replaced_function.into());
}
_ => {}
};
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_token(l_brack);
mutation.remove_element(NodeOrToken::Node(expression.syntax().clone()));
mutation.remove_token(r_brack);
}

Some(JsRuleAction {
category: biome_analyze::ActionCategory::QuickFix,
applicability: Applicability::MaybeIncorrect,
message: markup! { "Remove focus from test." }.to_owned(),
message: markup! { "Remove the 'only' method to ensure all tests are executed." }
.to_owned(),
mutation,
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
describe.only("test", () => {});
it.only("test", () => {});
test.only("test", () => {});
fdescribe('foo', () => {});
fit('foo', () => {});
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 () {});
Loading
Loading