1890

In order to turn off linting rule for a particular line in JSHint we use the following rule:

/* jshint ignore:start*/
$scope.someVar = ConstructorFunction();
/* jshint ignore:end */

I have been trying to locate the equivalent of the above for eslint.

runtimeZero
  • 26,466
  • 27
  • 73
  • 126

14 Answers14

2966

To disable next line:

// eslint-disable-next-line no-use-before-define
var thing = new Thing();

Or use the single line syntax:

var thing = new Thing(); // eslint-disable-line no-use-before-define

See the eslint docs


Thanks to KhalilRavanna comment

if you don't care about specificity you can just do //eslint-disable-line and it appears to disable all rules for the given line

var thing = new Thing() // eslint-disable-line
zcoop98
  • 2,590
  • 1
  • 18
  • 31
goofballLogic
  • 37,883
  • 8
  • 44
  • 62
  • 9
    Now I get another eslint problem: warning Unexpected comment inline with code no-inline-comments :( – arcseldon Dec 19 '15 at 10:59
  • 86
    Works great for me. Also if you don't care about specificity you can just do `//eslint-disable-line` and it appears to disable all rules for the given line. – KhalilRavanna Jan 22 '16 at 17:08
  • 7
    For some reason this doesn't work for me; I'm running eslint 3.8.0. I have to use /*eslint-disable */ and /*eslint-enable */. Any idea why this might be? I like the single line approach – SomethingOn Oct 17 '16 at 14:43
  • 14
    @SomethingOn had the same problem, turned out I had `--no-inline-config` turned on, which `Prevent comments from changing config or rules` – tibalt Dec 20 '16 at 11:18
  • 4
    Not working with "gulp-eslint": "^3.0.1". I have to use /*eslint-disable */ – olefrank Jan 22 '17 at 16:46
  • 1
    `eslint-disable-next-line` is probably best as a lot of linters will auto format to move the inline comment to next. eg. in a case you need to disable for a variable that is a function name (in our case its unchangable legacy) – mewc Jun 20 '19 at 00:25
  • Btw, you could not add comments after this until very recently (v7 2020) which added a new feature to allow you to add `-- my comment here` after you disable the rule: https://github.com/eslint/eslint/commit/cf46df70158a4ed4c09d5c9d655c07dc6df3ff5e – Joshua Pinter Apr 29 '20 at 03:51
  • It seems that I had `tslint:disable-next-line`, and it didn't work. The `eslint-disable-next-line` command works better. – gearcoded Sep 27 '21 at 14:50
  • Yuck. This is pollution. eslint is supposed to make things *pretty* if you're doing valid things. – Wyck Nov 03 '21 at 02:14
  • In typescript, if setting the name of the rule, I have to add `@typescript-eslint/` before the rule name: `// eslint-disable-line @typescript-eslint/no-unused-vars` – Hawkeye Parker Mar 27 '23 at 22:13
671

Update

ESlint has now been updated with a better way disable a single line

// eslint-disable-next-line $rulename

see @goofballLogic's excellent answer.

NOTE Though, the above not working for jsx eg React webapp


Old answer

You can use the following pair of disable/ enable comments - please note only /*eslint ... */ syntax working, //eslint ... NOT working

/* eslint-disable */
alert('suppress all warnings between comments')
/* eslint-enable */

/* eslint-disable eqeqeq */
alert('suppress specific warning eg eqeqeq between comments')
/* eslint-enable eqeqeq */

To disable a specific warning e.g. eqeqeq for an entire file, you can include a comment at the top of the file:

/* eslint eqeqeq:0 */

And when multi-rule to ignore, list them ON 1 LINE separated with a space

/* eslint jsx-a11y/alt-text:0 react/jsx-no-duplicate-props:0 */
zcoop98
  • 2,590
  • 1
  • 18
  • 31
Nick Tomlin
  • 28,402
  • 11
  • 61
  • 90
268

You can also disable a specific rule/rules (rather than all) by specifying them in the enable (open) and disable (close) blocks:

/* eslint-disable no-alert, no-console */

alert('foo');
console.log('bar');

/* eslint-enable no-alert */

via @goofballMagic's link above: http://eslint.org/docs/user-guide/configuring.html#configuring-rules

Darren Shewry
  • 10,179
  • 4
  • 50
  • 46
  • 7
    Be sure your eslint comments pass eslint! -> `Expected exception block, space or tab after '/*' in comment.` :) – Rimian Dec 29 '17 at 00:57
  • I use a combination of `prettier` and `eslint` to format my code. This does not allow for inline comments. Many `/* eslint-disable-next-line ... */` statements are hard to read and to spot in the code. – Bernhard Döbler Nov 20 '18 at 13:13
130

From Configuring ESLint - Disabling Rules with Inline Comments:

/* eslint-disable no-alert, no-console */


/* eslint-disable */

alert('foo');

/* eslint-enable */


/* eslint-disable no-alert, no-console */

alert('foo');
console.log('bar');

/* eslint-enable no-alert, no-console */


/* eslint-disable */

alert('foo');


/* eslint-disable no-alert */

alert('foo');


alert('foo'); // eslint-disable-line

// eslint-disable-next-line
alert('foo');


alert('foo'); // eslint-disable-line no-alert

// eslint-disable-next-line no-alert
alert('foo');


