mirror of https://github.com/sveltejs/svelte
parent
8e31c4e7b6
commit
116cdafdb9
@ -0,0 +1,30 @@
|
||||
import { walk } from 'zimmerframe';
|
||||
import { warn } from '../../../warnings.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 = {
|
||||
ComplexSelector(node, context) {
|
||||
if (!node.metadata.used) {
|
||||
for (let i = context.path.length - 1; i >= 0; i--) {
|
||||
const parent = context.path[i];
|
||||
if (parent.type === 'RelativeSelector' && parent.metadata.is_global) {
|
||||
return; // no need to recurse; everything below is global, too
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
Loading…
Reference in new issue