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

fix(consistent-selector-style): not reporting class selectors used in class directives #1108

Merged
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
5 changes: 5 additions & 0 deletions .changeset/purple-months-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

fix(consistent-selector-style): not reporting class selectors used in class directives
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default createRule('consistent-selector-style', {
const checkGlobal = context.options[0]?.checkGlobal ?? false;
const style = context.options[0]?.style ?? ['type', 'id', 'class'];

const whitelistedClasses: string[] = [];
const classSelections: Map<string, AST.SvelteHTMLElement[]> = new Map();
const idSelections: Map<string, AST.SvelteHTMLElement[]> = new Map();
const typeSelections: Map<string, AST.SvelteHTMLElement[]> = new Map();
Expand Down Expand Up @@ -109,6 +110,9 @@ export default createRule('consistent-selector-style', {
* Checks a class selector
*/
function checkClassSelector(node: SelectorClass): void {
if (whitelistedClasses.includes(node.value)) {
return;
}
const selection = classSelections.get(node.value) ?? [];
for (const styleValue of style) {
if (styleValue === 'class') {
Expand Down Expand Up @@ -194,6 +198,9 @@ export default createRule('consistent-selector-style', {
addToArrayMap(classSelections, className, node);
}
for (const attribute of node.startTag.attributes) {
if (attribute.type === 'SvelteDirective' && attribute.kind === 'Class') {
whitelistedClasses.push(attribute.key.name.name);
}
if (attribute.type !== 'SvelteAttribute' || attribute.key.name !== 'id') {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

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

<b class:conditional={true}>Text 3</b>

<style>
.link {
color: red;
Expand All @@ -32,4 +34,8 @@
.bold::before {
color: red;
}

.conditional {
color: red;
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

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

<b class:conditional={true}>Text 3</b>

<style>
.link {
color: red;
Expand All @@ -32,4 +34,8 @@
.bold::before {
color: red;
}

.conditional {
color: red;
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

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

<b class:conditional={true}>Text 4</b>

<style>
.link {
color: red;
Expand All @@ -32,4 +34,8 @@
.bold::before {
color: red;
}

.conditional {
color: red;
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

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

<b class:conditional={true}>Text 4</b>

<b class="conditional-two">Text 5</b>

<b class:conditional-two={true}>Text 6</b>

<style>
.link {
color: red;
Expand All @@ -34,4 +40,12 @@
.bold::before {
color: red;
}

.conditional {
color: red;
}

.conditional-two {
color: red;
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

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

<b class:conditinal={true}>Text 4</b>

<style>
.link {
color: red;
Expand All @@ -34,4 +36,8 @@
.bold::before {
color: red;
}

.conditinal {
color: red;
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

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

<b class:conditional={true}>Text 4</b>

<style>
.link {
color: red;
Expand Down Expand Up @@ -41,4 +43,7 @@
color: red;
}

.conditional {
color: red;
}
</style>