Skip to content

Commit 0a3c816

Browse files
authored
Fix error filtering by comment directives for program diagnostics (#1649)
1 parent 1182233 commit 0a3c816

10 files changed

+191
-10
lines changed

internal/compiler/program.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ func (p *Program) GetIncludeProcessorDiagnostics(sourceFile *ast.SourceFile) []*
437437
if checker.SkipTypeChecking(sourceFile, p.Options(), p, false) {
438438
return nil
439439
}
440-
return p.includeProcessor.getDiagnostics(p).GetDiagnosticsForFile(sourceFile.FileName())
440+
filtered, _ := p.getDiagnosticsWithPrecedingDirectives(sourceFile, p.includeProcessor.getDiagnostics(p).GetDiagnosticsForFile(sourceFile.FileName()))
441+
return filtered
441442
}
442443

443444
func (p *Program) getSourceFilesToEmit(targetSourceFile *ast.SourceFile, forceDtsEmit bool) []*ast.SourceFile {
@@ -1062,8 +1063,20 @@ func (p *Program) getSemanticDiagnosticsForFileNotFilter(ctx context.Context, so
10621063
})
10631064
}
10641065

1066+
filtered, directivesByLine := p.getDiagnosticsWithPrecedingDirectives(sourceFile, diags)
1067+
for _, directive := range directivesByLine {
1068+
// Above we changed all used directive kinds to @ts-ignore, so any @ts-expect-error directives that
1069+
// remain are unused and thus errors.
1070+
if directive.Kind == ast.CommentDirectiveKindExpectError {
1071+
filtered = append(filtered, ast.NewDiagnostic(sourceFile, directive.Loc, diagnostics.Unused_ts_expect_error_directive))
1072+
}
1073+
}
1074+
return filtered
1075+
}
1076+
1077+
func (p *Program) getDiagnosticsWithPrecedingDirectives(sourceFile *ast.SourceFile, diags []*ast.Diagnostic) ([]*ast.Diagnostic, map[int]ast.CommentDirective) {
10651078
if len(sourceFile.CommentDirectives) == 0 {
1066-
return diags
1079+
return diags, nil
10671080
}
10681081
// Build map of directives by line number
10691082
directivesByLine := make(map[int]ast.CommentDirective)
@@ -1093,14 +1106,7 @@ func (p *Program) getSemanticDiagnosticsForFileNotFilter(ctx context.Context, so
10931106
filtered = append(filtered, diagnostic)
10941107
}
10951108
}
1096-
for _, directive := range directivesByLine {
1097-
// Above we changed all used directive kinds to @ts-ignore, so any @ts-expect-error directives that
1098-
// remain are unused and thus errors.
1099-
if directive.Kind == ast.CommentDirectiveKindExpectError {
1100-
filtered = append(filtered, ast.NewDiagnostic(sourceFile, directive.Loc, diagnostics.Unused_ts_expect_error_directive))
1101-
}
1102-
}
1103-
return filtered
1109+
return filtered, directivesByLine
11041110
}
11051111

