Eslint CLI doesnt show prettier errors but VSCode does - reactjs

I wanted to use eslint to integrate the airbnb styleguide. Also I want the prettier style rules.
Problem is eslint is giving me no errors when using it from the command line but VSCode does.
VSCode shows in the Problems Tab "eslint(prettier/prettier)" as error.
When i run eslint src/** i get zero errors.
Also pressing save in VSCode Formats my Code using prettier ( Plugin ) but most of the time it just changes back to the old formatting then.
So i want when i execute eslint src/** to apply prettier rules like my VSCode does.
My .eslintrc.json
{
"extends": [
"airbnb",
"airbnb/hooks",
"airbnb-typescript",
"prettier"
],
"plugins": [
"prettier",
"#typescript-eslint"
],
"env": {
"browser": true,
"es6": true,
"jest": true
},
"ignorePatterns": [
"*.css",
"*.png",
"*.json",
"*.svg"
],
"parser": "#typescript-eslint/parser",
"parserOptions": {
"sourceType": "module",
"project": "./tsconfig.json"
},
"rules": {
"react/jsx-filename-extension": [
1,
{
"extensions": [
".tsx"
]
}
],
"import/extensions": [
"error",
"never",
{
"json": "always",
"svg": "always",
"css": "always"
}
],
"max-len": [
"error",
{
"code": 999,
"ignoreStrings": true,
"ignoreComments": true,
"ignoreTemplateLiterals": true
}
],
"func-names": [
"error",
"as-needed",
{
"generators": "never"
}
],
"react/jsx-props-no-spreading": [
"off"
],
"no-param-reassign": [
"error",
{ "props": false }
],
"import/order": [
"error",
{
"groups": [
"index",
"sibling",
"parent",
"internal",
"external",
"builtin",
"object",
"type"
],
"alphabetize": {
"order": "asc",
"caseInsensitive": true
}
}
],
"react/function-component-definition": [
2,
{
"namedComponents": "function-declaration"
}
],
"no-tabs": ["error", { "allowIndentationTabs": false }],
"prettier/prettier": "error"
}
}
.prettierrc
{
"printWidth": 999,
"singleQuote": true,
"useTabs": false
}

Related

ESLint issue when using babel module-resolver and typescript

TS is not inferring the type when using resolved imports, but it does when using relative imports. Any help would be appreciated.
useTheme has
"Unsafe call of an 'any' typed value."
This error does not show up if i use a relative import instead of '#theme/Theme'
My project structure is:
Here are my config files:
tsconfig.json
{
"extends": "../../tsconfig.json", // standard tsconfig params
"compilerOptions": {
"jsx": "react-native" /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */,
"baseUrl": "." /* Base directory to resolve non-absolute module names. */,
"paths": {
"#components/*": ["./src/common/components/*"],
"#theme/*": ["./src/lib/theme/*"],
"#hooks/*": ["./src/common/hooks/*"],
"#features/*": ["./src/features/*"]
},
"typeRoots": [
"./typings",
"../../node_modules/#types"
] /* List of folders to include type definitions from. */
},
"include": ["../"],
"exclude": [
"node_modules",
"babel.config.js",
"metro.config.js",
"jest.config.js"
]
}
babel.config.js
module.exports = {
presets: [
'module:metro-react-native-babel-preset',
'#babel/preset-typescript',
],
plugins: [
['#babel/plugin-proposal-optional-chaining'],
['#babel/plugin-proposal-nullish-coalescing-operator'],
['#babel/plugin-proposal-decorators', { legacy: true }],
[
'module:react-native-dotenv',
{
moduleName: '#env',
path: '.env',
},
],
[
'module-resolver',
{
alias: {
'#components': './src/common/components',
'#theme': ['./src/lib/theme'],
'#hooks': ['./src/common/hooks'],
'#features': ['./src/features'],
},
extensions: ['.js', '.jsx', '.ts', '.tsx'],
root: '.',
},
],
'react-native-reanimated/plugin',
],
};
.eslintrc
{
"root": true,
"extends": [
"airbnb-typescript", // React "airbnb-typescript/base" for just js. "airbnb","airbnb/base" for no TypeScript
"airbnb/hooks", // React
"plugin:#typescript-eslint/eslint-recommended",
"plugin:#typescript-eslint/recommended",
"plugin:#typescript-eslint/recommended-requiring-type-checking",
"#react-native-community" // React Native only
],
"parser": "#typescript-eslint/parser", // TypeScript only
"parserOptions": {
"project": "./tsconfig.json", // TypeScript only
"ecmaFeatures": { "jsx": true }
},
"settings": {
"import/resolver": {
"babel-module": {}
}
},
"plugins": ["import", "#typescript-eslint"],
"ignorePatterns": ["*.config.js"],
"overrides": [
{
/** Testing Files Extensions */
"files": ["*.{spec,test}.{js,ts,tsx}", "e2e/*.{js,ts,tsx}"],
"plugins": ["jest"],
"env": { "jest/globals": true },
"rules": {
"import/no-extraneous-dependencies": 0 // Allow dev-dependency imports
}
}
],
"rules": {
"no-unused-vars": [
"error",
{
"args": "none",
"caughtErrors": "none",
"ignoreRestSiblings": true,
"vars": "all"
}
],
// resolve "import/extensions"
"import/extensions": [
"error",
"ignorePackages",
{
"ts": "never",
"tsx": "never"
}
],
"curly": ["error", "multi-line"],
"no-void": ["error"], // Allow void as statement in order to ignore promise returns
"prettier/prettier": 0, // Disable Prettier
"import/no-cycle": 0, // Slows down all linting
"react/jsx-props-no-spreading": 0, // Enable Prop Spreading
"#typescript-eslint/no-empty-interface": 0, // Allow boilerplate empty interfaces for defining component props explicitly
"#typescript-eslint/no-unsafe-assignment": 0,
"#typescript-eslint/naming-convention": [
2,
{
"leadingUnderscore": "allow",
"format": ["camelCase", "PascalCase", "UPPER_CASE", "snake_case"],
"selector": "variable"
}
],
"#typescript-eslint/no-explicit-any": 0, // disabled to avoid using any type
"#typescript-eslint/no-use-before-define": "off",
"#typescript-eslint/restrict-template-expressions": 0,
"#typescript-eslint/no-floating-promises": [2, { "ignoreIIFE": true }], // Fixes issue where async await syntax wasn't being recognised.
"#typescript-eslint/explicit-function-return-type": [
0,
{ "allowExpressions": false }
],
"#typescript-eslint/no-unused-vars": [2, { "varsIgnorePattern": "^_" }],
// Fixes false-positives for enums in typescript https://github.com/typescript-eslint/typescript-eslint/issues/2484#issuecomment-687257773
"no-shadow": "off",
"#typescript-eslint/no-shadow": ["error"],
"max-len": [
"warn",
{
"code": 120,
"tabWidth": 2,
"ignoreComments": true,
"ignoreStrings": true,
"ignoreRegExpLiterals": true,
"ignoreTemplateLiterals": true
}
]
}
}
if you work in vscode, try use "TypeScript: Select TypeScript version" to select you workspace version

