Node.js API
While ESLint is designed to be run on the command line, it’s possible to use ESLint programmatically through the Node.js API. The purpose of the Node.js API is to allow plugin and tool authors to use the ESLint functionality directly, without going through the command line interface.
Note: Use undocumented parts of the API at your own risk. Only those parts that are specifically mentioned in this document are approved for use and will remain stable and reliable. Anything left undocumented is unstable and may change or be removed at any point.
SourceCode
The SourceCode
type represents the parsed source code that ESLint executes on. It’s used internally in ESLint and is also available so that already-parsed code can be used. You can create a new instance of SourceCode
by passing in the text string representing the code and an abstract syntax tree (AST) in ESTree format (including location information, range information, comments, and tokens):
var SourceCode = require("eslint").SourceCode;
var code = new SourceCode("var foo = bar;", ast);
The SourceCode
constructor throws an error if the AST is missing any of the required information.
The SourceCode
constructor strips Unicode BOM.
Please note the AST also should be parsed from stripped text.
var SourceCode = require("eslint").SourceCode;
var code = new SourceCode("\uFEFFvar foo = bar;", ast);
assert(code.hasBOM === true);
assert(code.text === "var foo = bar;");
splitLines()
This is a static function on SourceCode
that is used to split the source code text into an array of lines.
var SourceCode = require("eslint").SourceCode;
var code = "var a = 1;\nvar b = 2;"
// split code into an array
var codeLines = SourceCode.splitLines(code);
/*
Value of codeLines will be
[
"var a = 1;",
"var b = 2;"
]
*/
Linter
The Linter
object does the actual evaluation of the JavaScript code. It doesn’t do any filesystem operations, it simply parses and reports on the code. In particular, the Linter
object does not process configuration objects or files. You can retrieve instances of Linter
like this:
var Linter = require("eslint").Linter;
var linter = new Linter();
Linter#verify
The most important method on Linter
is verify()
, which initiates linting of the given text. This method accepts three arguments:
code
- the source code to lint (a string or instance ofSourceCode
).config
- a configuration object that has been processed and normalized by CLIEngine using eslintrc files and/or other configuration arguments.- Note: If you want to lint text and have your configuration be read and processed, use CLIEngine’s
executeOnFiles
orexecuteOnText
instead.
- Note: If you want to lint text and have your configuration be read and processed, use CLIEngine’s
options
- (optional) Additional options for this run.filename
- (optional) the filename to associate with the source code.preprocess
- (optional) A function that accepts a string containing source text, and returns an array of strings containing blocks of code to lint. Also see: Processors in Pluginspostprocess
- (optional) A function that accepts an array of problem lists (one list of problems for each block of code frompreprocess
), and returns a one-dimensional array of problems containing problems for the original, unprocessed text. Also see: Processors in PluginsallowInlineConfig
- (optional) set tofalse
to disable inline comments from changing eslint rules.
If the third argument is a string, it is interpreted as the filename
.
You can call verify()
like this:
var Linter = require("eslint").Linter;
var linter = new Linter();
var messages = linter.verify("var foo;", {
rules: {
semi: 2
}
}, { filename: "foo.js" });
// or using SourceCode
var linter = require("eslint").linter,
SourceCode = require("eslint").SourceCode;
var code = new SourceCode("var foo = bar;", ast);
var messages = linter.verify(code, {
rules: {
semi: 2
}
}, { filename: "foo.js" });
The verify()
method returns an array of objects containing information about the linting warnings and errors. Here’s an example:
{
fatal: false,
ruleId: "semi",
severity: 2,
line: 1,
column: 23,
message: "Expected a semicolon.",
fix: {
range: [1, 15],
text: ";"
}
}
The information available for each linting message is:
column
- the column on which the error occurred.fatal
- usually omitted, but will be set to true if there’s a parsing error (not related to a rule).line
- the line on which the error occurred.message
- the message that should be output.nodeType
- the node or token type that was reported with the problem.ruleId
- the ID of the rule that triggered the messages (or null iffatal
is true).severity
- either 1 or 2, depending on your configuration.source
- the line of code where the problem is (or empty string if it can’t be found).endColumn
- the end column of the range on which the error occurred (this property is omitted if it’s not range).endLine
- the end line of the range on which the error occurred (this property is omitted if it’s not range).fix
- an object describing the fix for the problem (this property is omitted if no fix is available).
Please note: the source
property will be removed from the linting messages in an upcoming breaking release. If you depend on this property, you can still use the getSourceCode
method described below to get the line of code for each message.
You can also get an instance of the SourceCode
object used inside of linter
by using the getSourceCode()
method:
var Linter = require("eslint").Linter;
var linter = new Linter();
var messages = linter.verify("var foo = bar;", {
rules: {
semi: 2
}
}, { filename: "foo.js" });
var code = linter.getSourceCode();
console.log(code.text); // "var foo = bar;"
In this way, you can retrieve the text and AST used for the last run of linter.verify()
.
Linter#verifyAndFix()
This method is similar to verify except that it also runs autofixing logic, similar to the --fix
flag on the command line. The result object will contain the autofixed code, along with any remaining linting messages for the code that were not autofixed.
var Linter = require("eslint").Linter;
var linter = new Linter();
var messages = linter.verifyAndFix("var foo", {
rules: {
semi: 2
}
});
Output object from this method:
{
fixed: true,
output: "var foo;",
messages: []
}
The information available is:
fixed
- True, if the code was fixed.output
- Fixed code text (might be the same as input if no fixes were applied).messages
- Collection of all messages for the given code (It has the same information as explained above underverify
block).
Linter#defineRule
Each Linter
instance holds a map of rule names to loaded rule objects. By default, all ESLint core rules are loaded. If you want to use Linter
with custom rules, you should use the defineRule
method to register your rules by ID.
const Linter = require("eslint").Linter;
const linter = new Linter();
linter.defineRule("my-custom-rule", {
// (an ESLint rule)
create(context) {
// ...
}
});
const results = linter.verify("// some source text", { rules: { "my-custom-rule": "error" } });
Linter#defineRules
This is a convenience method similar to Linter#defineRule
, except that it allows you to define many rules at once using an object.
const Linter = require("eslint").Linter;
const linter = new Linter();
linter.defineRules({
"my-custom-rule": { /* an ESLint rule */ create() {} },
"another-custom-rule": { /* an ESLint rule */ create() {} }
});
const results = linter.verify("// some source text", {
rules: {
"my-custom-rule": "error",
"another-custom-rule": "warn"
}
});
Linter#getRules
This method returns a map of all loaded rules.
const Linter = require("eslint").Linter;
const linter = new Linter();
linter.getRules();
/*
Map {
'accessor-pairs' => { meta: { docs: [Object], schema: [Array] }, create: [Function: create] },
'array-bracket-newline' => { meta: { docs: [Object], schema: [Array] }, create: [Function: create] },
...
}
*/
Linter#version
Each instance of Linter
has a version
property containing the semantic version number of ESLint that the Linter
instance is from.
const Linter = require("eslint").Linter;
const linter = new Linter();
linter.version; // => '4.5.0'
linter
The eslint.linter
object (deprecated) is an instance of the Linter
class as defined above. eslint.linter
exists for backwards compatibility, but we do not recommend using it because any mutations to it are shared among every module that uses eslint
. Instead, please create your own instance of eslint.Linter
.
var linter = require("eslint").linter;
var messages = linter.verify("var foo;", {
rules: {
semi: 2
}
}, { filename: "foo.js" });
Note: This API is deprecated as of 4.0.0.
CLIEngine
The primary Node.js API is CLIEngine
, which is the underlying utility that runs the ESLint command line interface. This object will read the filesystem for configuration and file information but will not output any results. Instead, it allows you direct access to the important information so you can deal with the output yourself.
You can get a reference to the CLIEngine
by doing the following:
var CLIEngine = require("eslint").CLIEngine;
The CLIEngine
is a constructor, and you can create a new instance by passing in the options you want to use. The available options are:
allowInlineConfig
- Set tofalse
to disable the use of configuration comments (such as/*eslint-disable*/
). Corresponds to--no-inline-config
.baseConfig
- Set to false to disable use of base config. Could be set to an object to override default base config as well.cache
- Operate only on changed files (default:false
). Corresponds to--cache
.cacheFile
- Name of the file where the cache will be stored (default:.eslintcache
). Corresponds to--cache-file
. Deprecated: usecacheLocation
instead.cacheLocation
- Name of the file or directory where the cache will be stored (default:.eslintcache
). Corresponds to--cache-location
.configFile
- The configuration file to use (default: null). Corresponds to-c
.cwd
- Path to a directory that should be considered as the current working directory.envs
- An array of environments to load (default: empty array). Corresponds to--env
.extensions
- An array of filename extensions that should be checked for code. The default is an array containing just".js"
. Corresponds to--ext
. It is only used in conjunction with directories, not with filenames or glob patterns.fix
- This can be a boolean or a function which will be provided each linting message and should return a boolean. True indicates that fixes should be included with the output report, and that errors and warnings should not be listed if they can be fixed. However, the files on disk will not be changed. To persist changes to disk, calloutputFixes()
.globals
- An array of global variables to declare (default: empty array). Corresponds to--global
.ignore
- False disables use of.eslintignore
,ignorePath
andignorePattern
(default: true). Corresponds to--no-ignore
.ignorePath
- The ignore file to use instead of.eslintignore
(default: null). Corresponds to--ignore-path
.ignorePattern
- Glob patterns for paths to ignore. String or array of strings.parser
- Specify the parser to be used (default:espree
). Corresponds to--parser
.parserOptions
- An object containing parser options (default: empty object). Corresponds to--parser-options
.plugins
- An array of plugins to load (default: empty array). Corresponds to--plugin
.rulePaths
- An array of directories to load custom rules from (default: empty array). Corresponds to--rulesdir
.rules
- An object of rules to use (default: null). Corresponds to--rule
.useEslintrc
- Set to false to disable use of.eslintrc
files (default: true). Corresponds to--no-eslintrc
.
For example:
var CLIEngine = require("eslint").CLIEngine;
var cli = new CLIEngine({
envs: ["browser", "mocha"],
useEslintrc: false,
rules: {
semi: 2
}
});
In this code, a new CLIEngine
instance is created that sets two environments, "browser"
and "mocha"
, disables loading of .eslintrc
and package.json
files, and enables the semi
rule as an error. You can then call methods on cli
and these options will be used to perform the correct action.
executeOnFiles()
If you want to lint one or more files, use the executeOnFiles()
method. This method accepts a single argument, which is an array of files and/or directories to traverse for files. You can pass the same values as you would using the ESLint command line interface, such as "."
to search all JavaScript files in the current directory. Here’s an example:
var CLIEngine = require("eslint").CLIEngine;
var cli = new CLIEngine({
envs: ["browser", "mocha"],
useEslintrc: false,
rules: {
semi: 2
}
});
// lint myfile.js and all files in lib/
var report = cli.executeOnFiles(["myfile.js", "lib/"]);
The return value is an object containing the results of the linting operation. Here’s an example of a report object:
{
results: [
{
filePath: "/Users/eslint/project/myfile.js",
messages: [{
ruleId: "semi",
severity: 2,
message: "Missing semicolon.",
line: 1,
column: 13,
nodeType: "ExpressionStatement",
source: "\"use strict\"", // Deprecated: see "please note" paragraph below.
fix: { range: [12, 12], text: ";" }
}],
errorCount: 1,
warningCount: 0,
fixableErrorCount: 1,
fixableWarningCount: 0,
source: "\"use strict\"\n"
}
],
errorCount: 1,
warningCount: 0,
fixableErrorCount: 1,
fixableWarningCount: 0
}
You can also pass fix: true
when instantiating the CLIEngine
in order to have it figure out what fixes can be applied.
var CLIEngine = require("eslint").CLIEngine;
var cli = new CLIEngine({
envs: ["browser", "mocha"],
fix: true, // difference from last example
useEslintrc: false,
rules: {
semi: 2,
quotes: [2, "double"]
}
});
// lint myfile.js and all files in lib/
var report = cli.executeOnFiles(["myfile.js", "lib/"]);
{
results: [
{
filePath: "/Users/eslint/project/myfile.js",
messages: [
{
ruleId: "semi",
severity: 2,
message: "Missing semicolon.",
line: 1,
column: 13,
nodeType: "ExpressionStatement",
source: "\"use strict\"", // Deprecated: see "please note" paragraph below.
fix: { range: [12, 12], text: ";" }
},
{
ruleId: "func-name-matching",
severity: 2,
message: "Function name `bar` should match variable name `foo`",
line: 2,
column: 5,
nodeType: "VariableDeclarator",
source: "var foo = function bar() {};"
}
],
errorCount: 2,
warningCount: 0,
fixableErrorCount: 1,
fixableWarningCount: 0,
output: "\"use strict\";\nvar foo = function bar() {};\nfoo();\n"
}
],
errorCount: 2,
warningCount: 0,
fixableErrorCount: 1,
fixableWarningCount: 0,
}
If the operation ends with a parsing error, you will get a single message for this file, with fatal: true
added as an extra property.
{
results: [
{
filePath: "./myfile.js",
messages: [
{
ruleId: null,
fatal: true,
severity: 2,
source: "fucntion foo() {}",
message: "Parsing error: Unexpected token foo",
line: 1,
column: 10
}
],
errorCount: 1,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
source: "fucntion foo() {}"
}
],
errorCount: 1,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
}
The top-level report object has a results
array containing all linting results for files that had warnings or errors (any files that did not produce a warning or error are omitted). Each file result includes:
filePath
- Path to the given file.messages
- Array containing the result of callinglinter.verify()
on the given file.errorCount
andwarningCount
- The exact number of errors and warnings respectively on the given file.source
- The source code for the given file. This property is omitted if this file has no errors/warnings or if theoutput
property is present.output
- The source code for the given file with as many fixes applied as possible, so you can use that to rewrite the files if necessary. This property is omitted if no fix is available.
The top-level report object also has errorCount
and warningCount
which give the exact number of errors and warnings respectively on all the files.
Please note: the source
property will be removed from the linting messages returned in messages
in an upcoming breaking release. If you depend on this property, you should now use the top-level source
or output
properties instead.
Once you get a report object, it’s up to you to determine how to output the results. Fixes will not be automatically applied to the files, even if you set fix: true
when constructing the CLIEngine
instance. To apply fixes to the files, call outputFixes
.
resolveFileGlobPatterns()
You can pass filesystem-style or glob patterns to ESLint and have it function properly. In order to achieve this, ESLint must resolve non-glob patterns into glob patterns before determining which files to execute on. The resolveFileGlobPatterns()
methods uses the current settings from CLIEngine
to resolve non-glob patterns into glob patterns. Pass an array of patterns that might be passed to the ESLint CLI and it will return an array of glob patterns that mean the same thing. Here’s an example:
var CLIEngine = require("eslint").CLIEngine;
var cli = new CLIEngine({
});
// pass an array of patterns
var globPatterns = cli.resolveFileGlobPatterns(["."]);
console.log(globPatterns[i]); // ["**/*.js"]
getConfigForFile()
If you want to retrieve a configuration object for a given file, use the getConfigForFile()
method. This method accepts one argument, a file path, and returns an object represented the calculated configuration of the file. Here’s an example:
var CLIEngine = require("eslint").CLIEngine;
var cli = new CLIEngine({
envs: ["browser", "mocha"],
useEslintrc: false,
rules: {
semi: 2
}
});
var config = cli.getConfigForFile("myfile.js");
Once you have the configuration information, you can pass it into the linter
object:
var CLIEngine = require("eslint").CLIEngine,
linter = require("eslint").linter;
var cli = new CLIEngine({
envs: ["browser", "mocha"],
useEslintrc: false,
rules: {
semi: 2
}
});
var config = cli.getConfigForFile("myfile.js");
var messages = linter.verify('var foo;', config);
executeOnText()
If you already have some text to lint, then you can use the executeOnText()
method to lint that text. The linter will assume that the text is a file in the current working directory, and so will still obey any .eslintrc
and .eslintignore
files that may be present. Here’s an example:
var CLIEngine = require("eslint").CLIEngine;
var cli = new CLIEngine({
envs: ["browser", "mocha"],
useEslintrc: false,
rules: {
semi: 2
}
});
// lint the supplied text and optionally set
// a filename that is displayed in the report
var report = cli.executeOnText("var foo = 'bar';", "foo.js");
The report
returned from executeOnText()
is in the same format as from executeOnFiles()
, but there is only ever one result in report.results
.
If a filename in the optional second parameter matches a file that is configured to be ignored, then this function returns no errors or warnings. To return a warning instead, call the method with true as the optional third parameter.
addPlugin()
Loads a plugin from configuration object with specified name. Name can include plugin prefix (“eslint-plugin-“)
var CLIEngine = require("eslint").CLIEngine;
var cli = new CLIEngine({
ignore: true
});
cli.addPlugin("eslint-plugin-processor", {
processors: {
".txt": {
preprocess: function(text) {
return [text];
},
postprocess: function(messages) {
return messages[0];
}
}
}
});
isPathIgnored()
Checks if a given path is ignored by ESLint.
var CLIEngine = require("eslint").CLIEngine;
var cli = new CLIEngine({
ignore: true,
ignorePath: ".customIgnoreFile"
});
var isIgnored = cli.isPathIgnored("foo/bar.js");
getFormatter()
Retrieves a formatter, which you can then use to format a report object. The argument is either the name of a built-in formatter:
- “checkstyle”
- “codeframe”
- “compact”
- “html”
- “jslint-xml”
- “json”
- “junit”
- “stylish” (the default)
- “table”
- “tap”
- “unix”
or the full path to a JavaScript file containing a custom formatter. You can also omit the argument to retrieve the default formatter.
var CLIEngine = require("eslint").CLIEngine;
var cli = new CLIEngine({
envs: ["browser", "mocha"],
useEslintrc: false,
rules: {
semi: 2
}
});
// lint myfile.js and all files in lib/
var report = cli.executeOnFiles(["myfile.js", "lib/"]);
// get the default formatter
var formatter = cli.getFormatter();
// Also could do...
// var formatter = cli.getFormatter("compact");
// var formatter = cli.getFormatter("./my/formatter.js");
// output to console
console.log(formatter(report.results));
Note: Also available as a static function on CLIEngine
.
// get the default formatter by calling the static function
var formatter = CLIEngine.getFormatter();
Important: You must pass in the results
property of the report. Passing in report
directly will result in an error.
getErrorResults()
This is a static function on CLIEngine
. It can be used to filter out all the non error messages from the report object.
var CLIEngine = require("eslint").CLIEngine;
var cli = new CLIEngine({
envs: ["browser", "mocha"],
useEslintrc: false,
rules: {
semi: 2
}
});
// lint myfile.js and all files in lib/
var report = cli.executeOnFiles(["myfile.js", "lib/"]);
// only get the error messages
var errorReport = CLIEngine.getErrorResults(report.results)
Important: You must pass in the results
property of the report. Passing in report
directly will result in an error.
outputFixes()
This is a static function on CLIEngine
that is used to output fixes from report
to disk. It does by looking for files that have an output
property in their results. Here’s an example:
var CLIEngine = require("eslint").CLIEngine;
var cli = new CLIEngine({
envs: ["browser", "mocha"],
fix: true,
useEslintrc: false,
rules: {
semi: 2
}
});
// lint myfile.js and all files in lib/
var report = cli.executeOnFiles(["myfile.js", "lib/"]);
// output fixes to disk
CLIEngine.outputFixes(report);
CLIEngine.version
CLIEngine
has a static version
property containing the semantic version number of ESLint that it comes from.
require("eslint").CLIEngine.version; // '4.5.0'
RuleTester
eslint.RuleTester
is a utility to write tests for ESLint rules. It is used internally for the bundled rules that come with ESLint, and it can also be used by plugins.
Example usage:
"use strict";
const rule = require("../../../lib/rules/my-rule");
const RuleTester = require("eslint").RuleTester;
const ruleTester = new RuleTester();
ruleTester.run("my-rule", rule, {
valid: [
{
code: "var foo = true",
options: [{ allowFoo: 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
If a string is provided as an error instead of an object, the string is used to assert the
message
of the error.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:
{
code: "let foo;",
parserOptions: { ecmaVersion: 2015 }
}
If a valid test case only uses the code
property, it can optionally be provided as a string containing the code, rather than an object with a code
key.
Customizing RuleTester
RuleTester
depends on two functions to run tests: describe
and it
. These functions can come from various places:
- If
RuleTester.describe
andRuleTester.it
have been set to function values,RuleTester
will useRuleTester.describe
andRuleTester.it
to run tests. You can use this to customize the behavior ofRuleTester
to match a test framework that you’re using. - Otherwise, if
describe
andit
are present as globals,RuleTester
will useglobal.describe
andglobal.it
to run tests. This allowsRuleTester
to work when using frameworks like Mocha without any additional configuration. - Otherwise,
RuleTester#run
will simply execute all of the tests in sequence, and will throw an error if one of them fails. This means you can simply execute a test file that callsRuleTester.run
usingnode
, without needing a testing framework.
RuleTester#run
calls the describe
function with two arguments: a string describing the rule, and a callback function. The callback calls the it
function with a string describing the test case, and a test function. The test function will return successfully if the test passes, and throw an error if the test fails. (Note that this is the standard behavior for test suites when using frameworks like Mocha; this information is only relevant if you plan to customize RuleTester.it
and RuleTester.describe
.)
Example of customizing RuleTester
:
"use strict";
const RuleTester = require("eslint").RuleTester;
const test = require("my-test-runner");
const myRule = require("../../../lib/rules/my-rule");
RuleTester.describe = function(text, method) {
RuleTester.it.title = text;
return method.call(this);
};
RuleTester.it = function(text, method) {
test(RuleTester.it.title + ": " + text, method);
};
// then use RuleTester as documented
const ruleTester = new RuleTester();
ruleTester.run("my-rule", myRule, {
valid: [
// valid test cases
],
invalid: [
// invalid test cases
]
})
Deprecated APIs
cli
- thecli
object has been deprecated in favor ofCLIEngine
. As of v1.0.0,cli
is no longer exported and should not be used by external tools.linter
- thelinter
object has has been deprecated in favor ofLinter
, as of v4.0.0