Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style - reactjs

I cloned a website based on react js in my windows pc, on running npm start I got this error
Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
after looking some answers in stack-overflow on adding this line in my .eslintrc
"linebreak-style": ["error", "windows"],
website started to work fine.
This is my .eslintrc file from cloned repo
{
"extends": "airbnb",
"parser": "babel-eslint",
"env": {
"browser": true,
"node": true,
"jest": true
},
"rules": {
"linebreak-style": ["error", "windows"],
"jsx-a11y/anchor-is-valid": ["error", {
"components": ["Link"],
"specialLink": ["to", "hrefLeft", "hrefRight"],
"aspects": ["noHref", "invalidHref", "preferButton"]
}],
"jsx-a11y/click-events-have-key-events": 0,
"jsx-a11y/href-no-hash": "off",
"jsx-a11y/no-static-element-interactions": 0,
"no-console": ["error", {
"allow": ["warn", "error", "info"]
}],
"no-underscore-dangle": 0,
"react/destructuring-assignment": 0,
"react/function-component-definition": [2, { "namedComponents": "arrow-function" }],
"react/jsx-filename-extension": [1, {
"extensions": [".js", ".jsx"]
}],
"react/jsx-no-useless-fragment": 0,
"react/jsx-one-expression-per-line": 0,
"react/jsx-props-no-spreading": 0,
"react/jsx-wrap-multilines": [1, {
"declaration": true,
"assignment": true,
"return": true
}]
},
"plugins": [
"react"
]
}
Query
On deploying this website on github it is showing this error again

