Skip to content

Commit d743ed3

Browse files
JLHwungnzakas
andauthored
docs: add metadata for parser/processor (#17438)
* docs: add metadata for parser/processor * Update docs/src/extend/custom-parsers.md Co-authored-by: Nicholas C. Zakas <[email protected]> * review comments * rearrange `meta` object after `postprocess` --------- Co-authored-by: Nicholas C. Zakas <[email protected]>
1 parent 224376c commit d743ed3

3 files changed

Lines changed: 54 additions & 30 deletions

File tree

docs/src/extend/custom-parsers.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ ESLint custom parsers let you extend ESLint to support linting new non-standard
1212

1313
## Creating a Custom Parser
1414

15+
### Methods in Custom Parsers
16+
1517
A custom parser is a JavaScript object with either a `parse` or `parseForESLint` method. The `parse` method only returns the AST, whereas `parseForESLint` also returns additional values that let the parser customize the behavior of ESLint even more.
1618

1719
Both methods should take in the source code as the first argument, and an optional configuration object as the second argument, which is provided as [`parserOptions`](../use/configure/language-options#specifying-parser-options) in a configuration file.
@@ -33,11 +35,11 @@ function parse(code, options) {
3335
module.exports = { parse };
3436
```
3537

36-
## `parse` Return Object
38+
### `parse` Return Object
3739

3840
The `parse` method should simply return the [AST](#ast-specification) object.
3941

40-
## `parseForESLint` Return Object
42+
### `parseForESLint` Return Object
4143

4244
The `parseForESLint` method should return an object that contains the required property `ast` and optional properties `services`, `scopeManager`, and `visitorKeys`.
4345

@@ -48,6 +50,22 @@ The `parseForESLint` method should return an object that contains the required p
4850
* `visitorKeys` can be an object to customize AST traversal. The keys of the object are the type of AST nodes. Each value is an array of the property names which should be traversed. The default is [KEYS of `eslint-visitor-keys`](https://github.com/eslint/eslint-visitor-keys#evkkeys).
4951
* Support for `visitorKeys` was added in ESLint v4.14.0. ESLint versions that support `visitorKeys` will provide an `eslintVisitorKeys: true` property in `parserOptions`, which can be used for feature detection.
5052

53+
### Meta Data in Custom Parsers
54+
55+
For easier debugging and more effective caching of custom parsers, it's recommended to provide a name and version in a `meta` object at the root of your custom parsers, like this:
56+
57+
```js
58+
// preferred location of name and version
59+
module.exports = {
60+
meta: {
61+
name: "eslint-parser-custom",
62+
version: "1.2.3"
63+
}
64+
};
65+
```
66+
67+
The `meta.name` property should match the npm package name for your custom parser and the `meta.version` property should match the npm package version for your custom parser. The easiest way to accomplish this is by reading this information from your `package.json`.
68+
5169
## AST Specification
5270

5371
The AST that custom parsers should create is based on [ESTree](https://github.com/estree/estree). The AST requires some additional properties about detail information of the source code.

docs/src/extend/custom-processors.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ In order to create a custom processor, the object exported from your module has
1818
module.exports = {
1919
processors: {
2020
"processor-name": {
21+
meta: {
22+
name: "eslint-processor-name",
23+
version: "1.2.3"
24+
},
2125
// takes text of the file and filename
2226
preprocess: function(text, filename) {
2327
// here, you can strip out any non-JS content
@@ -121,6 +125,8 @@ By default, ESLint does not perform autofixes when a custom processor is used, e
121125

122126
You can have both rules and custom 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.
123127

128+
**The `meta` object** helps ESLint cache the processor and provide more friendly debug message. The `meta.name` property should match the processor name and the `meta.version` property should match the npm package version for your processors. The easiest way to accomplish this is by reading this information from your `package.json`.
129+
124130
## Specifying Processor in Config Files
125131

126132
To use a processor, add its ID to a `processor` section in the config file. Processor ID is a concatenated string of plugin name and processor name with a slash as a separator. This can also be added to a `overrides` section of the config, to specify which processors should handle which files.

docs/src/extend/plugins.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,6 @@ Each plugin is an npm module with a name in the format of `eslint-plugin-<plugin
1818

1919
The easiest way to start creating a plugin is to use the [Yeoman generator](https://www.npmjs.com/package/generator-eslint). The generator will guide you through setting up the skeleton of a plugin.
2020

21-
### Metadata in Plugins
22-
23-
For easier debugging and more effective caching of plugins, it's recommended to provide a name and version in a `meta` object at the root of your plugin, like this:
24-
25-
```js
26-
// preferred location of name and version
27-
module.exports = {
28-
meta: {
29-
name: "eslint-plugin-custom",
30-
version: "1.2.3"
31-
}
32-
};
33-
```
34-
35-
The `meta.name` property should match the npm package name for your plugin and the `meta.version` property should match the npm package version for your plugin. The easiest way to accomplish this is by reading this information from your `package.json`.
36-
37-
As an alternative, you can also expose `name` and `version` properties at the root of your plugin, such as:
38-
39-
```js
40-
// alternate location of name and version
41-
module.exports = {
42-
name: "eslint-plugin-custom",
43-
version: "1.2.3"
44-
};
45-
```
46-
47-
While the `meta` object is the preferred way to provide the plugin name and version, this format is also acceptable and is provided for backward compatibility.
48-
4921
### Rules in Plugins
5022

5123
Plugins can expose custom 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). To learn more about creating custom rules in plugins, refer to [Custom Rules](custom-rules).
@@ -163,6 +135,34 @@ If the example plugin above were called `eslint-plugin-myPlugin`, the `myConfig`
163135

164136
```
165137

138+
### Meta Data in Plugins
139+
140+
For easier debugging and more effective caching of plugins, it's recommended to provide a name and version in a `meta` object at the root of your plugin, like this:
141+
142+
```js
143+
// preferred location of name and version
144+
module.exports = {
145+
meta: {
146+
name: "eslint-plugin-custom",
147+
version: "1.2.3"
148+
}
149+
};
150+
```
151+
152+
The `meta.name` property should match the npm package name for your plugin and the `meta.version` property should match the npm package version for your plugin. The easiest way to accomplish this is by reading this information from your `package.json`.
153+
154+
As an alternative, you can also expose `name` and `version` properties at the root of your plugin, such as:
155+
156+
```js
157+
// alternate location of name and version
158+
module.exports = {
159+
name: "eslint-plugin-custom",
160+
version: "1.2.3"
161+
};
162+
```
163+
164+
While the `meta` object is the preferred way to provide the plugin name and version, this format is also acceptable and is provided for backward compatibility.
165+
166166
### Peer Dependency
167167

168168
To make clear that the plugin requires ESLint to work correctly, you must declare ESLint as a peer dependency by mentioning it in the `peerDependencies` field of your plugin's `package.json`.

0 commit comments

Comments
 (0)