How to integrate ESLint with your React Typescript project (2021)

When we are working on a project of the most common problem is the code quality, for that reason, there are several tools that can be integrated into our project to make sure of that.

Importance of Code Quality?

Making software robust:…


This content originally appeared on DEV Community and was authored by Brayan Arrieta

When we are working on a project of the most common problem is the code quality, for that reason, there are several tools that can be integrated into our project to make sure of that.

Importance of Code Quality?

Importance of Code Quality.png

  • Making software robust: The ability to cope up with the errors during the program execution in spite of the unusual conditions.
  • Increasing readability and ease of editing: Good quality of code makes sure that codes are written in such a way that makes them highly readable. The use of comments, proper indentation, clear notations, and simplicity in the flow are some factors.
  • Making program sustainable: Software is said to be sustainable when it can survive over time with minimal changes.
  • Promotes easy transferability: Code quality practices make the translation of software across platforms straightforward with minimal changes.
  • Decreasing technical debt: One of the biggest problems in software projects is technical debt, a software with poor quality is set to fail early unless a significant number of changes are brought into the program repeatedly and hence increasing the technical debt. The extra development work is time and capital-consuming, which a high-quality code avoids.

In this post, we will see how to integrate ESLint in your React project and some common stuff related to configuration. ESLint is an npm package that helps to find and fix problems in projects. Provide some feature as find problems, fix automatically and also customization based in our project and team rules.

Install ESLint package dependency

First of all, we need to install ESLint

npm install eslint --save-dev

After running that you will see that eslint was added as a development dependency in the package.json file.

{
...
"devDependencies": {
    "eslint": "^7.26.0"
  },
...
}

Note: The Eslint version could be different there isn't a problem with that.

Setup ESLint in our project

Run the next command in the terminal inside the project folder

npx eslint --init

After running this command, you will need to answer some questions related to your project and the configuration you want.

How would you like to use ESLint?

Answer: to check syntax, find problems, and enforce code style

What type of modules does your project use?

Answer: JavaScript modules (import/export)

Which framework does your project use?

Answer: React

Does your project use TypeScript?

Answer: Yes

Where does your code run?

Answer: Browser

How would you like to define a style for your project?

Answer: Use a popular style guide

Which style guide do you want to follow?

Answer: Airbnb: https://github.com/airbnb/javascript

What format do you want your config file to be in?

Answer: JSON

Would you like to install them now with npm?

Answer: Yes

Then, it will install all the packages needed. After the installation, the development dependencies in the package.json file could something similar to

 "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^4.24.0",
    "@typescript-eslint/parser": "^4.24.0",
    "eslint": "^7.26.0",
    "eslint-config-airbnb": "^18.2.1",
    "eslint-plugin-import": "^2.23.3",
    "eslint-plugin-jsx-a11y": "^6.4.1",
    "eslint-plugin-react": "^7.23.2",
    "eslint-plugin-react-hooks": "^4.2.0",
  },

After that you will that was created a new file called .eslintrc.json at the root of the project with the rules and configuration related to ESLint. The file content should looks something similar to

{
    "env": {
        "browser": true,
        "es2021": true
    },
    "extends": [
        "plugin:react/recommended",
        "airbnb"
    ],
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaFeatures": {
            "jsx": true
        },
        "ecmaVersion": 12,
        "sourceType": "module"
    },
    "plugins": [
        "react",
        "@typescript-eslint"
    ],
    "rules": {}
}

Running ESLint

Install ESLint extension for VSCode, After that need to reload the VSCode window once to get proper linting.*

ESLint will automatically start detecting errors/warnings in the files. If that's not the case then either your project has no linting errors or ESLint is not properly set up.

To test if the linting works, we should run the ESLint command in the terminal with folder path and notice output.

npx eslint .

Finally, you can also add linting to scripts in package.json as a part of your pipeline process in the project

{
  "scripts": {
      "lint": "eslint src/*",
      "lint:fix": "eslint src/* --fix",
      "lint:quiet": "eslint src/* --quiet"   
  }
}

Notes:

  • To automatically fix some errors, you can use ‘--fix’ in the command
  • If you want to ignore warnings, you can use ‘ --quiet’ in the command
  • To specify some type of files to check you can use '--ext' and pass the extensions that want to be checked

.eslintignore

To disable the linting of some files/folders you could create a .eslintignore at the root of the project.

node_modules
dist
coverage

How to solve common ESLint rules problems

Problem: import/extensions

How to fix it?

In eslintrc.json, over rules, add the following

{
    "rules": {
        "import/extensions": ["error", "ignorePackages", { "ts": "never", "tsx": "never" } ]
    }
}

Problem: react/jsx-filename-extension

How to fix it?

In eslintrc.json, over rules, add the following

{
    "rules": {
        "react/jsx-filename-extension": ["warn", { "extensions": [".tsx",] }],
    }
}

Problem: import/no-extraneous-dependencies

How to fix it?

This is common when we are working in the devDependencies. To fix just add the following in eslintrc.json over rules.

{
    "rules": {
        "import/no-extraneous-dependencies": ["error", {"devDependencies": true}]
    }
}

Problem: import/no-unresolved

How to fix it?

Install eslint-import-resolver-typescript package

npm install eslint-import-resolver-typescript --save-dev

After that on eslintrc.json add a new property called settings

{
    "settings": {
        "import/resolver": {
            "typescript": {}
        }
    }
}

Conclusion

Eslint is an awesome tool that we need to integrate into every project due that improve the code quality in our React projects. Need to consider every specific case of use, just to make sure the best performance and render time speed in our React projects.

I will be updating this post based on your comments and recommendations so let me know in any case that you have a common rule problem with Eslint and React that can be added or something like that. Thanks for all! ?

References


This content originally appeared on DEV Community and was authored by Brayan Arrieta


Print Share Comment Cite Upload Translate Updates
APA

Brayan Arrieta | Sciencx (2021-05-31T17:33:02+00:00) How to integrate ESLint with your React Typescript project (2021). Retrieved from https://www.scien.cx/2021/05/31/how-to-integrate-eslint-with-your-react-typescript-project-2021/

MLA
" » How to integrate ESLint with your React Typescript project (2021)." Brayan Arrieta | Sciencx - Monday May 31, 2021, https://www.scien.cx/2021/05/31/how-to-integrate-eslint-with-your-react-typescript-project-2021/
HARVARD
Brayan Arrieta | Sciencx Monday May 31, 2021 » How to integrate ESLint with your React Typescript project (2021)., viewed ,<https://www.scien.cx/2021/05/31/how-to-integrate-eslint-with-your-react-typescript-project-2021/>
VANCOUVER
Brayan Arrieta | Sciencx - » How to integrate ESLint with your React Typescript project (2021). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/31/how-to-integrate-eslint-with-your-react-typescript-project-2021/
CHICAGO
" » How to integrate ESLint with your React Typescript project (2021)." Brayan Arrieta | Sciencx - Accessed . https://www.scien.cx/2021/05/31/how-to-integrate-eslint-with-your-react-typescript-project-2021/
IEEE
" » How to integrate ESLint with your React Typescript project (2021)." Brayan Arrieta | Sciencx [Online]. Available: https://www.scien.cx/2021/05/31/how-to-integrate-eslint-with-your-react-typescript-project-2021/. [Accessed: ]
rf:citation
» How to integrate ESLint with your React Typescript project (2021) | Brayan Arrieta | Sciencx | https://www.scien.cx/2021/05/31/how-to-integrate-eslint-with-your-react-typescript-project-2021/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.