Skip to content

Commit dd4fb8b

Browse files
committed
fix(consistent-selector-style): not reporting class selectors used in class directives
1 parent eaf56d4 commit dd4fb8b

File tree

7 files changed

+50
-0
lines changed

7 files changed

+50
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export default createRule('consistent-selector-style', {
6262
const checkGlobal = context.options[0]?.checkGlobal ?? false;
6363
const style = context.options[0]?.style ?? ['type', 'id', 'class'];
6464

65+
const whitelistedClasses: string[] = [];
6566
const classSelections: Map<string, AST.SvelteHTMLElement[]> = new Map();
6667
const idSelections: Map<string, AST.SvelteHTMLElement[]> = new Map();
6768
const typeSelections: Map<string, AST.SvelteHTMLElement[]> = new Map();
@@ -109,6 +110,9 @@ export default createRule('consistent-selector-style', {
109110
* Checks a class selector
110111
*/
111112
function checkClassSelector(node: SelectorClass): void {
113+
if (whitelistedClasses.includes(node.value)) {
114+
return;
115+
}
112116
const selection = classSelections.get(node.value) ?? [];
113117
for (const styleValue of style) {
114118
if (styleValue === 'class') {
@@ -194,6 +198,9 @@ export default createRule('consistent-selector-style', {
194198
addToArrayMap(classSelections, className, node);
195199
}
196200
for (const attribute of node.startTag.attributes) {
201+
if (attribute.type === 'SvelteDirective' && attribute.kind === 'Class') {
202+
whitelistedClasses.push(attribute.key.name.name);
203+
}
197204
if (attribute.type !== 'SvelteAttribute' || attribute.key.name !== 'id') {
198205
continue;
199206
}

packages/eslint-plugin-svelte/tests/fixtures/rules/consistent-selector-style/valid/id-class-type/class01-input.svelte

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
<b data-key="val">Text 2</b>
1010

11+
<b class:conditional={true}>Text 3</b>
12+
1113
<style>
1214
.link {
1315
color: red;
@@ -32,4 +34,8 @@
3234
.bold::before {
3335
color: red;
3436
}
37+
38+
.conditional {
39+
color: red;
40+
}
3541
</style>

packages/eslint-plugin-svelte/tests/fixtures/rules/consistent-selector-style/valid/id-type-class/class01-input.svelte

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
<b data-key="val">Text 2</b>
1010

11+
<b class:conditional={true}>Text 3</b>
12+
1113
<style>
1214
.link {
1315
color: red;
@@ -32,4 +34,8 @@
3234
.bold::before {
3335
color: red;
3436
}
37+
38+
.conditional {
39+
color: red;
40+
}
3541
</style>

packages/eslint-plugin-svelte/tests/fixtures/rules/consistent-selector-style/valid/type-class-id/class01-input.svelte

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
<b data-key="val">Text 3</b>
1010

11+
<b class:conditional={true}>Text 4</b>
12+
1113
<style>
1214
.link {
1315
color: red;
@@ -32,4 +34,8 @@
3234
.bold::before {
3335
color: red;
3436
}
37+
38+
.conditional {
39+
color: red;
40+
}
3541
</style>

packages/eslint-plugin-svelte/tests/fixtures/rules/consistent-selector-style/valid/type-id-class/class01-input.svelte

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
<b data-key="val">Text 3</b>
1212

13+
<b class:conditional={true}>Text 4</b>
14+
15+
<b class="conditional-two">Text 5</b>
16+
17+
<b class:conditional-two={true}>Text 6</b>
18+
1319
<style>
1420
.link {
1521
color: red;
@@ -34,4 +40,12 @@
3440
.bold::before {
3541
color: red;
3642
}
43+
44+
.conditional {
45+
color: red;
46+
}
47+
48+
.conditional-two {
49+
color: red;
50+
}
3751
</style>

packages/eslint-plugin-svelte/tests/fixtures/rules/consistent-selector-style/valid/type-id/class01-input.svelte

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
<b data-key="val">Text 3</b>
1212

13+
<b class:conditinal={true}>Text 4</b>
14+
1315
<style>
1416
.link {
1517
color: red;
@@ -34,4 +36,8 @@
3436
.bold::before {
3537
color: red;
3638
}
39+
40+
.conditinal {
41+
color: red;
42+
}
3743
</style>

packages/eslint-plugin-svelte/tests/fixtures/rules/consistent-selector-style/valid/type/class01-input.svelte

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
<b data-key="val">Text 3</b>
1414

15+
<b class:conditional={true}>Text 4</b>
16+
1517
<style>
1618
.link {
1719
color: red;
@@ -41,4 +43,7 @@
4143
color: red;
4244
}
4345
46+
.conditional {
47+
color: red;
48+
}
4449
</style>

0 commit comments

Comments
 (0)