Blog>
Snippets

Customizing ESLint rules for code quality

Explain how to customize ESLint rules, such as `no-unused-vars`, `eqeqeq`, and `no-console`, for specific code quality requirements in a Next.js application.
/* .eslintrc.js */
module.exports = {
  rules: {
    // Disallow unused variables except for those after an underscore
    'no-unused-vars': ['error', { 'argsIgnorePattern': '^_' }],

    // Enforce the use of triple equals === instead of double equals ==
    'eqeqeq': ['error', 'always'],

    // Allow console.log in development but not in production
    'no-console': process.env.NODE_ENV === 'production' ? ['error', { allow: ['warn', 'error'] }] : 'off'
  }
};
In this ESLint configuration for a Next.js application, we are customizing three rules. The 'no-unused-vars' rule is set to error when variables are unused, with an exception for variables starting with an underscore. The 'eqeqeq' rule is set to error unless triple equals are used for equality checks. The 'no-console' rule is environment-dependent, allowing console logs in development mode but not in production, while still permitting console.warn and console.error in production.