Working with Plugins
Each plugin is an npm module with a name in the format of eslint-plugin-<plugin-name>
, such as eslint-plugin-jquery
. You can also use scoped packages in the format of @<scope>/eslint-plugin-<plugin-name>
such as @jquery/eslint-plugin-jquery
.
Create a Plugin
The easiest way to start creating a plugin is to use the Yeoman generator. The generator will guide you through setting up the skeleton of a plugin.
Rules in Plugins
Plugins can expose additional rules for use in ESLint. To do so, the plugin must export a rules
object containing a key-value mapping of rule ID to rule. The rule ID does not have to follow any naming convention (so it can just be dollar-sign
, for instance).
module.exports = {
rules: {
"dollar-sign": {
create: function (context) {
// rule implementation ...
}
}
}
};
To use the rule in ESLint, you would use the unprefixed plugin name, followed by a slash, followed by the rule name. So if this plugin were named eslint-plugin-myplugin
, then in your configuration you'd refer to the rule by the name myplugin/dollar-sign
. Example: "rules": {"myplugin/dollar-sign": 2}
.
Environments in Plugins
Plugins can expose additional environments for use in ESLint. To do so, the plugin must export an environments
object. The keys of the environments
object are the names of the different environments provided and the values are the environment settings. For example:
module.exports = {
environments: {
jquery: {
globals: {
$: false
}
}
}
};
There's a jquery
environment defined in this plugin. To use the environment in ESLint, you would use the unprefixed plugin name, followed by a slash, followed by the environment name. So if this plugin were named eslint-plugin-myplugin
, then you would set the environment in your configuration to be "myplugin/jquery"
.
Plugin environments can define the following objects:
globals
- acts the sameglobals
in a configuration file. The keys are the names of the globals and the values aretrue
to allow the global to be overwritten andfalse
to disallow.parserOptions
- acts the same asparserOptions
in a configuration file.
Processors in Plugins
You can also create plugins that would tell ESLint how to process files other than JavaScript. In order to create a processor, object that is exported from your module has to conform to the following interface:
processors: {
// assign to the file extension you want (.js, .jsx, .html, etc.)
".ext": {
// takes text of the file and filename
preprocess: function(text, filename) {
// here, you can strip out any non-JS content
// and split into multiple strings to lint
return [string]; // return an array of strings to lint
},
// takes a Message[][] and filename
postprocess: function(messages, filename) {
// `messages` argument contains two-dimensional array of Message objects
// where each top-level array item contains array of lint messages related
// to the text that was returned in array from preprocess() method
// you need to return a one-dimensional array of the messages you want to keep
return [Message];
}
}
}
The preprocess
method takes the file contents and filename as arguments, and returns an array of strings to lint. The strings will be linted separately but still be registered to the filename. It's up to the plugin to decide if it needs to return just one part, or multiple pieces. For example in the case of processing .html
files, you might want to return just one item in the array by combining all scripts, but for .md
file where each JavaScript block might be independent, you can return multiple items.
The postprocess
method takes a two-dimensional array of arrays of lint messages and the filename. Each item in the input array corresponds to the part that was returned from the preprocess
method. The postprocess
method must adjust the location of all errors and aggregate them into a single flat array and return it.
You can have both rules and processors in a single plugin. You can also have multiple processors in one plugin. To support multiple extensions, add each one to the processors
element and point them to the same object.
Configs in Plugins
You can bundle configurations inside a plugin. This can be useful when you want to provide not just code style, but also some custom rules to support it. You can specify configurations under configs
key. Please note that when exposing configurations, you have to name each one, and there is no default. So your users will have to specify the name of the configuration they want to use.
configs: {
myConfig: {
env: ["browser"],
rules: {
semi: 2,
"myPlugin/my-rule": 2,
"eslint-plugin-myPlugin/another-rule": 2
}
}
}
Note: Please note that configuration will not automatically attach your rules and you have to specify your plugin name and any rules you want to enable that are part of the plugin. Any plugin rules must be prefixed with the short or long plugin name. See Configuring Plugins
Peer Dependency
To make clear that the plugin requires ESLint to work correctly you have to declare ESLint as a peerDependency
in your package.json
. The plugin support was introduced in ESLint version 0.8.0
. Ensure the peerDependency
points to ESLint 0.8.0
or later.
{
"peerDependencies": {
"eslint": ">=0.8.0"
}
}
Testing
You can test the rules of your plugin the same way as bundled ESLint rules using RuleTester.
Example:
"use strict";
var rule = require("../../../lib/rules/custom-plugin-rule"),
RuleTester = require("eslint").RuleTester;
var ruleTester = new RuleTester();
ruleTester.run("custom-plugin-rule", rule, {
valid: [
"var validVariable = true",
],
invalid: [
{
code: "var invalidVariable = true",
errors: [ { message: "Unexpected invalid variable." } ]
},
{
code: "var invalidVariable = true",
errors: [ { message: /^Unexpected.+variable/ } ]
}
]
});
The RuleTester
constructor accepts an optional object argument, which can be used to specify defaults for your test cases. For example, if all of your test cases use ES2015, you can set it as a default:
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } });
The RuleTester#run()
method is used to run the tests. It should be passed the following arguments:
- The name of the rule (string)
- The rule object itself (see "working with rules")
- An object containing
valid
andinvalid
properties, each of which is an array containing test cases.
A test case is an object with the following properties:
code
(string, required): The source code that the rule should be run onoptions
(array, optional): The options passed to the rule. The rule severity should not be included in this list.filename
(string, optional): The filename for the given case (useful for rules that make assertions about filenames)
In addition to the properties above, invalid test cases can also have the following properties:
errors
(number or array, required): Asserts some properties of the errors that the rule is expected to produce when run on this code. If this is a number, asserts the number of errors produced. Otherwise, this should be a list of objects, each containing information about a single reported error. The following properties can be used for an error (all are optional):message
(string/regexp): The message for the errortype
(string): The type of the reported AST nodeline
(number): The 1-based line number of the reported locationcolumn
(number): The 0-based column number of the reported locationendLine
(number): The 1-based line number of the end of the reported locationendColumn
(number): The 0-based column number of the end of the reported location
output
(string, optional): Asserts the output that will be produced when using this rule for a single pass of autofixing (e.g. with the--fix
command line flag). If this isnull
, asserts that none of the reported problems suggest autofixes.
Any additional properties of a test case will be passed directly to the linter as config options. For example, a test case can have a parserOptions
property to configure parser behavior.
Customizing RuleTester
To create tests for each valid and invalid case, RuleTester
internally uses describe
and it
methods from the Mocha test framework when it is available. If you use another test framework, you can override RuleTester.describe
and RuleTester.it
to make RuleTester
compatible with it and have proper individual tests and feedback.
Example:
"use strict";
var RuleTester = require("eslint").RuleTester;
var test = require("my-test-runner");
RuleTester.describe = function(text, method) {
RuleTester.it.title = text;
return method.apply(this);
};
RuleTester.it = function(text, method) {
test(RuleTester.it.title + ": " + text, method);
};
// then use RuleTester as documented
Share Plugins
In order to make your plugin available to the community you have to publish it on npm.
Recommended keywords:
eslint
eslintplugin
Add these keywords into your package.json
file to make it easy for others to find.