Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/rules/no-duplicate-keyframe-selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ Examples of **incorrect** code for this rule:
}
}

@keyframes test {
0%,
50% {
opacity: 0;
}

50% {
opacity: 1;
}
}

@keyframes test {
from {
opacity: 0;
Expand Down Expand Up @@ -97,6 +108,21 @@ Examples of **correct** code for this rule:
}
}

@keyframes test {
0% {
opacity: 0;
}

30%,
60% {
opacity: 0.5;
}

100% {
opacity: 1;
}
}

@keyframes test {
from {
opacity: 0;
Expand Down
72 changes: 46 additions & 26 deletions src/rules/no-duplicate-keyframe-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,57 @@ export default /** @satisfies {DuplicateKeyframeSelectorRuleDefinition} */ ({
}

// @ts-ignore - children is a valid property for prelude
const selector = node.prelude.children[0];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we simplify this change by iterating over node.prelude.children here and keeping the existing logic below? Is there a reason for the additional changes?

node.prelude.children.forEach(selector => {
	const value = [];

	selector.children.forEach(component => {
		if (component.type === "Percentage") {
			value.push(`${component.value}%`);
		} else if (component.type === "TypeSelector") {
			value.push(component.name.toLowerCase());
		}
	});

	const selectorValue = value.join(" ");
	const key = value
		.map(
			selectorPart =>
				keyframeSelectorAliases.get(selectorPart) ??
				selectorPart,
		)
		.join(" ");

	if (seen.has(key)) {
		context.report({
			loc: selector.loc,
			messageId: "duplicateKeyframeSelector",
			data: {
				selector: selectorValue,
			},
		});
	} else {
		seen.add(key);
	}
});

const selectors = node.prelude.children;
const value = [];

selector.children.forEach(component => {
if (component.type === "Percentage") {
value.push(`${component.value}%`);
} else if (component.type === "TypeSelector") {
value.push(component.name.toLowerCase());
selectors.forEach(selector => {
const component = selector.children[0];
const componentType = component.type;

if (selector.children.length === 1) {
if (componentType === "Percentage") {
value.push(`${component.value}%`);
} else if (componentType === "TypeSelector") {
value.push(component.name.toLowerCase());
}
} else {
if (
selector.children.some(
child => child.type === "Combinator",
)
) {
if (
Comment thread
DMartens marked this conversation as resolved.
componentType === "TypeSelector" &&
selector.children[1].type === "Combinator" &&
selector.children[2].type === "Percentage"
) {
value.push(
`${component.name.toLowerCase()} ${selector.children[2].value}%`,
);
}
}
}
});

const selectorValue = value.join(" ");
const key = value
.map(
selectorPart =>
keyframeSelectorAliases.get(selectorPart) ??
selectorPart,
)
.join(" ");

if (seen.has(key)) {
context.report({
loc: selector.loc,
messageId: "duplicateKeyframeSelector",
data: {
selector: selectorValue,
},
});
} else {
seen.add(key);
}
const keys = value.map(
selectorPart =>
keyframeSelectorAliases.get(selectorPart) ??
selectorPart,
);

keys.forEach((key, i) => {
if (seen.has(key)) {
context.report({
loc: selectors[i].loc,
messageId: "duplicateKeyframeSelector",
data: {
selector: value[i],
},
});
} else {
seen.add(key);
}
});
},
};
},
Expand Down
Loading