ESLint - Override rules from eslint-plugin-prettier - eslint

I am using ESLint with the prettier plugin and configuration:
// eslintrc.js
extends: [
`eslint:recommended`,
`plugin:react/recommended`,
`plugin:#typescript-eslint/recommended`,
`plugin:prettier/recommended`,
`prettier/react`,
`prettier/#typescript-eslint`
]
This works great, but I would like to disable a certain prettier rule, which is removing "unneeded" parentheses (removing them actually breaks my code):
// Replace `(state.counter)` with `state.counter` eslint(prettier/prettier)
return <div>{(state.counter)}</div>
As you can see from the message above, it doesn't state which rule exactly is causing this behavior and therefore I don't know which one to override.
I have tried to override all rules found in eslint-prettier-config, but nothing worked and I don't want to keep using // eslint-disable-next-line prettier/prettier.

Prettier is not so configurable. You can try configuration they have: https://prettier.io/docs/en/configuration.html
Put .prettierrc file, or eslint config like this:
{
rules: {
'prettier/prettier': [
'error',
{
trailingComma: 'all',
tabWidth: 2,
semi: false,
singleQuote: true,
bracketSpacing: true,
eslintIntegration: true,
printWidth: 120,
},
],
}
}

It is not currently possible to disable that specific rule from prettier through configuration, but to override rules in eslint that come from the extends block, you can either write in the rule like this:
"rules": {
"prettier/prettier": "off"
"#typescript-eslint/no-use-before-define": [
"error",
{ "functions": false, "variables": true, "classes": true },
],
}
Or to only override it for a specific file pattern you can override it in the overrides block.
"overrides": [
{
"files": ["*.html"],
"rules": {
"prettier/prettier": "off",
"#typescript-eslint/unbound-method": "off"
}
}
]
Here I am showing both the config you were looking for, and an inherited rule from a nested package to show future visitors how to do it.

Related

How to adjust (not 100% override) existing eslint rule's configuration coming from config I extend?

My project's eslint config extends from airbnb:
module.exports = {
root: true,
parser: "#typescript-eslint/parser",
parserOptions: {
project: "./tsconfig.eslint.json",
},
plugins: ["#typescript-eslint"],
extends: [
"airbnb-base",
"airbnb-typescript/base"
]
};
I would like to slightly adjust the import/no-extraneous-dependencies rule which is already configured with airbnb config.
I am interested in adding one more file to allowed for devDependencies imports:
rules: {
"import/no-extraneous-dependencies": [
"error",
{
devDependencies: ["vitest.config.ts"],
},
],
},
If I do like above, then I completely overwrite the list in 'import/no-extraneous-dependencies'[1].devDependencies and the configuration I inherited from airbnb gets lost. How to add one more file to devDependencies list instead of completely overwriting the rule's configuration?
Looks like configurations of the single rule from different configs do not get somehow merged. So, if the last config which configured import/no-extraneous-dependencies rule was airbnb-typescript/base, then I just need to import it and adjust it as needed.
Below is the solution for my case:
const airbnb = require("eslint-config-airbnb-typescript/lib/shared");
const airbnbNoExtraDepsRule = airbnb.rules["import/no-extraneous-dependencies"];
airbnbNoExtraDepsRule[1].devDependencies.push("vitest.config.ts");
module.exports = {
extends: ["airbnb-base", "prettier"],
rules: {
"import/no-commonjs": "on",
"import/no-extraneous-dependencies": airbnbNoExtraDepsRule,
}
};

ESLint options - overrideConfig not loading

