Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions internal/compiler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ func (p *Program) GetIncludeProcessorDiagnostics(sourceFile *ast.SourceFile) []*
if checker.SkipTypeChecking(sourceFile, p.Options(), p, false) {
return nil
}
return p.includeProcessor.getDiagnostics(p).GetDiagnosticsForFile(sourceFile.FileName())
filtered, _ := p.getDiagnosticsWithPrecedingDirectives(sourceFile, p.includeProcessor.getDiagnostics(p).GetDiagnosticsForFile(sourceFile.FileName()))
return filtered
}

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

filtered, directivesByLine := p.getDiagnosticsWithPrecedingDirectives(sourceFile, diags)
for _, directive := range directivesByLine {
// Above we changed all used directive kinds to @ts-ignore, so any @ts-expect-error directives that
// remain are unused and thus errors.
if directive.Kind == ast.CommentDirectiveKindExpectError {
filtered = append(filtered, ast.NewDiagnostic(sourceFile, directive.Loc, diagnostics.Unused_ts_expect_error_directive))
}
}
return filtered
}

func (p *Program) getDiagnosticsWithPrecedingDirectives(sourceFile *ast.SourceFile, diags []*ast.Diagnostic) ([]*ast.Diagnostic, map[int]ast.CommentDirective) {
if len(sourceFile.CommentDirectives) == 0 {
return diags
return diags, nil
}
// Build map of directives by line number
directivesByLine := make(map[int]ast.CommentDirective)
Expand Down Expand Up @@ -1089,14 +1102,7 @@ func (p *Program) getSemanticDiagnosticsForFileNotFilter(ctx context.Context, so
filtered = append(filtered, diagnostic)
}
}
for _, directive := range directivesByLine {
// Above we changed all used directive kinds to @ts-ignore, so any @ts-expect-error directives that
// remain are unused and thus errors.
if directive.Kind == ast.CommentDirectiveKindExpectError {
filtered = append(filtered, ast.NewDiagnostic(sourceFile, directive.Loc, diagnostics.Unused_ts_expect_error_directive))
}
}
return filtered
return filtered, directivesByLine
}

func (p *Program) getDeclarationDiagnosticsForFile(ctx context.Context, sourceFile *ast.SourceFile) []*ast.Diagnostic {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/node_modules/foo/index.d.ts(1,23): error TS2688: Cannot find type definition file for 'cookie-session'.


==== /tsconfig.json (0 errors) ====
{
"compilerOptions": {
"strict": true,
}
}
==== /index.ts (0 errors) ====
import { foo } from 'foo';
const y = foo;

==== /node_modules/foo/index.d.ts (1 errors) ====
/// <reference types="cookie-session"/>
~~~~~~~~~~~~~~
!!! error TS2688: Cannot find type definition file for 'cookie-session'.
export const foo = 1;

==== /node_modules/foo/package.json (0 errors) ====
{
"name": "foo",
"version": "1.0.0",
"types": "index.d.ts"
}
22 changes: 22 additions & 0 deletions testdata/baselines/reference/compiler/processingDiagnostic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/processingDiagnostic.ts] ////

//// [index.d.ts]
/// <reference types="cookie-session"/>
export const foo = 1;

//// [package.json]
{
"name": "foo",
"version": "1.0.0",
"types": "index.d.ts"
}
//// [index.ts]
import { foo } from 'foo';
const y = foo;


//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const foo_1 = require("foo");
const y = foo_1.foo;
15 changes: 15 additions & 0 deletions testdata/baselines/reference/compiler/processingDiagnostic.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [tests/cases/compiler/processingDiagnostic.ts] ////

=== /index.ts ===
import { foo } from 'foo';
>foo : Symbol(foo, Decl(index.ts, 0, 8))

const y = foo;
>y : Symbol(y, Decl(index.ts, 1, 5))
>foo : Symbol(foo, Decl(index.ts, 0, 8))

=== /node_modules/foo/index.d.ts ===
/// <reference types="cookie-session"/>
export const foo = 1;
>foo : Symbol(foo, Decl(index.d.ts, 1, 12))

16 changes: 16 additions & 0 deletions testdata/baselines/reference/compiler/processingDiagnostic.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [tests/cases/compiler/processingDiagnostic.ts] ////

=== /index.ts ===
import { foo } from 'foo';
>foo : 1

const y = foo;
>y : 1
>foo : 1

=== /node_modules/foo/index.d.ts ===
/// <reference types="cookie-session"/>
export const foo = 1;
>foo : 1
>1 : 1

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [tests/cases/compiler/processingDiagnosticTsIgnore.ts] ////

//// [index.d.ts]
// @ts-ignore
/// <reference types="cookie-session"/>
export const foo = 1;

//// [package.json]
{
"name": "foo",
"version": "1.0.0",
"types": "index.d.ts"
}
//// [index.ts]
import { foo } from 'foo';
const y = foo;


//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const foo_1 = require("foo");
const y = foo_1.foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [tests/cases/compiler/processingDiagnosticTsIgnore.ts] ////

=== /index.ts ===
import { foo } from 'foo';
>foo : Symbol(foo, Decl(index.ts, 0, 8))

const y = foo;
>y : Symbol(y, Decl(index.ts, 1, 5))
>foo : Symbol(foo, Decl(index.ts, 0, 8))

=== /node_modules/foo/index.d.ts ===
// @ts-ignore
/// <reference types="cookie-session"/>
export const foo = 1;
>foo : Symbol(foo, Decl(index.d.ts, 2, 12))

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [tests/cases/compiler/processingDiagnosticTsIgnore.ts] ////

=== /index.ts ===
import { foo } from 'foo';
>foo : 1

const y = foo;
>y : 1
>foo : 1

=== /node_modules/foo/index.d.ts ===
// @ts-ignore
/// <reference types="cookie-session"/>
export const foo = 1;
>foo : 1
>1 : 1

20 changes: 20 additions & 0 deletions testdata/tests/cases/compiler/processingDiagnostic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @filename: /node_modules/foo/index.d.ts
/// <reference types="cookie-session"/>
export const foo = 1;

// @filename: /node_modules/foo/package.json
{
"name": "foo",
"version": "1.0.0",
"types": "index.d.ts"
}
// @filename: /index.ts
import { foo } from 'foo';
const y = foo;

// @filename: /tsconfig.json
{
"compilerOptions": {
"strict": true,
}
}
21 changes: 21 additions & 0 deletions testdata/tests/cases/compiler/processingDiagnosticTsIgnore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @filename: /node_modules/foo/index.d.ts
// @ts-ignore
/// <reference types="cookie-session"/>
export const foo = 1;

// @filename: /node_modules/foo/package.json
{
"name": "foo",
"version": "1.0.0",
"types": "index.d.ts"
}
// @filename: /index.ts
import { foo } from 'foo';
const y = foo;

// @filename: /tsconfig.json
{
"compilerOptions": {
"strict": true,
}
}
Loading