Skip to content

Commit

Permalink
Add autofix to declaration-block-no-duplicate-properties (#6296)
Browse files Browse the repository at this point in the history
Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
  • Loading branch information
fpetrakov and ybiquitous committed Aug 24, 2022
1 parent b20e672 commit 8e6a576
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-lemons-reflect.md
@@ -0,0 +1,5 @@
---
"stylelint": minor
---

Added: `declaration-block-no-duplicate-properties` autofix
2 changes: 1 addition & 1 deletion docs/user-guide/rules/list.md
Expand Up @@ -61,7 +61,7 @@ Within each cateogory, the rules are grouped by the [_thing_](http://apps.workfl
### Declaration block

- [`declaration-block-no-duplicate-custom-properties`](../../../lib/rules/declaration-block-no-duplicate-custom-properties/README.md): Disallow duplicate custom properties within declaration blocks.
- [`declaration-block-no-duplicate-properties`](../../../lib/rules/declaration-block-no-duplicate-properties/README.md): Disallow duplicate properties within declaration blocks.
- [`declaration-block-no-duplicate-properties`](../../../lib/rules/declaration-block-no-duplicate-properties/README.md): Disallow duplicate properties within declaration blocks (Autofixable).
- [`declaration-block-no-shorthand-property-overrides`](../../../lib/rules/declaration-block-no-shorthand-property-overrides/README.md): Disallow shorthand properties that override related longhand properties.

### Block
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/declaration-block-no-duplicate-properties/README.md
Expand Up @@ -11,6 +11,8 @@ a { color: pink; color: orange; }

This rule ignores variables (`$sass`, `@less`, `--custom-property`).

The [`fix` option](../../../docs/user-guide/usage/options.md#fix) can automatically fix all of the problems reported by this rule.

## Options

### `true`
Expand Down
Expand Up @@ -5,6 +5,7 @@ const { messages, ruleName } = require('..');
testRule({
ruleName,
config: [true],
fix: true,

accept: [
{
Expand Down Expand Up @@ -48,6 +49,7 @@ testRule({
reject: [
{
code: 'a { color: pink; color: orange }',
fixed: 'a { color: orange }',
message: messages.rejected('color'),
line: 1,
column: 18,
Expand All @@ -56,6 +58,7 @@ testRule({
},
{
code: 'a { cOlOr: pink; color: orange }',
fixed: 'a { color: orange }',
message: messages.rejected('color'),
line: 1,
column: 18,
Expand All @@ -64,6 +67,7 @@ testRule({
},
{
code: 'a { color: pink; cOlOr: orange }',
fixed: 'a { cOlOr: orange }',
message: messages.rejected('cOlOr'),
line: 1,
column: 18,
Expand All @@ -72,6 +76,7 @@ testRule({
},
{
code: 'a { cOlOr: pink; cOlOr: orange }',
fixed: 'a { cOlOr: orange }',
message: messages.rejected('cOlOr'),
line: 1,
column: 18,
Expand All @@ -80,6 +85,7 @@ testRule({
},
{
code: 'a { COLOR: pink; color: orange }',
fixed: 'a { color: orange }',
message: messages.rejected('color'),
line: 1,
column: 18,
Expand All @@ -88,6 +94,7 @@ testRule({
},
{
code: 'a { color: pink; COLOR: orange }',
fixed: 'a { COLOR: orange }',
message: messages.rejected('COLOR'),
line: 1,
column: 18,
Expand All @@ -96,6 +103,7 @@ testRule({
},
{
code: 'a { color: pink; background: orange; color: orange }',
fixed: 'a { background: orange; color: orange }',
message: messages.rejected('color'),
line: 1,
column: 38,
Expand All @@ -104,6 +112,7 @@ testRule({
},
{
code: 'a { color: pink; background: orange; background: pink; }',
fixed: 'a { color: pink; background: pink; }',
message: messages.rejected('background'),
line: 1,
column: 38,
Expand All @@ -112,6 +121,7 @@ testRule({
},
{
code: 'a { color: pink; { &:hover { color: orange; color: black; } } }',
fixed: 'a { color: pink; { &:hover { color: black; } } }',
description: 'spec nested',
message: messages.rejected('color'),
line: 1,
Expand All @@ -121,6 +131,7 @@ testRule({
},
{
code: 'a { color: pink; @media { color: orange; color: black; } }',
fixed: 'a { color: pink; @media { color: black; } }',
description: 'nested',
message: messages.rejected('color'),
line: 1,
Expand All @@ -130,6 +141,7 @@ testRule({
},
{
code: '@media { color: orange; .foo { color: black; color: white; } }',
fixed: '@media { color: orange; .foo { color: white; } }',
description: 'nested',
message: messages.rejected('color'),
line: 1,
Expand All @@ -139,6 +151,7 @@ testRule({
},
{
code: 'a { color: pink; @media { color: orange; &::before { color: black; color: white; } } }',
fixed: 'a { color: pink; @media { color: orange; &::before { color: white; } } }',
description: 'double nested',
message: messages.rejected('color'),
line: 1,
Expand All @@ -148,6 +161,7 @@ testRule({
},
{
code: 'a { color: pink; @media { color: orange; .foo { color: black; color: white; } } }',
fixed: 'a { color: pink; @media { color: orange; .foo { color: white; } } }',
description: 'double nested again',
message: messages.rejected('color'),
line: 1,
Expand All @@ -157,6 +171,7 @@ testRule({
},
{
code: 'a { -webkit-border-radius: 12px; -webkit-border-radius: 10px; }',
fixed: 'a { -webkit-border-radius: 10px; }',
message: messages.rejected('-webkit-border-radius'),
line: 1,
column: 34,
Expand All @@ -165,6 +180,7 @@ testRule({
},
{
code: 'a { -WEBKIT-border-radius: 12px; -webkit-BORDER-radius: 10px; }',
fixed: 'a { -webkit-BORDER-radius: 10px; }',
message: messages.rejected('-webkit-BORDER-radius'),
line: 1,
column: 34,
Expand All @@ -177,6 +193,7 @@ testRule({
testRule({
ruleName,
config: [true, { ignore: ['consecutive-duplicates'] }],
fix: true,

accept: [
{
Expand All @@ -196,18 +213,22 @@ testRule({
reject: [
{
code: 'p { font-size: 16px; font-weight: 400; font-size: 1rem; }',
fixed: 'p { font-weight: 400; font-size: 1rem; }',
message: messages.rejected('font-size'),
},
{
code: 'p { display: inline-block; font-size: 16px; font-weight: 400; font-size: 1rem; }',
fixed: 'p { display: inline-block; font-weight: 400; font-size: 1rem; }',
message: messages.rejected('font-size'),
},
{
code: 'p { font-size: 16px; font-weight: 400; font-size: 1rem; color: red; }',
fixed: 'p { font-weight: 400; font-size: 1rem; color: red; }',
message: messages.rejected('font-size'),
},
{
code: 'p { display: inline-block; font-size: 16px; font-weight: 400; font-size: 1rem; color: red; }',
fixed: 'p { display: inline-block; font-weight: 400; font-size: 1rem; color: red; }',
message: messages.rejected('font-size'),
},
],
Expand All @@ -216,16 +237,19 @@ testRule({
testRule({
ruleName,
config: [true, { ignore: ['consecutive-duplicates-with-different-values'] }],
fix: true,

accept: [{ code: 'p { font-size: 16px; font-size: 1rem; font-weight: 400; }' }],

reject: [
{
code: 'p { font-size: 16px; font-weight: 400; font-size: 1rem; }',
fixed: 'p { font-weight: 400; font-size: 1rem; }',
message: messages.rejected('font-size'),
},
{
code: 'p { font-size: 16px; font-size: 16px; font-weight: 400; }',
fixed: 'p { font-size: 16px; font-weight: 400; }',
message: messages.rejected('font-size'),
},
],
Expand All @@ -234,6 +258,7 @@ testRule({
testRule({
ruleName,
config: [true, { ignore: ['consecutive-duplicates-with-same-prefixless-values'] }],
fix: true,

accept: [
{
Expand All @@ -253,14 +278,17 @@ testRule({
reject: [
{
code: 'p { width: fit-content; height: 32px; width: -moz-fit-content; }',
fixed: 'p { height: 32px; width: -moz-fit-content; }',
message: messages.rejected('width'),
},
{
code: 'p { width: 100%; width: -moz-fit-content; height: 32px; }',
fixed: 'p { width: -moz-fit-content; height: 32px; }',
message: messages.rejected('width'),
},
{
code: 'p { width: -moz-fit-content; width: -moz-fit-content; }',
fixed: 'p { width: -moz-fit-content; }',
message: messages.rejected('width'),
},
],
Expand All @@ -269,6 +297,7 @@ testRule({
testRule({
ruleName,
config: [true, { ignoreProperties: ['color'] }],
fix: true,

accept: [
{
Expand All @@ -285,10 +314,12 @@ testRule({
reject: [
{
code: 'p { color: pink; background: orange; background: white; }',
fixed: 'p { color: pink; background: white; }',
message: messages.rejected('background'),
},
{
code: 'p { background: orange; color: pink; background: white; }',
fixed: 'p { color: pink; background: white; }',
message: messages.rejected('background'),
},
],
Expand All @@ -297,6 +328,7 @@ testRule({
testRule({
ruleName,
config: [true, { ignoreProperties: ['/background-/'] }],
fix: true,

accept: [
{
Expand All @@ -310,10 +342,12 @@ testRule({
reject: [
{
code: 'p { color: pink; background: orange; background: white; }',
fixed: 'p { color: pink; background: white; }',
message: messages.rejected('background'),
},
{
code: 'p { background: orange; color: pink; background: white; }',
fixed: 'p { color: pink; background: white; }',
message: messages.rejected('background'),
},
],
Expand All @@ -323,6 +357,7 @@ testRule({
ruleName,
config: [true],
customSyntax: 'postcss-html',
fix: true,

accept: [
{
Expand All @@ -342,14 +377,17 @@ testRule({
reject: [
{
code: '<a style="color: pink; color: orange"></a>',
fixed: '<a style="color: orange"></a>',
message: messages.rejected('color'),
},
{
code: '<style>p { color: pink; background: orange; background: white; }</style>',
fixed: '<style>p { color: pink; background: white; }</style>',
message: messages.rejected('background'),
},
{
code: '<a style="background: orange; color: pink; background: white;"></a>',
fixed: '<a style="color: pink; background: white;"></a>',
message: messages.rejected('background'),
},
],
Expand Down

0 comments on commit 8e6a576

Please sign in to comment.