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

refactor: noFocusedTests code fix #1864

Merged
merged 1 commit into from
Feb 19, 2024
Merged
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
@@ -1,8 +1,14 @@
use biome_analyze::{
context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource, RuleSourceKind,
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::{AnyJsExpression, JsCallExpression, JsSyntaxToken, TextRange};
use biome_rowan::BatchMutationExt;

use crate::JsRuleAction;

declare_rule! {
/// Disallow focused tests.
Expand All @@ -22,12 +28,18 @@ declare_rule! {
/// ```js,expect_diagnostic
/// test.only("foo", () => {});
/// ```
///
/// ### Valid
/// ```js
/// test("foo", () => {});
/// ```
pub(crate) NoFocusedTests {
version: "next",
name: "noFocusedTests",
recommended: true,
source: RuleSource::EslintJest("no-focused-tests"),
source_kind: RuleSourceKind::Inspired,
fix_kind: FixKind::Unsafe,
}
}

Expand Down Expand Up @@ -69,6 +81,42 @@ impl Rule for NoFocusedTests {
.note("Remove it.")
)
}

fn action(ctx: &RuleContext<Self>, _: &Self::State) -> Option<JsRuleAction> {
let node = ctx.query();
let callee = node.callee().ok()?;
let function_name = get_function_name(&callee)?;
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()?;
// let member = member.as_js_name()?;
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());
}
_ => {}
};

Some(JsRuleAction {
category: biome_analyze::ActionCategory::QuickFix,
applicability: Applicability::MaybeIncorrect,
message: markup! { "Remove focus from test." }.to_owned(),
mutation,
})
}
}

fn get_function_name(callee: &AnyJsExpression) -> Option<JsSyntaxToken> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fit('foo', () => {});

# Diagnostics
```
invalid.js:1:10 lint/nursery/noFocusedTests ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid.js:1:10 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Don't focus the test.

Expand All @@ -27,11 +27,18 @@ invalid.js:1:10 lint/nursery/noFocusedTests ━━━━━━━━━━━━

i Remove it.

i Unsafe fix: Remove focus from test.

1 │ - describe.only("test",·()·=>·{});
1 │ + describe("test",·()·=>·{});
2 2 │ it.only("test", () => {});
3 3 │ test.only("test", () => {});


```

```
invalid.js:2:4 lint/nursery/noFocusedTests ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid.js:2:4 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Don't focus the test.

Expand All @@ -45,11 +52,19 @@ invalid.js:2:4 lint/nursery/noFocusedTests ━━━━━━━━━━━━

i Remove it.

i Unsafe fix: Remove focus from test.

1 1 │ describe.only("test", () => {});
2 │ - it.only("test",·()·=>·{});
2 │ + it("test",·()·=>·{});
3 3 │ test.only("test", () => {});
4 4 │ fdescribe('foo', () => {});


```

```
invalid.js:3:6 lint/nursery/noFocusedTests ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid.js:3:6 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Don't focus the test.

Expand All @@ -64,11 +79,20 @@ invalid.js:3:6 lint/nursery/noFocusedTests ━━━━━━━━━━━━

i Remove it.

i Unsafe fix: Remove focus from test.

1 1 │ describe.only("test", () => {});
2 2 │ it.only("test", () => {});
3 │ - test.only("test",·()·=>·{});
3 │ + test("test",·()·=>·{});
4 4 │ fdescribe('foo', () => {});
5 5 │ fit('foo', () => {});


```

```
invalid.js:4:1 lint/nursery/noFocusedTests ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid.js:4:1 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Don't focus the test.

Expand All @@ -83,11 +107,20 @@ invalid.js:4:1 lint/nursery/noFocusedTests ━━━━━━━━━━━━

i Remove it.

i Unsafe fix: Remove focus from test.

2 2 │ it.only("test", () => {});
3 3 │ test.only("test", () => {});
4 │ - fdescribe('foo',·()·=>·{});
4 │ + describe('foo',·()·=>·{});
5 5 │ fit('foo', () => {});
6 6 │


```

