Skip to content

Commit cf54019

Browse files
committed
Always get diagnostics when emitting irrespective of whether its declaration only emit
The diagnostics reporting and expression resolution caching is quite intermingled at present. Hence when we tried to get the declaration output without getting diagnostics, the resolution for functions return expression is cached but errors arent reported Symbols arent marked as referenced. So at later time when trying to get the diagnostics since the expression resolution is cached, it doesnt even go through all checks For now get diagnostics irrespective of declaration only output to avoid this issue. Fixes #21518
1 parent d0ab164 commit cf54019

4 files changed

Lines changed: 31 additions & 6 deletions

File tree

src/compiler/checker.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -749,12 +749,10 @@ namespace ts {
749749
return _jsxNamespace;
750750
}
751751

752-
function getEmitResolver(sourceFile: SourceFile, cancellationToken: CancellationToken, ignoreDiagnostics?: boolean) {
752+
function getEmitResolver(sourceFile: SourceFile, cancellationToken: CancellationToken) {
753753
// Ensure we have all the type information in place for this file so that all the
754754
// emitter questions of this resolver will return the right information.
755-
if (!ignoreDiagnostics) {
756-
getDiagnostics(sourceFile, cancellationToken);
757-
}
755+
getDiagnostics(sourceFile, cancellationToken);
758756
return emitResolver;
759757
}
760758

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ namespace ts {
11791179
// This is because in the -out scenario all files need to be emitted, and therefore all
11801180
// files need to be type checked. And the way to specify that all files need to be type
11811181
// checked is to not pass the file to getEmitResolver.
1182-
const emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver((options.outFile || options.out) ? undefined : sourceFile, cancellationToken, emitOnlyDtsFiles);
1182+
const emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver((options.outFile || options.out) ? undefined : sourceFile, cancellationToken);
11831183

11841184
performance.mark("beforeEmit");
11851185

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2892,7 +2892,7 @@ namespace ts {
28922892
// Should not be called directly. Should only be accessed through the Program instance.
28932893
/* @internal */ getDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
28942894
/* @internal */ getGlobalDiagnostics(): Diagnostic[];
2895-
/* @internal */ getEmitResolver(sourceFile?: SourceFile, cancellationToken?: CancellationToken, ignoreDiagnostics?: boolean): EmitResolver;
2895+
/* @internal */ getEmitResolver(sourceFile?: SourceFile, cancellationToken?: CancellationToken): EmitResolver;
28962896

28972897
/* @internal */ getNodeCount(): number;
28982898
/* @internal */ getIdentifierCount(): number;

src/harness/unittests/tscWatchMode.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,33 @@ namespace ts.tscWatch {
10861086
// This should be 0
10871087
host.checkTimeoutQueueLengthAndRun(0);
10881088
});
1089+
1090+
it("shouldnt report error about unused function incorrectly when file changes from global to module", () => {
1091+
const getFileContent = (asModule: boolean) => `
1092+
function one() {}
1093+
${asModule ? "export " : ""}function two() {
1094+
return function three() {
1095+
one();
1096+
}
1097+
}`;
1098+
const file: FileOrFolder = {
1099+
path: "/a/b/file.ts",
1100+
content: getFileContent(/*asModule*/ false)
1101+
};
1102+
const files = [file, libFile];
1103+
const host = createWatchedSystem(files);
1104+
const watch = createWatchOfFilesAndCompilerOptions([file.path], host, {
1105+
noUnusedLocals: true
1106+
});
1107+
checkProgramActualFiles(watch(), files.map(file => file.path));
1108+
checkOutputErrors(host, [], ExpectedOutputErrorsPosition.AfterCompilationStarting);
1109+
1110+
file.content = getFileContent(/*asModule*/ true);
1111+
host.reloadFS(files);
1112+
host.runQueuedTimeoutCallbacks();
1113+
checkProgramActualFiles(watch(), files.map(file => file.path));
1114+
checkOutputErrors(host, [], ExpectedOutputErrorsPosition.AfterFileChangeDetected);
1115+
});
10891116
});
10901117

10911118
describe("tsc-watch emit with outFile or out setting", () => {

0 commit comments

Comments
 (0)