Skip to content

Commit 12049c0

Browse files
authored
fix: ignore css_unused_selector error if style is global (#990)
1 parent 84c3370 commit 12049c0

10 files changed

+82
-10
lines changed

.changeset/nice-clocks-buy.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': patch
3+
---
4+
5+
fix: ignore `css_unused_selector` compile error if `<style>` tag has `global` attribute

packages/eslint-plugin-svelte/src/rules/valid-compile.ts

+44-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@ import { createRule } from '../utils/index.js';
22
import type { SvelteCompileWarnings, Warning } from '../shared/svelte-compile-warns/index.js';
33
import { getSvelteCompileWarnings } from '../shared/svelte-compile-warns/index.js';
44
import { getSourceCode } from '../utils/compat.js';
5+
import type { Position } from 'svelte-eslint-parser/lib/ast/common.js';
6+
7+
const ignores: string[] = [
8+
'missing-declaration',
9+
// Svelte v4
10+
'dynamic-slot-name',
11+
// Svelte v5
12+
'invalid-slot-name'
13+
] as const;
14+
15+
const unusedSelectorWarnings: string[] = ['css_unused_selector', 'css-unused-selector'] as const;
16+
17+
function isGlobalStyleNode(
18+
globalStyleRanges: [Position, Position][],
19+
start?: Position,
20+
end?: Position
21+
) {
22+
if (start == null || end == null) {
23+
return false;
24+
}
25+
return globalStyleRanges.some(([rangeStart, rangeEnd]) => {
26+
return (
27+
(rangeStart.line < start.line ||
28+
(rangeStart.line === start.line && rangeStart.column <= start.column)) &&
29+
(end.line < rangeEnd.line || (end.line === rangeEnd.line && end.column <= rangeEnd.column))
30+
);
31+
});
32+
}
533

634
export default createRule('valid-compile', {
735
meta: {
@@ -39,21 +67,19 @@ export default createRule('valid-compile', {
3967
: (warning) => warning;
4068

4169
const ignoreWarnings = Boolean(context.options[0]?.ignoreWarnings);
42-
43-
const ignores = [
44-
'missing-declaration',
45-
// Svelte v4
46-
'dynamic-slot-name',
47-
// Svelte v5
48-
'invalid-slot-name'
49-
];
70+
const globalStyleRanges: [Position, Position][] = [];
5071

5172
/**
5273
* report
5374
*/
5475
function report({ warnings, kind }: SvelteCompileWarnings) {
5576
for (const warn of warnings) {
56-
if (warn.code && ignores.includes(warn.code)) {
77+
if (
78+
warn.code &&
79+
(ignores.includes(warn.code) ||
80+
(isGlobalStyleNode(globalStyleRanges, warn.start, warn.end) &&
81+
unusedSelectorWarnings.includes(warn.code)))
82+
) {
5783
continue;
5884
}
5985
const reportWarn = kind === 'warn' ? transform(warn) : warn;
@@ -71,6 +97,15 @@ export default createRule('valid-compile', {
7197
}
7298

7399
return {
100+
SvelteStyleElement(node) {
101+
const { attributes } = node.startTag;
102+
for (const attr of attributes) {
103+
if (attr.type === 'SvelteAttribute' && attr.key.name === 'global') {
104+
globalStyleRanges.push([node.loc.start, node.loc.end]);
105+
break;
106+
}
107+
}
108+
},
74109
'Program:exit'() {
75110
const result = getSvelteCompileWarnings(context);
76111
if (ignoreWarnings && result.kind === 'warn') {

packages/eslint-plugin-svelte/tests/fixtures/rules/valid-compile/invalid/invalid-svelte-ignore01-svelte4-errors.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
line: 6
44
column: 3
55
suggestions: null
6-
- message: 'A11y: A form label must be associated with a control.(a11y-label-has-associated-control)'
6+
- message: 'A11y: A form label must be associated with a
7+
control.(a11y-label-has-associated-control)'
78
line: 6
89
column: 3
910
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- message: Unused CSS selector "input"(css-unused-selector)
2+
line: 2
3+
column: 2
4+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<style>
2+
input {
3+
@apply bg-surface-50-900-token h-full overflow-hidden;
4+
}
5+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"svelte": "^3.0.0 || ^4.0.0"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- message: |-
2+
Unused CSS selector "input"
3+
https://svelte.dev/e/css_unused_selector(css_unused_selector)
4+
line: 2
5+
column: 2
6+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<style>
2+
input {
3+
@apply bg-surface-50-900-token h-full overflow-hidden;
4+
}
5+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"svelte": ">=5.0.0-0"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<style global>
2+
input {
3+
@apply bg-surface-50-900-token h-full overflow-hidden;
4+
}
5+
</style>

0 commit comments

Comments
 (0)