New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor declaration-block-no-duplicate-properties #6545
Changes from 1 commit
cd61645
a17bf24
3740f4b
f14506f
c0608c1
2fd05f6
af60e72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,24 +88,23 @@ const rule = (primary, secondaryOptions, context) => { | |
|
||
const indexDuplicate = decls.findIndex((d) => d.prop.toLowerCase() === lowerProp); | ||
|
||
if (indexDuplicate !== -1) { | ||
const duplicateDecl = decls[indexDuplicate]; | ||
const duplicateValue = duplicateDecl ? duplicateDecl.value : ''; | ||
const duplicateImportant = duplicateDecl ? duplicateDecl.important : false; | ||
|
||
if (ignoreDiffValues || ignorePrefixlessSameValues) { | ||
// fails if duplicates are not consecutive | ||
if (indexDuplicate !== decls.length - 1) { | ||
if (context.fix) { | ||
if (!important && duplicateImportant) { | ||
decl.remove(); | ||
} else { | ||
removePreviousDuplicate(decls, lowerProp); | ||
} | ||
if (indexDuplicate === -1) { | ||
decls.push(decl); | ||
} | ||
|
||
return; | ||
} | ||
const duplicateDecl = decls[indexDuplicate]; | ||
|
||
if (!duplicateDecl) { | ||
return; | ||
} | ||
|
||
const duplicateValue = duplicateDecl.value || ''; | ||
const duplicateImportant = duplicateDecl.important || false; | ||
|
||
if (ignoreDiffValues || ignorePrefixlessSameValues) { | ||
// fails if duplicates are not consecutive | ||
if (indexDuplicate !== decls.length - 1) { | ||
if (!context.fix) { | ||
report({ | ||
message: messages.rejected(prop), | ||
node: decl, | ||
|
@@ -117,19 +116,21 @@ const rule = (primary, secondaryOptions, context) => { | |
return; | ||
} | ||
|
||
if (ignorePrefixlessSameValues) { | ||
// fails if values of consecutive, unprefixed duplicates are equal | ||
if (vendor.unprefixed(value) !== vendor.unprefixed(duplicateValue)) { | ||
if (context.fix) { | ||
if (!important && duplicateImportant) { | ||
decl.remove(); | ||
} else { | ||
removePreviousDuplicate(decls, lowerProp); | ||
} | ||
if (!important && duplicateImportant) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor put |
||
decl.remove(); | ||
|
||
return; | ||
} | ||
|
||
return; | ||
} | ||
duplicateDecl.remove(); | ||
|
||
return; | ||
} | ||
|
||
if (ignorePrefixlessSameValues) { | ||
// fails if values of consecutive, unprefixed duplicates are equal | ||
if (vendor.unprefixed(value) !== vendor.unprefixed(duplicateValue)) { | ||
if (!context.fix) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like you have
twice. What about // false if values of consecutive, unprefixed duplicates are equal
const foo = vendor.unprefixed(value) !== vendor.unprefixed(duplicateValue;
const bar = ignorePrefixlessSameValues && foo;
if (!duplicatesAreConsecutive || bar) {
…
} at line 107? |
||
report({ | ||
message: messages.rejected(prop), | ||
node: decl, | ||
|
@@ -140,40 +141,25 @@ const rule = (primary, secondaryOptions, context) => { | |
|
||
return; | ||
} | ||
} | ||
|
||
// fails if values of consecutive duplicates are equal | ||
if (value === duplicateValue) { | ||
if (context.fix) { | ||
removePreviousDuplicate(decls, lowerProp); | ||
if (!important && duplicateImportant) { | ||
decl.remove(); | ||
|
||
return; | ||
} | ||
|
||
report({ | ||
message: messages.rejected(prop), | ||
node: decl, | ||
result, | ||
ruleName, | ||
word: prop, | ||
}); | ||
duplicateDecl.remove(); | ||
|
||
return; | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (ignoreDuplicates && indexDuplicate === decls.length - 1) { | ||
if (value !== duplicateValue) { | ||
return; | ||
} | ||
|
||
if (context.fix) { | ||
if (!important && duplicateImportant) { | ||
decl.remove(); | ||
} else { | ||
removePreviousDuplicate(decls, lowerProp); | ||
} | ||
duplicateDecl.remove(); | ||
|
||
return; | ||
} | ||
|
@@ -185,25 +171,38 @@ const rule = (primary, secondaryOptions, context) => { | |
ruleName, | ||
word: prop, | ||
}); | ||
|
||
return; | ||
} | ||
|
||
decls.push(decl); | ||
if (ignoreDuplicates && indexDuplicate === decls.length - 1) { | ||
return; | ||
} | ||
|
||
if (!context.fix) { | ||
report({ | ||
message: messages.rejected(prop), | ||
node: decl, | ||
result, | ||
ruleName, | ||
word: prop, | ||
}); | ||
|
||
return; | ||
} | ||
|
||
if (!important && duplicateImportant) { | ||
decl.remove(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stylistic I prefer the form |
||
|
||
return; | ||
} | ||
|
||
duplicateDecl.remove(); | ||
}); | ||
}); | ||
}; | ||
}; | ||
|
||
/** | ||
* @param {import('postcss').Declaration[]} declarations | ||
* @param {string} lowerProperty | ||
* @returns {void} | ||
* */ | ||
function removePreviousDuplicate(declarations, lowerProperty) { | ||
const declToRemove = declarations.find((d) => d.prop.toLowerCase() === lowerProperty); | ||
|
||
if (declToRemove) declToRemove.remove(); | ||
} | ||
|
||
rule.ruleName = ruleName; | ||
rule.messages = messages; | ||
rule.meta = meta; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion
put
indexDuplicate !== decls.length - 1
in a const foo (dunno about the name but you should)and use
!foo
at l178