Skip to content

Commit 6d23224

Browse files
author
Andy Hanson
committed
getSemanticDiagnostics may return global diagnostics
1 parent 0301fed commit 6d23224

File tree

6 files changed

+27
-37
lines changed

6 files changed

+27
-37
lines changed

src/compiler/program.ts

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ namespace ts {
393393
return resolutions;
394394
}
395395

396-
interface DiagnosticCache {
397-
perFile?: Map<DiagnosticWithLocation[]>;
396+
interface DiagnosticCache<T extends Diagnostic> {
397+
perFile?: Map<T[]>;
398398
allDiagnostics?: Diagnostic[];
399399
}
400400

@@ -500,8 +500,8 @@ namespace ts {
500500
let classifiableNames: UnderscoreEscapedMap<true>;
501501
let modifiedFilePaths: Path[] | undefined;
502502

503-
const cachedSemanticDiagnosticsForFile: DiagnosticCache = {};
504-
const cachedDeclarationDiagnosticsForFile: DiagnosticCache = {};
503+
const cachedSemanticDiagnosticsForFile: DiagnosticCache<Diagnostic> = {};
504+
const cachedDeclarationDiagnosticsForFile: DiagnosticCache<DiagnosticWithLocation> = {};
505505

506506
let resolvedTypeReferenceDirectives = createMap<ResolvedTypeReferenceDirective>();
507507
let fileProcessingDiagnostics = createDiagnosticCollection();
@@ -1209,10 +1209,10 @@ namespace ts {
12091209
return filesByName.get(path);
12101210
}
12111211

1212-
function getDiagnosticsHelper(
1212+
function getDiagnosticsHelper<T extends Diagnostic>(
12131213
sourceFile: SourceFile,
1214-
getDiagnostics: (sourceFile: SourceFile, cancellationToken: CancellationToken) => ReadonlyArray<DiagnosticWithLocation>,
1215-
cancellationToken: CancellationToken): ReadonlyArray<DiagnosticWithLocation> {
1214+
getDiagnostics: (sourceFile: SourceFile, cancellationToken: CancellationToken) => ReadonlyArray<T>,
1215+
cancellationToken: CancellationToken): ReadonlyArray<T> {
12161216
if (sourceFile) {
12171217
return getDiagnostics(sourceFile, cancellationToken);
12181218
}
@@ -1228,7 +1228,7 @@ namespace ts {
12281228
return getDiagnosticsHelper(sourceFile, getSyntacticDiagnosticsForFile, cancellationToken);
12291229
}
12301230

1231-
function getSemanticDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): ReadonlyArray<DiagnosticWithLocation> {
1231+
function getSemanticDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): ReadonlyArray<Diagnostic> {
12321232
return getDiagnosticsHelper(sourceFile, getSemanticDiagnosticsForFile, cancellationToken);
12331233
}
12341234

@@ -1278,11 +1278,11 @@ namespace ts {
12781278
}
12791279
}
12801280

1281-
function getSemanticDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): ReadonlyArray<DiagnosticWithLocation> {
1281+
function getSemanticDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): ReadonlyArray<Diagnostic> {
12821282
return getAndCacheDiagnostics(sourceFile, cancellationToken, cachedSemanticDiagnosticsForFile, getSemanticDiagnosticsForFileNoCache);
12831283
}
12841284

1285-
function getSemanticDiagnosticsForFileNoCache(sourceFile: SourceFile, cancellationToken: CancellationToken): DiagnosticWithLocation[] {
1285+
function getSemanticDiagnosticsForFileNoCache(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
12861286
return runWithCancellationToken(() => {
12871287
// If skipLibCheck is enabled, skip reporting errors if file is a declaration file.
12881288
// If skipDefaultLibCheck is enabled, skip reporting errors if file contains a
@@ -1299,16 +1299,15 @@ namespace ts {
12991299
// By default, only type-check .ts, .tsx, and 'External' files (external files are added by plugins)
13001300
const includeBindAndCheckDiagnostics = sourceFile.scriptKind === ScriptKind.TS || sourceFile.scriptKind === ScriptKind.TSX ||
13011301
sourceFile.scriptKind === ScriptKind.External || isCheckJs;
1302-
const bindDiagnostics = includeBindAndCheckDiagnostics ? sourceFile.bindDiagnostics : emptyArray;
1303-
// TODO: GH#18217
1304-
const checkDiagnostics = includeBindAndCheckDiagnostics ? typeChecker.getDiagnostics(sourceFile, cancellationToken) as ReadonlyArray<DiagnosticWithLocation> : emptyArray;
1302+
const bindDiagnostics: ReadonlyArray<Diagnostic> = includeBindAndCheckDiagnostics ? sourceFile.bindDiagnostics : emptyArray;
1303+
const checkDiagnostics = includeBindAndCheckDiagnostics ? typeChecker.getDiagnostics(sourceFile, cancellationToken) : emptyArray;
13051304
const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName);
13061305
const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName);
13071306
let diagnostics = bindDiagnostics.concat(checkDiagnostics, fileProcessingDiagnosticsInFile, programDiagnosticsInFile);
13081307
if (isCheckJs) {
13091308
diagnostics = concatenate(diagnostics, sourceFile.jsDocDiagnostics);
13101309
}
1311-
return filter<DiagnosticWithLocation>(diagnostics, shouldReportDiagnostic);
1310+
return filter<Diagnostic>(diagnostics, shouldReportDiagnostic);
13121311
});
13131312
}
13141313

@@ -1531,36 +1530,25 @@ namespace ts {
15311530
return ts.getDeclarationDiagnostics(getEmitHost(noop), resolver, sourceFile);
15321531
});
15331532
}
1534-
function getAndCacheDiagnostics(
1535-
sourceFile: SourceFile,
1536-
cancellationToken: CancellationToken,
1537-
cache: DiagnosticCache,
1538-
getDiagnostics: (sourceFile: SourceFile, cancellationToken: CancellationToken) => DiagnosticWithLocation[]
1539-
): ReadonlyArray<DiagnosticWithLocation>;
1540-
function getAndCacheDiagnostics(
1541-
sourceFile: SourceFile | undefined,
1542-
cancellationToken: CancellationToken,
1543-
cache: DiagnosticCache,
1544-
getDiagnostics: (sourceFile: SourceFile, cancellationToken: CancellationToken) => DiagnosticWithLocation[],
1545-
): ReadonlyArray<Diagnostic>;
1546-
function getAndCacheDiagnostics(
1533+
1534+
function getAndCacheDiagnostics<T extends Diagnostic>(
15471535
sourceFile: SourceFile | undefined,
15481536
cancellationToken: CancellationToken,
1549-
cache: DiagnosticCache,
1550-
getDiagnostics: (sourceFile: SourceFile, cancellationToken: CancellationToken) => DiagnosticWithLocation[],
1551-
): ReadonlyArray<Diagnostic> {
1537+
cache: DiagnosticCache<T>,
1538+
getDiagnostics: (sourceFile: SourceFile, cancellationToken: CancellationToken) => T[],
1539+
): ReadonlyArray<T> {
15521540

15531541
const cachedResult = sourceFile
15541542
? cache.perFile && cache.perFile.get(sourceFile.path)
1555-
: cache.allDiagnostics;
1543+
: cache.allDiagnostics as T[]
15561544

15571545
if (cachedResult) {
15581546
return cachedResult;
15591547
}
15601548
const result = getDiagnostics(sourceFile, cancellationToken) || emptyArray;
15611549
if (sourceFile) {
15621550
if (!cache.perFile) {
1563-
cache.perFile = createMap<DiagnosticWithLocation[]>();
1551+
cache.perFile = createMap<T[]>();
15641552
}
15651553
cache.perFile.set(sourceFile.path, result);
15661554
}

src/compiler/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2686,7 +2686,8 @@ namespace ts {
26862686
getOptionsDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
26872687
getGlobalDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
26882688
getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<DiagnosticWithLocation>;
2689-
getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<DiagnosticWithLocation>;
2689+
/** The first time this is called, it will return global diagnostics (no location). */
2690+
getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
26902691
getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<DiagnosticWithLocation>;
26912692
getConfigFileParsingDiagnostics(): ReadonlyArray<Diagnostic>;
26922693

src/harness/harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,7 @@ namespace Harness {
14031403
const dupeCase = ts.createMap<number>();
14041404
for (const inputFile of inputFiles.filter(f => f.content !== undefined)) {
14051405
// Filter down to the errors in the file
1406-
const fileErrors = diagnostics.filter(e => {
1406+
const fileErrors = diagnostics.filter((e): e is ts.DiagnosticWithLocation => {
14071407
const errFn = e.file;
14081408
return errFn && errFn.fileName === inputFile.unitName;
14091409
});

src/services/codeFixProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ namespace ts {
8989
function eachDiagnostic({ program, sourceFile }: CodeFixAllContext, errorCodes: number[], cb: (diag: DiagnosticWithLocation) => void): void {
9090
for (const diag of program.getSemanticDiagnostics(sourceFile).concat(computeSuggestionDiagnostics(sourceFile, program))) {
9191
if (contains(errorCodes, diag.code)) {
92-
cb(diag as Diagnostic & { file: SourceFile });
92+
cb(diag as DiagnosticWithLocation);
9393
}
9494
}
9595
}

src/services/services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,7 @@ namespace ts {
13671367
* getSemanticDiagnostics return array of Diagnostics. If '-d' is not enabled, only report semantic errors
13681368
* If '-d' enabled, report both semantic and emitter errors
13691369
*/
1370-
function getSemanticDiagnostics(fileName: string): DiagnosticWithLocation[] {
1370+
function getSemanticDiagnostics(fileName: string): Diagnostic[] {
13711371
synchronizeHostData();
13721372

13731373
const targetSourceFile = getValidSourceFile(fileName);

src/services/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ namespace ts {
245245
cleanupSemanticCache(): void;
246246

247247
getSyntacticDiagnostics(fileName: string): DiagnosticWithLocation[];
248-
getSemanticDiagnostics(fileName: string): DiagnosticWithLocation[];
248+
/** The first time this is called, it will return global diagnostics (no location). */
249+
getSemanticDiagnostics(fileName: string): Diagnostic[];
249250
getSuggestionDiagnostics(fileName: string): DiagnosticWithLocation[];
250251

251252
// TODO: Rename this to getProgramDiagnostics to better indicate that these are any

0 commit comments

Comments
 (0)