New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: Update Linter API for v9 #17937
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -491,7 +491,7 @@ const codeLines = SourceCode.splitLines(code); | |
|
||
## 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. Unless you are working in the browser, you probably want to use the [ESLint class](#eslint-class) instead. | ||
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 files. Unless you are working in the browser, you probably want to use the [ESLint class](#eslint-class) instead. | ||
|
||
The `Linter` is a constructor, and you can create a new instance by passing in the options you want to use. The available options are: | ||
|
||
|
@@ -513,8 +513,8 @@ Those run on `linter2` will get `process.cwd()` if the global `process` object i | |
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 of `SourceCode`). | ||
* `config` - a configuration object that has been processed and normalized by `ESLint` using eslintrc files and/or other configuration arguments. | ||
* **Note**: If you want to lint text and have your configuration be read and processed, use [`ESLint#lintFiles()`][eslint-lintfiles] or [`ESLint#lintText()`][eslint-linttext] instead. | ||
* `config` - a [Configuration object] or an array of configuration objects. | ||
* **Note**: If you want to lint text and have your configuration be read from the file system, use [`ESLint#lintFiles()`][eslint-lintfiles] or [`ESLint#lintText()`][eslint-linttext] instead. | ||
* `options` - (optional) Additional options for this run. | ||
* `filename` - (optional) the filename to associate with the source code. | ||
* `preprocess` - (optional) A function that [Processors in Plugins](../extend/plugins#processors-in-plugins) documentation describes as the `preprocess` method. | ||
|
@@ -557,18 +557,20 @@ const messages = linter.verify(code, { | |
The `verify()` method returns an array of objects containing information about the linting warnings and errors. Here's an example: | ||
|
||
```js | ||
{ | ||
fatal: false, | ||
ruleId: "semi", | ||
severity: 2, | ||
line: 1, | ||
column: 23, | ||
message: "Expected a semicolon.", | ||
fix: { | ||
range: [1, 15], | ||
text: ";" | ||
[ | ||
{ | ||
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: | ||
|
@@ -601,8 +603,6 @@ const suppressedMessages = linter.getSuppressedMessages(); | |
console.log(suppressedMessages[0].suppressions); // [{ "kind": "directive", "justification": "Need to suppress" }] | ||
``` | ||
|
||
Linting message objects have a deprecated `source` property. This property **will be removed** from linting messages in an upcoming breaking release. If you depend on this property, you should now use the `SourceCode` instance provided by the linter. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This property was removed in ESLint v5 (27e3f24) |
||
|
||
You can also get an instance of the `SourceCode` object used inside of `linter` by using the `getSourceCode()` method: | ||
|
||
```js | ||
|
@@ -653,83 +653,6 @@ The information available is: | |
* `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 under `verify` 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. | ||
|
||
```js | ||
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. | ||
|
||
```js | ||
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. | ||
|
||
```js | ||
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#defineParser | ||
|
||
Each instance of `Linter` holds a map of custom parsers. If you want to define a parser programmatically, you can add this function | ||
with the name of the parser as first argument and the [parser object](../extend/custom-parsers) as second argument. The default `"espree"` parser will already be loaded for every `Linter` instance. | ||
|
||
```js | ||
const Linter = require("eslint").Linter; | ||
const linter = new Linter(); | ||
|
||
linter.defineParser("my-custom-parser", { | ||
parse(code, options) { | ||
// ... | ||
} | ||
}); | ||
|
||
const results = linter.verify("// some source text", { parser: "my-custom-parser" }); | ||
``` | ||
|
||
### Linter#version/Linter.version | ||
|
||
Each instance of `Linter` has a `version` property containing the semantic version number of ESLint that the `Linter` instance is from. | ||
|
@@ -738,15 +661,15 @@ Each instance of `Linter` has a `version` property containing the semantic versi | |
const Linter = require("eslint").Linter; | ||
const linter = new Linter(); | ||
|
||
linter.version; // => '4.5.0' | ||
linter.version; // => '9.0.0' | ||
``` | ||
|
||
There is also a `Linter.version` property that you can read without instantiating `Linter`: | ||
|
||
```js | ||
const Linter = require("eslint").Linter; | ||
|
||
Linter.version; // => '4.5.0' | ||
Linter.version; // => '9.0.0' | ||
``` | ||
|
||
--- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't a change in v9, I just wrapped the lint message example in
[]
becauseverify()
always returns arrays.