sass-lint 是否忽略了某一行?

Have sass-lint ignore a certain line?

我正在使用 sass-lint 和 Gulp。如何从 lint 控制台输出中禁用 sass 中特定样式的警告?

我发现了一个类似的问题,但我使用的是 sass-lint,而不是 scss-lint:

这是我正在使用的:https://www.npmjs.com/package/gulp-sass-lint

我尝试了一些基于 scss-lint 项目的变体:

// scss-lint:disable ImportantRule
// sass-lint:disable ImportantRule
// sass-lint:disable no-important

明确一点,我想在 SASS 中禁用针对特定样式的警告,而不是全局。当触发警告的事情是故意的时,我会使用它。例如,我可能会设置多种背景样式,以便其中一种可以作为旧浏览器的后备。但目前这会触发 no-duplicate-properties 警告。

通过评论禁用

2016 年 12 月更新 根据 the docs 现在可以使用以下语法:

Disable more than 1 rule for entire file

// sass-lint:disable border-zero, quotes

p {
  border: none; // No lint reported
  content: "hello"; // No lint reported
}

Disable a rule for a single line

p {
  border: none; // sass-lint:disable-line border-zero
}

Disable all lints within a block (and all contained blocks)

p {
  // sass-lint:disable-block border-zero
  border: none; // No result reported
}

新信息由评论者@IanRoutledge 提供。

但是,之前,如果要禁用某些规则,但只针对特定的代码块and/or块代码。 据我所知 tell from the underlying source code sass-lint 不可能。我也尝试了一些其他搜索,并大致浏览了代码库,但没有发现您正在寻找的功能存在的迹象。

为了比较,this query for the scss-lint source code 清楚地表明它 在那里实现的,在您使用的库中似乎没有类似解决方案的方式。

通过 yml 配置禁用

可以 禁用一般规则。您需要有一个 .sass-lint.yml 文件来禁用警告。

假设你有这个 gulpfile.js:

'use strict';

var gulp = require('gulp'),
    sassLint = require('gulp-sass-lint');

gulp.task('default', [], function() {
  gulp.src('sass/*.scss')
    .pipe(sassLint())
    .pipe(sassLint.format())
    .pipe(sassLint.failOnError());
});

还有这个package.json

{
  "devDependencies": {
    "gulp": "^3.9.0",
    "gulp-sass": "^2.1.1",
    "gulp-sass-lint": "^1.1.1"
  }
}

运行 在这个 styles.scss 文件上:

div { dsply: block; }

你得到这个输出:

[23:53:33] Using gulpfile D:\experiments\myfolder\gulpfile.js
[23:53:33] Starting 'default'...
[23:53:33] Finished 'default' after 8.84 ms

sass\styles.scss
  1:7   warning  Property `dsply` appears to be spelled incorrectly  no-misspelled-properties
  1:21  warning  Files must end with a new line                      final-newline

??? 2 problems (0 errors, 2 warnings)

现在,如果您在 gulpfile 旁边添加一个 .sass-lint.yml 文件,内容如下:

rules:
  no-misspelled-properties: 0

您会看到:

[23:54:56] Using gulpfile D:\experiments\myfolder\gulpfile.js
[23:54:56] Starting 'default'...
[23:54:56] Finished 'default' after 9.32 ms

sass\styles.scss
  1:21  warning  Files must end with a new line  final-newline

??? 1 problem (0 errors, 1 warning)

现在忽略其中一个警告。

The sass-lint readme.md links to the apparent default config 其中有更多示例。

明白了。

首先,这里是 sass-lint 的规则列表:

https://github.com/sasstools/sass-lint/tree/develop/lib/rules

接下来,您可以为您想要的规则设置一个选项:

rule:
    severity: 0

0 = 什么都没写

1 = 警告

2 = 错误

这是您需要的吗?

从 sass-lint 1.10 开始支持禁用特定行的 linting 规则。

https://github.com/sasstools/sass-lint#disabling-linters-via-source

到目前为止 sass-lint 不支持基于每个文件或每行的禁用规则。他们有一个 long-standing GitHub issue 跟踪功能。

从 sass-lint 1.10.0 开始,可以通过源注释

禁用 sass-lint 规则

下面是这个的不同排列,故意与之前的 scss-lint 方式几乎相同。

对整个文件禁用规则

// sass-lint:disable border-zero
p {
  border: none; // No lint reported
}

禁用超过 1 个规则

// sass-lint:disable border-zero, quotes
p {
  border: none; // No lint reported
  content: "hello"; // No lint reported
}

禁用单行规则

p {
  border: none; // sass-lint:disable-line border-zero
}

禁用块内的所有 lints(以及所有包含的块)

p {
  // sass-lint:disable-block border-zero
  border: none; // No result reported
}

a {
  border: none; // Failing result reported
}

禁用并再次启用

// sass-lint:disable border-zero
p {
  border: none; // No result reported
}
// sass-lint:enable border-zero

a {
  border: none; // Failing result reported
}

Disable/enable 所有 linters

// sass-lint:disable-all
p {
  border: none; // No result reported
}
// sass-lint:enable-all

a {
  border: none; // Failing result reported
}

sass-lint readme includes all of the details and the docs 也已更新。

grunt、gulp 和 atom 插件也都已更新,需要 v1.10 及更高版本,因此快速重新安装这些插件或更新,您就可以开始了。

我们很抱歉花了这么长时间才出来,但在我们确信它们有用之前,我们必须修复 AST 中的一些问题。