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 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rome_js_analyze):
noVar
(#3765)
- Loading branch information
Showing
20 changed files
with
480 additions
and
10 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
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
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.
101 changes: 101 additions & 0 deletions
101
crates/rome_js_analyze/src/semantic_analyzers/nursery/no_var.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,101 @@ | ||
use crate::{control_flow::JsAnyControlFlowRoot, semantic_services::Semantic, JsRuleAction}; | ||
use rome_analyze::{context::RuleContext, declare_rule, ActionCategory, Rule, RuleDiagnostic}; | ||
use rome_console::markup; | ||
use rome_diagnostics::Applicability; | ||
use rome_js_factory::make; | ||
use rome_js_syntax::{JsModule, JsScript, JsSyntaxKind}; | ||
|
||
use rome_rowan::{AstNode, BatchMutationExt}; | ||
|
||
use super::use_const::{ConstBindings, VariableDeclaration}; | ||
|
||
declare_rule! { | ||
/// Disallow the use of `var` | ||
/// | ||
/// ECMAScript 6 allows programmers to create variables with block scope instead of function scope using the let and const keywords. Block scope is common in many other programming languages and helps programmers avoid mistakes. | ||
/// | ||
/// Source: https://eslint.org/docs/latest/rules/no-var | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// var foo = 1; | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// const foo = 1; | ||
/// let bar = 1; | ||
///``` | ||
pub(crate) NoVar { | ||
version: "11.0.0", | ||
name: "noVar", | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for NoVar { | ||
type Query = Semantic<VariableDeclaration>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let declaration = ctx.query(); | ||
declaration.is_var().then_some(()) | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
let declaration = ctx.query(); | ||
let var_scope = declaration | ||
.syntax() | ||
.ancestors() | ||
.find(|x| JsAnyControlFlowRoot::can_cast(x.kind()))?; | ||
let contextual_note = if JsScript::can_cast(var_scope.kind()) { | ||
markup! { | ||
"A variable declared with "<Emphasis>"var"</Emphasis>" in the global scope pollutes the global object." | ||
} | ||
} else if JsModule::can_cast(var_scope.kind()) { | ||
markup! { | ||
"A variable declared with "<Emphasis>"var"</Emphasis>" is accessible in the whole module. Thus, the variable can be accessed before its initialization and outside the block where it is declared." | ||
} | ||
} else { | ||
markup! { | ||
"A variable declared with "<Emphasis>"var"</Emphasis>" is accessible in the whole body of the function. Thus, the variable can be accessed before its initialization and outside the block where it is declared." | ||
} | ||
}; | ||
Some(RuleDiagnostic::new( | ||
rule_category!(), | ||
declaration.range(), | ||
markup! { | ||
"Use "<Emphasis>"let"</Emphasis>" or "<Emphasis>"const"</Emphasis>" instead of "<Emphasis>"var"</Emphasis>"." | ||
}, | ||
).note(contextual_note).note( | ||
markup! { | ||
"See "<Hyperlink href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var">"MDN web docs"</Hyperlink>" for more details." | ||
} | ||
)) | ||
} | ||
|
||
fn action(ctx: &RuleContext<Self>, _: &Self::State) -> Option<JsRuleAction> { | ||
let declaration = ctx.query(); | ||
let model = ctx.model(); | ||
let maybe_const = ConstBindings::new(declaration, model)?; | ||
let replacing_token_kind = if maybe_const.can_fix { | ||
JsSyntaxKind::CONST_KW | ||
} else { | ||
JsSyntaxKind::LET_KW | ||
}; | ||
let mut mutation = ctx.root().begin(); | ||
mutation.replace_token(declaration.kind_token()?, make::token(replacing_token_kind)); | ||
Some(JsRuleAction { | ||
category: ActionCategory::QuickFix, | ||
applicability: Applicability::MaybeIncorrect, | ||
message: markup! { "Use '"<Emphasis>{replacing_token_kind.to_string()?}</Emphasis>"' instead." }.to_owned(), | ||
mutation, | ||
}) | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
crates/rome_js_analyze/tests/specs/nursery/noVar/invalidFunctions.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,9 @@ | ||
export function f(x) { | ||
if(x) { | ||
// assign 'y' | ||
var /* @type number */ y /*: number */ = 2*x; | ||
// assign 'y' to 'x' | ||
x = y; | ||
} | ||
return x; | ||
} |
48 changes: 48 additions & 0 deletions
48
crates/rome_js_analyze/tests/specs/nursery/noVar/invalidFunctions.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,48 @@ | ||
--- | ||
source: crates/rome_js_analyze/tests/spec_tests.rs | ||
assertion_line: 73 | ||
expression: invalidFunctions.js | ||
--- | ||
# Input | ||
```js | ||
export function f(x) { | ||
if(x) { | ||
// assign 'y' | ||
var /* @type number */ y /*: number */ = 2*x; | ||
// assign 'y' to 'x' | ||
x = y; | ||
} | ||
return x; | ||
} | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalidFunctions.js:4:9 lint/nursery/noVar FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Use let or const instead of var. | ||
2 │ if(x) { | ||
3 │ // assign 'y' | ||
> 4 │ var /* @type number */ y /*: number */ = 2*x; | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
5 │ // assign 'y' to 'x' | ||
6 │ x = y; | ||
i A variable declared with var is accessible in the whole body of the function. Thus, the variable can be accessed before its initialization and outside the block where it is declared. | ||
i See MDN web docs for more details. | ||
i Suggested fix: Use 'const' instead. | ||
2 2 │ if(x) { | ||
3 3 │ // assign 'y' | ||
4 │ - ········var·/*·@type·number·*/·y·/*:·number·*/·=·2*x; | ||
4 │ + ········const·/*·@type·number·*/·y·/*:·number·*/·=·2*x; | ||
5 5 │ // assign 'y' to 'x' | ||
6 6 │ x = y; | ||
``` | ||
|
||
|
2 changes: 2 additions & 0 deletions
2
crates/rome_js_analyze/tests/specs/nursery/noVar/invalidModule.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,2 @@ | ||
var x = 1; | ||
export const y = x; |
35 changes: 35 additions & 0 deletions
35
crates/rome_js_analyze/tests/specs/nursery/noVar/invalidModule.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,35 @@ | ||
--- | ||
source: crates/rome_js_analyze/tests/spec_tests.rs | ||
assertion_line: 73 | ||
expression: invalidModule.js | ||
--- | ||
# Input | ||
```js | ||
var x = 1; | ||
export const y = x; | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalidModule.js:1:1 lint/nursery/noVar FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Use let or const instead of var. | ||
> 1 │ var x = 1; | ||
│ ^^^^^^^^^ | ||
2 │ export const y = x; | ||
i A variable declared with var is accessible in the whole module. Thus, the variable can be accessed before its initialization and outside the block where it is declared. | ||
i See MDN web docs for more details. | ||
i Suggested fix: Use 'const' instead. | ||
1 │ - var·x·=·1; | ||
1 │ + const·x·=·1; | ||
2 2 │ export const y = x; | ||
``` | ||
|
||
|
7 changes: 7 additions & 0 deletions
7
crates/rome_js_analyze/tests/specs/nursery/noVar/invalidScript.jsonc
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,7 @@ | ||
[ | ||
"var x = 1; foo(x);", | ||
"for (var i in [1,2,3]) { foo(i); }", | ||
"for (var x of [1,2,3]) { foo(x); }", | ||
"var [x = -1, y] = [1,2]; y = 0;", | ||
"var {a: x = -1, b: y} = {a:1,b:2}; y = 0;" | ||
] |
Oops, something went wrong.