11061112
func (p *Program) getDeclarationDiagnosticsForFile(ctx context.Context, sourceFile *ast.SourceFile) []*ast.Diagnostic {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/node_modules/foo/index.d.ts(1,23): error TS2688: Cannot find type definition file for 'cookie-session'.
2+
3+
4+
==== /tsconfig.json (0 errors) ====
5+
{
6+
"compilerOptions": {
7+
"strict": true,
8+
}
9+
}
10+
==== /index.ts (0 errors) ====
11+
import { foo } from 'foo';
12+
const y = foo;
13+
14+
==== /node_modules/foo/index.d.ts (1 errors) ====
15+
/// <reference types="cookie-session"/>
16+
~~~~~~~~~~~~~~
17+
!!! error TS2688: Cannot find type definition file for 'cookie-session'.
18+
export const foo = 1;
19+
20+
==== /node_modules/foo/package.json (0 errors) ====
21+
{
22+
"name": "foo",
23+
"version": "1.0.0",
24+
"types": "index.d.ts"
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/processingDiagnostic.ts] ////
2+
3+
//// [index.d.ts]
4+
/// <reference types="cookie-session"/>
5+
export const foo = 1;
6+
7+
//// [package.json]
8+
{
9+
"name": "foo",
10+
"version": "1.0.0",
11+
"types": "index.d.ts"
12+
}
13+
//// [index.ts]
14+
import { foo } from 'foo';
15+
const y = foo;
16+
17+
18+
//// [index.js]
19+
"use strict";
20+
Object.defineProperty(exports, "__esModule", { value: true });
21+
const foo_1 = require("foo");
22+
const y = foo_1.foo;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/compiler/processingDiagnostic.ts] ////
2+
3+
=== /index.ts ===
4+
import { foo } from 'foo';
5+
>foo : Symbol(foo, Decl(index.ts, 0, 8))
6+
7+
const y = foo;
8+
>y : Symbol(y, Decl(index.ts, 1, 5))
9+
>foo : Symbol(foo, Decl(index.ts, 0, 8))
10+
11+
=== /node_modules/foo/index.d.ts ===
12+
/// <reference types="cookie-session"/>
13+
export const foo = 1;
14+
>foo : Symbol(foo, Decl(index.d.ts, 1, 12))
15+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/compiler/processingDiagnostic.ts] ////
2+
3+
=== /index.ts ===
4+
import { foo } from 'foo';
5+
>foo : 1
6+
7+
const y = foo;
8+
>y : 1
9+
>foo : 1
10+
11+
=== /node_modules/foo/index.d.ts ===
12+
/// <reference types="cookie-session"/>
13+
export const foo = 1;
14+
>foo : 1
15+
>1 : 1
16+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/compiler/processingDiagnosticTsIgnore.ts] ////
2+
3+
//// [index.d.ts]
4+
// @ts-ignore
5+
/// <reference types="cookie-session"/>
6+
export const foo = 1;
7+
8+
//// [package.json]
9+
{
10+
"name": "foo",
11+
"version": "1.0.0",
12+
"types": "index.d.ts"
13+
}
14+
//// [index.ts]
15+
import { foo } from 'foo';
16+
const y = foo;
17+
18+
19+
//// [index.js]
20+
"use strict";
21+
Object.defineProperty(exports, "__esModule", { value: true });
22+
const foo_1 = require("foo");
23+
const y = foo_1.foo;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/compiler/processingDiagnosticTsIgnore.ts] ////
2+
3+
=== /index.ts ===
4+
import { foo } from 'foo';
5+
>foo : Symbol(foo, Decl(index.ts, 0, 8))
6+
7+
const y = foo;
8+
>y : Symbol(y, Decl(index.ts, 1, 5))
9+
>foo : Symbol(foo, Decl(index.ts, 0, 8))
10+
11+
=== /node_modules/foo/index.d.ts ===
12+
// @ts-ignore
13+
/// <reference types="cookie-session"/>
14+
export const foo = 1;
15+
>foo : Symbol(foo, Decl(index.d.ts, 2, 12))
16+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/processingDiagnosticTsIgnore.ts] ////
2+
3+
=== /index.ts ===
4+
import { foo } from 'foo';
5+
>foo : 1
6+
7+
const y = foo;
8+
>y : 1
9+
>foo : 1
10+
11+
=== /node_modules/foo/index.d.ts ===
12+
// @ts-ignore
13+
/// <reference types="cookie-session"/>
14+
export const foo = 1;
15+
>foo : 1
16+
>1 : 1
17+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// @filename: /node_modules/foo/index.d.ts
2+
/// <reference types="cookie-session"/>
3+
export const foo = 1;
4+
5+
// @filename: /node_modules/foo/package.json
6+
{
7+
"name": "foo",
8+
"version": "1.0.0",
9+
"types": "index.d.ts"
10+
}
11+
// @filename: /index.ts
12+
import { foo } from 'foo';
13+
const y = foo;
14+
15+
// @filename: /tsconfig.json
16+
{
17+
"compilerOptions": {
18+
"strict": true,
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @filename: /node_modules/foo/index.d.ts
2+
// @ts-ignore
3+
/// <reference types="cookie-session"/>
4+
export const foo = 1;
5+
6+
// @filename: /node_modules/foo/package.json
7+
{
8+
"name": "foo",
9+
"version": "1.0.0",
10+
"types": "index.d.ts"
11+
}
12+
// @filename: /index.ts
13+
import { foo } from 'foo';
14+
const y = foo;
15+
16+
// @filename: /tsconfig.json
17+
{
18+
"compilerOptions": {
19+
"strict": true,
20+
}
21+
}

0 commit comments

Comments
 (0)