Skip to content

Commit 7ade816

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

File tree

11 files changed

+42
-48
lines changed

11 files changed

+42
-48
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/transformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ namespace ts {
9090
let onSubstituteNode: TransformationContext["onSubstituteNode"] = (_, node) => node;
9191
let onEmitNode: TransformationContext["onEmitNode"] = (hint, node, callback) => callback(hint, node);
9292
let state = TransformationState.Uninitialized;
93-
const diagnostics: Diagnostic[] = [];
93+
const diagnostics: DiagnosticWithLocation[] = [];
9494

9595
// The transformation context is provided to each transformer as part of transformer
9696
// initialization.

src/compiler/transformers/declarations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace ts {
66
}
77
const compilerOptions = host.getCompilerOptions();
88
const result = transformNodes(resolver, host, compilerOptions, file ? [file] : filter(host.getSourceFiles(), isSourceFileNotJavaScript), [transformDeclarations], /*allowDtsFiles*/ false);
9-
return result.diagnostics as DiagnosticWithLocation[]; // TODO: GH#18217
9+
return result.diagnostics;
1010
}
1111

1212
const declarationEmitNodeBuilderFlags = NodeBuilderFlags.MultilineObjectLiterals | TypeFormatFlags.WriteClassExpressionAsTypeLiteral | NodeBuilderFlags.UseTypeOfFunction | NodeBuilderFlags.UseStructuralFallback | NodeBuilderFlags.AllowEmptyTuple;

src/compiler/types.ts

Lines changed: 4 additions & 3 deletions
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

@@ -4942,15 +4943,15 @@ namespace ts {
49424943
*/
49434944
onEmitNode: (hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) => void;
49444945

4945-
/* @internal */ addDiagnostic(diag: Diagnostic): void;
4946+
/* @internal */ addDiagnostic(diag: DiagnosticWithLocation): void;
49464947
}
49474948

49484949
export interface TransformationResult<T extends Node> {
49494950
/** Gets the transformed source files. */
49504951
transformed: T[];
49514952

49524953
/** Gets diagnostics for the transformation. */
4953-
diagnostics?: Diagnostic[];
4954+
diagnostics?: DiagnosticWithLocation[];
49544955

49554956
/**
49564957
* Gets a substitute for a node, if one is available; otherwise, returns the original node.

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/transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace ts {
66
* @param compilerOptions Optional compiler options.
77
*/
88
export function transform<T extends Node>(source: T | T[], transformers: TransformerFactory<T>[], compilerOptions?: CompilerOptions) {
9-
const diagnostics: Diagnostic[] = [];
9+
const diagnostics: DiagnosticWithLocation[] = [];
1010
compilerOptions = fixupCompilerOptions(compilerOptions, diagnostics);
1111
const nodes = isArray(source) ? source : [source];
1212
const result = transformNodes(/*resolver*/ undefined, /*emitHost*/ undefined, compilerOptions, nodes, transformers, /*allowDtsFiles*/ true);

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

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,8 @@ declare namespace ts {
16971697
getOptionsDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
16981698
getGlobalDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
16991699
getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<DiagnosticWithLocation>;
1700-
getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<DiagnosticWithLocation>;
1700+
/** The first time this is called, it will return global diagnostics (no location). */
1701+
getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
17011702
getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<DiagnosticWithLocation>;
17021703
getConfigFileParsingDiagnostics(): ReadonlyArray<Diagnostic>;
17031704
/**
@@ -2679,7 +2680,7 @@ declare namespace ts {
26792680
/** Gets the transformed source files. */
26802681
transformed: T[];
26812682
/** Gets diagnostics for the transformation. */
2682-
diagnostics?: Diagnostic[];
2683+
diagnostics?: DiagnosticWithLocation[];
26832684
/**
26842685
* Gets a substitute for a node, if one is available; otherwise, returns the original node.
26852686
*
@@ -4401,7 +4402,8 @@ declare namespace ts {
44014402
interface LanguageService {
44024403
cleanupSemanticCache(): void;
44034404
getSyntacticDiagnostics(fileName: string): DiagnosticWithLocation[];
4404-
getSemanticDiagnostics(fileName: string): DiagnosticWithLocation[];
4405+
/** The first time this is called, it will return global diagnostics (no location). */
4406+
getSemanticDiagnostics(fileName: string): Diagnostic[];
44054407
getSuggestionDiagnostics(fileName: string): DiagnosticWithLocation[];
44064408
getCompilerOptionsDiagnostics(): Diagnostic[];
44074409
/**

tests/baselines/reference/api/typescript.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,8 @@ declare namespace ts {
16971697
getOptionsDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
16981698
getGlobalDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
16991699
getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<DiagnosticWithLocation>;
1700-
getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<DiagnosticWithLocation>;
1700+
/** The first time this is called, it will return global diagnostics (no location). */
1701+
getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
17011702
getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<DiagnosticWithLocation>;
17021703
getConfigFileParsingDiagnostics(): ReadonlyArray<Diagnostic>;
17031704
/**
@@ -2679,7 +2680,7 @@ declare namespace ts {
26792680
/** Gets the transformed source files. */
26802681
transformed: T[];
26812682
/** Gets diagnostics for the transformation. */
2682-
diagnostics?: Diagnostic[];
2683+
diagnostics?: DiagnosticWithLocation[];
26832684
/**
26842685
* Gets a substitute for a node, if one is available; otherwise, returns the original node.
26852686
*
@@ -4401,7 +4402,8 @@ declare namespace ts {
44014402
interface LanguageService {
44024403
cleanupSemanticCache(): void;
44034404
getSyntacticDiagnostics(fileName: string): DiagnosticWithLocation[];
4404-
getSemanticDiagnostics(fileName: string): DiagnosticWithLocation[];
4405+
/** The first time this is called, it will return global diagnostics (no location). */
4406+
getSemanticDiagnostics(fileName: string): Diagnostic[];
44054407
getSuggestionDiagnostics(fileName: string): DiagnosticWithLocation[];
44064408
getCompilerOptionsDiagnostics(): Diagnostic[];
44074409
/**

0 commit comments

Comments
 (0)