Skip to content
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: add metadata for parser/processor #17438

Merged
merged 4 commits into from Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 20 additions & 2 deletions docs/src/extend/custom-parsers.md
Expand Up @@ -12,6 +12,24 @@ ESLint custom parsers let you extend ESLint to support linting new non-standard

## Creating a Custom Parser

### Metadata in Custom Parsers
JLHwung marked this conversation as resolved.
Show resolved Hide resolved

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:

```js
// preferred location of name and version
module.exports = {
meta: {
name: "eslint-parser-custom",
version: "1.2.3"
}
};
```

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`.

### Methods in Custom Parsers

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rearrange this so the meta data section comes after these other sections? We'd like people to focus on the methods first and then think about meta data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Np. I copied the ordering from plugins.md. Also updated the ordering there.


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.
Expand All @@ -33,11 +51,11 @@ function parse(code, options) {
module.exports = { parse };
```

## `parse` Return Object
### `parse` Return Object

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

## `parseForESLint` Return Object
### `parseForESLint` Return Object

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

Expand Down
6 changes: 6 additions & 0 deletions docs/src/extend/custom-processors.md
Expand Up @@ -18,6 +18,10 @@ In order to create a custom processor, the object exported from your module has
module.exports = {
processors: {
"processor-name": {
meta: {
name: "eslint-processor-name",
version: "1.2.3"
},
// takes text of the file and filename
preprocess: function(text, filename) {
// here, you can strip out any non-JS content
Expand Down Expand Up @@ -45,6 +49,8 @@ module.exports = {
};
```

**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`.

**The `preprocess` method** takes the file contents and filename as arguments, and returns an array of code blocks to lint. The code blocks will be linted separately but still be registered to the filename.

A code block has two properties `text` and `filename`. The `text` property is the content of the block and the `filename` property is the name of the block. The name of the block can be anything, but should include the file extension, which tells the linter how to process the current block. The linter checks the [`--ext` CLI option](../use/command-line-interface#--ext) to see if the current block should be linted and resolves `overrides` configs to check how to process the current block.
Expand Down