Skip to content

module.exports = require('../../.eslintplugin')

License

Notifications You must be signed in to change notification settings

taskworld/eslint-plugin-local

Repository files navigation

This ESLint plugin allows you to implement a custom ESLint plugin including custom rules in your repository without installing them as a dependency.

Originally inspired by cletusw/eslint-plugin-local-rules.

Installation

npm install --save-dev eslint-plugin-local

Usage

The JavaScript file named eslint.local.js or eslint-plugin-local.js or .eslint-plugin-local/index.js must be created at the root of your repository, which the file has the content of an ESLint plugin. The extension .cjs can be used in place of .js in case ES module is not supported. For example:

module.exports = {
  rules: {
    sample: {
      // Optional
      meta: {
        // See https://eslint.org/docs/latest/extend/custom-rules#rule-structure
      },

      // Mandatory
      create: function (context) {
        // Implementation goes here
        // See https://eslint.org/docs/latest/extend/custom-rules
      },

      // Optional
      // Unit test can be triggered by `eslint-plugin-local test` command
      // See https://eslint.org/docs/latest/integrate/nodejs-api#ruletester
      tests: {
        valid: [...],
        invalid: [...],
      }
    }
  }
}

Then add the plugin to your eslint.config.js file:

const local = require('eslint-plugin-local')

module.exports = [{
  plugins: {
    local
  },
  rules: {
    'local/sample': 'error'
  }
}]

Additionally, this package provides eslint-plugin-local test command out of the box, which it scans for tests: { valid: [], invalid: [] } field in each rule and runs RuleTester internally.

To make it easy to debug your test cases, wrap one or more test objects inside the global only() function. Given the example below, only the first test case and every invalid case will be run.

module.exports = {
  rules: {
    sample: {
      tests: {
        valid: [
          only({
            code: 'var foo = 1',
          }),
          {
            code: 'var foo = 2',
          }
        ],
        invalid: only([...]),
      }
    }
  }
}