Skip to content

Commit e7b712e

Browse files
authored
refactor(noUndeclaredVariables): set checkTypes to false by default (#5414)
1 parent 3f6c415 commit e7b712e

File tree

10 files changed

+85
-77
lines changed

10 files changed

+85
-77
lines changed

.changeset/itchy-bananas-travel.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
"@biomejs/biome": major
3+
---
4+
5+
`noUndeclaredVariables` no longer reports TypeScript types.
6+
7+
In TypeScript projects, developers often use global declaration files to declare global types.
8+
Biome is currently unable to detect these global types.
9+
This creates many false positives for `noUndeclaredVariables`.
10+
11+
TypeScript is better suited to perform this kind of check.
12+
As proof of this, TypeScript ESLint doesn't provide any rule that extends the `no-undef` ESLint rule.
13+
14+
This is why Biome 1.9 introduced a new option `checkTypes` which, when it is set to false, ignores undeclared type references.
15+
The option was set to `true` by default.
16+
17+
This option is now set to `false` by default.
18+
To get the previous behavior, you have to set `checkTypes` to `true`:
19+
20+
```json
21+
{
22+
"linter": {
23+
"rules": {
24+
"correctness": {
25+
"noUndeclaredVariables": {
26+
"level": "on",
27+
"options": { "checkTypes": true }
28+
}
29+
}
30+
}
31+
}
32+
}
33+
```

crates/biome_js_analyze/src/lint/correctness/no_undeclared_variables.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ declare_lint_rule! {
1717
/// ## Options (Since v2.0.0)
1818
///
1919
/// The rule provides a `checkTypes` option that make the rule checks undeclared types.
20-
/// The option defaults to `true`.
20+
/// The option defaults to `false`.
2121
///
22-
/// ```json,options
22+
/// ```json
2323
/// {
2424
/// "options": {
2525
/// "checkTypes": true
@@ -126,18 +126,15 @@ impl Rule for NoUndeclaredVariables {
126126
}
127127
}
128128

129-
#[derive(Clone, Debug, Deserializable, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
129+
#[derive(
130+
Clone, Debug, Default, Deserializable, Eq, PartialEq, serde::Deserialize, serde::Serialize,
131+
)]
130132
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
131133
#[serde(default, rename_all = "camelCase")]
132134
pub struct UndeclaredVariablesOptions {
133135
/// Check undeclared types.
134136
check_types: bool,
135137
}
136-
impl Default for UndeclaredVariablesOptions {
137-
fn default() -> Self {
138-
Self { check_types: true }
139-
}
140-
}
141138

