Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore: [single-declaration] to declaration-block-trailing-semicolon #5165

Merged
merged 2 commits into from Mar 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"]`

Mouvedia marked this conversation as resolved.
Show resolved Hide resolved
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