/core-rules)
    * [ Governance ](/docs/latest/contribute/governance)
    * [ Report a Security Vulnerability ](/docs/latest/contribute/report-security-vulnerability)
  * [ Maintain ESLint ](/docs/latest/maintain/)
    * [ How ESLint is Maintained ](/docs/latest/maintain/overview)
    * [ Manage Issues and Pull Requests ](/docs/latest/maintain/manage-issues)
    * [ Review Pull Requests ](/docs/latest/maintain/review-pull-requests)
    * [ Manage Releases ](/docs/latest/maintain/manage-releases)
    * [ Working Groups ](/docs/latest/maintain/working-groups)

#  Custom Rule Tutorial

##  Table of Contents

  1. Why Create a Custom Rule? 
  2. Prerequisites 
  3. The Custom Rule 
  4. Step 1: Set up Your Project 
  5. Step 2: Stub Out the Rule File 
  6. Step 3: Add Rule Metadata 
  7. Step 4: Add Rule Visitor Methods 
  8. Step 5: Set up Testing 
  9. Step 6: Write the Test 
  10. Step 7: Bundle the Custom Rule in a Plugin 
  11. Step 8: Use the Plugin Locally 
  12. Step 9: Publish the Plugin 
  13. Step 10: Use the Published Custom Rule 
  14. Summary 
  15. View the Tutorial Code 

This tutorial covers how to create a custom rule for ESLint and distribute it
with a plugin.

You can create custom rules to validate if your code meets a certain
expectation, and determine what to do if it does not meet that expectation.
Plugins package custom rules and other configuration, allowing you to easily
share and reuse them in different projects.

To learn more about custom rules and plugins refer to the following
documentation:

  * [ Custom Rules ](custom-rules)
  * [ Plugins ](plugins)

##  Why Create a Custom Rule?

Create a custom rule if the ESLint [ built-in rules ](../rules/) and
community-published custom rules do not meet your needs. You might create a
custom rule to enforce a best practice for your company or project, prevent a
particular bug from recurring, or ensure compliance with a style guide.

Before creating a custom rule that isn’t specific to your company or project,
it’s worth searching the web to see if someone has published a plugin with a
custom rule that solves your use case. It’s quite possible the rule may
already exist.

##  Prerequisites

Before you begin, make sure you have the following installed in your
development environment:

  * [ Node.js ](https://nodejs.org/en/download/)
  * [ npm ](https://www.npmjs.com/)

This tutorial also assumes that you have a basic understanding of ESLint and
ESLint rules.

##  The Custom Rule

The custom rule in this tutorial requires that all ` const ` variables named `
foo ` are assigned the string literal ` "bar" ` . The rule is defined in the
file ` enforce-foo-bar.js ` . The rule also suggests replacing any other value
assigned to ` const foo ` with ` "bar" ` .

For example, say you had the following ` foo.js ` file:

    
    
    // foo.js
    
    const foo = "baz123";
    
    
    1  
    2  
    3  
    

Running ESLint with the rule would flag ` "baz123" ` as an incorrect value for
variable ` foo ` . If ESLint is running in autofix mode, then ESLint would fix
the file to contain the following:

    
    
    // foo.js
    
    const foo = "bar";
    
    
    1  
    2  
    3  
    

##  Step 1: Set up Your Project

First, create a new project for your custom rule. Create a new directory,
initiate a new npm project in it, and create a new file for the custom rule:

    
    
    mkdir eslint-custom-rule-example # create directory
    cd eslint-custom-rule-example # enter the directory
    npm init -y # init new npm project
    touch enforce-foo-bar.js # create file enforce-foo-bar.js
    
    
    1  
    2  
    3  
    4  
    

##  Step 2: Stub Out the Rule File

In the ` enforce-foo-bar.js ` file, add some scaffolding for the ` enforce-
foo-bar ` custom rule. Also, add a ` meta ` object with some basic information
about the rule.

    
    
    // enforce-foo-bar.js
    
    module.exports = {
        meta: {
           // TODO: add metadata
        },