ESLint extends vs plugins v2020 - javascript

There's answered question which in my opinion doesn't actually answers the question, on the difference between extends: [] vs plugins: [] in ESLint.
In my case, i just used extends section:
extends: [
'plugin:#typescript-eslint/recommended',
],
plugins: [],
rules: {
'#typescript-eslint/explicit-function-return-type': [
'error',
{
allowExpressions: true,
},
],
}
As you can see, i just used predefined config from plugin:#typescript-eslint/recommended and also overwritten #typescript-eslint/explicit-function-return-type rule in rules: {} section. But why do we need this PLUGINS section then? If everything works without it? What do i miss?

You have done it correctly.
For your example, there are 2 ways to do add typescript-eslint...
1st way:
{
parser: "#typescript-eslint/parser",
parserOptions: { sourceType: "module" },
plugins: ["#typescript-eslint"],
extends: [],
rules: {
"#typescript-eslint/explicit-function-return-type": [
"error",
{
allowExpressions: true
}
]
}
}
2nd way:
{
plugins: [],
extends: ["plugin:#typescript-eslint/recommended"],
rules: {
"#typescript-eslint/explicit-function-return-type": [
"error",
{
allowExpressions: true
}
]
}
}
The difference is...
1st way:
parser, parserOptions and plugins are manually added,
Only #typescript-eslint/explicit-function-return-type is enforced.
2nd way:
plugin:#typescript-eslint/recommended has automatically added parser, parserOptions and plugins.
Recommended typescript rules are added and enforced.
#typescript-eslint/explicit-function-return-type is augmented and enforced.
This is why when you use plugin:#typescript-eslint/recommended, things work normally even if plugins is empty. A well-written plugins/configs allows that to happen.

Related

Does somebody know a workaround for this problem with eslint

I am using lint staged and husky to verify the commits, however since today it shows me the following error in the precommit logs.
ESLint: 8.30.0
Error: .eslintrc.cjs:
Configuration for rule "indent" is invalid:
Value {"allowIndentationTabs":true} should be equal to one of the allowed values.
Value {"allowIndentationTabs":true} should be integer.
Value {"allowIndentationTabs":true} should match exactly one schema in oneOf.
This is my config in the .eslintrc.cjs file
module.exports = {
env: {
browser: true,
es2021: true
},
extends: [
'plugin:react/recommended',
'standard'
],
overrides: [
],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
},
plugins: [
'react'
],
rules: {
indent: ['error', { allowIndentationTabs: true }]
}
}
I have been trying different solutions but in the end they all give the same error
indent rule doesn't have that option
no-tabs has it but I guess it's not what you wanted

Align JavaScript imports "from" Statement to be Vertically Aligned

I would like the end result of my imports to be like this, via a tool that can automatically format my code onSave:
import { Stack, StackProps, Duration, Resource } from "aws-cdk-lib";
import { LambdaStack } from "./lambda-Stack";
import { Construct } from "constructs";
How can I align all of the "from" statements vertically in VS Code? I've looked at both prettier and eslint.
Prettier doesn't care what you want. :-) It's an opinionated tool useful for applying consistency to code within teams (and for avoiding arguments about different formatting styles [although you just get arguments about whether to use Prettier instead]).
If you don't want Prettier's formatting, don't use Prettier. There are other code formatters, some of which offer more control than Prettier does.
You could tell Prettier to ignore each of those import statements:
// prettier-ignore
import { Stack, StackProps, Duration, Resource } from "aws-cdk-lib";
// prettier-ignore
import { LambdaStack } from "./lambda-Stack";
// prettier-ignore
import { Construct } from "constructs";
or
/* prettier-ignore*/ import { Stack, StackProps, Duration, Resource } from "aws-cdk-lib";
/* prettier-ignore*/ import { LambdaStack } from "./lambda-Stack";
/* prettier-ignore*/ import { Construct } from "constructs";
For now there's no "block ignore" in Prettier for JavaScript (only for certain select other languages), although there's an open request for one.
Usually, with prettier or eslint you might want to limit printWidth to defined number of caracter per line. So imagine if you have many imports or long module name :
/* You better should break line overhere |----------| */
import { DepA, DepB, DepC, DepD, DepE, DepF, DepG, DepH, DepI, DepJ } from "./some-long-named-module";
import { OnlyOneImport } from "./other-module";
So my answer is not responding to "how can you align 'from' statements?" but it
may open another question 'should you ?'
Here is a common way to indent imports :
// common way to write import with vertical (Automatable)
import {
DepA,
DepB,
DepC,
DepD,
DepE,
DepF,
DepG,
DepH,
DepI,
DepJ
} from "./some-long-named-module";
import { OnlyOneImport } from "./other-module";
Here is the eslint rule to auto indent your code : https://eslint.org/docs/latest/rules/object-curly-newline
example for object-curly-newline rule in eslint:
# .estlintrc.json
{
...
"rules": {
...
"object-curly-newline": [
"error",
{
"consistent": true,
"multiline": true
}
]
}
}
PS:
Here some example of how I use it
# .estlintrc.json
{
"root": true,
"extends": [
"airbnb-base", // See https://www.npmjs.com/package/eslint-config-airbnb
"airbnb-base/whitespace",
"plugin:jest/recommended", // See https://www.npmjs.com/package/eslint-plugin-jest
"prettier" // See https://github.com/prettier/eslint-config-prettier
],
"env": {
"jest/globals": true
},
"plugins": [
"jest",
"...whatever-you-want"
],
"ignorePatterns": [
"dist/",
"node_modules/",
"...whatever-you-want"
],
"rules": {
"no-restricted-syntax": [
"error",
"WithStatement",
"BinaryExpression[operator='in']"
],
"no-console": [
0,
{
"allow": [
"info",
"warn",
"error"
]
}
],
"quotes": [
"error",
"single",
"avoid-escape"
],
"object-curly-newline": [
"error",
{
"consistent": true,
"multiline": true
}
],
"...whatever-you-want"
}
}
# .prettierrc
{
"printWidth": 80,
"trailingComma": "es5",
"useTabs": false,
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"bracketSpacing": true,
"bracketSameLine": false,
"proseWrap": "preserve",
"arrowParens": "avoid",
"endOfLine": "lf",
"parser": "babel"
}