Upgrade React-App to use JSX transform - breaks the app (Propery 'React' doesn't exist)

I do have an react-native project.
I've upgraded to recent react and react-native version, even as all other packages in the project.
As React 17 brings the JSX-Transform (https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#manual-babel-setup) I followed those steps to use it in my app.
But at least it doesn't work as expected.
I wonder if the new JSX-Transform feature doesn't be usable for react-native APP's, but only for general react webprojects?!
This is my babel.config.js:
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
[
'#babel/plugin-proposal-decorators',
{
legacy: true,
},
],
[
'#babel/plugin-transform-react-jsx',
{
runtime: 'automatic',
},
],
'react-native-reanimated/plugin',
],
};
this my eslintrc.json:
{
"parser": "#babel/eslint-parser",
"env": {
"browser": true,
"react-native/react-native": true,
"es6": true,
"node": true
},
"extends": [
"#react-native-community",
"plugin:react/recommended",
"airbnb-base",
"prettier/react"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"react",
"react-native",
"prettier"
],
"rules": {
"react/jsx-uses-react": "off",
"react/react-in-jsx-scope": "off",
"object-curly-newline": 0,
"max-len": [
"error",
{
"code": 100,
"comments": 150,
"ignoreTrailingComments": true,
"ignoreUrls": true
}
],
"indent": [
"warn",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
2,
"single",
{
"avoidEscape": true,
"allowTemplateLiterals": true
}
],
"semi": [
"error",
"always"
],
"react-native/no-unused-styles": 2,
"react-native/split-platform-components": 2,
"react-native/no-inline-styles": 2,
"react-native/no-color-literals": 2,
"react-native/no-raw-text": 2,
"prettier/prettier": [
"warn",
{
"endOfLine": "auto",
"bracketSpacing": true
}
]
}
}
You can try this solution to make it work with react native
https://github.com/facebook/metro/issues/646#issuecomment-799174473
So by using both useTransformReactJSXExperimental and runtime settings in babel configuration file.