Try to add "endOfLine": "auto" setting in .eslintrc.js and .prettierrc files.
So, file .eslintrc.js should have:
rules: {
/* ...the other code is omitted for the brevity */
'import/extensions': 'off',
'prettier/prettier': [
'error',
{
endOfLine: 'auto', /* this setting should be included */
},
],
And file .prettierrc should have:
{
/* ...the other code is omitted for the brevity */
"endOfLine": "auto" /* this setting should be included */
}

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.

problems loading config prettier and airbnb on heroku

I'm trying to deploy react app to heroku. It's deployed successfully but it throws me an error :
My .eslintrc file looks like this :
{
"env": {
"browser": true,
"es6": true
},
"extends": [
"airbnb",
"prettier"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"react"
],
"rules": {
"no-multi-spaces": "error",
"react/jsx-filename-extension": [
1,
{
"extensions": [
".js",
".jsx"
]
}
],
"import/imports-first": [
"error",
"absolute-first"
],
"no-shadow": [
2,
{
"allow": [
"done"
]
}
],
"react/state-in-constructor": 0,
"react/jsx-fragments": 0,
"react/jsx-props-no-spreading": 0,
"react/jsx-curly-newline": 0,
"react/jsx-wrap-multilines": ["error", {
"declaration": "parens-new-line",
"assignment": "parens-new-line",
"return": "parens-new-line",
"arrow": "parens-new-line",
"condition": "parens-new-line",
"logical": "ignore",
"prop": "ignore"
}],
"import/no-unresolved": 0,
"new-cap": 0,
"semi": 0,
"global-require": 0,
"no-underscore-dangle": 0,
"arrow-body-style": 0,
"no-console": 0,
"react/forbid-prop-types": 0,
"jsx-a11y/href-no-hash": 0,
"import/prefer-default-export": 0,
"react/prop-types": 0,
"comma-dangle": [2, "always-multiline"],
"arrow-parens": ["warn", "as-needed", { "requireForBlockBody": false }],
"jsx-a11y/anchor-is-valid": [ "error", {
"components": [ "Link" ],
"specialLink": [ "to", "hrefLeft", "hrefRight" ],
"aspects": [ "noHref", "invalidHref", "preferButton" ]
}]
},
"settings": {
"import/resolver": {
"node": {
"paths": [
"src",
"src/js"
]
}
}
},
"parser": "babel-eslint"
}
I reload the page where app is deployed and I can see my app for a sec and error arises.
Another app throws similar err:
It complains about airbnb. What should I reinstall or install or maybe amend the .eslintrc file ?
I ran into this issue as well. It is odd that heroku is triggering eslint on build. This is likely occurring because Heroku by default skips dev dependencies (NPM_CONFIG_PRODUCTION=TRUE) but airbnb is still being used in .eslintrc in the "extends" section. This is a workaround:
To prevent this error allow dev dependencies to be installed in prod by using: heroku config:set NPM_CONFIG_PRODUCTION=false -a appname
If you have unresolved linting/prettier errors or warnings that you don't want to appear in prod, use this to stop them from appearing: heroku config:set DISABLE_ESLINT_PLUGIN=true -a appname
If you are worried about stale npm module caching affecting your heroku deployment disable it with this: heroku config:set NODE_MODULES_CACHE=false -a appname
it will force heroku to rebuild all dependencies on every deployment
more info is here: https://devcenter.heroku.com/articles/nodejs-support

Dealing with "Failed to load plugin 'react' declared in '.eslintrc': Cannot find module 'eslint-plugin-react'" ESLint error

Recently I updated my react project using "create-react-app" (React 16.9)
Everything worked just OK before the update, but suddenly I get following ESLint error: (In the output tab)
[Error - 16:42:12]
Failed to load plugin 'react' declared in 'client\.eslintrc': Cannot find module 'eslint-plugin-react'
Require stack:
- C:\Or\Web\VisualizationTool\VisualizationTool\__placeholder__.js
Referenced from: C:\Or\Web\VisualizationTool\VisualizationTool\client\.eslintrc
Happened while validating C:\Or\Web\VisualizationTool\VisualizationTool\client\src\hoc\Layout\Layout.jsx
This can happen for a couple of reasons:
1. The plugin name is spelled incorrectly in an ESLint configuration file (e.g. .eslintrc).
2. If ESLint is installed globally, then make sure 'eslint-plugin-react' is installed globally as well.
3. If ESLint is installed locally, then 'eslint-plugin-react' isn't installed correctly.
My .eslintrc file:
{
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"extends": [
"react-app",
"eslint:recommended",
"plugin:react/recommended"
],
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 2018,
"ecmaFeatures": {
"jsx": true
},
"sourceType": "module"
},
"settings": {
"react": {
"pragma": "React",
"version": "16.8"
}
},
"plugins": [
"react"
],
"rules": {
"quotes": [
"error",
"single",
{
"allowTemplateLiterals": true
}
],
"semi": "off",
"default-case": [
"error",
{
"commentPattern": "^no default$"
}
],
"no-new-wrappers": 0,
"no-mixed-operators": 0,
"require-atomic-updates": "off",
"comma-dangle": "off",
"no-unused-vars": "off",
"no-useless-constructor": 0,
"react/jsx-uses-react": "error",
"react/jsx-uses-vars": "error",
"react/no-unescaped-entities": 0,
"react/display-name": 0,
"jsx-a11y/href-no-hash": "off",
"jsx-a11y/anchor-is-valid": "off",
"no-useless-escape": 0,
"no-console": 0,
"no-debugger": 0,
"no-empty": 0,
"linebreak-style": 0,
"import/first": 0,
"import/imports-first": 0,
"no-shadow": 0,
"disable-next-line": 0,
"no-case-declarations": 0,
}
}
I have both ESLint and eslint-plugin-react installed both globally and locally, anything else I am missing here?
I had the same problem in VS code. Add the following settings in VS code ESLint settings:
{
"eslint.workingDirectories": [
"Dir1",
"Dir2"
],
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
]
}
Note: Dir1 and Dir2 are two directories with their respective .eslintrc files.
npm install eslint-plugin-react#latest --save-dev
Thanks! I had the same problem, installing this worked for me
I also had a similar error (I can't be sure the reason was the same but this is how I found the issue).
So make sure the Path of the Require Stack (in the error) doesn't point outside your project directory.
So you have a root directory of your project (where you have the node_modules folder and such). In the terminal $ cd .. (go to the folder that holds your project) and there do this:
$ ls -a
If you see a file called .eslintrc.js, then remove it ($ rm .eslintr.js). Then just reload your project and the problem (at least in my case) will be gone.
Try to check your ESLint plugin Working directories given that you have a new one for your project with the create-react-app update. Had the same issue and I fixed mine by checking my working directories in the ESLint plugin settings.
Found it.
1. Keep only the "eslint:recommended" in the "extends" section. Remove all others.
2. Removed the "plugins" section.
3. Restart the VSCode.
4. Works like a charm!
My updated .eslintrc file looks like this:
{
"extends": "eslint:recommended",
"env": {
"browser": true,
"commonjs": true,
"node": true,
"es6": true
},
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 2018,
"ecmaFeatures": {
"jsx": true
},
"sourceType": "module"
},
"settings": {
"react": {
"pragma": "React",
"version": "16.9"
}
},
"rules": {
"quotes": [
"error",
"single",
{
"allowTemplateLiterals": true
}
],
"semi": 0,
"default-case": [
"error",
{
"commentPattern": "^no default$"
}
],
"react/jsx-uses-vars": 0,
"react/react-in-jsx-scope": 0,
"no-new-wrappers": 0,
"no-mixed-operators": 0,
"require-atomic-updates": "off",
"comma-dangle": "off",
"no-unused-vars": "off",
"no-useless-constructor": 0,
"react/no-unescaped-entities": 0,
"react/display-name": 0,
"jsx-a11y/href-no-hash": "off",
"jsx-a11y/anchor-is-valid": "off",
"no-useless-escape": 0,
"no-console": 0,
"no-debugger": 0,
"no-empty": 0,
"linebreak-style": 0,
"import/first": 0,
"import/imports-first": 0,
"no-shadow": 0,
"disable-next-line": 0,
"no-case-declarations": 0,
}
}

