hostnet/css-sniffer

此包已被废弃,不再维护。未建议替代包。

CSS嗅探器

2.4.0 2018-06-28 09:05 UTC

README

重要:由于内部变更,此存储库将不再维护。如果您正在使用此存储库或计划使用,请考虑替代方案,如https://stylelint.node.org.cn/

68747470733a2f2f7777772e686f73746e65742e6e6c2f696d616765732f686f73746e65742e737667

CSS嗅探器旨在帮助改进(并维护)CSS和LESS文件的代码风格一致性。它将通过强制执行规则来减少找出另一种风格所需的时间。因此,不再需要担心制表符与空格、单引号与双引号或颜色值的3位与6位。

此工具非常直接,只需运行它并指定要检查的文件即可。例如,如果您有一个如下所示的css文件

.good { background-image: url("/foo.com"); }
.bad { background-image: url('/foo.com'); }

只需运行该工具,您将得到以下输出。

$ vendor/bin/css-sniff sniff quotes.less

FILE: test/Sniff/fixtures/quotes.less
--------------------------------------------------------------------------------
FOUND 1 ERROR(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 2 | Text should use " as quotes.
--------------------------------------------------------------------------------

一些内置的嗅探器可能相当主观。如果您不同意,您有两个选择:打开一个问题来更改当前的行为或编写您自己的嗅探器。

安装

使用phar(推荐)

下载嗅探器最新版本附带的phar。

使用composer

  • $ composer require --dev hostnet/css-sniffer
  • 此库严格遵守语义版本

文档

基本用法如下

$ vendor/bin/css-sniff --help
Usage:
  sniff [options] [--] [<files>]...

Arguments:
  files                      Input file

Options:
      --format[=FORMAT]      Type of output format, default: console [default: "console"]
  -s, --standard[=STANDARD]  Code Standard to use, by default the Hostnet standard is used. This is the path to the xml file.
      --stdin                If given, this option will tell the sniffer to check the STDIN for input.
  -p, --pretty               Pretty format output
      --no-exit-code         Always return 0 as exit code, regardless of the result
  -h, --help                 Display this help message
  -q, --quiet                Do not output any message
  -V, --version              Display this application version
      --ansi                 Force ANSI output
      --no-ansi              Disable ANSI output
  -n, --no-interaction       Do not ask any interactive question
  -v|vv|vvv, --verbose       Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
  Sniffs the given input file and returns the result.

示例:

  • vendor/bin/css-sniff
  • vendor/bin/css-sniff sniff some/file.css
  • vendor/bin/css-sniff sniff --format=json -p some/file.css
  • cat some/file.css | vendor/bin/css-sniff sniff --stdin

CI嗅探

嗅探器的主要重点是集成到任何CI工具中。为此,建议将csssniff.xml.dist添加到项目根目录。这将允许您在运行嗅探器时配置要处理的文件和目录。一个常见的例子是

<?xml version="1.0"?>
<csssniffer>
    <directory>./app/styles</directory>

    <sniff rel="Hostnet" />
</csssniffer>

这将使用Hostnet标准处理从项目根目录相对于app/styles文件夹。有关xml结构的更多信息,请参阅规则集配置文档

输出格式化

支持多种输出格式。目前有console(默认)、checkstylejson。json输出的外观如下

$ vendor/bin/css-sniff sniff --format=json -p test/Sniff/fixtures/quotes.less
{
    "totals": {
        "errors": 0
    },
    "files": {
        "test\/Sniff\/fixtures\/quotes.less": {
            "errors": 3,
            "messages": [
                {
                    "message": "One statements found and should be on one line.",
                    "source": "Hostnet\\Component\\CssSniff\\Sniff\\CurlySniff",
                    "line": 1,
                    "column": 7
                },
                {
                    "message": "One statements found and should be on one line.",
                    "source": "Hostnet\\Component\\CssSniff\\Sniff\\CurlySniff",
                    "line": 4,
                    "column": 6
                },
                {
                    "message": "Text should use \" as quotes.",
                    "source": "Hostnet\\Component\\CssSniff\\Sniff\\QuoteTypeSniff",
                    "line": 5,
                    "column": 27
                }
            ]
        }
    }
}

-p仅用于美化格式,这是可选的但更易读。

STDIN输入

嗅探器还可以从STDIN读取。当将工具集成到IDE中,您可能没有文件但想要传递编辑器的内容时,这可能很有用。请确保添加--stdin以告诉嗅探器从STDIN读取。您还可以传递一个文件,以便您的匹配规则可以正常工作。

$ cat test/Sniff/fixtures/quotes.less | vendor/bin/css-sniff sniff --format=json -p --stdin test/Sniff/fixtures/quotes.less
{
    "totals": {
        "errors": 0
    },
    "files": {
        "test\/Sniff\/fixtures\/quotes.less": {
            "errors": 3,
            "messages": [
                {
                    "message": "One statements found and should be on one line.",
                    "source": "Hostnet\\Component\\CssSniff\\Sniff\\CurlySniff",
                    "line": 1,
                    "column": 7
                },
                {
                    "message": "One statements found and should be on one line.",
                    "source": "Hostnet\\Component\\CssSniff\\Sniff\\CurlySniff",
                    "line": 4,
                    "column": 6
                },
                {
                    "message": "Text should use \" as quotes.",
                    "source": "Hostnet\\Component\\CssSniff\\Sniff\\QuoteTypeSniff",
                    "line": 5,
                    "column": 27
                }
            ]
        }
    }
}

许可证

hostnet/css-sniffer采用MIT许可证,这意味着您可以在专有软件中重用代码,前提是所有许可软件的副本都包含MIT许可证条款和版权声明。

联系方式