Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lint): Implement noImplicitAnyLet #715

Merged
merged 5 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/biome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ define_categories! {
"lint/nursery/noDuplicateJsonKeys": "https://biomejs.dev/linter/rules/no-duplicate-json-keys",
"lint/nursery/noEmptyBlockStatements": "https://biomejs.dev/linter/rules/no-empty-block-statements",
"lint/nursery/noEmptyCharacterClassInRegex": "https://biomejs.dev/linter/rules/no-empty-character-class-in-regex",
"lint/nursery/noImplicitAnyLet": "https://biomejs.dev/lint/rules/no-implicit-any-let",
"lint/nursery/noInteractiveElementToNoninteractiveRole": "https://biomejs.dev/linter/rules/no-interactive-element-to-noninteractive-role",
"lint/nursery/noInvalidNewBuiltin": "https://biomejs.dev/linter/rules/no-invalid-new-builtin",
"lint/nursery/noMisleadingInstantiator": "https://biomejs.dev/linter/rules/no-misleading-instantiator",
Expand Down
2 changes: 2 additions & 0 deletions crates/biome_js_analyze/src/analyzers/nursery.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic};
use biome_console::markup;
use biome_js_syntax::{JsFileSource, JsVariableDeclaration, JsVariableDeclarator};

declare_rule! {
/// Disallow use of implicit `any` type on variable declarations.
///
/// TypeScript variable declaration without any type annotation and initialization have the `any` type.
/// The any type in TypeScript is a dangerous “escape hatch” from the type system.
/// Using any disables many type checking rules and is generally best used only as a last resort or when prototyping code.
/// TypeScript’s `--noImplicitAny` compiler option doesn't report this case.
///
///
///
/// Source: https://www.typescriptlang.org/tsconfig#noImplicitAny
///
/// ## Examples
///
/// ### Invalid
///
/// ```ts,expect_diagnostic
/// var a;
/// a = 2;
/// ````
///
/// ```ts,expect_diagnostic
/// 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! {
"This variable has implicitly the " <Emphasis>"any"</Emphasis> " type."
},
)
.note(markup! {
"Variable declarations without type annotation and initialization have implicitly the "<Emphasis>"any"</Emphasis>" type. Declare type or initialize the variable with some value."
}),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let someVar1;
someVar1 = '123';
someVar1 = 123;

var someVar1;
someVar1 = '123';
someVar1 = 123;

let x = 0, y, z = 0;
var x = 0, y, z = 0;
for(let a = 0, b; a < 5; a++) {}

function ex() {
let b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
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;

let x = 0, y, z = 0;
var x = 0, y, z = 0;
for(let a = 0, b; a < 5; a++) {}

function ex() {
let b;
}

```

# Diagnostics
```
invalid.ts:1:5 lint/nursery/noImplicitAnyLet ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This variable has implicitly the any type.

> 1 │ let someVar1;
│ ^^^^^^^^
2 │ someVar1 = '123';
3 │ someVar1 = 123;

i Variable declarations without type annotation and initialization have implicitly the any type. Declare type or initialize the variable with some value.


```

```
invalid.ts:5:5 lint/nursery/noImplicitAnyLet ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This variable has implicitly the any type.

3 │ someVar1 = 123;
4 │
> 5 │ var someVar1;
│ ^^^^^^^^
6 │ someVar1 = '123';
7 │ someVar1 = 123;

i Variable declarations without type annotation and initialization have implicitly the any type. Declare type or initialize the variable with some value.


```

```
invalid.ts:9:12 lint/nursery/noImplicitAnyLet ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This variable has implicitly the any type.

7 │ someVar1 = 123;
8 │
> 9 │ let x = 0, y, z = 0;
│ ^
10 │ var x = 0, y, z = 0;
11 │ for(let a = 0, b; a < 5; a++) {}

i Variable declarations without type annotation and initialization have implicitly the any type. Declare type or initialize the variable with some value.


```

```
invalid.ts:10:12 lint/nursery/noImplicitAnyLet ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This variable has implicitly the any type.

9 │ let x = 0, y, z = 0;
> 10 │ var x = 0, y, z = 0;
│ ^
11 │ for(let a = 0, b; a < 5; a++) {}
12 │

i Variable declarations without type annotation and initialization have implicitly the any type. Declare type or initialize the variable with some value.


```

```
invalid.ts:11:16 lint/nursery/noImplicitAnyLet ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This variable has implicitly the any type.

9 │ let x = 0, y, z = 0;
10 │ var x = 0, y, z = 0;
> 11 │ for(let a = 0, b; a < 5; a++) {}
│ ^
12 │
13 │ function ex() {

i Variable declarations without type annotation and initialization have implicitly the any type. Declare type or initialize the variable with some value.


```

```
invalid.ts:14:9 lint/nursery/noImplicitAnyLet ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This variable has implicitly the any type.

13 │ function ex() {
> 14 │ let b;
│ ^
15 │ }
16 │

i Variable declarations without type annotation and initialization have implicitly the any type. Declare type or initialize the variable with some value.


```


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* should not generate diagnostics */

let a: number;
let b = 1
var c : string;
var d = "abn"

const x = 0;
for(let y of xs) {}
using z = f();
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
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"

const x = 0;
for(let y of xs) {}
using z = f();

```


Loading