Skip to content

Commit bd90ce6

Browse files
docs(linter): Improve the docs and add test cases for eslint-no-shadow-restricted-names (#9597)
- Document the `Why is this bad section` and add add correctness examples - Add test cases for the rule which were in the eslint docs. Relates to #6050. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent a0c9f7c commit bd90ce6

2 files changed

Lines changed: 89 additions & 2 deletions

File tree

crates/oxc_linter/src/rules/eslint/no_shadow_restricted_names.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,27 @@ pub struct NoShadowRestrictedNames;
1818
declare_oxc_lint!(
1919
/// ### What it does
2020
///
21-
/// Disallow redefine the global variables like 'undefined', 'NaN', 'Infinity', 'eval', 'arguments'.
21+
/// Disallows the redefining of global variables such as `undefined`, `NaN`, `Infinity`, `eval`
22+
/// and `arguments`.
2223
///
2324
/// ### Why is this bad?
2425
///
26+
/// Value properties of the Global Object `NaN`, `Infinity`, `undefined` as well as the strict
27+
/// mode restricted identifiers `eval` and `arguments` are considered to be restricted names in
28+
/// JavaScript. Defining them to mean something else can have unintended consequences and
29+
/// confuse others reading the code. For example, there’s nothing preventing you from
30+
/// writing:
2531
///
26-
/// ### Example
32+
/// ```javascript
33+
/// var undefined = "foo";
34+
/// ```
35+
///
36+
/// Then any code used within the same scope would not get the global undefined, but rather the
37+
/// local version with a very different meaning.
38+
///
39+
/// ### Examples
40+
///
41+
/// Examples of **incorrect** code for this rule:
2742
/// ```javascript
2843
/// function NaN(){}
2944
///
@@ -33,6 +48,28 @@ declare_oxc_lint!(
3348
///
3449
/// try {} catch(eval){}
3550
/// ```
51+
///
52+
/// ```javascript
53+
/// import NaN from "foo";
54+
///
55+
/// import { undefined } from "bar";
56+
///
57+
/// class Infinity {}
58+
/// ```
59+
///
60+
/// Examples of **correct** code for this rule:
61+
/// ```javascript
62+
/// var Object;
63+
///
64+
/// function f(a, b){}
65+
///
66+
/// // Exception: `undefined` may be shadowed if the variable is never assigned a value.
67+
/// var undefined;
68+
/// ```
69+
///
70+
/// ```javascript
71+
/// import { undefined as undef } from "bar";
72+
/// ```
3673
NoShadowRestrictedNames,
3774
eslint,
3875
correctness
@@ -97,6 +134,7 @@ fn test() {
97134
"parserOptions": { "ecmaVersion": 2019 }
98135
})),
99136
),
137+
("var Object;", None),
100138
("var undefined;", None),
101139
("var undefined;var undefined", None),
102140
(
@@ -115,9 +153,13 @@ fn test() {
115153
})),
116154
),
117155
("var normal, undefined; var undefined;", None),
156+
(r#"import { undefined as undef } from "bar";"#, None),
118157
];
119158

120159
let fail = vec![
160+
("var undefined = 5;", None),
161+
("function NaN(){}", None),
162+
("try {} catch(eval){}", None),
121163
("function NaN(NaN) { var NaN; !function NaN(NaN) { try {} catch(NaN) {} }; }", None),
122164
(
123165
"function undefined(undefined) { !function undefined(undefined) { try {} catch(undefined) {} }; }",
@@ -161,6 +203,9 @@ fn test() {
161203
("class undefined { }", None),
162204
("class foo { undefined(undefined) { } }", None),
163205
("class foo { #undefined(undefined) { } }", None),
206+
("class Infinity {}", None),
207+
(r#"import { undefined } from "bar";"#, None),
208+
(r#"import NaN from "foo";"#, None),
164209
];
165210

166211
Tester::new(NoShadowRestrictedNames::NAME, NoShadowRestrictedNames::PLUGIN, pass, fail)

crates/oxc_linter/src/snapshots/eslint_no_shadow_restricted_names.snap

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
---
22
source: crates/oxc_linter/src/tester.rs
33
---
4+
eslint(no-shadow-restricted-names): Shadowing of global properties such as 'undefined' is not allowed.
5+
╭─[no_shadow_restricted_names.tsx:1:5]
6+
1var undefined = 5;
7+
· ─────────
8+
╰────
9+
help: Shadowing of global properties 'undefined'.
10+
11+
eslint(no-shadow-restricted-names): Shadowing of global properties such as 'undefined' is not allowed.
12+
╭─[no_shadow_restricted_names.tsx:1:10]
13+
1function NaN(){}
14+
· ───
15+
╰────
16+
help: Shadowing of global properties 'NaN'.
17+
18+
eslint(no-shadow-restricted-names): Shadowing of global properties such as 'undefined' is not allowed.
19+
╭─[no_shadow_restricted_names.tsx:1:14]
20+
1try {} catch(eval){}
21+
· ────
22+
╰────
23+
help: Shadowing of global properties 'eval'.
24+
425
eslint(no-shadow-restricted-names): Shadowing of global properties such as 'undefined' is not allowed.
526
╭─[no_shadow_restricted_names.tsx:1:10]
627
1function NaN(NaN) { var NaN; !function NaN(NaN) { try {} catch(NaN) {} }; }
@@ -315,3 +336,24 @@ source: crates/oxc_linter/src/tester.rs
315336
· ─────────
316337
╰────
317338
help: Shadowing of global properties 'undefined'.
339+
340+
eslint(no-shadow-restricted-names): Shadowing of global properties such as 'undefined' is not allowed.
341+
╭─[no_shadow_restricted_names.tsx:1:7]
342+
1class Infinity {}
343+
· ────────
344+
╰────
345+
help: Shadowing of global properties 'Infinity'.
346+
347+
eslint(no-shadow-restricted-names): Shadowing of global properties such as 'undefined' is not allowed.
348+
╭─[no_shadow_restricted_names.tsx:1:10]
349+
1import { undefined } from "bar";
350+
· ─────────
351+
╰────
352+
help: Shadowing of global properties 'undefined'.
353+
354+
eslint(no-shadow-restricted-names): Shadowing of global properties such as 'undefined' is not allowed.
355+
╭─[no_shadow_restricted_names.tsx:1:8]
356+
1import NaN from "foo";
357+
· ───
358+
╰────
359+
help: Shadowing of global properties 'NaN'.

0 commit comments

Comments
 (0)