Skip to content

Commit

Permalink
Add ignore: ["single-declaration"] to declaration-block-trailing-semi…
Browse files Browse the repository at this point in the history
…colon
  • Loading branch information
Mouvedia committed Feb 22, 2021
1 parent ae67767 commit b286c0e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
29 changes: 29 additions & 0 deletions lib/rules/declaration-block-trailing-semicolon/README.md
Expand Up @@ -88,3 +88,32 @@ a { color: pink }
```css
a { background: orange; color: pink }
```

## Optional secondary options

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

#### `"single-declaration"`

The following patterns are _not_ considered violations:

<!-- prettier-ignore -->
```css
foo {
property: value
}
```

<!-- prettier-ignore -->
```css
foo { property: value }
```

With `"always"`:

The following patterns are _still_ considered violations:

<!-- prettier-ignore -->
```css
foo { property: value; bar: qux }
```
32 changes: 32 additions & 0 deletions lib/rules/declaration-block-trailing-semicolon/__tests__/index.js
Expand Up @@ -62,6 +62,38 @@ testRule({
],
});

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

accept: [
{
code: 'foo { property: value }',
description: 'single declaration without trailing semicolon (single-line)',
},
{
code: `foo {
property: value
}`,
description: 'single declaration without trailing semicolon (multi-line)',
},
{
code: 'foo { property: value; }',
description: 'single declaration with trailing semicolon',
},
],

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

testRule({
ruleName,
config: ['never'],
Expand Down
32 changes: 27 additions & 5 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 @@ -57,6 +69,16 @@ function rule(expectation, _, context) {
let message;

if (expectation === 'always') {
const singleDeclarationOptionEnabled = optionsMatches(
options,
'ignore',
'single-declaration',
);

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

if (node.parent.raws.semicolon) {
return;
}
Expand Down

0 comments on commit b286c0e

Please sign in to comment.