Skip to content

Commit

Permalink
Add ignore: [single-declaration] to declaration-block-trailing-semico…
Browse files Browse the repository at this point in the history
…lon (#5165)
  • Loading branch information
Mouvedia committed Mar 6, 2021
1 parent 7b894ba commit 29bcec9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 7 deletions.
18 changes: 18 additions & 0 deletions lib/rules/declaration-block-trailing-semicolon/README.md
Expand Up @@ -88,3 +88,21 @@ a { color: pink }
```css
a { background: orange; color: pink }
```

## Optional secondary options

### `ignore: ["single-declaration"]`

Ignore declaration blocks that contain a single declaration.

The following patterns are _not_ considered violations:

<!-- prettier-ignore -->
```css
a { color: pink }
```

<!-- prettier-ignore -->
```css
a { color: pink; }
```
52 changes: 52 additions & 0 deletions lib/rules/declaration-block-trailing-semicolon/__tests__/index.js
Expand Up @@ -62,6 +62,58 @@ testRule({
],
});

testRule({
ruleName,
config: ['always', { ignore: 'single-declaration' }],
fix: true,

accept: [
{
code: 'a { color: pink }',
description: 'single declaration without trailing semicolon',
},
{
code: 'a { color: pink; }',
description: 'single declaration with trailing semicolon',
},
{
code: '@keyframes foo { from { top: 0px } to { top: 1px; } }',
description: 'inconsistent case (with and without)',
},
],

reject: [
{
code: 'a { background: orange; color: pink }',
fixed: 'a { background: orange; color: pink; }',
description: 'multi declaration block without trailing semicolon',
message: messages.expected,
line: 1,
column: 35,
},
],
});

testRule({
ruleName,
config: ['never', { ignore: ['single-declaration'] }],

accept: [
{
code: 'a { color: pink }',
description: 'single declaration without trailing semicolon',
},
{
code: 'a { color: pink; }',
description: 'single declaration with trailing semicolon',
},
{
code: '@keyframes foo { from { top: 0px } to { top: 1px; } }',
description: 'inconsistent case (with and without)',
},
],
});

testRule({
ruleName,
config: ['never'],
Expand Down
32 changes: 25 additions & 7 deletions lib/rules/declaration-block-trailing-semicolon/index.js
Expand Up @@ -3,6 +3,7 @@
'use strict';

const hasBlock = require('../../utils/hasBlock');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
Expand All @@ -14,12 +15,23 @@ const messages = ruleMessages(ruleName, {
rejected: 'Unexpected trailing semicolon',
});

function rule(expectation, _, context) {
function rule(expectation, options, context) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
possible: ['always', 'never'],
});
const validOptions = validateOptions(
result,
ruleName,
{
actual: expectation,
possible: ['always', 'never'],
},
{
actual: options,
possible: {
ignore: ['single-declaration'],
},
optional: true,
},
);

if (!validOptions) {
return;
Expand Down Expand Up @@ -54,10 +66,16 @@ function rule(expectation, _, context) {
});

function checkLastNode(node) {
const hasSemicolon = node.parent.raws.semicolon;
const ignoreSingleDeclaration = optionsMatches(options, 'ignore', 'single-declaration');
let message;

if (ignoreSingleDeclaration && node.parent.first === node) {
return;
}

if (expectation === 'always') {
if (node.parent.raws.semicolon) {
if (hasSemicolon) {
return;
}

Expand All @@ -75,7 +93,7 @@ function rule(expectation, _, context) {

message = messages.expected;
} else if (expectation === 'never') {
if (!node.parent.raws.semicolon) {
if (!hasSemicolon) {
return;
}

Expand Down

0 comments on commit 29bcec9

Please sign in to comment.