Skip to content

Commit

Permalink
Add support for descriptions in stylelint command comments (#4848)
Browse files Browse the repository at this point in the history
  • Loading branch information
glen-84 committed Jul 6, 2020
1 parent 968248d commit 055ebea
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/user-guide/ignore-code.md
Expand Up @@ -62,6 +62,16 @@ stylelint supports complex, overlapping disabling & enabling patterns:

**Caveat:** Comments within _selector and value lists_ are currently ignored.

You may also include a description at the end of the comment, after two hyphens:

```css
/* stylelint-disable -- Reason for disabling stylelint. */
/* stylelint-disable foo -- Reason for disabling the foo rule. */
/* stylelint-disable foo, bar -- Reason for disabling the foo and bar rules. */
```

**Important:** There must be a space on both sides of the hyphens.

## Files entirely

You can use a `.stylelintignore` file to ignore specific files. For example:
Expand Down
33 changes: 33 additions & 0 deletions lib/__tests__/disableRanges-integration.test.js
Expand Up @@ -27,6 +27,22 @@ testRule({
{
code: 'b { color: pink;}\n/* stylelint-disable */\na {}',
},
// Command comment descriptions.
{
code: '/* stylelint-disable -- Description */\na {}',
},
{
code: '/* stylelint-disable-line -- Description */ a {}',
},
{
code: 'a {} /* stylelint-disable-line -- Description */ ',
},
{
code: '/* stylelint-disable-next-line -- Description */\na {}',
},
{
code: 'b { color: pink;}\n/* stylelint-disable -- Description */\na {}',
},
],

reject: [
Expand Down Expand Up @@ -58,6 +74,23 @@ testRule({
code: 'a {}\n/* stylelint-disable-next-line */',
message: blockNoEmpty.messages.rejected,
},
// Command comment descriptions.
{
code: '/* stylelint-disable--Description */\na {}',
message: blockNoEmpty.messages.rejected,
},
{
code: '/* stylelint-disable-- Description */\na {}',
message: blockNoEmpty.messages.rejected,
},
{
code: '/* stylelint-disable --Description */\na {}',
message: blockNoEmpty.messages.rejected,
},
{
code: '/* stylelint-disable - - Description */\na {}',
message: blockNoEmpty.messages.rejected,
},
],
});

Expand Down
126 changes: 126 additions & 0 deletions lib/__tests__/disableRanges.test.js
Expand Up @@ -644,6 +644,132 @@ it('enable rule without disabling rule', () => {
});
});

// Command comment descriptions.

it('disable (with description) without re-enabling', () => {
return testDisableRanges('/* stylelint-disable -- Description */\na {}').then((result) => {
expect(result.stylelint.disabledRanges).toEqual({
all: [
{
start: 1,
strictStart: true,
},
],
});
});
});

it('disable and re-enable (with descriptions)', () => {
return testDisableRanges(`a {}
/* stylelint-disable -- Description */
b {}
/* stylelint-enable -- Description */
.foo {}`).then((result) => {
expect(result.stylelint.disabledRanges).toEqual({
all: [
{
start: 2,
end: 4,
strictStart: true,
strictEnd: true,
},
],
});
});
});

it('disable rule (with description) without re-enabling', () => {
return testDisableRanges('/* stylelint-disable foo-bar -- Description */\na {}').then(
(result) => {
expect(result.stylelint.disabledRanges).toEqual({
all: [],
'foo-bar': [
{
start: 1,
strictStart: true,
end: undefined,
strictEnd: undefined,
},
],
});
},
);
});

it('disable rules (with description) with newline in rule list', () => {
return testDisableRanges(
'/* stylelint-disable foo-bar, hoo-hah,\n\tslime -- Description */\n' + 'b {}\n',
).then((result) => {
expect(result.stylelint.disabledRanges).toEqual({
all: [],
'foo-bar': [
{
start: 1,
strictStart: true,
},
],
'hoo-hah': [
{
start: 1,
strictStart: true,
},
],
slime: [
{
start: 1,
strictStart: true,
},
],
});
});
});

it('SCSS // line-disabling comment (with description)', () => {
const scssSource = `a {
color: pink !important; // stylelint-disable-line declaration-no-important -- Description
}`;

return postcss()
.use(assignDisabledRanges)
.process(scssSource, { syntax: scss, from: undefined })
.then((result) => {
expect(result.stylelint.disabledRanges).toEqual({
all: [],
'declaration-no-important': [
{
start: 2,
end: 2,
strictStart: true,
strictEnd: true,
},
],
});
});
});

it('Less // line-disabling comment (with description)', () => {
const lessSource = `a {
color: pink !important; // stylelint-disable-line declaration-no-important -- Description
}`;

return postcss()
.use(assignDisabledRanges)
.process(lessSource, { syntax: less, from: undefined })
.then((result) => {
expect(result.stylelint.disabledRanges).toEqual({
all: [],
'declaration-no-important': [
{
start: 2,
end: 2,
strictStart: true,
strictEnd: true,
},
],
});
});
});

function testDisableRanges(source, cb) {
return postcss().use(assignDisabledRanges).process(source, { from: undefined }).then(cb);
}
2 changes: 2 additions & 0 deletions lib/assignDisabledRanges.js
Expand Up @@ -241,6 +241,8 @@ module.exports = function (root, result) {
function getCommandRules(command, fullText) {
const rules = fullText
.slice(command.length)
.split(/\s-{2,}\s/u)[0] // Allow for description (f.e. /* stylelint-disable a, b -- Description */).
.trim()
.split(',')
.filter(Boolean)
.map((r) => r.trim());
Expand Down

0 comments on commit 055ebea

Please sign in to comment.