Skip to content

Commit c5446d1

Browse files
authored
don't emit "unused @ts-expect-error" in unchecked js files (microsoft#40046)
* don't emit unused ts-expect-error in unchecked js files * simplify code
1 parent 2ff70f0 commit c5446d1

12 files changed

+103
-3
lines changed

src/compiler/program.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,13 +1747,13 @@ namespace ts {
17471747
const bindDiagnostics: readonly Diagnostic[] = includeBindAndCheckDiagnostics ? sourceFile.bindDiagnostics : emptyArray;
17481748
const checkDiagnostics = includeBindAndCheckDiagnostics ? typeChecker.getDiagnostics(sourceFile, cancellationToken) : emptyArray;
17491749

1750-
return getMergedBindAndCheckDiagnostics(sourceFile, bindDiagnostics, checkDiagnostics, isCheckJs ? sourceFile.jsDocDiagnostics : undefined);
1750+
return getMergedBindAndCheckDiagnostics(sourceFile, includeBindAndCheckDiagnostics, bindDiagnostics, checkDiagnostics, isCheckJs ? sourceFile.jsDocDiagnostics : undefined);
17511751
});
17521752
}
17531753

1754-
function getMergedBindAndCheckDiagnostics(sourceFile: SourceFile, ...allDiagnostics: (readonly Diagnostic[] | undefined)[]) {
1754+
function getMergedBindAndCheckDiagnostics(sourceFile: SourceFile, includeBindAndCheckDiagnostics: boolean, ...allDiagnostics: (readonly Diagnostic[] | undefined)[]) {
17551755
const flatDiagnostics = flatten(allDiagnostics);
1756-
if (!sourceFile.commentDirectives?.length) {
1756+
if (!includeBindAndCheckDiagnostics || !sourceFile.commentDirectives?.length) {
17571757
return flatDiagnostics;
17581758
}
17591759

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/conformance/directives/a.js(3,1): error TS2578: Unused '@ts-expect-error' directive.
2+
3+
4+
==== tests/cases/conformance/directives/a.js (1 errors) ====
5+
// there should be a "Unused @ts-expect-error" error since js files are being checked
6+
7+
// @ts-expect-error
8+
~~~~~~~~~~~~~~~~~~~
9+
!!! error TS2578: Unused '@ts-expect-error' directive.
10+
const a = 1;
11+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/directives/a.js ===
2+
// there should be a "Unused @ts-expect-error" error since js files are being checked
3+
4+
// @ts-expect-error
5+
const a = 1;
6+
>a : Symbol(a, Decl(a.js, 3, 5))
7+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/directives/a.js ===
2+
// there should be a "Unused @ts-expect-error" error since js files are being checked
3+
4+
// @ts-expect-error
5+
const a = 1;
6+
>a : 1
7+
>1 : 1
8+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/directives/a.js ===
2+
// there should not be a "Unused @ts-expect-error" error since js files are not being checked
3+
4+
// @ts-expect-error
5+
const a = 1;
6+
>a : Symbol(a, Decl(a.js, 3, 5))
7+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/directives/a.js ===
2+
// there should not be a "Unused @ts-expect-error" error since js files are not being checked
3+
4+
// @ts-expect-error
5+
const a = 1;
6+
>a : 1
7+
>1 : 1
8+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [ts-expect-error-nocheck.ts]
2+
// @ts-nocheck
3+
4+
// there should not be a "Unused @ts-expect-error" error due to the // @ts-nocheck
5+
6+
// @ts-expect-error
7+
const a = 1;
8+
9+
10+
//// [ts-expect-error-nocheck.js]
11+
// @ts-nocheck
12+
// there should not be a "Unused @ts-expect-error" error due to the // @ts-nocheck
13+
// @ts-expect-error
14+
var a = 1;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/conformance/directives/ts-expect-error-nocheck.ts ===
2+
// @ts-nocheck
3+
4+
// there should not be a "Unused @ts-expect-error" error due to the // @ts-nocheck
5+
6+
// @ts-expect-error
7+
const a = 1;
8+
>a : Symbol(a, Decl(ts-expect-error-nocheck.ts, 5, 5))
9+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/conformance/directives/ts-expect-error-nocheck.ts ===
2+
// @ts-nocheck
3+
4+
// there should not be a "Unused @ts-expect-error" error due to the // @ts-nocheck
5+
6+
// @ts-expect-error
7+
const a = 1;
8+
>a : 1
9+
>1 : 1
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @noEmit: true
4+
5+
// @fileName: a.js
6+
7+
// there should be a "Unused @ts-expect-error" error since js files are being checked
8+
9+
// @ts-expect-error
10+
const a = 1;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @allowJs: true
2+
// @checkJs: false
3+
// @noEmit: true
4+
5+
// @fileName: a.js
6+
7+
// there should not be a "Unused @ts-expect-error" error since js files are not being checked
8+
9+
// @ts-expect-error
10+
const a = 1;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @ts-nocheck
2+
3+
// there should not be a "Unused @ts-expect-error" error due to the // @ts-nocheck
4+
5+
// @ts-expect-error
6+
const a = 1;

0 commit comments

Comments
 (0)