Blog>
Snippets

Customizing ESLint rules for a project

Provide a code snippet that customizes specific ESLint rules within an ESLint configuration file for a unique project requirement.
module.exports = {
  // Extend from the recommended ESLint rules
  extends: 'eslint:recommended',
  // Define the environment: browser and/or node
  env: {
    browser: true,
    node: true
  },
  // Customize specific ESLint rules
  rules: {
    // Enable console statements
    'no-console': 'off',
    // Set indent to 4 spaces instead of 2
    'indent': ['error', 4],
    // Enforce single quotes for strings
    'quotes': ['error', 'single'],
    // Require semicolons at the end of statements
    'semi': ['error', 'always'],
    // Disallow unused variables (except for function arguments beginning with '_')
    'no-unused-vars': ['error', { 'argsIgnorePattern': '^_' }],
    // Allow async-await
    'no-async-promise-executor': 'off'
  }
};
This ESLint configuration file extends the recommended ESLint rules, sets up the environment (browser and Node.js), and customizes specific rules. 'no-console' is turned off to allow console statements, indent is set to 4 spaces, single quotes are enforced for strings, semicolons are required at the end of statements, unused variables rule is customized to ignore function arguments starting with '_', and the rule against async functions inside promises is disabled.