JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionstooling
PrevNext

Learn the concept

Custom ESLint Rules & Plugins

tooling
senior
custom

How do you create custom ESLint rules and plugins?

eslint
plugins
ast
custom-rules
Quick Answer

Create custom ESLint rules by writing AST visitors that detect patterns and report errors. Rules receive context with report() for violations. Package multiple rules as a plugin with rules and recommended configs. Use AST Explorer to understand code structure.

Detailed Explanation

Custom Rule Structure:

  • meta: Rule metadata (docs, schema)
  • create: Returns AST visitors
  • Report violations with context.report()

AST Visitors:

  • Visit specific node types
  • Access node properties
  • Check patterns
  • Report issues

Plugin Structure:

  • rules: Object of rule definitions
  • configs: Shareable configurations
  • processors: For non-JS files

Tools:

  • AST Explorer for analysis
  • @typescript-eslint/utils for TS

Code Examples

Custom ESLint ruleJavaScript
// eslint-plugin-myorg/rules/no-console-log.js
module.exports = {
  meta: {
    type: 'suggestion',
    docs: {
      description: 'Disallow console.log',
      category: 'Best Practices',
      recommended: true,
    },
    fixable: 'code',
    schema: [{
      type: 'object',
      properties: {
        allow: {
          type: 'array',
          items: { type: 'string' }
        }
      }
    }],
    messages: {
      noConsoleLog: 'Unexpected console.log. Use logger instead.',
    },
  },
  
  create(context) {
    const options = context.options[0] || {};
    const allowed = options.allow || [];
    
    return {
      // Visit CallExpression nodes
      CallExpression(node) {
        if (
          node.callee.type === 'MemberExpression' &&
          node.callee.object.name === 'console' &&
          node.callee.property.name === 'log' &&
          !allowed.includes('log')
        ) {
          context.report({
            node,
            messageId: 'noConsoleLog',
            fix(fixer) {
              return fixer.replaceText(
                node.callee,
                'logger.debug'
              );
            },
          });
        }
      },
    };
  },
};

Real-World Applications

Use Cases

Enforcing highly specific coding standards or architectural patterns within a large enterprise codebase that are not covered by standard ESLint rules

Developing custom ESLint rules to prevent anti-patterns, enforce internal conventions (e.g., specific module import paths, naming conventions for services), or ensure compliance with security policies, improving code quality and maintainability.

Automating code transformations or refactorings that align with a company's internal best practices or framework usage

Creating custom autofixable ESLint rules that not only report violations but also automatically correct them, enabling developers to quickly conform to established standards without manual intervention.

Integrating a custom domain-specific language (DSL) or framework into a JavaScript project and ensuring its correct usage

Building an ESLint plugin that understands the syntax and semantics of the custom DSL/framework, providing intelligent linting, autocomplete suggestions, and error checking tailored to its specific rules.

Mini Projects

Custom Console Log Rule

intermediate

Create a custom ESLint rule that disallows `console.log` statements in production code, with an option to allow specific console methods (e.g., `console.warn`, `console.error`).

Custom React Hook Naming Rule

intermediate

Develop an ESLint rule that enforces a specific naming convention for custom React hooks (e.g., `use[Name]`), ensuring consistency across a React project.

Industry Examples

Meta (Facebook)

Meta maintains an extensive suite of internal ESLint rules and plugins to enforce coding standards, optimize performance, and ensure the stability of its massive React and JavaScript applications. These rules often codify best practices derived from years of large-scale development.

Shopify

Shopify's frontend development teams utilize custom ESLint rules to enforce conventions specific to their Polaris design system and internal framework usage, promoting consistency and reducing cognitive overhead for developers working on various Shopify applications.

Resources

ESLint - Working with Rules

docs

AST Explorer

docs

Related Questions

What is ESLint and why should you use it?

junior
linting

How does Babel work and how do you configure it?

senior
transpilation
Previous
What is a monorepo and how do you set one up?
Next
How does Babel work and how do you configure it?
PrevNext