Skip to content

Commit

Permalink
feat(biome_js_analyze): update the methods of noFocusedTests (#1999)
Browse files Browse the repository at this point in the history
  • Loading branch information
chansuke authored Mar 11, 2024
1 parent 94de621 commit ffc349c
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 103 deletions.
82 changes: 58 additions & 24 deletions crates/biome_js_analyze/src/analyzers/nursery/no_focused_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::JsRuleAction;
use biome_analyze::{
context::RuleContext, declare_rule, Ast, FixKind, Rule, RuleDiagnostic, RuleSource,
RuleSourceKind,
Expand All @@ -6,9 +7,7 @@ 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 @@ -44,6 +43,7 @@ declare_rule! {
}

const FUNCTION_NAMES: [&str; 3] = ["only", "fdescribe", "fit"];
const CALEE_NAMES: [&str; 3] = ["describe", "it", "test"];

impl Rule for NoFocusedTests {
type Query = Ast<JsCallExpression>;
Expand All @@ -53,6 +53,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 +64,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,36 +101,46 @@ 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(function_name) = callee.get_callee_member_name() {
let replaced_function;
match function_name.text_trimmed() {
"only" => {
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());
}
}
"fdescribe" => {
replaced_function = make::js_reference_identifier(make::ident("describe"));
mutation.replace_element(function_name.into(), replaced_function.into());
}
"fit" => {
replaced_function = make::js_reference_identifier(make::ident("it"));
mutation.replace_element(function_name.into(), replaced_function.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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
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 () {});

fdescribe("foo", () => {});
fit("foo", () => {});
Loading

0 comments on commit ffc349c

Please sign in to comment.