Skip to content

fix: ignore css_unused_selector error if style is global #990

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

Merged
merged 6 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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/nice-clocks-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

fix: ignore css_unused_selector error if style tag has global attribute
33 changes: 32 additions & 1 deletion packages/eslint-plugin-svelte/src/rules/valid-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createRule } from '../utils/index.js';
import type { SvelteCompileWarnings, Warning } from '../shared/svelte-compile-warns/index.js';
import { getSvelteCompileWarnings } from '../shared/svelte-compile-warns/index.js';
import { getSourceCode } from '../utils/compat.js';
import type { Position } from 'svelte-eslint-parser/lib/ast/common.js';

export default createRule('valid-compile', {
meta: {
Expand Down Expand Up @@ -48,12 +49,33 @@ export default createRule('valid-compile', {
'invalid-slot-name'
];

const unusedSelectorWarnings = ['css_unused_selector', 'css-unused-selector'];
const globalStyleRanges: [Position, Position][] = [];

function isGlobalStyleNode(start?: Position, end?: Position) {
if (start == null || end == null) {
return false;
}
return globalStyleRanges.some(([rangeStart, rangeEnd]) => {
return (
(rangeStart.line < start.line ||
(rangeStart.line === start.line && rangeStart.column <= start.column)) &&
(end.line < rangeEnd.line ||
(end.line === rangeEnd.line && end.column <= rangeEnd.column))
);
});
}

/**
* report
*/
function report({ warnings, kind }: SvelteCompileWarnings) {
for (const warn of warnings) {
if (warn.code && ignores.includes(warn.code)) {
if (
warn.code &&
(ignores.includes(warn.code) ||
(isGlobalStyleNode(warn.start, warn.end) && unusedSelectorWarnings.includes(warn.code)))
) {
continue;
}
const reportWarn = kind === 'warn' ? transform(warn) : warn;
Expand All @@ -71,6 +93,15 @@ export default createRule('valid-compile', {
}

return {
SvelteStyleElement(node) {
const { attributes } = node.startTag;
for (const attr of attributes) {
if (attr.type === 'SvelteAttribute' && attr.key.name === 'global') {
globalStyleRanges.push([node.loc.start, node.loc.end]);
break;
}
}
},
'Program:exit'() {
const result = getSvelteCompileWarnings(context);
if (ignoreWarnings && result.kind === 'warn') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<style global>
input {
@apply bg-surface-50-900-token h-full overflow-hidden;
}
</style>
Loading