DEV Community

Bhavik Mistry
Bhavik Mistry

Posted on

Streamlining Code Quality with Linters, Formatters, and Editor Integration

It's critical in the world of web development to keep code clear, understandable, and free of errors. In order to do this, I set out to enhance my project, TxtToHTML, by combining ESLint and Prettier, two powerful tools. With the use of these tools, I could automatically style the code and enforce coding standards, creating a more unified and manageable codebase.

Choosing the Right Tools

Linter: ESLint
ESLint was my choice for a JavaScript linter. ESLint is a popular and highly configurable linter that assists in enforcing coding standards and identifying common code errors. It provides a wide range of rules and plugins to adjust to the needs of individual projects.

Formatter: Prettier
I chose Prettier, a code formatter that enforces consistent code style, for code formatting. Code is automatically formatted by Prettier, ending arguments over preferred formatting. It works with popular code editors and supports a number of languages with ease.

Setting Up the Tools

ESLint Configuration

I set up ESLint in my project using the following .eslintrc.js configuration:

module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true,
  },
  extends: 'eslint:recommended',
  parserOptions: {
    ecmaVersion: 'latest',
  },
  rules: {},
};

Enter fullscreen mode Exit fullscreen mode

This configuration expands on the ESLint recommended rules and includes basic definitions for the environment. The parserOptions define compatibility with the most recent version of ECMAScript. If more rules are required, they can be added.

Prettier Configuration

My Prettier configuration in .prettierrc is as follows:

{
  "arrowParens": "always",
  "bracketSpacing": true,
  "endOfLine": "lf",
  "insertPragma": false,
  "proseWrap": "preserve",
  "requirePragma": false,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "printWidth": 100
}
Enter fullscreen mode Exit fullscreen mode

These settings ensure consistent code formatting, such as single quotes, two spaces for indentation, and a maximum line width of 100 characters.

VSCode Integration

To integrate these tools into Visual Studio Code (VSCode), I used the .vscode/settings.json configuration:

{
  "editor.insertSpaces": true,
  "editor.tabSize": 2,
  "editor.detectIndentation": false,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.formatOnSave": true,
  "editor.codeActionsOnSave": {
    "source.fixAll": true
  },
  "files.eol": "\n",
  "files.insertFinalNewline": true
}
Enter fullscreen mode Exit fullscreen mode

These settings enable automatic code formatting and linting on save. The default formatter is set to Prettier, ensuring consistent formatting in the editor.

What the Tools Found

After configuring ESLint and Prettier, I ran them on my project, and here's what I discovered:

ESLint: ESLint detected a some problems with the code right away, including possible syntactic errors and unutilized variables. To make sure the code followed the established guidelines, I had to address these problems.

Prettier: Using the specified preferences, Prettier automatically formatted my code. It assisted in fixing inconsistent line breaks, quotation marks, and indentation in code formatting.

I have added both ESLint and Prettier in my Package.json file:

"scripts": {
    "prettier": "prettier . --write",
    "lint": "eslint ."
}

Enter fullscreen mode Exit fullscreen mode

Integration with VSCode

Writing code, when merged with the recommended settings for VSCode, makes formatting and linting easy. VSCode automatically formats the code and highlights linting errors when it is saved. By using this integration, developers can be guaranteed to keep a consistent coding style and spot issues early on.

Key Takeaways

Through this journey of integrating linters, formatters, and VSCode integration, we learned valuable lessons:

- Consistency Matters: Maintaining code consistency and enforcing coding standards through the use of formatters and linters is essential for teamwork.

- Automation Is Key: Utilizing tools to automate code analysis and formatting streamlines the development process, reduces human error, and saves time.

- Editor Integration Enhances Productivity: Ensuring code quality during development and identifying problems in real-time are made possible by integrating tools with the code editor/IDE.

- Documenting the Setup Is Valuable: Including clear instructions in your CONTRIBUTING.md file enables contributors to set up linting and formatting easily.

Top comments (0)