.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / docs / developer-guide / processors.md
1 # Writing processors
2
3 Processors are functions that hook into stylelint's pipeline, modifying code on its way into stylelint and modifying results on their way out.
4
5 *Processors can only be used with the CLI and the Node API, not with the PostCSS plugin.*
6
7 Processor modules are functions that accept an options object and return an object with the following the functions, which hook into the processing of each file:
8
9 -   **code**: A function that accepts two arguments, the file's code and the file's path, and returns a string for stylelint to lint.
10 -   **result**: A function that accepts two arguments, the file's stylelint result object and the file's path, and either mutates the result object (returning nothing) or returns a new one.
11
12 ```js
13 // my-processor.js
14 module.exports = function(options) {
15   return {
16     code: function(input, filepath) {
17       // ...
18       return transformedCode;
19     },
20     result: function(stylelintResult, filepath) {
21       // ...
22       return transformedResult;
23     }
24   };
25 }
26 ```
27
28 Processors can enable stylelint to lint the CSS within non-stylesheet files. For example, let's say you want to lint the CSS within `<style>` tags in HTML. If you just feed stylelint your HTML code, you'll run into problems, because PostCSS does not parse HTML by default. Instead, you can create a processor that does the following:
29
30 -   In the `code` processor function, extract CSS from the `<style>` tags in the HTML code. Return a CSS string containing all that extracted CSS, which is what stylelint will inspect.
31 -   Build a sourcemap while performing the extraction, so warning positions can be tailored to match the original source HTML.
32 -   In the `result` processor function, modify the line/column position of each warning using your sourcemap.
33
34 *Processor options must be JSON-friendly*, because users will need to include them in `.stylelintrc` files.
35
36 ## Sharing processors
37
38 -   Use the `stylelint-processor` keyword within your `package.json`.
39 -   Once your processor is published, please send us a Pull Request to add your processor to [the list](../user-guide/processors.md).