How to disable autofix for specific rules in eslint using CLI? - eslint

I want to be able to disable the rules that --fix fixes when running eslint in a console for a repo. The reason for this is because --fix leads to undesirable behavior for our jsdoc eslint rules. It creates invalid or sometimes empty jsdoc in our project- which typically leads to even more eslint rules (due to our configuration) and leads to tons of manual fixing or removal.
I want to just run --fix without it touching any jsdoc rules so that I can complete jsdoc manually- while eslint fixes all the rest of the rules.
I came across this SO question- but package mentioned in the answer does not support non-core plugins.
ESLint autofix ignore rule
Is there anything I can do short of modifying my eslintrc file every time I run the linter or using vscode for linting and modifying the config for the web editor instead? Its not a big deal for newer files- but linting legacy files is a chore when there's hundreds of spacing errors that I can't automatically fix easily.

Using an .eslintrc.js file you can do something like this:
const isFixMode = process.argv.includes('--fix');
const config = {
rules: {
'no-console': 'error',
...(isFixMode && {
'no-console': 'warn',
}),
},
};
module.exports = config;

Technically, that already exists, right? If a user wants to avoid auto-fixing a particular rule, they can use:
eslint somedir --fix --rule "somerule: 0"
Or, if they want to whitelist a set of rules, they can use &nbps --no-eslintrc and specify rules directly or use --config and point to a particular config that they want to use for fixes.

Ok here’s another idea. Convert you .eslinrc file into .eslintrc.js. This will allow you to programmatically set eslint config.
Then you could use the commander library to detect the —fix flag and set a Boolean to determine which eslint rules you would like to disable.

What worked best for me was setting the rule to warn and afterwards run.
eslint . --fix --quiet
It is not an error anymore, but better than getting my code broken by a erroneous fixer.

Related

ESLint disable default export

So far I've been unable to figure out a ready to use solution for throwing an error if something does a default export.
We internally have the standard to only do named exports, but I'd like the linter to ensure it doesn't happen.
Is there a way to accomplish this short of writing a custom rule?
EDIT: I'm assuming, but could very easily be wrong, that I could use the no-restricted-syntax rule like I was pointed to here. I just didn't want to reach for that if there was a better solution.
It would be best to use eslint-plugin-import to enforce import and export rules. It has a lovely rule to prevent default exports import/no-default-export.
npm install --save-dev eslint-plugin-import
.eslintrc
{
"plugins": [
"import"
],
"rules": {
"import/no-default-export": "error"
}
}

Can't enable no-console using eslint cli

I'm using eslint, and in my configuration file I have "no-console": "off".
I want to turn it on for my CI system, so I've been using the command line (vue cli syntax):
vue-cli-service lint --rule '"no-console": "error"'
This doesn't work.
However, if I invert things (set error in the configuration, and pass off as a flag) it does work.
Anyone know why?
EDIT: it should probably look like vue-cli-service --rule 'no-console: 2'
PS: "error" may be working too I guess.
You can make a config in a lot of places but the usual one is probably .eslintrc.js in which you can write
module.exports = {
[...]
// add your custom rules here
rules: {
"no-console": "off",
},
}
As shown here: https://eslint.org/docs/rules/no-console
This one should work but it always depend on how your project is setup too.

ESLint Vue plugin showing false positives for vue/comment-directive

