Skip to content

Commit cb8b412

Browse files
committed
Fix the error filtering
1 parent 5d1d69a commit cb8b412

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

internal/compiler/program.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ func (p *Program) GetIncludeProcessorDiagnostics(sourceFile *ast.SourceFile) []*
433433
if checker.SkipTypeChecking(sourceFile, p.Options(), p, false) {
434434
return nil
435435
}
436-
return p.includeProcessor.getDiagnostics(p).GetDiagnosticsForFile(sourceFile.FileName())
436+
filtered, _ := p.getDiagnosticsWithPrecedingDirectives(sourceFile, p.includeProcessor.getDiagnostics(p).GetDiagnosticsForFile(sourceFile.FileName()))
437+
return filtered
437438
}
438439

439440
func (p *Program) getSourceFilesToEmit(targetSourceFile *ast.SourceFile, forceDtsEmit bool) []*ast.SourceFile {
@@ -1058,8 +1059,20 @@ func (p *Program) getSemanticDiagnosticsForFileNotFilter(ctx context.Context, so
10581059
})
10591060
}
10601061

1061-
if len(sourceFile.CommentDirectives) == 0 {
1062-
return diags
1062+
filtered, directivesByLine := p.getDiagnosticsWithPrecedingDirectives(sourceFile, diags)
1063+
for _, directive := range directivesByLine {
1064+
// Above we changed all used directive kinds to @ts-ignore, so any @ts-expect-error directives that
1065+
// remain are unused and thus errors.
1066+
if directive.Kind == ast.CommentDirectiveKindExpectError {
1067+
filtered = append(filtered, ast.NewDiagnostic(sourceFile, directive.Loc, diagnostics.Unused_ts_expect_error_directive))
1068+
}
1069+
}
1070+
return filtered
1071+
}
1072+
1073+
func (p *Program) getDiagnosticsWithPrecedingDirectives(sourceFile *ast.SourceFile, diags []*ast.Diagnostic) ([]*ast.Diagnostic, map[int]ast.CommentDirective) {
1074+
if len(diags) == 0 || len(sourceFile.CommentDirectives) == 0 {
1075+
return diags, nil
10631076
}
10641077
// Build map of directives by line number
10651078
directivesByLine := make(map[int]ast.CommentDirective)
@@ -1089,14 +1102,7 @@ func (p *Program) getSemanticDiagnosticsForFileNotFilter(ctx context.Context, so
10891102
filtered = append(filtered, diagnostic)
10901103
}
10911104
}
1092-
for _, directive := range directivesByLine {
1093-
// Above we changed all used directive kinds to @ts-ignore, so any @ts-expect-error directives that
1094-
// remain are unused and thus errors.
1095-
if directive.Kind == ast.CommentDirectiveKindExpectError {
1096-
filtered = append(filtered, ast.NewDiagnostic(sourceFile, directive.Loc, diagnostics.Unused_ts_expect_error_directive))
1097-
}
1098-
}
1099-
return filtered
1105+
return filtered, directivesByLine
11001106
}
11011107

11021108
func (p *Program) getDeclarationDiagnosticsForFile(ctx context.Context, sourceFile *ast.SourceFile) []*ast.Diagnostic {

0 commit comments

Comments
 (0)