Skip to content

Commit 9e859b9

Browse files
committed
feat(consistent-selector-style): matching dynamic ID prefixes
1 parent a2a3160 commit 9e859b9

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

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

+15-5
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,17 @@ export default createRule('consistent-selector-style', {
7373

7474
const selections: {
7575
class: Selections;
76-
id: Map<string, AST.SvelteHTMLElement[]>;
76+
id: Selections;
7777
type: Map<string, AST.SvelteHTMLElement[]>;
7878
} = {
7979
class: {
8080
exact: new Map(),
8181
prefixes: new Map()
8282
},
83-
id: new Map(),
83+
id: {
84+
exact: new Map(),
85+
prefixes: new Map()
86+
},
8487
type: new Map()
8588
};
8689

@@ -156,7 +159,7 @@ export default createRule('consistent-selector-style', {
156159
* Checks an ID selector
157160
*/
158161
function checkIdSelector(node: SelectorIdentifier): void {
159-
const selection = selections.id.get(node.value) ?? [];
162+
const selection = matchSelection(selections.id, node.value);
160163
for (const styleValue of style) {
161164
if (styleValue === 'class') {
162165
context.report({
@@ -227,8 +230,15 @@ export default createRule('consistent-selector-style', {
227230
addToArrayMap(selections.class.prefixes, prefix, node);
228231
}
229232
}
230-
if (attribute.key.name === 'id' && value.type === 'SvelteLiteral') {
231-
addToArrayMap(selections.id, value.value, node);
233+
if (attribute.key.name === 'id') {
234+
if (value.type === 'SvelteLiteral') {
235+
addToArrayMap(selections.id.exact, value.value, node);
236+
} else if (value.type === 'SvelteMustacheTag') {
237+
const prefix = extractExpressionPrefixLiteral(context, value.expression);
238+
if (prefix !== null) {
239+
addToArrayMap(selections.id.prefixes, prefix, node);
240+
}
241+
}
232242
}
233243
}
234244
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<script>
2+
import { value } from "package";
3+
4+
const derived = "link-three-" + value;
5+
</script>
6+
7+
<a>Click me!</a>
8+
9+
<a id={"link-one-" + value}>Click me two!</a>
10+
11+
<a id={`link-two-${value}`}>Click me three!</a>
12+
13+
<a id={derived}>Click me four!</a>
14+
15+
<style>
16+
#link-one-foo {
17+
color: red;
18+
}
19+
20+
#link-two-foo {
21+
color: red;
22+
}
23+
24+
#link-three-foo {
25+
color: red;
26+
}
27+
</style>

0 commit comments

Comments
 (0)