How to use allow: options with eslintrc.js

I am having the following eslintrc.js:
module.exports = {
extends: ['airbnb-typescript/base'],
parserOptions: {
project: './tsconfig.json',
sourceType: 'module',
},
ignorePatterns: ['*.js'],
rules: {
'no-underscore-dangle': 0,
...
}
};
And I'd like to include some exceptions with allow: [ /** */ ]. But each time when I am adding this as a key: value property in my file, ESLint returns me an error with the following text: unable to use allow on top level. So the question is, how to achieve such results, with allow?
Long story short, I am unable to use my set of rules with MongoDB _id naming, so I have ESLint just to ignore only _id in variable naming, instead of disabling the naming-convention rule itself.
Is there any way to solve this problem?
It works for me:
"no-underscore-dangle": ["error", { allow: ["_id"] }]
If you are using the #typescript-eslint/naming-convention rule, you may also need to add this:
"#typescript-eslint/naming-convention": [
"error",
{
selector: ["variable"],
format: ["strictCamelCase", "PascalCase", "UPPER_CASE"],
filter: {
regex: "^_id$",
match: false,
},
},
],

eslint throwing errors where there are no errors

I’m receiving the following errors when running yarn eslint:
/…/src/a.js
4:1 error Expected indentation of 1 tab but found 2 spaces indent
4:43 error Strings must use singlequote quotes
5:1 error Expected indentation of 2 tabs but found 4 spaces indent
6:1 error Expected indentation of 1 tab but found 2 spaces indent
6:6 error Strings must use singlequote quotes
✖ 5 problems (5 errors, 0 warnings)
5 errors and 0 warnings potentially fixable with the `--fix` option.
This is my .eslintrc.js file:
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
'eslint:recommended',
'plugin:react/recommended',
],
parserOptions: {
ecmaFeatures: { jsx: true },
ecmaVersion: 12,
sourceType: 'module',
},
plugins: ['react'],
settings: {
react: { version: 'detect' },
},
rules: {
indent: [
'error',
'tab'
],
'linebreak-style': [
'error',
'unix',
],
quotes: [
'error',
'single'
],
semi: [
'error',
'always'
]
}
};
… and the file in question:
import React from "react";
import { css } from "./a.css";
export function App() {
\treturn (
\t\t<div className={css.App}>
\t\t\tHello.
\t\t</div>
\t);
}
Note: I include \t here to denote tabs as Stack Overflow replaces actual tabs with 4 spaces.
None of the errors reported by eslint are actual errors in the file. All indents are correct, and I only use single quotes. Hell, there isn’t even a line 43 in that file, as eslint is reporting.
What might be happening here?
indent: [
'error',
'tab'
],
quotes: [
'error',
'single'
],
This rule in you eslint tell that you shall not use space but a tab Key for indentation either remove this rule OR on the lines where the INDENT error has occured you shall find space used for indentation remove them and use the Tab key
Also for the Quote Error check you variable for the string values saved using single quote, if you are using vscode then you might need to remove this rule or update the vscode setting and prevent it from updating the single quote to double quote while auto saving
facepalm.
This ended up being a problem with an aspect of the project I didn’t mention. I’m using Rollup to bundle by code, along with the #rollup/plugin-eslint plugin to run eslint when building. I had my plugins in this order:
plugins: [
babel({
babelHelpers: 'bundled',
exclude: 'node_modules/**',
}),
resolve({
browser: true,
}),
eslint({
include: '**/*.js',
throwOnError: true,
}),
styles({
modules: true,
}),
]
I just had to move the eslint() plugin to the front of the array and all was golden.
plugins: [
eslint({
include: '**/*.js',
throwOnError: true,
}),
babel({
babelHelpers: 'bundled',
exclude: 'node_modules/**',
}),
resolve({
browser: true,
}),
styles({
modules: true,
}),
]

no-use-before-define wrongly catches use of the global window variable

Working in electron vue-cli project. Trying to upgrade eslint but the default setup has no-use-before-define enabled.
This causes it to catch every use of things like window.process.env.username as an error.
I tried setting my rules as "no-use-before-define": ["error", { "variables": false }], but it didn't help. Adding window as a global also didn't help. Is there a way to fix eslint to be happy with the window variable?
I do like the idea of this feature, so I would like to leave it on if possible. But just turning it off is ok too if that's the best answer.
If it helps, this is my current .eslintrc.js file
// https://eslint.org/docs/user-guide/configuring
module.exports = {
root: true,
parserOptions: {
parser: 'babel-eslint'
},
env: {
browser: true
},
extends: [
// https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevention
// consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules.
'plugin:vue/essential',
// https://github.com/standard/standard/blob/master/docs/RULES-en.md
'standard'
],
// required to lint *.vue files
plugins: [
'vue'
],
// add your custom rules here
rules: {
// Don't error on variables that are global, like window, not working.
"no-use-before-define": ["error", { "variables": false }],
'generator-star-spacing': 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
"vue/html-self-closing": ["error", {
"html": {
"void": "always",
"normal": "any",
"component": "always"
},
"svg": "always",
"math": "always"
}]
}
}
Thanks
-Edit
Tried
globals: {
window: 'writable'
},
But to no effect

Categories

Resources