Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: no-implicit-globals supports exported block comment #16343

Merged
merged 6 commits into from Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:::

To close ::: correct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! cdb5e89

:::

## 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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a new property variable.eslintExported.
This represents the variable specified by the /* exported variableName */ comment (It represents export only unlike eslintUsed).

I'm not sure if this is a good. Is there a better way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the right approach. variable.eslintUsed could have been set to true by another rule via context.markVariableAsUsed(), so it's not a reliable indicator that the variable is intended to be used by other scripts, which is what we need in this rule and what /* exported */ means.

But, this is a change in the core so I'd like more opinions from @eslint/eslint-tsc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me.

}
});

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