From 45c9ccecd8254d452ded36573bee8e2c6ac69b9f Mon Sep 17 00:00:00 2001 From: Ben Perlmutter Date: Sat, 21 Jan 2023 15:53:19 -0500 Subject: [PATCH] docs: new page for custom processors Resolves #16761 --- docs/src/extend/custom-processors.md | 120 +++++++++++++++++++++++++++ docs/src/extend/index.md | 4 + docs/src/extend/plugins.md | 103 ++--------------------- docs/src/extend/shareable-configs.md | 2 +- docs/src/integrate/nodejs-api.md | 2 +- 5 files changed, 133 insertions(+), 98 deletions(-) create mode 100644 docs/src/extend/custom-processors.md diff --git a/docs/src/extend/custom-processors.md b/docs/src/extend/custom-processors.md new file mode 100644 index 00000000000..160c893db77 --- /dev/null +++ b/docs/src/extend/custom-processors.md @@ -0,0 +1,120 @@ +--- +title: Custom Processors +eleventyNavigation: + key: custom processors + parent: extend eslint + title: Custom Processors + order: 5 + +--- +You can also create custom processors that tell ESLint how to process files other than JavaScript. + +## Custom Processor Specification + +In order to create a processor, the object that is exported from your module has to conform to the following interface: + +```js +module.exports = { + processors: { + "processor-name": { + // 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 [ // return an array of code blocks to lint + { text: code1, filename: "0.js" }, + { text: code2, filename: "1.js" }, + ]; + }, + + // 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 [].concat(...messages); + }, + + supportsAutofix: true // (optional, defaults to false) + } + } +}; +``` + +**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. Name of the block can be anything, but should include the file extension, that would tell the linter how to process the current block. The linter will check [`--ext` CLI option](../use/command-line-interface#--ext) to see if the current block should be linted, and resolve `overrides` configs to check how to process the current block. + +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 locations of all errors to correspond to locations in the original, unprocessed code, and aggregate them into a single flat array and return it. + +Reported problems have the following location information: + +```typescript +{ + line: number, + column: number, + + endLine?: number, + endColumn?: number +} +``` + +By default, ESLint will not perform autofixes when a processor is used, even when the `--fix` flag is enabled on the command line. To allow ESLint to autofix code when using your processor, you should take the following additional steps: + +1. Update the `postprocess` method to additionally transform the `fix` property of reported problems. All autofixable problems will have a `fix` property, which is an object with the following schema: + + ```js + { + range: [number, number], + text: string + } + ``` + + The `range` property contains two indexes in the code, referring to the start and end location of a contiguous section of text that will be replaced. The `text` property refers to the text that will replace the given range. + + In the initial list of problems, the `fix` property will refer to a fix in the processed JavaScript. The `postprocess` method should transform the object to refer to a fix in the original, unprocessed file. + +2. Add a `supportsAutofix: true` property to the processor. + +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. + +## Specifying Processor in Config Files + +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. + +For example: + +```yml +plugins: + - a-plugin +overrides: + - files: "*.md" + processor: a-plugin/markdown +``` + +See [Specifying Processor](../use/configure/plugins#specify-a-processor) for details. + +## File Extension-named Processor + +If a processor name starts with `.`, ESLint handles the processor as a **file extension-named processor** especially and applies the processor to the kind of files automatically. People don't need to specify the file extension-named processors in their config files. + +For example: + +```js +module.exports = { + processors: { + // This processor will be applied to `*.md` files automatically. + // Also, people can use this processor as "plugin-id/.md" explicitly. + ".md": { + preprocess(text, filename) { /* ... */ }, + postprocess(messageLists, filename) { /* ... */ } + } + } +} +``` diff --git a/docs/src/extend/index.md b/docs/src/extend/index.md index 0ca8c777529..426000d49e6 100644 --- a/docs/src/extend/index.md +++ b/docs/src/extend/index.md @@ -33,6 +33,10 @@ This section explains how you can create a custom formatter to control what ESLi If you don't want to use the default parser of ESLint, this section explains how to create custom parsers. +## [Custom Processors](custom-processors) + +This section explains how you can use a custom processor to have ESLint process files other than JavaScript. + ## [Share Configurations](shareable-configs) This section explains how you can bundle and share ESLint configuration in a JavaScript package. diff --git a/docs/src/extend/plugins.md b/docs/src/extend/plugins.md index 407b4039ba9..970581ae4f2 100644 --- a/docs/src/extend/plugins.md +++ b/docs/src/extend/plugins.md @@ -57,109 +57,20 @@ Plugin environments can define the following objects: ### 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, the object that is exported from your module has to conform to the following interface: - -```js -module.exports = { - processors: { - "processor-name": { - // 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 [ // return an array of code blocks to lint - { text: code1, filename: "0.js" }, - { text: code2, filename: "1.js" }, - ]; - }, - - // 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 [].concat(...messages); - }, - - supportsAutofix: true // (optional, defaults to false) - } - } -}; -``` - -**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. Name of the block can be anything, but should include the file extension, that would tell the linter how to process the current block. The linter will check [`--ext` CLI option](../use/command-line-interface#--ext) to see if the current block should be linted, and resolve `overrides` configs to check how to process the current block. - -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 locations of all errors to correspond to locations in the original, unprocessed code, and aggregate them into a single flat array and return it. - -Reported problems have the following location information: - -```typescript -{ - line: number, - column: number, - - endLine?: number, - endColumn?: number -} -``` - -By default, ESLint will not perform autofixes when a processor is used, even when the `--fix` flag is enabled on the command line. To allow ESLint to autofix code when using your processor, you should take the following additional steps: - -1. Update the `postprocess` method to additionally transform the `fix` property of reported problems. All autofixable problems will have a `fix` property, which is an object with the following schema: - - ```js - { - range: [number, number], - text: string - } - ``` - - The `range` property contains two indexes in the code, referring to the start and end location of a contiguous section of text that will be replaced. The `text` property refers to the text that will replace the given range. - - In the initial list of problems, the `fix` property will refer to a fix in the processed JavaScript. The `postprocess` method should transform the object to refer to a fix in the original, unprocessed file. - -2. Add a `supportsAutofix: true` property to the processor. - -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. - -#### Specifying Processor in Config Files - -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. - -For example: - -```yml -plugins: - - a-plugin -overrides: - - files: "*.md" - processor: a-plugin/markdown -``` - -See [Specifying Processor](../use/configure/plugins#specify-a-processor) for details. - -#### File Extension-named Processor - -If a processor name starts with `.`, ESLint handles the processor as a **file extension-named processor** especially and applies the processor to the kind of files automatically. People don't need to specify the file extension-named processors in their config files. - -For example: +You can add processors to plugins by including the processor functions in the `processors` key. For more information on defining custom processors, refer to [Custom Processors](custom-processors). ```js module.exports = { processors: { // This processor will be applied to `*.md` files automatically. - // Also, people can use this processor as "plugin-id/.md" explicitly. ".md": { preprocess(text, filename) { /* ... */ }, - postprocess(messageLists, filename) { /* ... */ } + postprocess(messages, filename) { /* ... */ } + } + "processor-name": { + preprocess: function(text, filename) {/* ... */}, + + postprocess: function(messages, filename) { /* ... */ }, } } } diff --git a/docs/src/extend/shareable-configs.md b/docs/src/extend/shareable-configs.md index 31f59b3ef12..297229b9694 100644 --- a/docs/src/extend/shareable-configs.md +++ b/docs/src/extend/shareable-configs.md @@ -4,7 +4,7 @@ eleventyNavigation: key: share configs parent: extend eslint title: Share Configurations - order: 5 + order: 6 --- diff --git a/docs/src/integrate/nodejs-api.md b/docs/src/integrate/nodejs-api.md index de035ca731c..08583fc3448 100644 --- a/docs/src/integrate/nodejs-api.md +++ b/docs/src/integrate/nodejs-api.md @@ -4,7 +4,7 @@ eleventyNavigation: key: node.js api parent: extend eslint title: Node.js API Reference - order: 6 + order: 7 --- 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.