```
invalid.js:5:1 lint/nursery/noFocusedTests ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid.js:5:1 lint/nursery/noFocusedTests FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Don't focus the test.

Expand All @@ -101,6 +134,14 @@ invalid.js:5:1 lint/nursery/noFocusedTests ━━━━━━━━━━━━

i Remove it.

i Unsafe fix: Remove focus from test.

3 3 │ test.only("test", () => {});
4 4 │ fdescribe('foo', () => {});
5 │ - fit('foo',·()·=>·{});
5 │ + it('foo',·()·=>·{});
6 6 │


```

Expand Down
2 changes: 1 addition & 1 deletion website/src/content/docs/linter/rules/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ Rules that belong to this group <strong>are not subject to semantic version</str
| [noDuplicateJsonKeys](/linter/rules/no-duplicate-json-keys) | Disallow two keys with the same name inside a JSON object. | |
| [noEmptyBlockStatements](/linter/rules/no-empty-block-statements) | Disallow empty block statements and static blocks. | |
| [noEmptyTypeParameters](/linter/rules/no-empty-type-parameters) | Disallow empty type parameters in type aliases and interfaces. | |
| [noFocusedTests](/linter/rules/no-focused-tests) | Disallow focused tests. | |
| [noFocusedTests](/linter/rules/no-focused-tests) | Disallow focused tests. | <span aria-label="The rule has an unsafe fix" role="img" title="The rule has an unsafe fix">⚠️ </span> |
| [noGlobalAssign](/linter/rules/no-global-assign) | Disallow assignments to native objects and read-only global variables. | |
| [noGlobalEval](/linter/rules/no-global-eval) | Disallow the use of global <code>eval()</code>. | |
| [noInvalidUseBeforeDeclaration](/linter/rules/no-invalid-use-before-declaration) | Disallow the use of variables and function parameters before their declaration | |
Expand Down
22 changes: 20 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 @@ -28,7 +28,7 @@ However, in pull/merge request, you usually want to run all the test suite.
describe.only("foo", () => {});
```

<pre class="language-text"><code class="language-text">nursery/noFocusedTests.js:1:10 <a href="https://biomejs.dev/linter/rules/no-focused-tests">lint/nursery/noFocusedTests</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
<pre class="language-text"><code class="language-text">nursery/noFocusedTests.js:1:10 <a href="https://biomejs.dev/linter/rules/no-focused-tests">lint/nursery/noFocusedTests</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

<strong><span style="color: Tomato;"> </span></strong><strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Don't focus the test.</span>

Expand All @@ -40,13 +40,19 @@ describe.only("foo", () => {});

<strong><span style="color: lightgreen;"> </span></strong><strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Remove it.</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>
<strong>2</strong> <strong>2</strong><strong> │ </strong>

</code></pre>

```jsx
test.only("foo", () => {});
```

<pre class="language-text"><code class="language-text">nursery/noFocusedTests.js:1:6 <a href="https://biomejs.dev/linter/rules/no-focused-tests">lint/nursery/noFocusedTests</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
<pre class="language-text"><code class="language-text">nursery/noFocusedTests.js:1:6 <a href="https://biomejs.dev/linter/rules/no-focused-tests">lint/nursery/noFocusedTests</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

<strong><span style="color: Tomato;"> </span></strong><strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Don't focus the test.</span>

Expand All @@ -58,8 +64,20 @@ test.only("foo", () => {});

<strong><span style="color: lightgreen;"> </span></strong><strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Remove it.</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>
<strong>2</strong> <strong>2</strong><strong> │ </strong>

</code></pre>

### Valid

```jsx
test("foo", () => {});
```

## Related links

- [Disable a rule](/linter/#disable-a-lint-rule)
Expand Down
Loading