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

Conversation

fpetrakov
Copy link
Contributor

Which issue, if any, is this issue related to?

Closes #6544

Is there anything in the PR that needs further explanation?

No, it's self-explanatory.

@changeset-bot
Copy link

changeset-bot bot commented Dec 23, 2022

⚠️ No Changeset found

Latest commit: af60e72

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

}

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

} 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)


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

@Mouvedia
Copy link
Contributor

@fpetrakov
Copy link
Contributor Author

perf

verify that it's at least marginally equivalent https://github.com/stylelint/stylelint/blob/main/docs/developer-guide/rules.md#improve-the-performance-of-a-rule

before:
image

after:
image

@fpetrakov fpetrakov requested review from Mouvedia and jeddy3 and removed request for Mouvedia December 24, 2022 16:20
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?

@Mouvedia
Copy link
Contributor

a separate function

You are correct you can reuse

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

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

	return duplicateDecl.remove();
};

at line l113 and l151.

} else {
removePreviousDuplicate(decls, lowerProp);
}
if (!duplicateDecl) return;
Copy link
Contributor

Choose a reason for hiding this comment

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

either this one is not necessary or the one at l97 is
i.e. if we reach l123 duplicateDecl is true because it's a const

Copy link
Contributor

@Mouvedia Mouvedia Dec 27, 2022

Choose a reason for hiding this comment

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

at l97 you have

				if (!duplicateDecl) {
					return;
				}

the l123 check is called at l133 and l157 which are after l97; since you chose to use a function declaration it's being hoisted hence it could have been called before l97.
It seems to me that duplicateDecl cannot be undefined at that stage because Boolean(undefined) === false hence it would have returned early.
If it's a type check problem you can ask @ybiquitous for help.
e.g. a /** @type … */ comment may be necessary

Copy link
Member

Choose a reason for hiding this comment

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

To remove duplicate if (!duplicateDecl) return;, we need to convert function fixOrReport to const fixOrReport. Because using function causes hoisting.

-				function fixOrReport() {
+				const fixOrReport = () => {

Copy link
Member

@ybiquitous ybiquitous left a comment

Choose a reason for hiding this comment

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

Thank you. LGTM 👍🏼

@ybiquitous ybiquitous mentioned this pull request Dec 28, 2022
6 tasks
Copy link
Member

@jeddy3 jeddy3 left a comment

Choose a reason for hiding this comment

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

@fpetrakov Thank you for the refactor. It reads well.

@jeddy3 jeddy3 merged commit 121acce into stylelint:main Dec 28, 2022
@fpetrakov
Copy link
Contributor Author

@fpetrakov Thank you for the refactor. It reads well.

Glad to help 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Refactor declaration-block-no-duplicate-properties
4 participants