homeresume
 
   

Linting JavaScript codebase with Eslint

Published April 5, 2023Last updated October 23, 20241 min read

Linting represents static code analysis based on specified rules. Please include it in the CI pipeline.

Setup

Run the following commands to generate the linter configuration using the eslint package.

npm init -y
npm init @eslint/config

Below is an example of the configuration. Some rules can be ignored or suppressed as warnings, ignore the files using ignores field.

// eslint.config.js
import globals from 'globals';
import pluginJs from '@eslint/js';
export default [
{ languageOptions: { globals: globals.node } },
pluginJs.configs.recommended,
{
ignores: ['dist/**/*.js']
},
{
rules: {
'no-console': ['off'],
'no-unused-vars': ['warn']
}
}
];

Linting

Configure and run the script with the npm run lint command. Some errors can be fixed automatically with the --fix option.

// package.json
{
"scripts": {
// ...
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix"
}
}

Demo

The demo with the mentioned examples is available here.