Skip to content

Commit

Permalink
fix: restore fdescribe and fit support
Browse files Browse the repository at this point in the history
  • Loading branch information
chansuke committed Mar 10, 2024
1 parent 0986acd commit 6abe775
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 41 deletions.
52 changes: 33 additions & 19 deletions crates/biome_js_analyze/src/analyzers/nursery/no_focused_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use biome_analyze::{
};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_factory::make;
use biome_js_syntax::{JsCallExpression, TextRange};
use biome_rowan::{AstNode, BatchMutationExt, NodeOrToken};

Expand Down Expand Up @@ -41,7 +42,7 @@ declare_rule! {
}
}

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

impl Rule for NoFocusedTests {
Expand Down Expand Up @@ -108,30 +109,43 @@ impl Rule for NoFocusedTests {
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()?;
let function_name = callee.get_callee_member_name()?;
let replaced_function;

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()?;
let mut mutation = ctx.root().begin();

mutation.remove_token(l_brack);
mutation.remove_element(NodeOrToken::Node(expression.syntax().clone()));
mutation.remove_token(r_brack);
}
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());
} 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);
}
}
"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());
}
_ => {}
};

Some(JsRuleAction {
category: biome_analyze::ActionCategory::QuickFix,
applicability: Applicability::MaybeIncorrect,
message: markup! { "Remove the 'only' method to ensure all tests are executed." }
.to_owned(),
message: markup! { "Remove focus from test." }.to_owned(),
mutation,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ test.only("bar", () => {});
describe["only"]("bar", function () {});
it["only"]("bar", function () {});
test["only"]("bar", function () {});

fdescribe("foo", () => {});
fit("foo", () => {});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ describe["only"]("bar", function () {});
it["only"]("bar", function () {});
test["only"]("bar", function () {});

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

```

# Diagnostics
Expand All @@ -33,7 +36,7 @@ invalid.js:1:10 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━
i Consider removing 'only' to ensure all tests are executed.
i Unsafe fix: Remove the 'only' method to ensure all tests are executed.
i Unsafe fix: Remove focus from test.
1 │ - describe.only("bar",·function·()·{});
1 │ + describe("bar",·function·()·{});
Expand All @@ -58,7 +61,7 @@ invalid.js:2:4 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━
i Consider removing 'only' to ensure all tests are executed.
i Unsafe fix: Remove the 'only' method to ensure all tests are executed.
i Unsafe fix: Remove focus from test.
1 1 │ describe.only("bar", function () {});
2 │ - it.only("bar",·function·()·{});
Expand All @@ -85,7 +88,7 @@ invalid.js:3:6 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━
i Consider removing 'only' to ensure all tests are executed.
i Unsafe fix: Remove the 'only' method to ensure all tests are executed.
i Unsafe fix: Remove focus from test.
1 1 │ describe.only("bar", function () {});
2 2 │ it.only("bar", function () {});
Expand Down Expand Up @@ -113,7 +116,7 @@ invalid.js:5:10 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━
i Consider removing 'only' to ensure all tests are executed.
i Unsafe fix: Remove the 'only' method to ensure all tests are executed.
i Unsafe fix: Remove focus from test.
3 3 │ test.only("bar", function () {});
4 4 │
Expand All @@ -140,7 +143,7 @@ invalid.js:6:4 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━
i Consider removing 'only' to ensure all tests are executed.
i Unsafe fix: Remove the 'only' method to ensure all tests are executed.
i Unsafe fix: Remove focus from test.
4 4 │
5 5 │ describe.only("bar", () => {});
Expand Down Expand Up @@ -168,7 +171,7 @@ invalid.js:7:6 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━
i Consider removing 'only' to ensure all tests are executed.
i Unsafe fix: Remove the 'only' method to ensure all tests are executed.
i Unsafe fix: Remove focus from test.
5 5 │ describe.only("bar", () => {});
6 6 │ it.only("bar", () => {});
Expand All @@ -181,7 +184,7 @@ invalid.js:7:6 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━
```

```
invalid.js:9:1 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid.js:9:1 lint/nursery/noFocusedTests ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Don't focus the test.
Expand All @@ -196,15 +199,11 @@ invalid.js:9:1 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━
i Consider removing 'only' to ensure all tests are executed.
i Unsafe fix: Remove the 'only' method to ensure all tests are executed.
9 │ describe["only"]("bar",·function·()·{});
│ --------
```

```
invalid.js:10:1 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid.js:10:1 lint/nursery/noFocusedTests ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Don't focus the test.
Expand All @@ -218,15 +217,11 @@ invalid.js:10:1 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━
i Consider removing 'only' to ensure all tests are executed.
i Unsafe fix: Remove the 'only' method to ensure all tests are executed.
10 │ it["only"]("bar",·function·()·{});
│ --------
```

```
invalid.js:11:1 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid.js:11:1 lint/nursery/noFocusedTests ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Don't focus the test.
Expand All @@ -235,15 +230,65 @@ invalid.js:11:1 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━
> 11 │ test["only"]("bar", function () {});
│ ^^^^^^^^^^^^
12 │
13 │ fdescribe("foo", () => {});
i The 'only' method is often used for debugging or during implementation. It should be removed before deploying to production.
i Consider removing 'only' to ensure all tests are executed.
```

```
invalid.js:13:1 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Don't focus the test.
11 │ test["only"]("bar", function () {});
12 │
> 13 │ fdescribe("foo", () => {});
│ ^^^^^^^^^
14 │ fit("foo", () => {});
15 │
i The 'only' method is often used for debugging or during implementation. It should be removed before deploying to production.
i Consider removing 'only' to ensure all tests are executed.
i Unsafe fix: Remove focus from test.
11 11 │ test["only"]("bar", function () {});
12 12 │
13 │ - fdescribe("foo",·()·=>·{});
13 │ + describe("foo",·()·=>·{});
14 14 │ fit("foo", () => {});
15 15 │
```

```
invalid.js:14:1 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Don't focus the test.
13 │ fdescribe("foo", () => {});
> 14 │ fit("foo", () => {});
│ ^^^
15 │
i The 'only' method is often used for debugging or during implementation. It should be removed before deploying to production.
i Consider removing 'only' to ensure all tests are executed.
i Unsafe fix: Remove the 'only' method to ensure all tests are executed.
i Unsafe fix: Remove focus from test.
12 12 │
13 13 │ fdescribe("foo", () => {});
14 │ - fit("foo",·()·=>·{});
14 │ + it("foo",·()·=>·{});
15 15 │
11 │ test["only"]("bar",·function·()·{});
│ --------
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
describe.skip("test", () => {});
it.skip("test", () => {});
test.skip("test", () => {});
xdescribe("test", () => {});
xit("test", () => {})
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ expression: valid.js
describe.skip("test", () => {});
it.skip("test", () => {});
test.skip("test", () => {});
xdescribe("test", () => {});
xit("test", () => {})

```

Expand Down
4 changes: 2 additions & 2 deletions website/src/content/docs/linter/rules/no-focused-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe.only("foo", () => {});

<strong><span style="color: lightgreen;"> </span></strong><strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider removing 'only' to ensure all tests are executed.</span>

<strong><span style="color: lightgreen;"> </span></strong><strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Remove the 'only' method to ensure all tests are executed.</span>
<strong><span style="color: lightgreen;"> </span></strong><strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Remove focus from test.</span>

<strong>1</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;"><strong>d</strong></span><span style="color: Tomato;"><strong>e</strong></span><span style="color: Tomato;"><strong>s</strong></span><span style="color: Tomato;"><strong>c</strong></span><span style="color: Tomato;"><strong>r</strong></span><span style="color: Tomato;"><strong>i</strong></span><span style="color: Tomato;"><strong>b</strong></span><span style="color: Tomato;"><strong>e</strong></span><span style="color: Tomato;"><strong>.</strong></span><span style="color: Tomato;"><strong>o</strong></span><span style="color: Tomato;"><strong>n</strong></span><span style="color: Tomato;"><strong>l</strong></span><span style="color: Tomato;"><strong>y</strong></span><span style="color: Tomato;">(</span><span style="color: Tomato;">&quot;</span><span style="color: Tomato;">f</span><span style="color: Tomato;">o</span><span style="color: Tomato;">o</span><span style="color: Tomato;">&quot;</span><span style="color: Tomato;">,</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">(</span><span style="color: Tomato;">)</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">=</span><span style="color: Tomato;">&gt;</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">{</span><span style="color: Tomato;">}</span><span style="color: Tomato;">)</span><span style="color: Tomato;">;</span>
<strong>1</strong><strong> │ </strong><span style="color: MediumSeaGreen;">+</span> <span style="color: MediumSeaGreen;"><strong>d</strong></span><span style="color: MediumSeaGreen;"><strong>e</strong></span><span style="color: MediumSeaGreen;"><strong>s</strong></span><span style="color: MediumSeaGreen;"><strong>c</strong></span><span style="color: MediumSeaGreen;"><strong>r</strong></span><span style="color: MediumSeaGreen;"><strong>i</strong></span><span style="color: MediumSeaGreen;"><strong>b</strong></span><span style="color: MediumSeaGreen;"><strong>e</strong></span><span style="color: MediumSeaGreen;">(</span><span style="color: MediumSeaGreen;">&quot;</span><span style="color: MediumSeaGreen;">f</span><span style="color: MediumSeaGreen;">o</span><span style="color: MediumSeaGreen;">o</span><span style="color: MediumSeaGreen;">&quot;</span><span style="color: MediumSeaGreen;">,</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">(</span><span style="color: MediumSeaGreen;">)</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">=</span><span style="color: MediumSeaGreen;">&gt;</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">{</span><span style="color: MediumSeaGreen;">}</span><span style="color: MediumSeaGreen;">)</span><span style="color: MediumSeaGreen;">;</span>
Expand All @@ -64,7 +64,7 @@ test.only("foo", () => {});

<strong><span style="color: lightgreen;"> </span></strong><strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider removing 'only' to ensure all tests are executed.</span>

<strong><span style="color: lightgreen;"> </span></strong><strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Remove the 'only' method to ensure all tests are executed.</span>
<strong><span style="color: lightgreen;"> </span></strong><strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Remove focus from test.</span>

<strong>1</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;"><strong>t</strong></span><span style="color: Tomato;"><strong>e</strong></span><span style="color: Tomato;"><strong>s</strong></span><span style="color: Tomato;"><strong>t</strong></span><span style="color: Tomato;"><strong>.</strong></span><span style="color: Tomato;"><strong>o</strong></span><span style="color: Tomato;"><strong>n</strong></span><span style="color: Tomato;"><strong>l</strong></span><span style="color: Tomato;"><strong>y</strong></span><span style="color: Tomato;">(</span><span style="color: Tomato;">&quot;</span><span style="color: Tomato;">f</span><span style="color: Tomato;">o</span><span style="color: Tomato;">o</span><span style="color: Tomato;">&quot;</span><span style="color: Tomato;">,</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">(</span><span style="color: Tomato;">)</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">=</span><span style="color: Tomato;">&gt;</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">{</span><span style="color: Tomato;">}</span><span style="color: Tomato;">)</span><span style="color: Tomato;">;</span>
<strong>1</strong><strong> │ </strong><span style="color: MediumSeaGreen;">+</span> <span style="color: MediumSeaGreen;"><strong>t</strong></span><span style="color: MediumSeaGreen;"><strong>e</strong></span><span style="color: MediumSeaGreen;"><strong>s</strong></span><span style="color: MediumSeaGreen;"><strong>t</strong></span><span style="color: MediumSeaGreen;">(</span><span style="color: MediumSeaGreen;">&quot;</span><span style="color: MediumSeaGreen;">f</span><span style="color: MediumSeaGreen;">o</span><span style="color: MediumSeaGreen;">o</span><span style="color: MediumSeaGreen;">&quot;</span><span style="color: MediumSeaGreen;">,</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">(</span><span style="color: MediumSeaGreen;">)</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">=</span><span style="color: MediumSeaGreen;">&gt;</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">{</span><span style="color: MediumSeaGreen;">}</span><span style="color: MediumSeaGreen;">)</span><span style="color: MediumSeaGreen;">;</span>
Expand Down

0 comments on commit 6abe775

Please sign in to comment.