4

How to disable the rule of discouraging the use of var and encouraging the use of const or let instead on ESlint?

Ninja
  • 83
  • 1
  • 1
  • 6

3 Answers3

9

In your package.json (assuming that is what you are using), include:

"eslintConfig": {
    "rules": {
        "no-var": 0
    }
}

no-var is the rule, and 0 sets the rule to "off".

If you're not using package.json, you can set the the same in an .eslintrc.js, or, on a per-file basis, include a comment at the top of the file /* eslint no-var: 0 */.

All this comes from the ESlint Configuration Documentation.

Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
3

Extending the previous answer, all of these variants work for comments at the beginning of the block:

/* eslint no-var: off */
...

/* eslint no-var: */
...

/* eslint no-var: 0 */
...
Roman
  • 19,236
  • 15
  • 93
  • 97
0

The valid cases for using var are extremely limited, so it's good to consider disabling the no-var rule only on specific lines where's it's justified.

The //eslint-disable-line [RULE] comment can be used to turn off a single rule on a single line.

For example, if you're working with the Google Tracking dataLayer array:

declare global { var dataLayer: unknown[] }  //eslint-disable-line no-var
...
globalThis.dataLayer.push(args);

For more details:
https://eslint.org/docs/latest/use/configure/rules#disabling-rules

Dem Pilafian
  • 5,625
  • 6
  • 39
  • 67