UNPKG

2.26 kBMarkdownView Raw
1# Writing custom formatters
2
3A formatter is a function with the following signature:
4
5```js
6/**
7 * @type {import('stylelint').Formatter}
8 */
9function formatter(results, returnValue) {
10 return "a string of formatted results";
11}
12```
13
14Where the first argument (`results`) is an array of Stylelint result objects (type `Array<StylelintResult>`) in the form:
15
16```js
17// A Stylelint result object
18{
19 "source": "path/to/file.css", // The filepath or PostCSS identifier like <input css 1>
20 "errored": true, // This is `true` if at least one rule with an "error"-level severity triggered a warning
21 "warnings": [
22 // Array of rule problem warning objects, each like the following ...
23 {
24 "line": 3,
25 "column": 12,
26 "rule": "block-no-empty",
27 "severity": "error",
28 "text": "You should not have an empty block (block-no-empty)"
29 }
30 ],
31 "deprecations": [
32 // Array of deprecation warning objects, each like the following ...
33 {
34 "text": "Feature X has been deprecated and will be removed in the next major version.",
35 "reference": "https://stylelint.io/docs/feature-x.md"
36 }
37 ],
38 "invalidOptionWarnings": [
39 // Array of invalid option warning objects, each like the following ...
40 {
41 "text": "Invalid option X for rule Y"
42 }
43 ],
44 "ignored": false // This is `true` if the file's path matches a provided ignore pattern
45}
46```
47
48And the second argument (`returnValue`) is an object (type `LinterResult`) with one or more of the following keys:
49
50```js
51{
52 "errored": false, // `true` if there were any warnings with "error" severity
53 "maxWarningsExceeded": {
54 // Present if Stylelint was configured with a `maxWarnings` count
55 "maxWarnings": 10,
56 "foundWarnings": 15
57 }
58}
59```
60
61## Passing arguments
62
63You can use environmental variables in your formatter. For example, pass `SKIP_WARNINGS`:
64
65```console
66SKIP_WARNINGS=true stylelint "*.css" --custom-formatter ./my-formatter.js
67```
68
69Alternatively, you can create a separate formatting program and pipe the output from the built-in JSON formatter into it:
70
71```console
72stylelint -f json "*.css" | my-program-that-reads-JSON --option
73```
74
75## `stylelint.formatters`
76
77Stylelint's internal formatters are exposed publicly in `stylelint.formatters`.