Skip to content

Commit 990f45a

Browse files
committed
ignore css_unused_selector error if style is global
1 parent 2bd1799 commit 990f45a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

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

+32-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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';
56

67
export default createRule('valid-compile', {
78
meta: {
@@ -48,12 +49,33 @@ export default createRule('valid-compile', {
4849
'invalid-slot-name'
4950
];
5051

52+
const unusedSelectorWarnings = ['css_unused_selector', 'css-unused-selector'];
53+
const globalStyleRanges: [Position, Position][] = [];
54+
55+
function isGlobalStyleNode(start?: Position, end?: Position) {
56+
if (start == null || end == null) {
57+
return false;
58+
}
59+
return globalStyleRanges.some(([rangeStart, rangeEnd]) => {
60+
return (
61+
(rangeStart.line < start.line ||
62+
(rangeStart.line === start.line && rangeStart.column <= start.column)) &&
63+
(end.line < rangeEnd.line ||
64+
(end.line === rangeEnd.line && end.column <= rangeEnd.column))
65+
);
66+
});
67+
}
68+
5169
/**
5270
* report
5371
*/
5472
function report({ warnings, kind }: SvelteCompileWarnings) {
5573
for (const warn of warnings) {
56-
if (warn.code && ignores.includes(warn.code)) {
74+
if (
75+
warn.code &&
76+
(ignores.includes(warn.code) ||
77+
(isGlobalStyleNode(warn.start, warn.end) && unusedSelectorWarnings.includes(warn.code)))
78+
) {
5779
continue;
5880
}
5981
const reportWarn = kind === 'warn' ? transform(warn) : warn;
@@ -71,6 +93,15 @@ export default createRule('valid-compile', {
7193
}
7294

7395
return {
96+
SvelteStyleElement(node) {
97+
const { attributes } = node.startTag;
98+
for (const attr of attributes) {
99+
if (attr.type === 'SvelteAttribute' && attr.key.name === 'global') {
100+
globalStyleRanges.push([node.loc.start, node.loc.end]);
101+
break;
102+
}
103+
}
104+
},
74105
'Program:exit'() {
75106
const result = getSvelteCompileWarnings(context);
76107
if (ignoreWarnings && result.kind === 'warn') {
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)