ChatGPT解决这个技术问题 Extra ChatGPT

Turning off eslint rule for a specific line

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.

Use eslint-disable-next-line:
Starting with ESLint v7, you can specify the reason for disabling a given rule in the same comment.
To disable multiple specific rules: // eslint-disable-line no-console, max-len

m
mikemaccana

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


Now I get another eslint problem: warning Unexpected comment inline with code no-inline-comments :(
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.
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 had the same problem, turned out I had --no-inline-config turned on, which Prevent comments from changing config or rules
Not working with "gulp-eslint": "^3.0.1". I have to use /*eslint-disable */
S
SherylHohman

Update

ESlint has now been updated with a better way disable a single line, see @goofballLogic's excellent answer.

Old answer:

You can use the following

/*eslint-disable */

//suppress all warnings between comments
alert('foo');

/*eslint-enable */

Which is slightly buried in the "configuring rules" section of the docs;

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

/*eslint eqeqeq:0*/

BTW, it seems that the // comment syntax does not work…
Also a side note, i was looking for disabling eslint for one line in html. this works :thumbsup:
D
Darren Shewry

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


Be sure your eslint comments pass eslint! -> Expected exception block, space or tab after '/*' in comment. :)
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.
J
James Skemp

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

B
Brylie Christopher Oxley

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


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 eslint.org/docs/user-guide/configuring/rules#disabling-rules
L
Lee Goddard

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.)


No offence, but this is the same solution as the accepted answer, so not sure how this helps?
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.
@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.
@FilipDupanović Maybe eslint should have a meta rule for requiring explicit rule to ignore? Maybe it already does.
@Josef.B there's a non-standard rule for that: github.com/sindresorhus/eslint-plugin-unicorn/blob/master/docs/… It works really nicely.
R
Raymond Wachaga

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

// eslint-disable-next-line class-methods-use-this, no-unused-vars

F
Farhan Salam

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

/* eslint no-undef: "off"*/
const uploadData = new FormData();

or /* eslint-disable no-new */
4
4b0

To disable all rules on a specific line:

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

S
SherylHohman

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!)

S
Sid

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.)


Y
Yubin Shinhmar

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


it asked for one line, not the whole file
He could also stop using eslint... If your just going to ignore every file that has issue why use it at all?