@@ -2,7 +2,7 @@ import type { AST } from "svelte-eslint-parser"
2
2
import * as compiler from "svelte/compiler"
3
3
import type { SourceMapMappings } from "@jridgewell/sourcemap-codec"
4
4
import { decode } from "@jridgewell/sourcemap-codec"
5
- import type { RuleContext } from "../../types"
5
+ import type { ASTNodeWithParent , RuleContext } from "../../types"
6
6
import { LinesAndColumns } from "../../utils/lines-and-columns"
7
7
import type { TransformResult } from "./transform/types"
8
8
import {
@@ -21,6 +21,18 @@ import { getLangValue } from "../../utils/ast-utils"
21
21
import path from "path"
22
22
import fs from "fs"
23
23
24
+ type WarningTargetNode =
25
+ | ( AST . SvelteProgram & ASTNodeWithParent )
26
+ | ( AST . SvelteElement & ASTNodeWithParent )
27
+ | ( AST . SvelteStyleElement & ASTNodeWithParent )
28
+ | ( AST . SvelteScriptElement [ "body" ] [ number ] & ASTNodeWithParent )
29
+ type IgnoreTargetNode =
30
+ | WarningTargetNode
31
+ | ( AST . SvelteIfBlock & ASTNodeWithParent )
32
+ | ( AST . SvelteKeyBlock & ASTNodeWithParent )
33
+ | ( AST . SvelteEachBlock & ASTNodeWithParent )
34
+ | ( AST . SvelteAwaitBlock & ASTNodeWithParent )
35
+
24
36
const STYLE_TRANSFORMS : Record <
25
37
string ,
26
38
typeof transformWithPostCSS | undefined
@@ -477,21 +489,22 @@ function processIgnore(
477
489
if ( ! warning . code ) {
478
490
continue
479
491
}
480
- const node = getWarningNode ( warning )
481
- if ( ! node ) {
482
- continue
483
- }
484
- for ( const comment of extractLeadingComments ( context , node ) . reverse ( ) ) {
485
- const ignoreItem = ignoreComments . find (
486
- ( item ) => item . token === comment && item . code === warning . code ,
487
- )
488
- if ( ignoreItem ) {
489
- unusedIgnores . delete ( ignoreItem )
490
- remainingWarning . delete ( warning )
491
- break
492
+ let node : IgnoreTargetNode | null = getWarningNode ( warning )
493
+ while ( node ) {
494
+ for ( const comment of extractLeadingComments ( context , node ) . reverse ( ) ) {
495
+ const ignoreItem = ignoreComments . find (
496
+ ( item ) => item . token === comment && item . code === warning . code ,
497
+ )
498
+ if ( ignoreItem ) {
499
+ unusedIgnores . delete ( ignoreItem )
500
+ remainingWarning . delete ( warning )
501
+ break
502
+ }
492
503
}
504
+ node = getIgnoreParent ( node )
493
505
}
494
506
}
507
+
495
508
// Stripped styles are ignored from compilation and cannot determine css errors.
496
509
for ( const node of stripStyleElements ) {
497
510
for ( const comment of extractLeadingComments ( context , node ) . reverse ( ) ) {
@@ -509,8 +522,42 @@ function processIgnore(
509
522
unusedIgnores : [ ...unusedIgnores ] ,
510
523
}
511
524
525
+ /** Get ignore target parent node */
526
+ function getIgnoreParent ( node : IgnoreTargetNode ) : IgnoreTargetNode | null {
527
+ if (
528
+ node . type !== "SvelteElement" &&
529
+ node . type !== "SvelteIfBlock" &&
530
+ node . type !== "SvelteKeyBlock" &&
531
+ node . type !== "SvelteEachBlock" &&
532
+ node . type !== "SvelteAwaitBlock"
533
+ ) {
534
+ return null
535
+ }
536
+ const parent = node . parent
537
+ if ( parent . type === "SvelteElseBlock" ) {
538
+ return parent . parent // SvelteIfBlock or SvelteEachBlock
539
+ }
540
+ if (
541
+ parent . type === "SvelteAwaitPendingBlock" ||
542
+ parent . type === "SvelteAwaitThenBlock" ||
543
+ parent . type === "SvelteAwaitCatchBlock"
544
+ ) {
545
+ return parent . parent // SvelteAwaitBlock
546
+ }
547
+ if (
548
+ parent . type !== "SvelteElement" &&
549
+ parent . type !== "SvelteIfBlock" &&
550
+ parent . type !== "SvelteKeyBlock" &&
551
+ parent . type !== "SvelteEachBlock"
552
+ // && parent.type !== "SvelteAwaitBlock"
553
+ ) {
554
+ return null
555
+ }
556
+ return parent
557
+ }
558
+
512
559
/** Get warning node */
513
- function getWarningNode ( warning : Warning ) {
560
+ function getWarningNode ( warning : Warning ) : WarningTargetNode | null {
514
561
const indexes = getWarningIndexes ( warning )
515
562
if ( indexes . start != null ) {
516
563
const node = getWarningTargetNodeFromIndex ( indexes . start )
@@ -534,7 +581,9 @@ function processIgnore(
534
581
/**
535
582
* Get warning target node from the given index
536
583
*/
537
- function getWarningTargetNodeFromIndex ( index : number ) {
584
+ function getWarningTargetNodeFromIndex (
585
+ index : number ,
586
+ ) : WarningTargetNode | null {
538
587
let targetNode = sourceCode . getNodeByRangeIndex ( index )
539
588
while ( targetNode ) {
540
589
if (
@@ -548,7 +597,7 @@ function processIgnore(
548
597
targetNode . parent . type === "Program" ||
549
598
targetNode . parent . type === "SvelteScriptElement"
550
599
) {
551
- return targetNode
600
+ return targetNode as WarningTargetNode
552
601
}
553
602
} else {
554
603
return null
0 commit comments