I installed ESLint and set these settings in setting.json but EsLint does not start properly globally for VS Code.
Invalid Options: - Unknown options: envs, globals, parserOptions, rules
- 'envs' has been removed. Please use the 'overrideConfig.env' option instead.
- 'globals' has been removed. Please use the 'overrideConfig.globals' option instead.
- 'parserOptions' has been removed. Please use the 'overrideConfig.parserOptions' option instead.
- 'rules' has been removed. Please use the 'overrideConfig.rules' option instead.
These are my settings:
"eslint.options": {
"envs": [
"es6",
"browser",
"node",
"mocha"
],
"globals": [
"expect"
],
"parserOptions": {
"ecmaVersion": 2019,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"rules": {
"semi": "error",
"curly": "error",
"quotes": [
"warn",
"single"
],
"no-undef": "error"
}
},
In case you are still looking for an answer, I just had the same issue.. You need to wrap your eslint options into an overrideConfig object. In this case the aimed object in the error message overrideConfig.(...) will be provided correctly.
Similar issue here

bracket issue use prettier with eslint

I use prettier with eslint in vs code as follow setting.
//.eslintrc
{
"parser": "babel-eslint",
"root": true,
"extends": [
"airbnb",
"plugin:vue/essential",
"plugin:prettier/recommended",
"eslint:recommended"
],
"rules": {
"no-console": 0
}
}
//.prettierc
{
"printWidth": 100,
"singleQuote": true,
"jsxBracketSameLine": true
}
but some eslint recommend conflict autoformatting from prettier.
prettier make code like this.
import { mapGetters, mapActions } from 'vuex'
(autosaving)
import {
mapGetters,
mapActions
} from 'vuex'
but now eslint draw red line.
// example
Replace `␍⏎··mapActions,␍⏎··mapGetters␍⏎` with `·mapActions,·mapGetters·`eslint(prettier/prettier)
I don't want eslint red line anywhere...
so I was found some document, but cannot found prettier setting..
how disable this red line?
Since prettier is very opinionated, it might cause trouble with es-lint sometimes. You might want to use a library like prettier-eslint
This will format your code with prettier, then try to fix it with eslint.
You can probably disable the conflicting rules as described in the prettier docs.
https://prettier.io/docs/en/eslint.html
They've mentioned adding
{ "extends": ["prettier"] }
to your .eslintrc.json might help along with other configuration.

Turn off error for console.log on my eslint

I'm trying to turn off lint warning for my eslint at VS code.
My code is contains.
console.log('blabla..');
eslint said as below.
error Unexpected console statement no-console
Even though already add no-restricted-syntax at my .eslintrc.json, no fixed.
Here is my .eslintrc.json
{
"env": {
"es6": true,
"node": true,
"amd": true,
"mocha": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
},
"rules": {
"linebreak-style": [
"error",
"windows"
],
"no-restricted-syntax": [
"error",
{
"selector": "CallExpression[callee.object.name='console'][callee.property.name=/^(log|warn|error|info|trace)$/]",
"message": "Unexpected property on console object was called"
}
]
}
}
What did I mistake?
Allowing the console.log to stay in code is requiring a separate rule.
You need to add 'no-console' statement to your eslint config rules.
Like so:
rules: {
"no-console": 0
}
A better practice is to warn you about all the console.log in your code, like so:
rules: {
"no-console": 1
}
I followed the advice of Elad and went into the package.json to add the rule of "no-console": 0 and at first it didn't seem like it worked but this is because I needed to restart the development server. If it seems like it's not working, make sure you restart the development server because if you change anything in the package-json, you have to restart your server.
You can add // eslint-disable-line if you need to ignore a specific (or multiple) line

How to tell eslint that you prefer single quotes around your strings

I'm new to eslint and it's spewing out a ton of errors telling me to use doublequotes:
error Strings must use doublequote
That's not my preference. I've got an .eslintrc file set up with the basics:
{
"env": {
"node": 1
}
}
I'd like to configure it for single quotes.
http://eslint.org/docs/rules/quotes.html
{
"env": {
"node": 1
},
"rules": {
"quotes": [2, "single", { "avoidEscape": true }]
}
}
If you're using TypeScript/ES6 you might want to include template literals (backticks). This rule prefers single quotes, and allows template literals.
TypeScript Examples
"#typescript-eslint/quotes": [
"error",
"single",
{
"allowTemplateLiterals": true
}
]
Another useful option is to allow single or double quotes as long as the string contains an escapable quote like "lorem ipsum 'donor' eta" or 'lorem ipsum "donor" eta'.
"#typescript-eslint/quotes": [
"error",
"single",
{
"avoidEscape": true,
"allowTemplateLiterals": true
}
]
References:
ESLint
https://eslint.org/docs/rules/quotes
TypeScript ESLint
https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/quotes.md
rules: {
'prettier/prettier': [
'warn',
{
singleQuote: true,
semi: true,
}
],
},
In version "eslint": "^7.21.0"
For jsx strings, if you would like to set this rule for all files, create the rule in the eslint config file.
rules: {
'jsx-quotes': [2, 'prefer-single'],
}
Or 'prefer-double' for double quotes.
If you also want to allow double quotes if they would avoid escaping a single quote inside the string, and allow template literals (the reasonable defaults, imo) try the following:
{
"env": {
"node": 1
},
"rules": {
"quotes": [2, "single", { "avoidEscape": true, "allowTemplateLiterals": true }]
}
}

Resources