alert('foo'); // eslint-disable-line no-alert, quotes, semi

// eslint-disable-next-line no-alert, quotes, semi
alert('foo');


foo(); // eslint-disable-line example/rule-name
James Skemp
  • 8,018
  • 9
  • 64
  • 107
86

Answer

You can use an inline comment: // eslint-disable-next-line rule-name.

Example

// eslint-disable-next-line no-console
console.log('eslint will ignore the no-console on this line of code');

Reference

ESLint - Disabling Rules with Inline Comments

Brylie Christopher Oxley
  • 1,684
  • 1
  • 17
  • 34
  • Just fwiw, it appears the most recent docs page (as of today, at least) with examples of how to temporarily turn things off with comments has moved from the one you provide to https://eslint.org/docs/user-guide/configuring/rules#disabling-rules – ruffin Feb 08 '21 at 14:30
33

The general end of line comment, // eslint-disable-line, does not need anything after it: no need to look up a code to specify what you wish ES Lint to ignore.

If you need to have any syntax ignored for any reason other than a quick debugging, you have problems: why not update your delint config?

I enjoy // eslint-disable-line to allow me to insert console for a quick inspection of a service, without my development environment holding me back because of the breach of protocol. (I generally ban console, and use a logging class - which sometimes builds upon console.)

Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
  • No offence, but this is the same solution as the accepted answer, so not sure how this helps? – Darren Shewry Jan 28 '16 at 10:23
  • 31
    Do NOT do this. If you have 5 errors on the line, they will all be ignored. Always explicitly set the rule which is to be ignored or the kids will make a mess. – Filip Dupanović Mar 17 '16 at 10:48
  • 3
    @FilipDupanović If your programmers can make a mess within a single line of code, just because eslint is not looking over their shoulders for a single line. There is really something else going wrong at your company... That said, saying what rule you wish to ignore makes it more clear why you put the disable line there in the first place. – Edwin Stoteler May 02 '16 at 14:14
  • @EdwinStoteler Well, then the software would have 2 problems: the programmers AND the sloppiness in the eslint rules. It's no excuse moving the fault to just on one and harder to fix cause of the problem. – Andre Figueiredo Oct 08 '16 at 14:21
  • 2
    @FilipDupanović Maybe eslint should have a meta rule for requiring explicit rule to ignore? Maybe it already does. – Josef.B Dec 03 '16 at 15:39
  • 1
    @Josef.B there's a non-standard rule for that: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/master/docs/rules/no-abusive-eslint-disable.md It works really nicely. – Craig Harley Jul 18 '19 at 14:10
  • 'Do not do this' you say — but if your client insists against all reason on running eslint non-stop during the dev process whilst watching files, being told 'won't compile as you have a two spaces instead of one, and a console.log' is reallyl annoying. Ofc use of pre-push hooks (even pre-commit if you must) would help there, but you can't tell some clients, even when they pay you to tell them. – Lee Goddard Jul 19 '19 at 17:37
22

Or for multiple ignores on the next line, string the rules using commas

// eslint-disable-next-line class-methods-use-this, no-unused-vars
Wachaga Mwaura
  • 3,310
  • 3
  • 28
  • 31
13

To disable a single rule for the rest of the file below:

/* eslint no-undef: "off"*/
const uploadData = new FormData();
Farhan Salam
  • 1,257
  • 11
  • 16
13

To disable all rules on a specific line:

alert('foo'); // eslint-disable-line
4b0
  • 21,981
  • 30
  • 95
  • 142
Debbie Ellis
  • 131
  • 1
  • 4
13

My answer, similar to others given, but shows how you can also add a comment to yourself on the same line.

// eslint-disable-line // THIS WON"T WORK

Use -- if you also need to write a comment on that line (eg. maybe why eslint is disabled)

// eslint-disable-line -- comment to self (This DOES work)

Can be used in conjunction with specific eslint rules to ignore:

// eslint-disable-line no-console -- comment to self (This Also Works!)
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
2

To disable all rules on a specific line:

// @ts-ignore
$scope.someVar = ConstructorFunction();
Pervez
  • 516
  • 6
  • 10
  • 1
    As of this date in 2023, this works in VSCode, and in typescriptlang.org Playground, when all other ESLint rules provided here do not work. There is probably something specific to the ESLint rules themselves, that preclude the other directives from working, perhaps a difference between TS and ES? Anyone? – TonyG May 26 '23 at 21:09
  • @TonyG In VS 2022 with TS/React, I had to add a qualifier before the rule name: `// eslint-disable-next-line @typescript-eslint/no-empty-interface`. The warning tooltip told me the name. – Tim M. Jun 14 '23 at 23:14
1

single line comment did not work for me inside a react dumb functional component, I have used file level disabling by adding /* eslint-disable insertEslintErrorDefinitionHere */

(normally if you are using vs code and getting eslint error, you can click on the line which gives error and a bulb would show up in vs code, right click on the light bulb and choose any disable option and vs code will do it for you.)

Sid
  • 189
  • 1
  • 4
1

I have disabled that perticular line some thing below

<TableRow
   key={index}
   className={classes.tableRow}
   onClick={this.onSelectedValue.bind(this, user)} // eslint-disable-line
>{index}
</TableRow>
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
-8

You can add the files which give error to .eslintignore file in your project.Like for all the .vue files just add /*.vue