Set eslint rule for unused class methods in React component

I am trying to set an eslint rule for methods in class that are never used. Like in the following react component I have a method unUsedMethod which is never used, but eslint does not show an error for it.
class Sample extends Component {
unUsedMethod() {
console.log('I am never used');
}
render() {
return 'Hello!';
}
}
My eslint file looks like this
{
"parser": "babel-eslint",
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": ["eslint:recommended", "plugin:react/recommended"],
"parserOptions": {
"ecmaFeatures": {
"experimentalObjectRestSpread": true,
"jsx": true,
"sourceType": "module",
"allowImportExportEverywhere": false,
"codeFrame": false
},
"sourceType": "module"
},
"plugins": [
"react"
],
"rules": {
"indent": [2, 4, {"SwitchCase": 1, "ObjectExpression": "first"}],
"linebreak-style": [
"error",
"unix"
],
"semi": [
"error",
"always"
],
"react/display-name": 0,
"react/prop-types": 0, // Temporary
"react/no-unescaped-entities": 0,
"no-trailing-spaces": 1
}
}
This plugin does what you are asking for. However a word of caution.
https://www.npmjs.com/package/eslint-plugin-no-unused-react-component-methods
Currently it is impossible to simply have a parser that would check for unused component properties, because component properties can be called dynamically.
For example:
class Sample extends Component {
// Plugin falsely flags this as unused.
unUsedMethod() {
console.log('I am used dynamically');
}
render() {
// No way to parse dynamic function calls reliably
this['unUsedMethod']();
return 'Hello!';
}
}
It also wouldn't work with react-onclickoutside package, as it requires a function to be attached to the component, that is called by wrapping the component in a HOC. https://www.npmjs.com/package/react-onclickoutside
Still the plugin helped me find a few unused functions, so it is worth a try in my opinion.
First install the package: npm i eslint-plugin-no-unused-react-component-methods --save-dev
Add "no-unused-react-component-methods" to your eslint configuration in the plugins section:
{
"plugins": [
"no-unused-react-component-methods"
],
}
And add into the rules section
{
"rules": {
"no-unused-react-component-methods/no-unused-react-component-methods": 2,
}
}
So your config would look like:
{
"parser": "babel-eslint",
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": ["eslint:recommended", "plugin:react/recommended"],
"parserOptions": {
"ecmaFeatures": {
"experimentalObjectRestSpread": true,
"jsx": true,
"sourceType": "module",
"allowImportExportEverywhere": false,
"codeFrame": false
},
"sourceType": "module"
},
"plugins": [
"react",
"no-unused-react-component-methods"
],
"rules": {
"no-unused-react-component-methods/no-unused-react-component-methods": 2,
"indent": [2, 4, { "SwitchCase": 1, "ObjectExpression": "first" }],
"linebreak-style": ["error", "unix"],
"semi": ["error", "always"],
"react/display-name": 0,
"react/prop-types": 0, // Temporary
"react/no-unescaped-entities": 0,
"no-trailing-spaces": 1
}
}
Now it should highlight any seemingly unused functions! Let me know if it works or not.

Resources