After migrating from VueCLI to Vite, I have to do the linting "manually" as far as I understand; correct me if I'm wrong.
As I only want to lint my .ts and .html files (I separate them even for components), I have this script in my package json:
"lint": "eslint --ext .ts --ext .html src/"
It found some issues like missing :key in loops, but it also shows me this error for each template:
error clear vue/comment-directive
And this is always the closing tag of any root elements within my template.html
If there is only one root element I get one warning for the file, if there are multiple root elements I get a warning for each closing tag.
I don't understand what this rule complains as, according its documentation, it is there for the eslint-disable comments, which I don't have in my templates.
I had the same issue but in nuxt with eslint, i just needed to update eslint-config and eslint-module:
"#nuxtjs/eslint-config": "^5.0.0",
"#nuxtjs/eslint-module": "^3.0.1",
source: https://github.com/nuxt/eslint-plugin-nuxt/issues/121
I've just updated my npm dependencies and I have the same error.
I was reading the eslint documentation and finally I've realized that you can remove the false error if you setup the rule in the .eslintrc.js config file.
this is my .eslintrc.js config file:
module.exports = {
root: true,
env: {
browser: true,
node: true
},
parserOptions: {
parser: 'babel-eslint'
},
extends: [
'#nuxtjs',
'prettier',
'prettier/vue',
'plugin:prettier/recommended',
'plugin:nuxt/recommended'
],
plugins: [
'prettier'
],
// add your custom rules here
rules: {
"vue/comment-directive": 0
}
}
add the rule "vue/comment-directive": 0 and that is!, the error message is removed!.
the possible values are:
0 means disabled
1 means warning
2 means error
Try to change it in your IDE to how it works
(In my case I've had to stop the server and re-run it every time that I've changed a value in this config file.)
I have the same error.
I was taught how to fix this error.
https://qiita.com/tashinoso/items/a72741ca8e2fd928ca77#comment-3e6cd674353056ecbb3a
module.exports = {
...
overrides: [
{
files: ["*.vue"],
processor: "vue/.vue"
}
]
}
Set this snippet on .eslintrc.js
"vue/comment-directive": ["error", {
"reportUnusedDisableDirectives": false
}]
Solve my issue, i wonder why. Solution from documentation
Node v12.20.0
This is a kind of a temporary fix that worked for me and I think it will work for you as well.
vue/comment-directive
This rule is included in all of "plugin:vue/base", "plugin:vue/essential", "plugin:vue/vue3-essential", "plugin:vue/strongly-recommended", "plugin:vue/vue3-strongly-recommended", "plugin:vue/recommended" and "plugin:vue/vue3-recommended".
ESLint doesn't provide any API to enhance eslint-disable functionality and ESLint rules cannot affect other rules. But ESLint provides processors API.
This rule sends all eslint-disable-like comments as errors to the post-process of the .vue file processor, then the post-process removes all vue/comment-directive errors and the reported errors in disabled areas.
All you need to do is add
eslint-disable-next-line vue/component-tags-order
this line as comment above anywhere you using comments within tags in each block you need to specify if comments are added.
For more information please visit:- https://eslint.vuejs.org/rules/comment-directive.html

throwing an eslint error if disabling a rule that is not triggered

Is there an eslint rule that can throw an if you disable a rule that is not triggering an error in the line/file.
For instance, using /* eslint-disable no-console */ would throw an error if used in a file that is not using console.
Does such rule exist already? I'm heavily using eslint in a React project and there seems that there could be many practical uses for such a rule.
There's no rule to do so on the base eslint rules.
You could create one for yourself or add a plugin that adds this functionality, like:
eslint-plugin-eslint-comments
Rule: https://github.com/mysticatea/eslint-plugin-eslint-comments/blob/master/docs/rules/no-unused-disable.md
ESLint has a command line flag --report-unused-disable-directives for this purpose.
See docs for details:
https://eslint.org/docs/latest/use/command-line-interface#--report-unused-disable-directives
If you want to use a plugin, then eslint-plugin-eslint-comments as mentioned by #Wayrex is a good option.

Suppress a specific ESLint warning: File ignored by default

Hopefully this is a reasonable question but it's possible I'm misunderstanding something. I get this warning raised on 9 seemingly random .js files in my project's node_modules dependency directory when I run ESLint from grunt. For example:
node_modules/cosmiconfig/node_modules/js-yaml/lib/js-yaml/type/js/function.js
0:0 warning File ignored by default. Use "--ignore-pattern '!node_modules/*'" to override
The docs say,
/node_modules/* and /bower_components/* in the project root are ignored by default
That's all well and good, but I don't want these warnings raised every time I lint my actual code.
I have tried explicitly adding the /node_modules/* pattern to the .eslintignore in my project root, which hasn't helped. Using the command line flag suggested just makes ESLint raise errors on my dependencies, which isn't what I want either.
Is there a way to tell ESLint that I explicitly don't want to lint my dependencies so that this warning won't be raised? Having a default that raises warnings and cannot be turned off seems pretty silly.
Thanks for any help. Here are some of the relevant configs (full code is in the repo but of course node_modules is not checked in):
https://github.com/jshields/joshuashields.com/blob/master/.eslintrc.js
https://github.com/jshields/joshuashields.com/blob/master/.eslintignore
https://github.com/jshields/joshuashields.com/blob/master/package.json
Can you try adding **/node_modules/* and **/bower_components/* to see if it solves the problem? (It might be that you have nested node_modules and bower_components).
Resulting .eslintignore:
/node_modules/*
**/node_modules/*
/bower_components/*
**/bower_components/*
My problem was the files that I was targeting in my Gruntfile.js. My task was reaching down into the node_modules instead of my own code. I changed '**/js/*.js' to simply 'js/*.js':
diff --git a/Gruntfile.js b/Gruntfile.js
index 6b549f7..872abb2 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
## -10,7 +10,7 ## module.exports = function(grunt) {
//configFile:
},
files: {
- src: ['Gruntfile.js', 'stylelint.config.js', '**/js/*.js']
+ src: ['Gruntfile.js', 'stylelint.config.js', 'js/*.js']
}
}
},

Resources