Why does typescript-eslint detect a problem in functional members with explicitly named arguments?

I am attempting to make use of typescript-eslint in an existing react project, and eslint is detecting an issue that I don't think is correct, and I'm curious of the proper way to get around it. As an example to demonstrate the issue, here is a simple interface with a functional member:
export interface MyInterface {
myProperty: number,
myFunction: (myParam: string) => string
}
eslint underlines myParam: string in yellow and supplies the following warning:
'myParam' is defined but never used. eslint(no-unused-vars)
Here are my eslint configuration settings:
{
"env": {
"browser": true,
"es6": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:#typescript-eslint/recommended"
],
"globals": {
"module": true,
"__dirname": true,
"require": false,
"$": false
},
"parser": "#typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true,
"modules": true
},
"ecmaVersion": 6,
"sourceType": "module"
},
"plugins": [
"react"
],
"rules": {
"indent": [
"off",
2
],
"linebreak-style": [
"off",
"windows"
],
"quotes": [
"warn",
"single"
],
"semi": [
"error",
"always"
],
"no-console": [
"off"
],
"no-debugger": [
"warn"
],
"no-extra-semi": [
"warn"
],
"no-unused-vars": [
"warn"
]
},
"settings": {
"react": {
"version": "detect"
}
}
}
I understand that I have "no-unused-vars": ["warn"] in my rules, but it seems to me no-unused-vars should not apply to a function definition. Has anyone else come across this issue?
Thank you so much in advance for any help!

ESLint Extra Parentheses In JSX

I've noticed that extra parentheses are getting added to my multi-line conditional renders using logical AND (&&) in my jsx files. For example, this code from the React docs...
{unreadMessages.length > 0 &&
<h2>
You have {unreadMessages.length} unread messages.
</h2>
}
...would get modified as follows:
{unreadMessages.length > 0 && (
<h2>
You have {unreadMessages.length} unread messages.
</h2>
)}
Here is my ESLint config:
"eslintConfig": {
"root": true,
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true,
"impliedStrict": true
}
},
"env": {
"browser": true,
"node": true,
"es6": true,
"mocha": true
},
"plugins": [
"react"
],
"extends": [
"eslint:recommended",
"plugin:react/recommended"
],
"rules": {
"computed-property-spacing": [
"error"
],
"indent": [
"error",
2
],
"jsx-quotes": [
"error"
],
"key-spacing": [
"error"
],
"no-case-declarations": [
"off"
],
"no-console": [
"off"
],
"no-var": [
"error"
],
"object-curly-spacing": [
"error",
"always"
],
"prefer-const": [
"error"
],
"quotes": [
"error",
"single",
{
"avoidEscape": true,
"allowTemplateLiterals": true
}
],
"react/no-children-prop": "off",
"react/prop-types": "off",
"semi": [
"error",
"never"
]
}
}
Have I inadvertently caused this, or is there a good reason for it? If not, how can I prevent this? It seems like overkill to disallow unnecessary parentheses.
It looks like this is caused by react/jsx-wrap-multilines and can be prevented by setting the logical syntax type to "ignore".

how to turn off eslint rule `angular/file-name` in eslint-plugin-angular

i have configuration like this.
`module.exports = {
"globals": {
"angular": 1
},
"env": {
"browser": true,
"commonjs": true,
"es6": true
},
extends: ['plugin:angular/johnpapa', 'eslint:recommended'],
"parserOptions": {
"sourceType": "module"
},
"plugins": [
"angular"
],
"rules": {
"no-console": 0,
"angular/ng_controller_name": 0,
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
};
`
and i get this warning in terminal
Filename must be "callsController.js" angular/file-name
The angular plugin is running a file name rule, I'm guessing by default since I don't see it in your rule list. You should be able to override this with "angular/file-name":0 in your eslintrc.
In your .eslintrc add "angular/file-name": 0 in the rules section, like so:
{
"extends": "eslint:recommended",
"plugins": ["angular"],
"env": {
...
},
"globals": {
"angular": true,
"module": true,
"inject": true
},
"rules": {
"angular/file-name": 0
}
}

Resources