-
-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): add rule
noSkippedTests
(#1635)
- Loading branch information
Showing
35 changed files
with
1,421 additions
and
787 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
crates/biome_js_analyze/src/analyzers/nursery/no_focused_tests.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use biome_analyze::{ | ||
context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource, RuleSourceKind, | ||
}; | ||
use biome_console::markup; | ||
use biome_js_syntax::{AnyJsExpression, JsCallExpression, JsSyntaxToken, TextRange}; | ||
|
||
declare_rule! { | ||
/// Disallow focused tests. | ||
/// | ||
/// Disabled test are useful when developing and debugging, because it forces the test suite to run only certain tests. | ||
/// | ||
/// However, in pull/merge request, you usually want to run all the test suite. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// describe.only("foo", () => {}); | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// test.only("foo", () => {}); | ||
/// ``` | ||
pub(crate) NoFocusedTests { | ||
version: "next", | ||
name: "noFocusedTests", | ||
recommended: true, | ||
source: RuleSource::EslintJest("no-focused-tests"), | ||
source_kind: RuleSourceKind::Inspired, | ||
} | ||
} | ||
|
||
impl Rule for NoFocusedTests { | ||
type Query = Ast<JsCallExpression>; | ||
type State = TextRange; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
|
||
if node.is_test_call_expression().ok()? { | ||
let callee = node.callee().ok()?; | ||
if callee.contains_a_test_pattern().ok()? { | ||
let function_name = get_function_name(&callee)?; | ||
|
||
if function_name.text_trimmed() == "only" { | ||
return Some(function_name.text_trimmed_range()); | ||
} | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
fn diagnostic(_: &RuleContext<Self>, range: &Self::State) -> Option<RuleDiagnostic> { | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
range, | ||
markup! { | ||
"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.") | ||
) | ||
} | ||
} | ||
|
||
fn get_function_name(callee: &AnyJsExpression) -> Option<JsSyntaxToken> { | ||
match callee { | ||
AnyJsExpression::JsStaticMemberExpression(node) => { | ||
let member = node.member().ok()?; | ||
let member = member.as_js_name()?; | ||
member.value_token().ok() | ||
} | ||
AnyJsExpression::JsIdentifierExpression(node) => node.name().ok()?.value_token().ok(), | ||
_ => None, | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
crates/biome_js_analyze/src/analyzers/nursery/no_skipped_tests.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use biome_analyze::{ | ||
context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource, RuleSourceKind, | ||
}; | ||
use biome_console::markup; | ||
use biome_js_syntax::{AnyJsExpression, JsCallExpression, JsSyntaxToken}; | ||
use biome_rowan::TextRange; | ||
|
||
declare_rule! { | ||
/// Disallow disabled tests. | ||
/// | ||
/// Disabled test are useful when developing and debugging, although they should not be committed in production. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// describe.skip("test", () => {}); | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// test.skip("test", () => {}); | ||
/// ``` | ||
/// | ||
/// ## Valid | ||
/// | ||
/// ```js | ||
/// test.only("test", () => {}); | ||
/// test("test", () => {}); | ||
/// ``` | ||
/// | ||
pub(crate) NoSkippedTests { | ||
version: "next", | ||
name: "noSkippedTests", | ||
recommended: false, | ||
source: RuleSource::EslintJest("no-disabled-tests"), | ||
source_kind: RuleSourceKind::Inspired, | ||
} | ||
} | ||
|
||
const FUNCTION_NAMES: [&str; 4] = ["skip", "xdescribe", "xit", "xtest"]; | ||
|
||
impl Rule for NoSkippedTests { | ||
type Query = Ast<JsCallExpression>; | ||
type State = TextRange; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
|
||
if node.is_test_call_expression().ok()? { | ||
let callee = node.callee().ok()?; | ||
if callee.contains_a_test_pattern().ok()? { | ||
let function_name = get_function_name(&callee)?; | ||
|
||
if FUNCTION_NAMES.contains(&function_name.text_trimmed()) { | ||
return Some(function_name.text_trimmed_range()); | ||
} | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
fn diagnostic(_: &RuleContext<Self>, range: &Self::State) -> Option<RuleDiagnostic> { | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
range, | ||
markup! { | ||
"Don't disable tests." | ||
}, | ||
) | ||
.note("Disabling tests is useful when debugging or creating placeholder while working.") | ||
.note("If this is intentional, and you want to commit a disabled test, add a suppression comment.") | ||
) | ||
} | ||
} | ||
|
||
fn get_function_name(callee: &AnyJsExpression) -> Option<JsSyntaxToken> { | ||
match callee { | ||
AnyJsExpression::JsStaticMemberExpression(node) => { | ||
let member = node.member().ok()?; | ||
let member = member.as_js_name()?; | ||
member.value_token().ok() | ||
} | ||
AnyJsExpression::JsIdentifierExpression(node) => node.name().ok()?.value_token().ok(), | ||
_ => None, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
crates/biome_js_analyze/tests/specs/nursery/noFocusedTests/invalid.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe.only("test", () => {}); | ||
it.only("test", () => {}); | ||
test.only("test", () => {}); | ||
fdescribe('foo', () => {}); | ||
fit('foo', () => {}); |
70 changes: 70 additions & 0 deletions
70
crates/biome_js_analyze/tests/specs/nursery/noFocusedTests/invalid.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```jsx | ||
describe.only("test", () => {}); | ||
it.only("test", () => {}); | ||
test.only("test", () => {}); | ||
fdescribe('foo', () => {}); | ||
fit('foo', () => {}); | ||
|
||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.js:1:10 lint/nursery/noFocusedTests ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Don't focus the test. | ||
> 1 │ describe.only("test", () => {}); | ||
│ ^^^^ | ||
2 │ it.only("test", () => {}); | ||
3 │ test.only("test", () => {}); | ||
i This is likely a change done during debugging or implementation phases, but it's unlikely what you want in production. | ||
i Remove it. | ||
``` | ||
|
||
``` | ||
invalid.js:2:4 lint/nursery/noFocusedTests ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Don't focus the test. | ||
1 │ describe.only("test", () => {}); | ||
> 2 │ it.only("test", () => {}); | ||
│ ^^^^ | ||
3 │ test.only("test", () => {}); | ||
4 │ fdescribe('foo', () => {}); | ||
i This is likely a change done during debugging or implementation phases, but it's unlikely what you want in production. | ||
i Remove it. | ||
``` | ||
|
||
``` | ||
invalid.js:3:6 lint/nursery/noFocusedTests ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Don't focus the test. | ||
1 │ describe.only("test", () => {}); | ||
2 │ it.only("test", () => {}); | ||
> 3 │ test.only("test", () => {}); | ||
│ ^^^^ | ||
4 │ fdescribe('foo', () => {}); | ||
5 │ fit('foo', () => {}); | ||
i This is likely a change done during debugging or implementation phases, but it's unlikely what you want in production. | ||
i Remove it. | ||
``` | ||
|
||
|
5 changes: 5 additions & 0 deletions
5
crates/biome_js_analyze/tests/specs/nursery/noFocusedTests/valid.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe.skip("test", () => {}); | ||
it.skip("test", () => {}); | ||
test.skip("test", () => {}); | ||
fdescribe('foo', () => {}); | ||
fit('foo', () => {}); |
15 changes: 15 additions & 0 deletions
15
crates/biome_js_analyze/tests/specs/nursery/noFocusedTests/valid.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: valid.js | ||
--- | ||
# Input | ||
```jsx | ||
describe.skip("test", () => {}); | ||
it.skip("test", () => {}); | ||
test.skip("test", () => {}); | ||
fdescribe('foo', () => {}); | ||
fit('foo', () => {}); | ||
|
||
``` | ||
|
||
|
6 changes: 6 additions & 0 deletions
6
crates/biome_js_analyze/tests/specs/nursery/noSkippedTests/invalid.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
describe.skip("test", () => {}); | ||
it.skip("test", () => {}); | ||
test.skip("test", () => {}); | ||
xdescribe('foo', () => {}); | ||
xit('foo', () => {}); | ||
xtest('foo', () => {}); |
Oops, something went wrong.