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

docs: update and fix examples for no-unused-vars #17788

Merged
merged 2 commits into from Nov 24, 2023
Merged
Changes from 1 commit
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
40 changes: 35 additions & 5 deletions docs/src/rules/no-unused-vars.md
Expand Up @@ -56,6 +56,7 @@ function fact(n) {
function getY([x, y]) {
return y;
}
getY(["a", "b"]);
```

:::
Expand Down Expand Up @@ -89,6 +90,7 @@ myFunc = setTimeout(function() {
function getY([, y]) {
return y;
}
getY(["a", "b"]);
```

:::
Expand All @@ -105,23 +107,45 @@ Note that `/* exported */` has no effect for any of the following:

The line comment `// exported variableName` will not work as `exported` is not line-specific.

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

var global_var = 42;
```

Examples of **correct** code for `/* exported variableName */` operation with `no-unused-var`:
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
Examples of **correct** code for `/* exported variableName */` operation with `no-unused-var`:
Examples of **correct** code for `/* exported variableName */` operation with `no-unused-vars`:


::: correct
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
::: correct
::: correct { "sourceType": "script" }

We can add this config to make the example valid in the Playground.


```js
/*eslint no-unused-vars: "error"*/
/* exported global_var */

// rule will not report global_var
// sourcType must be set to script
// environment should not be node or commonjs
// globalReturn must be set to false

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
// rule will not report global_var
// sourcType must be set to script
// environment should not be node or commonjs
// globalReturn must be set to false

I think there's no need for additional comments because the ESLint configuration with which this will be valid is already explained at the beginning of this section. I'm guessing you added this as instructions on how to configure the Playground for this example to be valid, but with the configuration we'll pass to the Playground (#17788 (comment)) it will be valid right away.

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!

var global_var = 42;
```

:::

## Options

This rule takes one argument which can be a string or an object. The string settings are the same as those of the `vars` property (explained below).
This rule takes one argument which can be a string or an object.

By default this rule is enabled with `all` option for variables and `after-used` for arguments.
String setting has two possible modes `all` and `local` to allow global and local variables (works similar as `vars` option explained below).

```json
{
"rules": {
"no-unused-vars": ["error", "local"]
}
}
```

Object setting has several options.

```json
{
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 section was fine and seems better to leave it as is. The description of string settings refers to the vars property which is explained in detail below (note that "local" allows unused global variables, and neither of these two allows unused local variables, so the added description might be confusing). The config below is intended to show defaults for this rule, not an example of the object option.

Expand All @@ -131,6 +155,8 @@ By default this rule is enabled with `all` option for variables and `after-used`
}
```

By default this rule is enabled with `all` option for variables and `after-used` for arguments.

### vars

The `vars` option has two settings:
Expand Down Expand Up @@ -386,11 +412,15 @@ Examples of **correct** code for the `{ "ignoreRestSiblings": true }` option:

```js
/*eslint no-unused-vars: ["error", { "ignoreRestSiblings": true }]*/

// 'foo' and 'bar' were ignored because they have a rest property sibling.
var { foo, ...coords } = data;
var { foo, ...rest } = data;
console.log(rest);

// OR

var bar;
({ bar, ...coords } = data);
({ bar, ...rest } = data);
```

:::
Expand Down