-
-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lint): rule
noMisplacedAssertion
- Loading branch information
Showing
13 changed files
with
302 additions
and
16 deletions.
There are no files selected for viewing
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
116 changes: 116 additions & 0 deletions
116
crates/biome_js_analyze/src/analyzers/nursery/no_misplaced_assertion.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,116 @@ | ||
use crate::semantic_services::Semantic; | ||
use biome_analyze::{ | ||
context::RuleContext, declare_rule, Rule, RuleDiagnostic, RuleSource, RuleSourceKind, | ||
}; | ||
use biome_console::markup; | ||
use biome_deserialize::TextRange; | ||
use biome_js_syntax::{ | ||
AnyJsNamedImportSpecifier, JsCallExpression, JsIdentifierBinding, JsImport, JsModule, | ||
}; | ||
use biome_rowan::{AstNode, WalkEvent}; | ||
|
||
declare_rule! { | ||
/// Succinct description of the rule. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// ``` | ||
/// | ||
pub NoMisplacedAssertion { | ||
version: "next", | ||
name: "noMisplacedAssertion", | ||
recommended: false, | ||
source: RuleSource::EslintJest("no-standalone-expect"), | ||
source_kind: RuleSourceKind::Inspired, | ||
} | ||
} | ||
|
||
impl Rule for NoMisplacedAssertion { | ||
type Query = Semantic<JsModule>; | ||
type State = TextRange; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
let model = ctx.model(); | ||
let preorder = node.syntax().preorder(); | ||
let mut inside_describe_call = false; | ||
let mut inside_it_call = false; | ||
let mut assertion_call = None; | ||
for event in preorder { | ||
match event { | ||
WalkEvent::Enter(node) => { | ||
if let Some(node) = JsCallExpression::cast(node) { | ||
if let Ok(callee) = node.callee() { | ||
if callee.is_test_describe_call() { | ||
inside_describe_call = true | ||
} | ||
if callee.is_test_it_call() { | ||
inside_it_call = true | ||
} | ||
} | ||
} | ||
} | ||
WalkEvent::Leave(node) => { | ||
if let Some(node) = JsCallExpression::cast(node) { | ||
if let Ok(callee) = node.callee() { | ||
if callee.is_test_describe_call() { | ||
inside_describe_call = false | ||
} | ||
if callee.is_test_it_call() { | ||
inside_it_call = false | ||
} | ||
if callee.is_assertion_call() { | ||
if inside_describe_call && !inside_it_call { | ||
assertion_call = | ||
Some(node.callee().ok()?.as_js_reference_identifier()?); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if let Some(assertion_call) = assertion_call { | ||
let call_text = assertion_call.value_token().ok()?; | ||
let binding = model.binding(&assertion_call); | ||
if let Some(binding) = binding { | ||
let ident = JsIdentifierBinding::cast_ref(binding.syntax())?; | ||
let import_specifier = ident.parent::<AnyJsNamedImportSpecifier>()?; | ||
let import = import_specifier.import_clause()?.parent::<JsImport>()?; | ||
let source_text = import.source_text().ok()?; | ||
if (matches!(source_text.text(), "chai") && call_text.text_trimmed() == "expect") | ||
|| (matches!(call_text.text_trimmed(), "assert") | ||
&& matches!(source_text.text(), "node:assert" | "node:assert/strict")) | ||
{ | ||
return Some(assertion_call.range()); | ||
} | ||
} else { | ||
return Some(assertion_call.range()); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
Some(RuleDiagnostic::new( | ||
rule_category!(), | ||
state, | ||
markup! { | ||
"The assertion isn't inside an it call." | ||
}, | ||
)) | ||
} | ||
} |
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
3 changes: 3 additions & 0 deletions
3
crates/biome_js_analyze/tests/specs/nursery/noMisplacedAssertion/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,3 @@ | ||
describe(() => { | ||
expect("something").toBeTrue() | ||
}) |
26 changes: 26 additions & 0 deletions
26
crates/biome_js_analyze/tests/specs/nursery/noMisplacedAssertion/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,26 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```jsx | ||
describe(() => { | ||
expect("something").toBeTrue() | ||
}) | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.js:2:2 lint/nursery/noMisplacedAssertion ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! The assertion isn't inside an it call. | ||
1 │ describe(() => { | ||
> 2 │ expect("something").toBeTrue() | ||
│ ^^^^^^^^^^^^^^^^^^^ | ||
3 │ }) | ||
``` | ||
|
||
|
4 changes: 4 additions & 0 deletions
4
crates/biome_js_analyze/tests/specs/nursery/noMisplacedAssertion/invalidImportedChai.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,4 @@ | ||
import { expect } from "chai"; | ||
describe(() => { | ||
expect("something").toBeTrue() | ||
}) |
28 changes: 28 additions & 0 deletions
28
crates/biome_js_analyze/tests/specs/nursery/noMisplacedAssertion/invalidImportedChai.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,28 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalidImportedChai.js | ||
--- | ||
# Input | ||
```jsx | ||
import { expect } from "chai"; | ||
describe(() => { | ||
expect("something").toBeTrue() | ||
}) | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalidImportedChai.js:3:2 lint/nursery/noMisplacedAssertion ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! The assertion isn't inside an it call. | ||
1 │ import { expect } from "chai"; | ||
2 │ describe(() => { | ||
> 3 │ expect("something").toBeTrue() | ||
│ ^^^^^^^^^^^^^^^^^^^ | ||
4 │ }) | ||
``` | ||
|
||
|
6 changes: 6 additions & 0 deletions
6
crates/biome_js_analyze/tests/specs/nursery/noMisplacedAssertion/invalidImportedNode.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 @@ | ||
import assert from "node:assert"; | ||
import { describe } from "node:test"; | ||
|
||
describe(() => { | ||
assert.equal("something", "something") | ||
}) |
5 changes: 5 additions & 0 deletions
5
crates/biome_js_analyze/tests/specs/nursery/noMisplacedAssertion/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("msg", () => { | ||
it("msg", () => { | ||
expect("something").toBeTrue() | ||
}) | ||
}) |
14 changes: 14 additions & 0 deletions
14
crates/biome_js_analyze/tests/specs/nursery/noMisplacedAssertion/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,14 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: valid.js | ||
--- | ||
# Input | ||
```jsx | ||
describe("msg", () => { | ||
it("msg", () => { | ||
expect("something").toBeTrue() | ||
}) | ||
}) | ||
``` | ||
|
||
|
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
56 changes: 56 additions & 0 deletions
56
website/src/content/docs/linter/rules/no-misplaced-assertion.md
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,56 @@ | ||
--- | ||
title: noMisplacedAssertion (not released) | ||
--- | ||
|
||
**Diagnostic Category: `lint/nursery/noMisplacedAssertion`** | ||
|
||
:::danger | ||
This rule hasn't been released yet. | ||
::: | ||
|
||
:::caution | ||
This rule is part of the [nursery](/linter/rules/#nursery) group. | ||
::: | ||
|
||
Succinct description of the rule. | ||
|
||
Put context and details about the rule. | ||
As a starting point, you can take the description of the corresponding _ESLint_ rule (if any). | ||
|
||
Try to stay consistent with the descriptions of implemented rules. | ||
|
||
Add a link to the corresponding ESLint rule (if any): | ||
|
||
## Examples | ||
|
||
### Invalid | ||
|
||
```jsx | ||
var a = 1; | ||
a = 2; | ||
``` | ||
|
||
<pre class="language-text"><code class="language-text">nursery/noMisplacedAssertion.js:1:11 <a href="https://biomejs.dev/linter/rules/no-misplaced-assertion">lint/nursery/noMisplacedAssertion</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
<strong><span style="color: Orange;"> </span></strong><strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Variable is read here.</span> | ||
|
||
<strong><span style="color: Tomato;"> </span></strong><strong><span style="color: Tomato;">></span></strong> <strong>1 │ </strong>var a = 1; | ||
<strong> │ </strong> | ||
<strong><span style="color: Tomato;"> </span></strong><strong><span style="color: Tomato;">></span></strong> <strong>2 │ </strong>a = 2; | ||
<strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> | ||
<strong>3 │ </strong> | ||
|
||
<strong><span style="color: lightgreen;"> </span></strong><strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This note will give you more information.</span> | ||
|
||
</code></pre> | ||
|
||
### Valid | ||
|
||
```jsx | ||
var a = 1; | ||
``` | ||
|
||
## Related links | ||
|
||
- [Disable a rule](/linter/#disable-a-lint-rule) | ||
- [Rule options](/linter/#rule-options) |