-
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lint/noGlobalAssign): add rule (#1377)
Co-authored-by: Victorien Elvinger <victorien@elvinger.fr>
- Loading branch information
Showing
17 changed files
with
491 additions
and
57 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
91 changes: 91 additions & 0 deletions
91
crates/biome_js_analyze/src/semantic_analyzers/nursery/no_global_assign.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,91 @@ | ||
use crate::globals::browser::BROWSER; | ||
use crate::globals::node::NODE; | ||
use crate::globals::runtime::{BUILTIN, ES_2021}; | ||
|
||
use crate::semantic_services::SemanticServices; | ||
use biome_analyze::{context::RuleContext, declare_rule, Rule, RuleDiagnostic}; | ||
use biome_console::markup; | ||
use biome_js_syntax::{JsSyntaxKind, TextRange}; | ||
|
||
declare_rule! { | ||
/// Disallow assignments to native objects and read-only global variables. | ||
/// | ||
/// _JavaScript environments contain numerous built-in global variables, such as `window` in browsers and `process` in _Node.js. | ||
/// Assigning values to these global variables can be problematic as it can override essential functionality. | ||
/// | ||
/// Source: https://eslint.org/docs/latest/rules/no-global-assign | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// Object = null; | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// window = {}; | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// undefined = true; | ||
/// ``` | ||
/// | ||
/// ## Valid | ||
/// | ||
/// ```js | ||
/// a = 0; | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// let window; | ||
/// window = {}; | ||
/// ``` | ||
pub(crate) NoGlobalAssign { | ||
version: "next", | ||
name: "noGlobalAssign", | ||
recommended: true, | ||
} | ||
} | ||
|
||
impl Rule for NoGlobalAssign { | ||
type Query = SemanticServices; | ||
type State = TextRange; | ||
type Signals = Vec<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let global_refs = ctx.query().all_unresolved_references(); | ||
let mut result = vec![]; | ||
for global_ref in global_refs { | ||
let is_write = global_ref.syntax().kind() == JsSyntaxKind::JS_IDENTIFIER_ASSIGNMENT; | ||
if is_write { | ||
let identifier = global_ref.syntax().text_trimmed(); | ||
let text = identifier.to_string(); | ||
let is_global_var = NODE.binary_search(&text.as_str()).is_ok() | ||
|| BROWSER.binary_search(&text.as_str()).is_ok() | ||
|| BUILTIN.binary_search(&text.as_str()).is_ok() | ||
|| ES_2021.binary_search(&text.as_str()).is_ok(); | ||
if is_global_var { | ||
result.push(*global_ref.range()); | ||
} | ||
} | ||
} | ||
result | ||
} | ||
|
||
fn diagnostic(_ctx: &RuleContext<Self>, range: &Self::State) -> Option<RuleDiagnostic> { | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
range, | ||
markup! { | ||
"A global variable should not be reassigned." | ||
}, | ||
) | ||
.note(markup! { | ||
"Assigning to a global variable can override essential functionality." | ||
}), | ||
) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
crates/biome_js_analyze/tests/specs/nursery/noGlobalAssign/invalid.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,8 @@ | ||
window = {}; | ||
Object = null; | ||
undefined = 1; | ||
length = 1; | ||
top = 1; | ||
String++; | ||
({Object = 0, String = 0} = {}); | ||
require = 0; |
174 changes: 174 additions & 0 deletions
174
crates/biome_js_analyze/tests/specs/nursery/noGlobalAssign/invalid.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,174 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```js | ||
window = {}; | ||
Object = null; | ||
undefined = 1; | ||
length = 1; | ||
top = 1; | ||
String++; | ||
({Object = 0, String = 0} = {}); | ||
require = 0; | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.js:1:1 lint/nursery/noGlobalAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! A global variable should not be reassigned. | ||
> 1 │ window = {}; | ||
│ ^^^^^^^ | ||
2 │ Object = null; | ||
3 │ undefined = 1; | ||
i Assigning to a global variable can override essential functionality. | ||
``` | ||
|
||
``` | ||
invalid.js:1:13 lint/nursery/noGlobalAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! A global variable should not be reassigned. | ||
> 1 │ window = {}; | ||
│ | ||
> 2 │ Object = null; | ||
│ ^^^^^^^ | ||
3 │ undefined = 1; | ||
4 │ length = 1; | ||
i Assigning to a global variable can override essential functionality. | ||
``` | ||
|
||
``` | ||
invalid.js:2:15 lint/nursery/noGlobalAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! A global variable should not be reassigned. | ||
1 │ window = {}; | ||
> 2 │ Object = null; | ||
│ | ||
> 3 │ undefined = 1; | ||
│ ^^^^^^^^^^ | ||
4 │ length = 1; | ||
5 │ top = 1; | ||
i Assigning to a global variable can override essential functionality. | ||
``` | ||
|
||
``` | ||
invalid.js:3:15 lint/nursery/noGlobalAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! A global variable should not be reassigned. | ||
1 │ window = {}; | ||
2 │ Object = null; | ||
> 3 │ undefined = 1; | ||
│ | ||
> 4 │ length = 1; | ||
│ ^^^^^^^ | ||
5 │ top = 1; | ||
6 │ String++; | ||
i Assigning to a global variable can override essential functionality. | ||
``` | ||
|
||
``` | ||
invalid.js:4:12 lint/nursery/noGlobalAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! A global variable should not be reassigned. | ||
2 │ Object = null; | ||
3 │ undefined = 1; | ||
> 4 │ length = 1; | ||
│ | ||
> 5 │ top = 1; | ||
│ ^^^^ | ||
6 │ String++; | ||
7 │ ({Object = 0, String = 0} = {}); | ||
i Assigning to a global variable can override essential functionality. | ||
``` | ||
|
||
``` | ||
invalid.js:5:9 lint/nursery/noGlobalAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! A global variable should not be reassigned. | ||
3 │ undefined = 1; | ||
4 │ length = 1; | ||
> 5 │ top = 1; | ||
│ | ||
> 6 │ String++; | ||
│ ^^^^^^ | ||
7 │ ({Object = 0, String = 0} = {}); | ||
8 │ require = 0; | ||
i Assigning to a global variable can override essential functionality. | ||
``` | ||
|
||
``` | ||
invalid.js:7:3 lint/nursery/noGlobalAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! A global variable should not be reassigned. | ||
5 │ top = 1; | ||
6 │ String++; | ||
> 7 │ ({Object = 0, String = 0} = {}); | ||
│ ^^^^^^^ | ||
8 │ require = 0; | ||
i Assigning to a global variable can override essential functionality. | ||
``` | ||
|
||
``` | ||
invalid.js:7:15 lint/nursery/noGlobalAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! A global variable should not be reassigned. | ||
5 │ top = 1; | ||
6 │ String++; | ||
> 7 │ ({Object = 0, String = 0} = {}); | ||
│ ^^^^^^^ | ||
8 │ require = 0; | ||
i Assigning to a global variable can override essential functionality. | ||
``` | ||
|
||
``` | ||
invalid.js:7:33 lint/nursery/noGlobalAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! A global variable should not be reassigned. | ||
5 │ top = 1; | ||
6 │ String++; | ||
> 7 │ ({Object = 0, String = 0} = {}); | ||
│ | ||
> 8 │ require = 0; | ||
│ ^^^^^^^^ | ||
i Assigning to a global variable can override essential functionality. | ||
``` | ||
|
||
|
4 changes: 4 additions & 0 deletions
4
crates/biome_js_analyze/tests/specs/nursery/noGlobalAssign/valid.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,4 @@ | ||
a = 1; | ||
|
||
let window; | ||
window = {}; |
14 changes: 14 additions & 0 deletions
14
crates/biome_js_analyze/tests/specs/nursery/noGlobalAssign/valid.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,14 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: valid.js | ||
--- | ||
# Input | ||
```js | ||
a = 1; | ||
|
||
let window; | ||
window = {}; | ||
|
||
``` | ||
|
||
|
Oops, something went wrong.