Skip to content

Commit d8a4e99

Browse files
committed
fix(consistent-selector-style): Fixed counting of elements in Svelte blocks for type selectors
1 parent 03d61fd commit d8a4e99

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

.changeset/fast-birds-crash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': patch
3+
---
4+
5+
fix(consistent-selector-style): Fixed detections of repeated elements such as in {#each}

packages/eslint-plugin-svelte/src/rules/consistent-selector-style.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export default createRule('consistent-selector-style', {
144144
if (styleValue === 'class') {
145145
return;
146146
}
147-
if (styleValue === 'id' && canUseIdSelector(selection)) {
147+
if (styleValue === 'id' && canUseIdSelector(selection.map(([elem]) => elem))) {
148148
context.report({
149149
messageId: 'classShouldBeId',
150150
loc: styleSelectorNodeLoc(node) as AST.SourceLocation
@@ -286,11 +286,13 @@ function addToArrayMap<T>(
286286
/**
287287
* Finds all nodes in selections that could be matched by key
288288
*/
289-
function matchSelection(selections: Selections, key: string): AST.SvelteHTMLElement[] {
290-
const selection = selections.exact.get(key) ?? [];
289+
function matchSelection(selections: Selections, key: string): [AST.SvelteHTMLElement, boolean][] {
290+
const selection = (selections.exact.get(key) ?? []).map<[AST.SvelteHTMLElement, boolean]>(
291+
(elem) => [elem, true]
292+
);
291293
selections.affixes.forEach((nodes, [prefix, suffix]) => {
292294
if ((prefix === null || key.startsWith(prefix)) && (suffix === null || key.endsWith(suffix))) {
293-
selection.push(...nodes);
295+
selection.push(...nodes.map<[AST.SvelteHTMLElement, boolean]>((elem) => [elem, false]));
294296
}
295297
});
296298
return selection;
@@ -311,19 +313,32 @@ function canUseIdSelector(selection: AST.SvelteHTMLElement[]): boolean {
311313
* Checks whether a given selection could be obtained using a type selector
312314
*/
313315
function canUseTypeSelector(
314-
selection: AST.SvelteHTMLElement[],
316+
selection: [AST.SvelteHTMLElement, boolean][],
315317
typeSelections: Map<string, AST.SvelteHTMLElement[]>
316318
): boolean {
317-
const types = new Set(selection.map((node) => node.name.name));
319+
const types = new Set(selection.map(([node]) => node.name.name));
318320
if (types.size > 1) {
319321
return false;
320322
}
321323
if (types.size < 1) {
322324
return true;
323325
}
326+
if (
327+
selection.some(
328+
([elem, exact]) => !exact && elementOccurrenceCount(elem) === ElementOccurenceCount.ZeroToInf
329+
)
330+
) {
331+
return false;
332+
}
324333
const type = [...types][0];
325334
const typeSelection = typeSelections.get(type);
326-
return typeSelection !== undefined && arrayEquals(typeSelection, selection);
335+
return (
336+
typeSelection !== undefined &&
337+
arrayEquals(
338+
typeSelection,
339+
selection.map(([elem]) => elem)
340+
)
341+
);
327342
}
328343

329344
/**

0 commit comments

Comments
 (0)