Skip to content

Commit

Permalink
add autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
fpetrakov committed Aug 23, 2022
1 parent d66207b commit 725fce2
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 30 deletions.
226 changes: 204 additions & 22 deletions lib/rules/declaration-block-no-duplicate-properties/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,95 @@ testRule({
],
});

testRule({
ruleName,
config: [true],
fix: true,

reject: [
{
code: 'a { color: pink; color: orange }',
fixed: 'a { color: orange }',
message: messages.rejected('color'),
},
{
code: 'a { cOlOr: pink; color: orange }',
fixed: 'a { color: orange }',
message: messages.rejected('color'),
},
{
code: 'a { color: pink; cOlOr: orange }',
fixed: 'a { cOlOr: orange }',
message: messages.rejected('cOlOr'),
},
{
code: 'a { cOlOr: pink; cOlOr: orange }',
fixed: 'a { cOlOr: orange }',
message: messages.rejected('cOlOr'),
},
{
code: 'a { COLOR: pink; color: orange }',
fixed: 'a { color: orange }',
message: messages.rejected('color'),
},
{
code: 'a { color: pink; COLOR: orange }',
fixed: 'a { COLOR: orange }',
message: messages.rejected('COLOR'),
},
{
code: 'a { color: pink; background: orange; color: orange }',
fixed: 'a { background: orange; color: orange }',
message: messages.rejected('color'),
},
{
code: 'a { color: pink; background: orange; background: pink; }',
fixed: 'a { color: pink; background: pink; }',
message: messages.rejected('background'),
},
{
code: 'a { color: pink; { &:hover { color: orange; color: black; } } }',
fixed: 'a { color: pink; { &:hover { color: black; } } }',
description: 'spec nested',
message: messages.rejected('color'),
},
{
code: 'a { color: pink; @media { color: orange; color: black; } }',
fixed: 'a { color: pink; @media { color: black; } }',
description: 'nested',
message: messages.rejected('color'),
},
{
code: '@media { color: orange; .foo { color: black; color: white; } }',
fixed: '@media { color: orange; .foo { color: white; } }',
description: 'nested',
message: messages.rejected('color'),
},
{
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'),
},
{
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'),
},
{
code: 'a { -webkit-border-radius: 12px; -webkit-border-radius: 10px; }',
fixed: 'a { -webkit-border-radius: 10px; }',
message: messages.rejected('-webkit-border-radius'),
},
{
code: 'a { -WEBKIT-border-radius: 12px; -webkit-BORDER-radius: 10px; }',
fixed: 'a { -webkit-BORDER-radius: 10px; }',
message: messages.rejected('-webkit-BORDER-radius'),
},
],
});

testRule({
ruleName,
config: [true, { ignore: ['consecutive-duplicates'] }],
Expand Down Expand Up @@ -218,40 +307,25 @@ testRule({
config: [true, { ignore: ['consecutive-duplicates'] }],
fix: true,

accept: [
{
code: 'p { font-size: 16px; font-size: 1rem; }',
},
{
code: 'p { display: inline-block; font-size: 16px; font-size: 1rem; }',
},
{
code: 'p { font-size: 16px; font-size: 1rem; color: red; }',
},
{
code: 'p { display: inline-block; font-size: 16px; font-size: 1rem; color: red; }',
},
],

reject: [
{
code: 'p { font-size: 16px; font-weight: 400; font-size: 1rem; }',
fixed: 'p { font-size: 16px; font-weight: 400; }',
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-size: 16px; font-weight: 400; }',
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-size: 16px; font-weight: 400; 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-size: 16px; font-weight: 400; color: red; }',
fixed: 'p { display: inline-block; font-weight: 400; font-size: 1rem; color: red; }',
message: messages.rejected('font-size'),
},
],
Expand All @@ -260,7 +334,6 @@ testRule({
testRule({
ruleName,
config: [true, { ignore: ['consecutive-duplicates-with-different-values'] }],
fix: false,

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

Expand All @@ -281,15 +354,15 @@ testRule({
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 Down Expand Up @@ -330,6 +403,30 @@ testRule({
],
});

testRule({
ruleName,
config: [true, { ignore: ['consecutive-duplicates-with-same-prefixless-values'] }],
fix: true,

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'),
},
],
});

testRule({
ruleName,
config: [true, { ignoreProperties: ['color'] }],
Expand Down Expand Up @@ -358,6 +455,25 @@ testRule({
],
});

testRule({
ruleName,
config: [true, { ignoreProperties: ['color'] }],
fix: true,

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'),
},
],
});

testRule({
ruleName,
config: [true, { ignoreProperties: ['/background-/'] }],
Expand All @@ -383,6 +499,25 @@ testRule({
],
});

testRule({
ruleName,
config: [true, { ignoreProperties: ['/background-/'] }],
fix: true,

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'),
},
],
});

testRule({
ruleName,
config: [true],
Expand Down Expand Up @@ -419,6 +554,31 @@ testRule({
],
});

testRule({
ruleName,
config: [true],
customSyntax: 'postcss-html',
fix: true,

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'),
},
],
});

testRule({
skip: true,
ruleName,
Expand All @@ -441,3 +601,25 @@ testRule({
},
],
});

testRule({
skip: true,
ruleName,
config: [true],
customSyntax: 'postcss-css-in-js',
fix: true,

reject: [
{
code: "import styled from 'styled-components';\nexport default styled.div` color: pink; color: orange; `;",
fixed:
"import styled from 'styled-components';\nexport default styled.div` color: orange; `;",
message: messages.rejected('color'),
},
{
code: "import styled from 'react-emotion'\nexport default styled.div` color: pink; color: orange; `;",
fixed: "import styled from 'react-emotion'\nexport default styled.div` color: orange; `;",
message: messages.rejected('color'),
},
],
});

0 comments on commit 725fce2

Please sign in to comment.