Skip to content

Commit 28d1902

Browse files
author
Sosuke Suzuki
authored
feat: no-implicit-globals supports exported block comment (#16343)
* feat: support `"exported"` block comments test: add valid tests test: add invalid tests * docs: mention about `exported` comment docs: add correct example * chore: use `variable.eslintExported` directly * docs: add `:::` to close correct example * fix: fix `exported` comment for multiple variables * fix: add more invalid tests for global variable leaks
1 parent 35916ad commit 28d1902

4 files changed

Lines changed: 401 additions & 1 deletion

File tree

docs/src/rules/no-implicit-globals.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ rule_type: suggestion
55
related_rules:
66
- no-undef
77
- no-global-assign
8+
- no-unused-vars
89
further_reading:
910
- https://benalman.com/news/2010/11/immediately-invoked-function-expression/
1011
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Undeclared_var
@@ -255,6 +256,22 @@ window.MyGlobalFunction = (function() {
255256

256257
:::
257258

259+
### exported
260+
261+
You can use `/* exported variableName */` block comments in the same way as in [`no-unused-vars`](./no-unused-vars). See the [`no-unused-vars` exported section](./no-unused-vars#exported) for details.
262+
263+
Examples of **correct** code for `/* exported variableName */` operation:
264+
265+
::: correct
266+
267+
```js
268+
/* exported global_var */
269+
270+
var global_var = 42;
271+
```
272+
273+
:::
274+
258275
## When Not To Use It
259276

260277
In the case of a browser script, if you want to be able to explicitly declare variables and functions in the global scope,

lib/linter/linter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ function addDeclaredGlobals(globalScope, configGlobals, { exportedVariables, ena
213213

214214
if (variable) {
215215
variable.eslintUsed = true;
216+
variable.eslintExported = true;
216217
}
217218
});
218219

lib/rules/no-implicit-globals.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ module.exports = {
7777
return;
7878
}
7979

80+
// Variables exported by "exported" block comments
81+
if (variable.eslintExported) {
82+
return;
83+
}
84+
8085
variable.defs.forEach(def => {
8186
const defNode = def.node;
8287

0 commit comments

Comments
 (0)