-
-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(biome_js_analyzer): implement noImplicitAnyLet
- Loading branch information
Showing
15 changed files
with
356 additions
and
30 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.
90 changes: 90 additions & 0 deletions
90
crates/biome_js_analyze/src/analyzers/nursery/no_implicit_any_let.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,90 @@ | ||
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic}; | ||
use biome_console::markup; | ||
use biome_js_syntax::{JsFileSource, JsVariableDeclaration, JsVariableDeclarator}; | ||
|
||
declare_rule! { | ||
/// Restrict use of implicit any type in Typescript. | ||
/// | ||
/// Typescript variable declaration without any `type` or `initialization` can cause issue later in the code. | ||
/// | ||
/// | ||
/// | ||
/// Source: https://www.typescriptlang.org/tsconfig#noImplicitAny | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// var a; | ||
/// a = 2; | ||
/// let b; | ||
/// b = 1 | ||
/// ``` | ||
/// | ||
/// ## Valid | ||
/// | ||
/// ```ts | ||
/// var a = 1; | ||
/// let a:number; | ||
/// var b: number | ||
/// var b =10; | ||
/// ``` | ||
/// | ||
pub(crate) NoImplicitAnyLet { | ||
version: "next", | ||
name: "noImplicitAnyLet", | ||
recommended: true, | ||
} | ||
} | ||
|
||
impl Rule for NoImplicitAnyLet { | ||
type Query = Ast<JsVariableDeclaration>; | ||
type State = JsVariableDeclarator; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let source_type = ctx.source_type::<JsFileSource>().language(); | ||
let is_ts_source = source_type.is_typescript(); | ||
let node = ctx.query(); | ||
let is_declaration = source_type.is_definition_file(); | ||
|
||
if node.is_const() || is_declaration || !is_ts_source { | ||
return None; | ||
} | ||
|
||
for declarator in node.declarators() { | ||
let variable = declarator.ok()?; | ||
let is_initialized = variable.initializer().is_some(); | ||
let is_type_annotated = variable.variable_annotation().is_some(); | ||
if !is_initialized && !is_type_annotated { | ||
return Some(variable); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
fn diagnostic(_: &RuleContext<Self>, node: &Self::State) -> Option<RuleDiagnostic> { | ||
let variable = node | ||
.id() | ||
.ok()? | ||
.as_any_js_binding()? | ||
.as_js_identifier_binding()? | ||
.name_token() | ||
.ok()?; | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
variable.text_range(), | ||
markup! { | ||
"Variable " <Emphasis>{variable.text()}</Emphasis> " has implicitly " <Emphasis>"any"</Emphasis> " type" | ||
}, | ||
) | ||
.note(markup! { | ||
"Declare type or initialize the variable with some value" | ||
}), | ||
) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
crates/biome_js_analyze/tests/specs/nursery/noImplicitAnyLet/invalid.ts
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,13 @@ | ||
let someVar1; | ||
someVar1 = '123'; | ||
someVar1 = 123; | ||
|
||
|
||
var someVar1; | ||
someVar1 = '123'; | ||
someVar1 = 123; | ||
|
||
|
||
function ex() { | ||
let b; | ||
} |
68 changes: 68 additions & 0 deletions
68
crates/biome_js_analyze/tests/specs/nursery/noImplicitAnyLet/invalid.ts.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,68 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.ts | ||
--- | ||
# Input | ||
```js | ||
let someVar1; | ||
someVar1 = '123'; | ||
someVar1 = 123; | ||
|
||
|
||
var someVar1; | ||
someVar1 = '123'; | ||
someVar1 = 123; | ||
|
||
|
||
function ex() { | ||
let b; | ||
} | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.ts:1:5 lint/nursery/noImplicitAnyLet ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Variable someVar1 has implicitly any type | ||
> 1 │ let someVar1; | ||
│ ^^^^^^^^ | ||
2 │ someVar1 = '123'; | ||
3 │ someVar1 = 123; | ||
i Declare type or initialize the variable with some value | ||
``` | ||
|
||
``` | ||
invalid.ts:6:5 lint/nursery/noImplicitAnyLet ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Variable someVar1 has implicitly any type | ||
> 6 │ var someVar1; | ||
│ ^^^^^^^^ | ||
7 │ someVar1 = '123'; | ||
8 │ someVar1 = 123; | ||
i Declare type or initialize the variable with some value | ||
``` | ||
|
||
``` | ||
invalid.ts:12:9 lint/nursery/noImplicitAnyLet ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Variable b has implicitly any type | ||
11 │ function ex() { | ||
> 12 │ let b; | ||
│ ^ | ||
13 │ } | ||
i Declare type or initialize the variable with some value | ||
``` | ||
|
||
|
6 changes: 6 additions & 0 deletions
6
crates/biome_js_analyze/tests/specs/nursery/noImplicitAnyLet/valid.ts
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 @@ | ||
/* should not generate diagnostics */ | ||
|
||
let a: number; | ||
let b = 1 | ||
var c : string; | ||
var d = "abn" |
16 changes: 16 additions & 0 deletions
16
crates/biome_js_analyze/tests/specs/nursery/noImplicitAnyLet/valid.ts.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,16 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: valid.ts | ||
--- | ||
# Input | ||
```js | ||
/* should not generate diagnostics */ | ||
|
||
let a: number; | ||
let b = 1 | ||
var c : string; | ||
var d = "abn" | ||
|
||
``` | ||
|
||
|
Oops, something went wrong.