Skip to content

Commit f18115c

Browse files
authored
feat!: no-shadow-restricted-names report globalThis by default (#20027)
* feat!: no-shadow-restricted-names report globalThis by default * add correct code for `reportGlobalThis: false` * update v10 migration guide
1 parent e251671 commit f18115c

5 files changed

Lines changed: 70 additions & 20 deletions

File tree

docs/src/rules/no-shadow-restricted-names.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ import { undefined as undef } from "bar";
8383

8484
This rule has an object option:
8585

86-
* `"reportGlobalThis"`: `true` (default `false`) reports declarations of `globalThis`.
86+
* `"reportGlobalThis"`: `true` (default) reports declarations of `globalThis`.
8787

8888
### reportGlobalThis
8989

90-
Examples of **incorrect** code for the `{ "reportGlobalThis": true }` option:
90+
Examples of **incorrect** code for the default `{ "reportGlobalThis": true }` option:
9191

9292
::: incorrect
9393

@@ -129,7 +129,7 @@ class globalThis {}
129129

130130
:::
131131

132-
Examples of **correct** code for the `{ "reportGlobalThis": true }` option:
132+
Examples of **correct** code for the default `{ "reportGlobalThis": true }` option:
133133

134134
::: correct
135135

@@ -146,3 +146,45 @@ import { globalThis as baz } from "foo";
146146
```
147147

148148
:::
149+
150+
Examples of **correct** code for the `{ "reportGlobalThis": false }` option:
151+
152+
::: correct
153+
154+
```js
155+
/*eslint no-shadow-restricted-names: ["error", { "reportGlobalThis": false }]*/
156+
157+
const globalThis = "foo";
158+
```
159+
160+
:::
161+
162+
::: correct
163+
164+
```js
165+
/*eslint no-shadow-restricted-names: ["error", { "reportGlobalThis": false }]*/
166+
167+
function globalThis() {}
168+
```
169+
170+
:::
171+
172+
::: correct
173+
174+
```js
175+
/*eslint no-shadow-restricted-names: ["error", { "reportGlobalThis": false }]*/
176+
177+
import { globalThis } from "bar";
178+
```
179+
180+
:::
181+
182+
::: correct
183+
184+
```js
185+
/*eslint no-shadow-restricted-names: ["error", { "reportGlobalThis": false }]*/
186+
187+
class globalThis {}
188+
```
189+
190+
:::

docs/src/use/migrate-to-10.0.0.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The lists below are ordered roughly by the number of users each change is expect
1616
### Breaking changes for users
1717

1818
- [Node.js < v20.19, v21, v23 are no longer supported](#drop-old-node)
19+
- [`no-shadow-restricted-names` now reports `globalThis` by default](#no-shadow-restricted-names)
1920

2021
### Breaking changes for plugin developers
2122

@@ -38,3 +39,22 @@ ESLint is officially dropping support for these versions of Node.js starting wit
3839
**To address:** Make sure you upgrade to at least Node.js v20.19.0 when using ESLint v10. One important thing to double check is the Node.js version supported by your editor when using ESLint via editor integrations. If you are unable to upgrade, we recommend continuing to use ESLint v9 until you are able to upgrade Node.js.
3940

4041
**Related issue(s):** [#19969](https://github.com/eslint/eslint/issues/19969)
42+
43+
## <a name="no-shadow-restricted-names"></a> `no-shadow-restricted-names` now reports `globalThis` by default
44+
45+
In ESLint v10, the [`no-shadow-restricted-names`](../rules/no-shadow-restricted-names) rule now treats `globalThis` as a restricted name by default. Consequently, the `reportGlobalThis` option now defaults to `true` (previously `false`). As a result, declarations such as `const globalThis = "foo";` or `function globalThis() {}` will now be reported by default.
46+
47+
**To address:**
48+
49+
- Rename local identifiers named `globalThis` to avoid shadowing the global.
50+
- Or restore the previous behavior by configuring the rule explicitly:
51+
52+
```json
53+
{
54+
"rules": {
55+
"no-shadow-restricted-names": ["error", { "reportGlobalThis": false }]
56+
}
57+
}
58+
```
59+
60+
**Related issue(s):** [#19673](https://github.com/eslint/eslint/issues/19673)

lib/rules/no-shadow-restricted-names.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = {
3434

3535
defaultOptions: [
3636
{
37-
reportGlobalThis: false,
37+
reportGlobalThis: true,
3838
},
3939
],
4040

lib/types/rules.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3783,7 +3783,7 @@ export interface ESLintRules extends Linter.RulesRecord {
37833783
[
37843784
Partial<{
37853785
/**
3786-
* @default false
3786+
* @default true
37873787
*/
37883788
reportGlobalThis: boolean;
37893789
}>,

tests/lib/rules/no-shadow-restricted-names.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,36 @@ ruleTester.run("no-shadow-restricted-names", rule, {
5353
},
5454
{
5555
code: "let globalThis;",
56+
options: [{ reportGlobalThis: false }],
5657
languageOptions: { ecmaVersion: 2020 },
5758
},
5859
{
5960
code: "class globalThis {}",
61+
options: [{ reportGlobalThis: false }],
6062
languageOptions: { ecmaVersion: 2020 },
6163
},
6264
{
6365
code: "import { baz as globalThis } from 'foo';",
66+
options: [{ reportGlobalThis: false }],
6467
languageOptions: {
6568
ecmaVersion: 2020,
6669
sourceType: "module",
6770
},
6871
},
6972
{
7073
code: "globalThis.foo",
71-
options: [{ reportGlobalThis: true }],
7274
languageOptions: { ecmaVersion: 2020 },
7375
},
7476
{
7577
code: "const foo = globalThis",
76-
options: [{ reportGlobalThis: true }],
7778
languageOptions: { ecmaVersion: 2020 },
7879
},
7980
{
8081
code: "function foo() { return globalThis; }",
81-
options: [{ reportGlobalThis: true }],
8282
languageOptions: { ecmaVersion: 2020 },
8383
},
8484
{
8585
code: "import { globalThis as foo } from 'bar'",
86-
options: [{ reportGlobalThis: true }],
8786
languageOptions: { ecmaVersion: 2020, sourceType: "module" },
8887
},
8988
],
@@ -425,7 +424,6 @@ ruleTester.run("no-shadow-restricted-names", rule, {
425424
},
426425
{
427426
code: "function globalThis(globalThis) { var globalThis; !function globalThis(globalThis) { try {} catch(globalThis) {} }; }",
428-
options: [{ reportGlobalThis: true }],
429427
languageOptions: { ecmaVersion: 2015 },
430428
errors: [
431429
{
@@ -462,7 +460,6 @@ ruleTester.run("no-shadow-restricted-names", rule, {
462460
},
463461
{
464462
code: "function globalThis(globalThis) { var globalThis; !function globalThis(globalThis) { try {} catch(globalThis) {} }; }",
465-
options: [{ reportGlobalThis: true }],
466463
languageOptions: { ecmaVersion: 2020 },
467464
errors: [
468465
{
@@ -499,7 +496,6 @@ ruleTester.run("no-shadow-restricted-names", rule, {
499496
},
500497
{
501498
code: "const [globalThis] = [1]",
502-
options: [{ reportGlobalThis: true }],
503499
languageOptions: { ecmaVersion: 2020 },
504500
errors: [
505501
{
@@ -511,7 +507,6 @@ ruleTester.run("no-shadow-restricted-names", rule, {
511507
},
512508
{
513509
code: "var {globalThis} = obj; var {a: globalThis} = obj; var {a: {b: {globalThis}}} = obj; var {a, ...globalThis} = obj;",
514-
options: [{ reportGlobalThis: true }],
515510
languageOptions: { ecmaVersion: 2020 },
516511
errors: [
517512
{
@@ -538,7 +533,6 @@ ruleTester.run("no-shadow-restricted-names", rule, {
538533
},
539534
{
540535
code: "let globalThis; globalThis = 5;",
541-
options: [{ reportGlobalThis: true }],
542536
languageOptions: { ecmaVersion: 2020 },
543537
errors: [
544538
{
@@ -550,7 +544,6 @@ ruleTester.run("no-shadow-restricted-names", rule, {
550544
},
551545
{
552546
code: "class globalThis {}",
553-
options: [{ reportGlobalThis: true }],
554547
languageOptions: { ecmaVersion: 2020 },
555548
errors: [
556549
{
@@ -562,7 +555,6 @@ ruleTester.run("no-shadow-restricted-names", rule, {
562555
},
563556
{
564557
code: "(class globalThis {})",
565-
options: [{ reportGlobalThis: true }],
566558
languageOptions: { ecmaVersion: 2020 },
567559
errors: [
568560
{
@@ -574,7 +566,6 @@ ruleTester.run("no-shadow-restricted-names", rule, {
574566
},
575567
{
576568
code: "import globalThis from 'foo';",
577-
options: [{ reportGlobalThis: true }],
578569
languageOptions: {
579570
ecmaVersion: 2020,
580571
sourceType: "module",
@@ -589,7 +580,6 @@ ruleTester.run("no-shadow-restricted-names", rule, {
589580
},
590581
{
591582
code: "import { globalThis } from 'foo';",
592-
options: [{ reportGlobalThis: true }],
593583
languageOptions: {
594584
ecmaVersion: 2020,
595585
sourceType: "module",
@@ -604,7 +594,6 @@ ruleTester.run("no-shadow-restricted-names", rule, {
604594
},
605595
{
606596
code: "import { baz as globalThis } from 'foo';",
607-
options: [{ reportGlobalThis: true }],
608597
languageOptions: {
609598
ecmaVersion: 2020,
610599
sourceType: "module",
@@ -619,7 +608,6 @@ ruleTester.run("no-shadow-restricted-names", rule, {
619608
},
620609
{
621610
code: "import * as globalThis from 'foo';",
622-
options: [{ reportGlobalThis: true }],
623611
languageOptions: {
624612
ecmaVersion: 2020,
625613
sourceType: "module",

0 commit comments

Comments
 (0)