diff --git a/internal/compiler/program.go b/internal/compiler/program.go index 128b508a96..e1c18b2b6b 100644 --- a/internal/compiler/program.go +++ b/internal/compiler/program.go @@ -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 { @@ -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) @@ -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 { diff --git a/testdata/baselines/reference/compiler/processingDiagnostic.errors.txt b/testdata/baselines/reference/compiler/processingDiagnostic.errors.txt new file mode 100644 index 0000000000..625ef6d8ac --- /dev/null +++ b/testdata/baselines/reference/compiler/processingDiagnostic.errors.txt @@ -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) ==== + /// + ~~~~~~~~~~~~~~ +!!! 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" + } \ No newline at end of file diff --git a/testdata/baselines/reference/compiler/processingDiagnostic.js b/testdata/baselines/reference/compiler/processingDiagnostic.js new file mode 100644 index 0000000000..3977ef89e5 --- /dev/null +++ b/testdata/baselines/reference/compiler/processingDiagnostic.js @@ -0,0 +1,22 @@ +//// [tests/cases/compiler/processingDiagnostic.ts] //// + +//// [index.d.ts] +/// +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; diff --git a/testdata/baselines/reference/compiler/processingDiagnostic.symbols b/testdata/baselines/reference/compiler/processingDiagnostic.symbols new file mode 100644 index 0000000000..f9a4c949cc --- /dev/null +++ b/testdata/baselines/reference/compiler/processingDiagnostic.symbols @@ -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 === +/// +export const foo = 1; +>foo : Symbol(foo, Decl(index.d.ts, 1, 12)) + diff --git a/testdata/baselines/reference/compiler/processingDiagnostic.types b/testdata/baselines/reference/compiler/processingDiagnostic.types new file mode 100644 index 0000000000..16dea9a2ce --- /dev/null +++ b/testdata/baselines/reference/compiler/processingDiagnostic.types @@ -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 === +/// +export const foo = 1; +>foo : 1 +>1 : 1 + diff --git a/testdata/baselines/reference/compiler/processingDiagnosticTsIgnore.js b/testdata/baselines/reference/compiler/processingDiagnosticTsIgnore.js new file mode 100644 index 0000000000..6d9dcc4b20 --- /dev/null +++ b/testdata/baselines/reference/compiler/processingDiagnosticTsIgnore.js @@ -0,0 +1,23 @@ +//// [tests/cases/compiler/processingDiagnosticTsIgnore.ts] //// + +//// [index.d.ts] +// @ts-ignore +/// +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; diff --git a/testdata/baselines/reference/compiler/processingDiagnosticTsIgnore.symbols b/testdata/baselines/reference/compiler/processingDiagnosticTsIgnore.symbols new file mode 100644 index 0000000000..56e272d005 --- /dev/null +++ b/testdata/baselines/reference/compiler/processingDiagnosticTsIgnore.symbols @@ -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 +/// +export const foo = 1; +>foo : Symbol(foo, Decl(index.d.ts, 2, 12)) + diff --git a/testdata/baselines/reference/compiler/processingDiagnosticTsIgnore.types b/testdata/baselines/reference/compiler/processingDiagnosticTsIgnore.types new file mode 100644 index 0000000000..1d3f9ce4f9 --- /dev/null +++ b/testdata/baselines/reference/compiler/processingDiagnosticTsIgnore.types @@ -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 +/// +export const foo = 1; +>foo : 1 +>1 : 1 + diff --git a/testdata/tests/cases/compiler/processingDiagnostic.ts b/testdata/tests/cases/compiler/processingDiagnostic.ts new file mode 100644 index 0000000000..2ffc9f4adc --- /dev/null +++ b/testdata/tests/cases/compiler/processingDiagnostic.ts @@ -0,0 +1,20 @@ +// @filename: /node_modules/foo/index.d.ts +/// +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, + } +} \ No newline at end of file diff --git a/testdata/tests/cases/compiler/processingDiagnosticTsIgnore.ts b/testdata/tests/cases/compiler/processingDiagnosticTsIgnore.ts new file mode 100644 index 0000000000..166595c816 --- /dev/null +++ b/testdata/tests/cases/compiler/processingDiagnosticTsIgnore.ts @@ -0,0 +1,21 @@ +// @filename: /node_modules/foo/index.d.ts +// @ts-ignore +/// +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, + } +} \ No newline at end of file