Skip to content

Commit

Permalink
Add singleDeclaration: "ignore-always" to declaration-block-trailing-…
Browse files Browse the repository at this point in the history
…semicolon
  • Loading branch information
Mouvedia committed Feb 23, 2021
1 parent 06f3694 commit 3ff9292
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
34 changes: 33 additions & 1 deletion lib/rules/declaration-block-trailing-semicolon/README.md
Expand Up @@ -91,7 +91,7 @@ a { background: orange; color: pink }

## Optional secondary options

### `singleDeclaration: "never"`
### `singleDeclaration: "never"|"ignore-always"`

#### `"never"`

Expand Down Expand Up @@ -129,3 +129,35 @@ foo { property: value; bar: qux }
```css
@keyframes name { from { property: 0 } to { property: 1; } }
```

#### `"ignore-always"`

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 _not_ considered violations:

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

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

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

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

testRule({
ruleName,
config: ['never'],
Expand Down
15 changes: 11 additions & 4 deletions lib/rules/declaration-block-trailing-semicolon/index.js
Expand Up @@ -27,7 +27,7 @@ function rule(expectation, options, context) {
{
actual: options,
possible: {
singleDeclaration: 'never',
singleDeclaration: ['never', 'ignore-always'],
},
optional: true,
},
Expand Down Expand Up @@ -70,12 +70,19 @@ function rule(expectation, options, context) {
let message;

if (expectation === 'always') {
const singleDeclarationNeverEnabled = optionsMatches(options, 'singleDeclaration', 'never');
const isNeverException = singleDeclarationNeverEnabled && node.parent.first === node;
const singleDeclarationNever = optionsMatches(options, 'singleDeclaration', 'never');
const singleDeclarationIgnoreAlways = optionsMatches(
options,
'singleDeclaration',
'ignore-always',
);
const hasOneDeclaration = node.parent.first === node;
const isNeverException = singleDeclarationNever && hasOneDeclaration;
const isIgnoreAlwaysException = singleDeclarationIgnoreAlways && hasOneDeclaration;

if (isNeverException) {
if (!hasSemicolon) return;
} else if (hasSemicolon) {
} else if (hasSemicolon || isIgnoreAlwaysException) {
return;
}

Expand Down

0 comments on commit 3ff9292

Please sign in to comment.