admin管理员组

文章数量:1125465

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.

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.

Share Improve this question edited Oct 11, 2016 at 1:14 runtimeZero asked Jan 1, 2015 at 15:47 runtimeZeroruntimeZero 28k28 gold badges78 silver badges130 bronze badges 5
  • 173 Use eslint-disable-next-line: – Brylie Christopher Oxley Commented May 17, 2017 at 6:53
  • 7 Starting with ESLint v7, you can specify the reason for disabling a given rule in the same comment. – Dan Dascalescu Commented May 16, 2020 at 6:24
  • 9 To disable multiple specific rules: // eslint-disable-line no-console, max-len – totymedli Commented Feb 4, 2021 at 20:15
  • Should be // eslint-disable-next-line no-console, max-len – pixelpax Commented Apr 10, 2024 at 16:32
  • eslint doc on disabling-rules – tinystone Commented Jul 3, 2024 at 11:21
Add a comment  | 

14 Answers 14

Reset to default 3169

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

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 */

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

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

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

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

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

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

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

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

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

To disable all rules on a specific line:

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

To disable all rules on a specific line:

// @ts-ignore
$scope.someVar = ConstructorFunction();

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>

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

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

本文标签: