@@ -2,6 +2,7 @@ import { createRule } from '../utils/index.js';
2
2
import type { SvelteCompileWarnings , Warning } from '../shared/svelte-compile-warns/index.js' ;
3
3
import { getSvelteCompileWarnings } from '../shared/svelte-compile-warns/index.js' ;
4
4
import { getSourceCode } from '../utils/compat.js' ;
5
+ import type { Position } from 'svelte-eslint-parser/lib/ast/common.js' ;
5
6
6
7
export default createRule ( 'valid-compile' , {
7
8
meta : {
@@ -48,12 +49,33 @@ export default createRule('valid-compile', {
48
49
'invalid-slot-name'
49
50
] ;
50
51
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
+
51
69
/**
52
70
* report
53
71
*/
54
72
function report ( { warnings, kind } : SvelteCompileWarnings ) {
55
73
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
+ ) {
57
79
continue ;
58
80
}
59
81
const reportWarn = kind === 'warn' ? transform ( warn ) : warn ;
@@ -71,6 +93,15 @@ export default createRule('valid-compile', {
71
93
}
72
94
73
95
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
+ } ,
74
105
'Program:exit' ( ) {
75
106
const result = getSvelteCompileWarnings ( context ) ;
76
107
if ( ignoreWarnings && result . kind === 'warn' ) {
0 commit comments