Skip to content

Commit

Permalink
refactor declaration-block-no-duplicate-properties to use fix cal…
Browse files Browse the repository at this point in the history
…lback instead of `context.fix`
  • Loading branch information
Mouvedia committed Jun 13, 2024
1 parent eb6d613 commit 5398e4d
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,73 +53,73 @@ testRule({
fixed: 'a { color: orange }',
message: messages.rejected('color'),
line: 1,
column: 18,
column: 5,
endLine: 1,
endColumn: 23,
endColumn: 10,
},
{
code: 'a { cOlOr: pink; color: orange }',
fixed: 'a { color: orange }',
message: messages.rejected('color'),
message: messages.rejected('cOlOr'),
line: 1,
column: 18,
column: 5,
endLine: 1,
endColumn: 23,
endColumn: 10,
},
{
code: 'a { color: pink; cOlOr: orange }',
fixed: 'a { cOlOr: orange }',
message: messages.rejected('cOlOr'),
message: messages.rejected('color'),
line: 1,
column: 18,
column: 5,
endLine: 1,
endColumn: 23,
endColumn: 10,
},
{
code: 'a { cOlOr: pink; cOlOr: orange }',
fixed: 'a { cOlOr: orange }',
message: messages.rejected('cOlOr'),
line: 1,
column: 18,
column: 5,
endLine: 1,
endColumn: 23,
endColumn: 10,
},
{
code: 'a { COLOR: pink; color: orange }',
fixed: 'a { color: orange }',
message: messages.rejected('color'),
message: messages.rejected('COLOR'),
line: 1,
column: 18,
column: 5,
endLine: 1,
endColumn: 23,
endColumn: 10,
},
{
code: 'a { color: pink; COLOR: orange }',
fixed: 'a { COLOR: orange }',
message: messages.rejected('COLOR'),
message: messages.rejected('color'),
line: 1,
column: 18,
column: 5,
endLine: 1,
endColumn: 23,
endColumn: 10,
},
{
code: 'a { color: pink; color: pink; color: pink }',
code: 'a { COlOr: pink; coLOR: pink; color: pink }',
fixed: 'a { color: pink }',
description: 'multiple duplicates - same',
warnings: [
{
message: messages.rejected('color'),
message: messages.rejected('COlOr'),
line: 1,
column: 18,
column: 5,
endLine: 1,
endColumn: 23,
endColumn: 10,
},
{
message: messages.rejected('color'),
message: messages.rejected('coLOR'),
line: 1,
column: 31,
column: 18,
endLine: 1,
endColumn: 36,
endColumn: 23,
},
],
},
Expand All @@ -131,16 +131,16 @@ testRule({
{
message: messages.rejected('color'),
line: 1,
column: 18,
column: 5,
endLine: 1,
endColumn: 23,
endColumn: 10,
},
{
message: messages.rejected('color'),
line: 1,
column: 31,
column: 18,
endLine: 1,
endColumn: 36,
endColumn: 23,
},
],
},
Expand All @@ -149,86 +149,86 @@ testRule({
fixed: 'a { background: orange; color: orange }',
message: messages.rejected('color'),
line: 1,
column: 38,
column: 5,
endLine: 1,
endColumn: 43,
endColumn: 10,
},
{
code: 'a { color: pink; background: orange; background: pink; }',
fixed: 'a { color: pink; background: pink; }',
message: messages.rejected('background'),
line: 1,
column: 38,
column: 18,
endLine: 1,
endColumn: 48,
endColumn: 28,
},
{
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,
column: 45,
column: 30,
endLine: 1,
endColumn: 50,
endColumn: 35,
},
{
code: 'a { color: pink; @media { color: orange; color: black; } }',
fixed: 'a { color: pink; @media { color: black; } }',
description: 'nested',
message: messages.rejected('color'),
line: 1,
column: 42,
column: 27,
endLine: 1,
endColumn: 47,
endColumn: 32,
},
{
code: '@media { color: orange; .foo { color: black; color: white; } }',
fixed: '@media { color: orange; .foo { color: white; } }',
description: 'nested',
message: messages.rejected('color'),
line: 1,
column: 46,
column: 32,
endLine: 1,
endColumn: 51,
endColumn: 37,
},
{
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,
column: 68,
column: 54,
endLine: 1,
endColumn: 73,
endColumn: 59,
},
{
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,
column: 63,
column: 49,
endLine: 1,
endColumn: 68,
endColumn: 54,
},
{
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,
column: 5,
endLine: 1,
endColumn: 55,
endColumn: 26,
},
{
code: 'a { -WEBKIT-border-radius: 12px; -webkit-BORDER-radius: 10px; }',
fixed: 'a { -webkit-BORDER-radius: 10px; }',
message: messages.rejected('-webkit-BORDER-radius'),
message: messages.rejected('-WEBKIT-border-radius'),
line: 1,
column: 34,
column: 5,
endLine: 1,
endColumn: 55,
endColumn: 26,
},
{
code: 'a { color: red !important; color: blue; }',
Expand All @@ -244,9 +244,9 @@ testRule({
fixed: 'a { color: blue !important; }',
message: messages.rejected('color'),
line: 1,
column: 28,
column: 5,
endLine: 1,
endColumn: 33,
endColumn: 10,
},
],
});
Expand Down Expand Up @@ -325,11 +325,19 @@ testRule({
code: 'p { font-size: 16px !important; font-weight: 400; font-size: 1rem; }',
fixed: 'p { font-size: 16px !important; font-weight: 400; }',
message: messages.rejected('font-size'),
line: 1,
column: 51,
endLine: 1,
endColumn: 60,
},
{
code: 'p { font-size: 16px; font-size: 16px !important; font-weight: 400; }',
fixed: 'p { font-size: 16px !important; font-weight: 400; }',
message: messages.rejected('font-size'),
line: 1,
column: 5,
endLine: 1,
endColumn: 14,
},
],
});
Expand Down Expand Up @@ -466,11 +474,19 @@ testRule({
code: 'p { width: -moz-fit-content; width: -moz-fit-content !important; }',
fixed: 'p { width: -moz-fit-content !important; }',
message: messages.rejected('width'),
line: 1,
column: 5,
endLine: 1,
endColumn: 10,
},
{
code: 'p { width: 100% !important; width: -moz-fit-content; }',
fixed: 'p { width: 100% !important; }',
message: messages.rejected('width'),
line: 1,
column: 29,
endLine: 1,
endColumn: 34,
},
],
});
Expand Down
51 changes: 29 additions & 22 deletions lib/rules/declaration-block-no-duplicate-properties/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const isEqualValueSyntaxes = (value1, value2) => {
};

