@@ -2,6 +2,7 @@ import { createRule } from '../utils/index.js';
22import type { SvelteCompileWarnings , Warning } from '../shared/svelte-compile-warns/index.js' ;
33import { getSvelteCompileWarnings } from '../shared/svelte-compile-warns/index.js' ;
44import { getSourceCode } from '../utils/compat.js' ;
5+ import type { Position } from 'svelte-eslint-parser/lib/ast/common.js' ;
56
67export 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' ) {
0 commit comments