Skip to content
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

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
117 changes: 58 additions & 59 deletions lib/rules/declaration-block-no-duplicate-properties/index.js
Expand Up @@ -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) {
Copy link
Contributor

@Mouvedia Mouvedia Dec 23, 2022

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

if (!context.fix) {
report({
message: messages.rejected(prop),
node: decl,
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor

put !important && duplicateImportant in a const
(being reused at l145)

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like you have

if (!context.fix) {
	return report({
		message: messages.rejected(prop),
		node: decl,
		result,
		ruleName,
		word: prop,
	});
}

if (duplicateIsMoreImportant) {
	return decl.remove();
}

return duplicateDecl.remove();

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,
Expand All @@ -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;
}
Expand All @@ -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();
Copy link
Contributor

@Mouvedia Mouvedia Dec 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stylistic

I prefer the form return fn();.
ditto for l183, l108, etc


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;
Expand Down