mirror of https://github.com/sveltejs/svelte
chore: css unused selector warnings (#11098)
The character adjustments in the existing warnings are because we remove some tabs from empty lines when initializing the Svelte 5 repo; the warnings were just not checked at that time yet.pull/11093/head
parent
ed9bab9200
commit
d2b6159d2e
@ -0,0 +1,34 @@
|
|||||||
|
import { walk } from 'zimmerframe';
|
||||||
|
import { warn } from '../../../warnings.js';
|
||||||
|
import { is_keyframes_node } from '../../css.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {import('#compiler').Css.StyleSheet} stylesheet
|
||||||
|
* @param {import('../../types.js').RawWarning[]} warnings
|
||||||
|
*/
|
||||||
|
export function warn_unused(stylesheet, warnings) {
|
||||||
|
walk(stylesheet, { warnings, stylesheet }, visitors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @type {import('zimmerframe').Visitors<import('#compiler').Css.Node, { warnings: import('../../types.js').RawWarning[], stylesheet: import('#compiler').Css.StyleSheet }>} */
|
||||||
|
const visitors = {
|
||||||
|
Atrule(node, context) {
|
||||||
|
if (!is_keyframes_node(node)) {
|
||||||
|
context.next();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
PseudoClassSelector(node, context) {
|
||||||
|
if (node.name === 'is' || node.name === 'where') {
|
||||||
|
context.next();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ComplexSelector(node, context) {
|
||||||
|
if (!node.metadata.used) {
|
||||||
|
const content = context.state.stylesheet.content;
|
||||||
|
const text = content.styles.substring(node.start - content.start, node.end - content.start);
|
||||||
|
warn(context.state.warnings, node, context.path, 'css-unused-selector', text);
|
||||||
|
}
|
||||||
|
|
||||||
|
context.next();
|
||||||
|
}
|
||||||
|
};
|
@ -0,0 +1,20 @@
|
|||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
warnings: [
|
||||||
|
{
|
||||||
|
code: 'css-unused-selector',
|
||||||
|
end: {
|
||||||
|
character: 44,
|
||||||
|
column: 14,
|
||||||
|
line: 4
|
||||||
|
},
|
||||||
|
message: 'Unused CSS selector "p[type=\'B\' s]"',
|
||||||
|
start: {
|
||||||
|
character: 31,
|
||||||
|
column: 1,
|
||||||
|
line: 4
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
@ -0,0 +1,20 @@
|
|||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
warnings: [
|
||||||
|
{
|
||||||
|
code: 'css-unused-selector',
|
||||||
|
end: {
|
||||||
|
character: 33,
|
||||||
|
column: 6,
|
||||||
|
line: 6
|
||||||
|
},
|
||||||
|
message: 'Unused CSS selector "x y z"',
|
||||||
|
start: {
|
||||||
|
character: 28,
|
||||||
|
column: 1,
|
||||||
|
line: 6
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
@ -0,0 +1,20 @@
|
|||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
warnings: [
|
||||||
|
{
|
||||||
|
code: 'css-unused-selector',
|
||||||
|
end: {
|
||||||
|
character: 38,
|
||||||
|
column: 11,
|
||||||
|
line: 6
|
||||||
|
},
|
||||||
|
message: 'Unused CSS selector "z"',
|
||||||
|
start: {
|
||||||
|
character: 37,
|
||||||
|
column: 10,
|
||||||
|
line: 6
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
@ -0,0 +1,104 @@
|
|||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
warnings: [
|
||||||
|
{
|
||||||
|
code: 'css-unused-selector',
|
||||||
|
end: {
|
||||||
|
character: 239,
|
||||||
|
column: 13,
|
||||||
|
line: 20
|
||||||
|
},
|
||||||
|
message: 'Unused CSS selector ".unused"',
|
||||||
|
start: {
|
||||||
|
character: 232,
|
||||||
|
column: 6,
|
||||||
|
line: 20
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'css-unused-selector',
|
||||||
|
end: {
|
||||||
|
character: 302,
|
||||||
|
column: 10,
|
||||||
|
line: 27
|
||||||
|
},
|
||||||
|
message: 'Unused CSS selector ".unused"',
|
||||||
|
start: {
|
||||||
|
character: 295,
|
||||||
|
column: 3,
|
||||||
|
line: 27
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'css-unused-selector',
|
||||||
|
end: {
|
||||||
|
character: 328,
|
||||||
|
column: 6,
|
||||||
|
line: 30
|
||||||
|
},
|
||||||
|
message: 'Unused CSS selector ".c"',
|
||||||
|
start: {
|
||||||
|
character: 326,
|
||||||
|
column: 4,
|
||||||
|
line: 30
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'css-unused-selector',
|
||||||
|
end: {
|
||||||
|
character: 381,
|
||||||
|
column: 10,
|
||||||
|
line: 37
|
||||||
|
},
|
||||||
|
message: 'Unused CSS selector ".unused"',
|
||||||
|
start: {
|
||||||
|
character: 374,
|
||||||
|
column: 3,
|
||||||
|
line: 37
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'css-unused-selector',
|
||||||
|
end: {
|
||||||
|
character: 471,
|
||||||
|
column: 7,
|
||||||
|
line: 47
|
||||||
|
},
|
||||||
|
message: 'Unused CSS selector "& &"',
|
||||||
|
start: {
|
||||||
|
character: 468,
|
||||||
|
column: 4,
|
||||||
|
line: 47
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'css-unused-selector',
|
||||||
|
end: {
|
||||||
|
character: 634,
|
||||||
|
column: 5,
|
||||||
|
line: 66
|
||||||
|
},
|
||||||
|
message: 'Unused CSS selector "&.b"',
|
||||||
|
start: {
|
||||||
|
character: 631,
|
||||||
|
column: 2,
|
||||||
|
line: 66
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'css-unused-selector',
|
||||||
|
end: {
|
||||||
|
character: 666,
|
||||||
|
column: 9,
|
||||||
|
line: 70
|
||||||
|
},
|
||||||
|
message: 'Unused CSS selector ".unused"',
|
||||||
|
start: {
|
||||||
|
character: 659,
|
||||||
|
column: 2,
|
||||||
|
line: 70
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
@ -0,0 +1,20 @@
|
|||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
warnings: [
|
||||||
|
{
|
||||||
|
code: 'css-unused-selector',
|
||||||
|
end: {
|
||||||
|
character: 32,
|
||||||
|
column: 3,
|
||||||
|
line: 5
|
||||||
|
},
|
||||||
|
message: 'Unused CSS selector "h2"',
|
||||||
|
start: {
|
||||||
|
character: 30,
|
||||||
|
column: 1,
|
||||||
|
line: 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
Loading…
Reference in new issue