Skip to content

Instantly share code, notes, and snippets.

@montasim
Created July 6, 2024 14:28
Show Gist options
  • Save montasim/bc0a36ff21250590a40c66d143d967e2 to your computer and use it in GitHub Desktop.
Save montasim/bc0a36ff21250590a40c66d143d967e2 to your computer and use it in GitHub Desktop.
This ESLint configuration is crafted to ensure that the JavaScript code in your project adheres to a set of defined standards and practices, enhancing code quality and maintainability.
/**
* ESLint Configuration File
* @fileoverview This configuration file sets up linting rules and environments for a JavaScript project.
* It includes settings for ECMAScript 2020 features, enforces coding styles, and configures plugins for additional linting capabilities.
*/
export default {
// Specifies the types of files ESLint will lint
files: ['**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx'],
// Language options define the ECMAScript features and global variables
languageOptions: {
ecmaVersion: 2020, // Allows parsing of modern ECMAScript features
sourceType: 'module', // Treats files as ECMAScript modules
globals: {
jest: 'readonly', // Indicates global variables provided by Jest that should not be overwritten
},
},
// Linter options for managing the linting process
linterOptions: {
reportUnusedDisableDirectives: true, // Reports unused eslint-disable comments
},
// Plugins extend ESLint with new settings, environments, rules, and so on
plugins: {
jest: {}, // Adds Jest testing support
security: {}, // Adds additional rules for security
prettier: {}, // Integrates Prettier for code formatting
},
// Rules define how ESLint applies linting to the code
rules: {
'no-console': 'warn', // Warns about console usage
'func-names': 'off', // Turns off the requirement to name functions
'no-underscore-dangle': 'off', // Allows dangling underscores in identifiers
'consistent-return': 'off', // Does not require function return values to be consistent
'jest/expect-expect': 'off', // Turns off a rule that expects a Jest test to have an assertion
'security/detect-object-injection': 'off', // Disables a security rule about object injection that may not be applicable
quotes: [
'error', // Enforces the use of single quotes
'single',
{ avoidEscape: true, allowTemplateLiterals: true },
],
semi: ['error', 'always'], // Requires semicolons at the end of statements
'prefer-arrow-callback': ['error', { allowNamedFunctions: false }], // Enforces the use of arrow functions for callbacks
'prefer-const': 'error', // Requires use of const for variables that are never reassigned
'arrow-spacing': ['error', { before: true, after: true }], // Enforces space around the arrow of arrow functions
'no-var': 'error', // Requires let or const, not var
'object-shorthand': ['error', 'always'], // Requires object literal shorthand syntax
'prefer-template': 'error', // Prefers template literals over string concatenation
},
// Paths to ignore during linting
ignores: [
'.idea/**', // Ignores all files in the .idea folder
'node_modules/**', // Ignores all files in node_modules
'build/**', // Ignores all files in the build output directory
'logs/**', // Ignores log files
'yarn.lock', // Ignores the yarn lock file
],
};
@montasim
Copy link
Author

montasim commented Jul 6, 2024

File Overview

eslint.config.mjs is an ESLint configuration file that uses the modern JavaScript module syntax (indicated by the .mjs extension). It specifies how ESLint should lint the JavaScript project, defines which ECMAScript features to support, and details the rules that determine what the linter will flag as errors, warnings, or off.

Configuration Details

files: Specifies the types of files that ESLint will lint within the project. This setup includes JavaScript, JSX, TypeScript, and TSX files, ensuring that all potential script files in the project are covered.

languageOptions:

ecmaVersion: 2020: Allows ESLint to parse modern ECMAScript features, ensuring that the linter understands newer syntax introduced up to the year 2020.
sourceType: 'module': Treats the files as ECMAScript modules, thus expecting import and export statements.
globals: Defines global variables that are read-only; in this case, jest is specified, which is useful for projects using Jest for testing.
linterOptions:

reportUnusedDisableDirectives: Reports any eslint-disable comments that are no longer necessary because the rules they were disabling are not triggering warnings or errors.
plugins: Extends ESLint's base functionality with additional rules, environments, and settings from third-party plugins:

jest: Adds specific rules and environments needed for linting Jest test files.
security: Introduces rules focused on identifying potential security issues.
prettier: Ensures that ESLint's formatting rules don't conflict with Prettier, a code formatter.
rules: Defines specific linting rules and their severity levels (error, warn, off) for the project. This includes stylistic preferences like quote type and semicolon usage, best practices like preferring constants and template literals, and functional adjustments like allowing unnamed functions.

ignores: Specifies paths that should be ignored by the linter. This helps prevent ESLint from linting directories and files that do not contain relevant code, such as node_modules, build outputs, and configuration files from IDEs like .idea.

Purpose and Benefits

Using an eslint.config.mjs configuration file in your project serves multiple benefits:

Consistency: Enforces a consistent coding style and practices across the project, which is especially beneficial in team environments.
Quality Assurance: Helps catch common coding mistakes and potential bugs before they make it to production.
Efficiency: Automates the process of code checks, saving time in code reviews and manual testing.
Integration: Seamlessly integrates with build tools and editors to provide real-time linting and feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment