Skip to content

Commit

Permalink
feat: no-implicit-globals supports exported block comment (#16343)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sosukesuzuki committed Oct 14, 2022
1 parent 35916ad commit 28d1902
Show file tree
Hide file tree
Showing 4 changed files with 401 additions and 1 deletion.
17 changes: 17 additions & 0 deletions docs/src/rules/no-implicit-globals.md
Expand Up @@ -5,6 +5,7 @@ rule_type: suggestion
related_rules:
- no-undef
- no-global-assign
- no-unused-vars
further_reading:
- https://benalman.com/news/2010/11/immediately-invoked-function-expression/
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Undeclared_var
Expand Down Expand Up @@ -255,6 +256,22 @@ window.MyGlobalFunction = (function() {

:::

### exported

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.

Examples of **correct** code for `/* exported variableName */` operation:

::: correct

```js
/* exported global_var */

var global_var = 42;
```

:::

## When Not To Use It

In the case of a browser script, if you want to be able to explicitly declare variables and functions in the global scope,
Expand Down
1 change: 1 addition & 0 deletions lib/linter/linter.js
Expand Up @@ -213,6 +213,7 @@ function addDeclaredGlobals(globalScope, configGlobals, { exportedVariables, ena

if (variable) {
variable.eslintUsed = true;
variable.eslintExported = true;
}
});

Expand Down
5 changes: 5 additions & 0 deletions lib/rules/no-implicit-globals.js
Expand Up @@ -77,6 +77,11 @@ module.exports = {
return;
}

// Variables exported by "exported" block comments
if (variable.eslintExported) {
return;
}

variable.defs.forEach(def => {
const defNode = def.node;

Expand Down

0 comments on commit 28d1902

Please sign in to comment.