编写自定义格式化程序
格式化程序是一个具有以下签名的函数
/**
* @type {import('stylelint').Formatter}
*/
export default function formatter(results, returnValue) {
return "a string of formatted results";
}
其中第一个参数 (results
) 是一个 Stylelint 结果对象的数组 (类型 Array<StylelintResult>
),形式如下
// A Stylelint result object
{
"source": "path/to/file.css", // The filepath or PostCSS identifier like <input css 1>
"errored": true, // This is `true` if at least one rule with an "error"-level severity triggered a warning
"warnings": [
// Array of rule problem warning objects, each like the following ...
{
"line": 3,
"column": 12,
"endLine": 4,
"endColumn": 15,
"rule": "block-no-empty",
"severity": "error",
"text": "You should not have an empty block (block-no-empty)"
}
],
"deprecations": [
// Array of deprecation warning objects, each like the following ...
{
"text": "Feature X has been deprecated and will be removed in the next major version.",
"reference": "https://stylelint.node.org.cn/docs/feature-x.md"
}
],
"invalidOptionWarnings": [
// Array of invalid option warning objects, each like the following ...
{
"text": "Invalid option X for rule Y"
}
],
"ignored": false // This is `true` if the file's path matches a provided ignore pattern
}
第二个参数 (returnValue
) 是一个对象 (类型 LinterResult
),包含以下一个或多个键
{
"errored": false, // `true` if there were any warnings with "error" severity
"maxWarningsExceeded": {
// Present if Stylelint was configured with a `maxWarnings` count
"maxWarnings": 10,
"foundWarnings": 15
},
"ruleMetadata": {
"block-no-empty": {
"url": "https://stylelint.node.org.cn/user-guide/rules/block-no-empty"
},
// other rules...
}
}
传递参数
您可以在格式化程序中使用环境变量。例如,传递 SKIP_WARNINGS
SKIP_WARNINGS=true stylelint "*.css" --custom-formatter ./my-formatter.js
或者,您可以创建一个单独的格式化程序,并将内置 JSON 格式化程序的输出管道到该程序中
stylelint -f json "*.css" 2>&1 | my-program-that-reads-JSON --option
stylelint.formatters
Stylelint 的内部格式化程序在 stylelint.formatters
中公开。