This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 653
feat(rome_js_analyze): noClassAssign
rule
#4033
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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.
116 changes: 116 additions & 0 deletions
116
crates/rome_js_analyze/src/semantic_analyzers/nursery/no_class_assign.rs
This file contains hidden or 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 rome_analyze::context::RuleContext; | ||
use rome_analyze::{declare_rule, Rule, RuleDiagnostic}; | ||
use rome_console::markup; | ||
use rome_js_semantic::{Reference, ReferencesExtensions}; | ||
use rome_js_syntax::AnyJsClass; | ||
|
||
use crate::semantic_services::Semantic; | ||
|
||
declare_rule! { | ||
/// Disallow reassigning class members. | ||
/// | ||
/// A class declaration creates a variable that we can modify, however, the modification is a mistake in most cases. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// class A {} | ||
/// A = 0; | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// A = 0; | ||
/// class A {} | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// class A { | ||
/// b() { | ||
/// A = 0; | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// let A = class A { | ||
/// b() { | ||
/// A = 0; | ||
/// // `let A` is shadowed by the class name. | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// let A = class A {} | ||
/// A = 0; // A is a variable. | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// let A = class { | ||
/// b() { | ||
/// A = 0; // A is a variable. | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// class A { | ||
/// b(A) { | ||
/// A = 0; // A is a parameter. | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
pub(crate) NoClassAssign { | ||
version: "12.0.0", | ||
name: "noClassAssign", | ||
recommended: true, | ||
} | ||
} | ||
|
||
impl Rule for NoClassAssign { | ||
type Query = Semantic<AnyJsClass>; | ||
type State = Reference; | ||
type Signals = Vec<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
let model = ctx.model(); | ||
|
||
if let Ok(Some(id)) = node.id() { | ||
if let Some(id_binding) = id.as_js_identifier_binding() { | ||
return id_binding.all_writes(model).collect(); | ||
} | ||
} | ||
|
||
Vec::new() | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, reference: &Self::State) -> Option<RuleDiagnostic> { | ||
let binding = ctx | ||
.query() | ||
.id() | ||
.ok()?? | ||
.as_js_identifier_binding()? | ||
.name_token() | ||
.ok()?; | ||
let class_name = binding.text_trimmed(); | ||
|
||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
reference.syntax().text_trimmed_range(), | ||
markup! {"'"{class_name}"' is a class."}, | ||
) | ||
.detail( | ||
binding.text_trimmed_range(), | ||
markup! {"'"{class_name}"' is defined here."}, | ||
), | ||
) | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
crates/rome_js_analyze/tests/specs/nursery/noClassAssign.js
This file contains hidden or 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,110 @@ | ||
/* Valid */ | ||
function case1() { | ||
class A { } | ||
foo(A); | ||
} | ||
|
||
function case2() { | ||
let A = class A { }; | ||
foo(A); | ||
} | ||
|
||
function case3() { | ||
class A { | ||
b(A) { | ||
A = 0; | ||
} | ||
} | ||
} | ||
|
||
function case4() { | ||
class A { | ||
b() { | ||
let A; | ||
A = 0; | ||
} | ||
} | ||
} | ||
|
||
function case5() { | ||
let A = class { | ||
b() { | ||
A = 0; | ||
} | ||
} | ||
} | ||
|
||
// /* Ignores non class. */ | ||
function case6() { | ||
var x = 0; | ||
x = 1; | ||
} | ||
|
||
function case7() { | ||
let x = 0; | ||
x = 1; | ||
} | ||
|
||
function case8() { | ||
const x = 0; | ||
x = 1; | ||
} | ||
|
||
function case9() { | ||
function x() {} | ||
x = 1; | ||
} | ||
|
||
function case10(x) { | ||
x = 1; | ||
} | ||
|
||
function case11() { | ||
try {} | ||
catch (x) { | ||
x = 1; | ||
} | ||
} | ||
|
||
// /* Invalid */ | ||
function case12() { | ||
class A { } | ||
A = 0; | ||
} | ||
|
||
function case13() { | ||
class B { } | ||
({B} = 0); | ||
} | ||
|
||
function case14() { | ||
class C { } | ||
({b: C = 0} = {}); | ||
} | ||
|
||
function case15() { | ||
D = 0; | ||
class D { } | ||
} | ||
|
||
function case16() { | ||
class E { | ||
b() { | ||
E = 0; | ||
} | ||
} | ||
} | ||
|
||
function case17() { | ||
let F = class F { | ||
b() { | ||
F = 0; | ||
} | ||
} | ||
} | ||
|
||
function case18() { | ||
class G { } | ||
G = 0; | ||
G = 1; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
Nit: feel free to ignore:
You can avoid the
collect()
and the allocation with