142139
fn is_global(reference_name: &str, source_type: &JsFileSource) -> bool {
143140
match source_type.language() {

crates/biome_js_analyze/tests/specs/correctness/noUndeclaredVariables/ignore-types.options.json crates/biome_js_analyze/tests/specs/correctness/noUndeclaredVariables/infer_incorrect.options.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"rules": {
44
"correctness": {
55
"noUndeclaredVariables": {
6-
"level": "error",
6+
"level": "on",
77
"options": {
8-
"checkTypes": false
8+
"checkTypes": true
99
}
1010
}
1111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"linter": {
3+
"rules": {
4+
"correctness": {
5+
"noUndeclaredVariables": {
6+
"level": "on",
7+
"options": {
8+
"checkTypes": true
9+
}
10+
}
11+
}
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"linter": {
3+
"rules": {
4+
"correctness": {
5+
"noUndeclaredVariables": {
6+
"level": "on",
7+
"options": {
8+
"checkTypes": true
9+
}
10+
}
11+
}
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"linter": {
3+
"rules": {
4+
"correctness": {
5+
"noUndeclaredVariables": {
6+
"level": "on",
7+
"options": {
8+
"checkTypes": true
9+
}
10+
}
11+
}
12+
}
13+
}
14+
}

crates/biome_js_analyze/tests/specs/correctness/noUndeclaredVariables/noUndeclaredVariables.ts.snap crates/biome_js_analyze/tests/specs/correctness/noUndeclaredVariables/noUndeclaredVariablesTypes.ts.snap

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: crates/biome_js_analyze/tests/spec_tests.rs
3-
expression: noUndeclaredVariables.ts
4-
snapshot_kind: text
3+
expression: noUndeclaredVariablesTypes.ts
54
---
65
# Input
76
```ts
@@ -36,7 +35,7 @@ export type Invalid<S extends number> = `Hello ${T}`
3635
3736
# Diagnostics
3837
```
39-
noUndeclaredVariables.ts:26:50 lint/correctness/noUndeclaredVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
38+
noUndeclaredVariablesTypes.ts:26:50 lint/correctness/noUndeclaredVariables ━━━━━━━━━━━━━━━━━━━━━━━━━
4039
4140
i The T variable is undeclared.
4241

crates/biome_js_analyze/tests/suppression/correctness/noUndeclaredVariables/noUndeclaredVariables.ts.snap

-63
Original file line numberDiff line numberDiff line change
@@ -11,66 +11,3 @@ export type Invalid<S extends number> = `
1111
${T}
1212
`
1313
```
14-
15-
# Diagnostics
16-
```
17-
noUndeclaredVariables.ts:1:50 lint/correctness/noUndeclaredVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━
18-
19-
i The T variable is undeclared.
20-
21-
> 1 │ export type Invalid<S extends number> = `Hello ${T}`
22-
│ ^
23-
2 │
24-
3 │ export type Invalid<S extends number> = `
25-
26-
i By default, Biome recognizes browser and Node.js globals.
27-
You can ignore more globals using the javascript.globals configuration.
28-
29-
i Safe fix: Suppress rule lint/correctness/noUndeclaredVariables for this line.
30-
31-
1 │ - export·type·Invalid<S·extends·number>·=·`Hello·${T}`
32-
1 │ + export·type·Invalid<S·extends·number>·=·`Hello·${//·biome-ignore·lint/correctness/noUndeclaredVariables:·<explanation>
33-
2+ T}`
34-
2 3 │
35-
3 4 │ export type Invalid<S extends number> = `
36-
37-
i Safe fix: Suppress rule lint/correctness/noUndeclaredVariables for the whole file.
38-
39-
1 │ + /**·biome-ignore-all·lint/correctness/noUndeclaredVariables:·<explanation>·*/
40-
1 2 │ export type Invalid<S extends number> = `Hello ${T}`
41-
2 3 │
42-
43-
44-
```
45-
46-
```
47-
noUndeclaredVariables.ts:5:7 lint/correctness/noUndeclaredVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━
48-
49-
i The T variable is undeclared.
50-
51-
3 │ export type Invalid<S extends number> = `
52-
4 │ Hello
53-
> 5 │ ${T}
54-
│ ^
55-
6 │ `
56-
57-
i By default, Biome recognizes browser and Node.js globals.
58-
You can ignore more globals using the javascript.globals configuration.
59-
60-
i Safe fix: Suppress rule lint/correctness/noUndeclaredVariables for this line.
61-
62-
3 3 │ export type Invalid<S extends number> = `
63-
4 4 │ Hello
64-
5 │ - ····${T}
65-
5 │ + ····${//·biome-ignore·lint/correctness/noUndeclaredVariables:·<explanation>
66-
6+ T}
67-
6 7 │ `
68-
69-
i Safe fix: Suppress rule lint/correctness/noUndeclaredVariables for the whole file.
70-
71-
1 │ + /**·biome-ignore-all·lint/correctness/noUndeclaredVariables:·<explanation>·*/
72-
1 2 │ export type Invalid<S extends number> = `Hello ${T}`
73-
2 3 │
74-
75-
76-
```

packages/@biomejs/biome/configuration_schema.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)