/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions, context) => {
const rule = (primary, secondaryOptions) => {
return (root, result) => {
const validOptions = validateOptions(
result,
Expand Down Expand Up @@ -204,25 +204,29 @@ const rule = (primary, secondaryOptions, context) => {
vendor.unprefixed(value) === vendor.unprefixed(duplicateValue);

const fixOrReport = () => {
if (!context.fix) {
return report({
message: messages.rejected,
messageArgs: [prop],
node: decl,
result,
ruleName,
word: prop,
});
}
const node = duplicateIsMoreImportant ? decl : duplicateDecl;
const word = duplicateIsMoreImportant ? prop : duplicateDecl.prop;

if (duplicateIsMoreImportant) {
return decl.remove();
if (!duplicateIsMoreImportant) {
// replace previous "active" decl with current one
decls[indexDuplicate] = decl;
}

// replace previous "active" decl with current one
decls[indexDuplicate] = decl;
const fix = () => {
node.remove();

return node.rangeBy({ index: 0, endIndex: 0 });
};

return duplicateDecl.remove();
report({
message: messages.rejected,
messageArgs: [word],
node,
result,
ruleName,
word,
fix,
});
};

if (ignoreDiffValues || ignoreDiffSyntaxes || ignorePrefixlessSameValues) {
Expand All @@ -249,17 +253,20 @@ const rule = (primary, secondaryOptions, context) => {
return;
}

if (context.fix) {
return duplicateDecl.remove();
}
const fix = () => {
duplicateDecl.remove();

return duplicateDecl.rangeBy({ index: 0, endIndex: 0 });
};

return report({
message: messages.rejected,
messageArgs: [prop],
node: decl,
messageArgs: [duplicateDecl.prop],
node: duplicateDecl,
result,
ruleName,
word: prop,
word: duplicateDecl.prop,
fix,
});
}

Expand Down
Loading

0 comments on commit 5398e4d

Please sign in to comment.