Skip to content

Commit 639b265

Browse files
committed
feat(consistent-selector-style): switching off for universal selector
1 parent 9e859b9 commit 639b265

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { createRule } from '../utils/index.js';
1515
interface Selections {
1616
exact: Map<string, AST.SvelteHTMLElement[]>;
1717
prefixes: Map<string, AST.SvelteHTMLElement[]>;
18+
universalSelector: boolean;
1819
}
1920

2021
export default createRule('consistent-selector-style', {
@@ -78,11 +79,13 @@ export default createRule('consistent-selector-style', {
7879
} = {
7980
class: {
8081
exact: new Map(),
81-
prefixes: new Map()
82+
prefixes: new Map(),
83+
universalSelector: false
8284
},
8385
id: {
8486
exact: new Map(),
85-
prefixes: new Map()
87+
prefixes: new Map(),
88+
universalSelector: false
8689
},
8790
type: new Map()
8891
};
@@ -130,7 +133,7 @@ export default createRule('consistent-selector-style', {
130133
* Checks a class selector
131134
*/
132135
function checkClassSelector(node: SelectorClass): void {
133-
if (whitelistedClasses.includes(node.value)) {
136+
if (selections.class.universalSelector || whitelistedClasses.includes(node.value)) {
134137
return;
135138
}
136139
const selection = matchSelection(selections.class, node.value);
@@ -159,6 +162,9 @@ export default createRule('consistent-selector-style', {
159162
* Checks an ID selector
160163
*/
161164
function checkIdSelector(node: SelectorIdentifier): void {
165+
if (selections.id.universalSelector) {
166+
return;
167+
}
162168
const selection = matchSelection(selections.id, node.value);
163169
for (const styleValue of style) {
164170
if (styleValue === 'class') {
@@ -228,6 +234,8 @@ export default createRule('consistent-selector-style', {
228234
const prefix = extractExpressionPrefixLiteral(context, value.expression);
229235
if (prefix !== null) {
230236
addToArrayMap(selections.class.prefixes, prefix, node);
237+
} else {
238+
selections.class.universalSelector = true;
231239
}
232240
}
233241
if (attribute.key.name === 'id') {
@@ -237,6 +245,8 @@ export default createRule('consistent-selector-style', {
237245
const prefix = extractExpressionPrefixLiteral(context, value.expression);
238246
if (prefix !== null) {
239247
addToArrayMap(selections.id.prefixes, prefix, node);
248+
} else {
249+
selections.id.universalSelector = true;
240250
}
241251
}
242252
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script>
2+
import { value } from "package";
3+
</script>
4+
5+
<a>Click me!</a>
6+
7+
<a class={value}>Click me two!</a>
8+
9+
<a class={value}>Click me two!</a>
10+
11+
<style>
12+
.link-one {
13+
color: red;
14+
}
15+
16+
.link-two {
17+
color: red;
18+
}
19+
20+
.link-three {
21+
color: red;
22+
}
23+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script>
2+
import { value } from "package";
3+
</script>
4+
5+
<a>Click me!</a>
6+
7+
<a id={value}>Click me two!</a>
8+
9+
<a id={value}>Click me two!</a>
10+
11+
<style>
12+
#link-one {
13+
color: red;
14+
}
15+
16+
#link-two {
17+
color: red;
18+
}
19+
20+
#link-three {
21+
color: red;
22+
}
23+
</style>

0 commit comments

Comments
 (0)