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_analyzer): implement rule noRedeclaration, no-redeclare #4053
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
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.
106 changes: 106 additions & 0 deletions
106
crates/rome_js_analyze/src/semantic_analyzers/nursery/no_redeclaration.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,106 @@ | ||
use std::{collections::HashMap, hash::Hash, vec::IntoIter}; | ||
|
||
use rome_console::markup; | ||
use rome_js_semantic::{Binding, Scope}; | ||
|
||
use crate::semantic_services::Semantic; | ||
use rome_analyze::{context::RuleContext, Rule, RuleDiagnostic}; | ||
use rome_js_syntax::{JsModule, JsScript, TextRange}; | ||
|
||
use rome_analyze::declare_rule; | ||
use rome_rowan::{declare_node_union, AstNode}; | ||
|
||
declare_rule! { | ||
/// Eliminate variables that have multiple declarations in the same scope. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// var a = 3; | ||
/// var a = 10; | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// class C { | ||
/// static { | ||
/// var c = 3; | ||
/// var c = 10; | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// var a = 3; | ||
/// a = 10; | ||
/// ``` | ||
/// | ||
pub(crate) NoRedeclaration { | ||
version: "12.0.0", | ||
name: "noRedeclaration", | ||
recommended: true, | ||
} | ||
} | ||
|
||
declare_node_union! { | ||
pub(crate) AnyJsBlock = JsScript | JsModule | ||
} | ||
|
||
type Duplicates = HashMap<String, Vec<Binding>>; | ||
|
||
type Redeclaration = (String, TextRange, Binding); | ||
|
||
impl Rule for NoRedeclaration { | ||
type Query = Semantic<AnyJsBlock>; | ||
type State = Redeclaration; | ||
type Signals = IntoIter<Redeclaration>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let mut redeclarations = Vec::default(); | ||
for scope in ctx.model().scopes() { | ||
check_redeclarations_in_single_scope(&scope, &mut redeclarations); | ||
} | ||
redeclarations.into_iter() | ||
} | ||
|
||
fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
let (name, text_range, binding) = state; | ||
let diag = RuleDiagnostic::new( | ||
rule_category!(), | ||
binding.syntax().text_trimmed_range(), | ||
markup! { | ||
"Shouldn't redeclare '"{ name }"'. Consider to delete it or rename it" | ||
}, | ||
) | ||
.detail( | ||
text_range, | ||
markup! { | ||
"'"{ name }"' is defined here." | ||
}, | ||
); | ||
Some(diag) | ||
} | ||
} | ||
|
||
fn check_redeclarations_in_single_scope(scope: &Scope, redeclarations: &mut Vec<Redeclaration>) { | ||
let mut duplicates = Duplicates::default(); | ||
let bindings = scope.bindings(); | ||
for binding in bindings { | ||
let name = binding.tree().text(); | ||
duplicates.entry(name).or_default().push(binding) | ||
} | ||
|
||
// only keep the actual redeclarations | ||
duplicates.retain(|_, list| list.len() > 1); | ||
|
||
for (name, list) in duplicates { | ||
let first_binding_range = list[0].syntax().text_trimmed_range(); | ||
list.into_iter() | ||
.skip(1) // skip the first binding | ||
.for_each(|binding| redeclarations.push((name.clone(), first_binding_range, binding))) | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
crates/rome_js_analyze/tests/specs/nursery/noRedeclaration/invalid.jsonc
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,28 @@ | ||
[ | ||
"var a = 3; var a = 10;", | ||
"var c; { var a; var a;} ", | ||
|
||
// in non-strict mode, function a is hoisted | ||
"var a; { function a(){} }", | ||
|
||
"switch(foo) { case a: var b = 3;\ncase b: var b = 4}", | ||
"var a = 3; var a = 10;", | ||
"var a = {}; var a = [];", | ||
"var a; function a() {}", | ||
"function a() {} function a() {}", | ||
"var a = function() { }; var a = function() { }", | ||
"var a = function() { }; var a = new Date();", | ||
"var a = 3; var a = 10; var a = 15;", | ||
"var a; var a;", | ||
"export var a; var a;", | ||
"class C { static { var a; var a; } }", | ||
"class C { static { var a; { var a; } } }", | ||
"class C { static { { var a; } var a; } }", | ||
"class C { static { { var a; } { var a; } } }", | ||
"var a; var {a = 0, b: Object = 0} = {};", | ||
"var a; var {a = 0, b: globalThis = 0} = {};", | ||
"function f() { var a; var a; }", | ||
"function f() { var a; if (test) { var a; } }", | ||
"for (var a, a;;);", | ||
"for (;;){ var a, a,;}" | ||
] |
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.
Uh oh!
There was an error while loading. Please reload this page.