diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 73ef633955209..36fbbeb2ec74b 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -158,7 +158,7 @@ namespace ts { * If so, the node _must_ be in the current file (as that's the only way anything could have traversed to it to yield it as the error node) * This version of `createDiagnosticForNode` uses the binder's context to account for this, and always yields correct diagnostics even in these situations. */ - function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): Diagnostic { + function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): DiagnosticWithLocation { return createDiagnosticForNodeInSourceFile(getSourceFileOfNode(node) || file, node, message, arg0, arg1, arg2); } diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8f7a54089bcbc..b7f31d46728a4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -313,11 +313,11 @@ namespace ts { getSuggestionDiagnostics: file => { return (suggestionDiagnostics.get(file.fileName) || emptyArray).concat(getUnusedDiagnostics()); - function getUnusedDiagnostics(): ReadonlyArray { + function getUnusedDiagnostics(): ReadonlyArray { if (file.isDeclarationFile) return emptyArray; checkSourceFile(file); - const diagnostics: Diagnostic[] = []; + const diagnostics: DiagnosticWithLocation[] = []; Debug.assert(!!(getNodeLinks(file).flags & NodeCheckFlags.TypeChecked)); checkUnusedIdentifiers(getPotentiallyUnusedIdentifiers(file), (kind, diag) => { if (!unusedIsError(kind)) { @@ -481,7 +481,7 @@ namespace ts { const diagnostics = createDiagnosticCollection(); // Suggestion diagnostics must have a file. Keyed by source file name. - const suggestionDiagnostics = createMultiMap(); + const suggestionDiagnostics = createMultiMap(); const enum TypeFacts { None = 0, @@ -628,7 +628,7 @@ namespace ts { Local, Parameter, } - type AddUnusedDiagnostic = (type: UnusedKind, diagnostic: Diagnostic) => void; + type AddUnusedDiagnostic = (type: UnusedKind, diagnostic: DiagnosticWithLocation) => void; const builtinGlobals = createSymbolTable(); builtinGlobals.set(undefinedSymbol.escapedName, undefinedSymbol); @@ -824,7 +824,7 @@ namespace ts { diagnostics.add(diagnostic); } - function addErrorOrSuggestion(isError: boolean, diagnostic: Diagnostic) { + function addErrorOrSuggestion(isError: boolean, diagnostic: DiagnosticWithLocation) { if (isError) { diagnostics.add(diagnostic); } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 7f03ba06b1139..5307c1de7b470 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -14,8 +14,8 @@ namespace ts { return pathIsRelative(moduleName) || isRootedDiskPath(moduleName); } - export function sortAndDeduplicateDiagnostics(diagnostics: ReadonlyArray): Diagnostic[] { - return sortAndDeduplicate(diagnostics, compareDiagnostics); + export function sortAndDeduplicateDiagnostics(diagnostics: ReadonlyArray): T[] { + return sortAndDeduplicate(diagnostics, compareDiagnostics); } } @@ -1619,8 +1619,8 @@ namespace ts { return localizedDiagnosticMessages && localizedDiagnosticMessages[message.key] || message.message; } - export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: (string | number)[]): Diagnostic; - export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage): Diagnostic { + export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: (string | number)[]): DiagnosticWithLocation; + export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage): DiagnosticWithLocation { Debug.assertGreaterThanOrEqual(start, 0); Debug.assertGreaterThanOrEqual(length, 0); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index e9c0ca66f1d8c..975331e715366 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -595,7 +595,7 @@ namespace ts { // tslint:enable variable-name let sourceFile: SourceFile; - let parseDiagnostics: Diagnostic[]; + let parseDiagnostics: DiagnosticWithLocation[]; let syntaxCursor: IncrementalParser.SyntaxCursor; let currentToken: SyntaxKind; diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 365e948f749e4..c453080f3bf9b 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -394,8 +394,8 @@ namespace ts { return resolutions; } - interface DiagnosticCache { - perFile?: Map; + interface DiagnosticCache { + perFile?: Map; allDiagnostics?: Diagnostic[]; } @@ -454,7 +454,7 @@ namespace ts { export function getConfigFileParsingDiagnostics(configFileParseResult: ParsedCommandLine): ReadonlyArray { return configFileParseResult.options.configFile ? - configFileParseResult.options.configFile.parseDiagnostics.concat(configFileParseResult.errors) : + [...configFileParseResult.options.configFile.parseDiagnostics, ...configFileParseResult.errors] : configFileParseResult.errors; } @@ -517,8 +517,8 @@ namespace ts { let classifiableNames: UnderscoreEscapedMap; let modifiedFilePaths: Path[] | undefined; - const cachedSemanticDiagnosticsForFile: DiagnosticCache = {}; - const cachedDeclarationDiagnosticsForFile: DiagnosticCache = {}; + const cachedSemanticDiagnosticsForFile: DiagnosticCache = {}; + const cachedDeclarationDiagnosticsForFile: DiagnosticCache = {}; let resolvedTypeReferenceDirectives = createMap(); let fileProcessingDiagnostics = createDiagnosticCollection(); @@ -1313,10 +1313,10 @@ namespace ts { return filesByName.get(path); } - function getDiagnosticsHelper( + function getDiagnosticsHelper( sourceFile: SourceFile, - getDiagnostics: (sourceFile: SourceFile, cancellationToken: CancellationToken) => ReadonlyArray, - cancellationToken: CancellationToken): ReadonlyArray { + getDiagnostics: (sourceFile: SourceFile, cancellationToken: CancellationToken) => ReadonlyArray, + cancellationToken: CancellationToken): ReadonlyArray { if (sourceFile) { return getDiagnostics(sourceFile, cancellationToken); } @@ -1328,7 +1328,7 @@ namespace ts { })); } - function getSyntacticDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): ReadonlyArray { + function getSyntacticDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): ReadonlyArray { return getDiagnosticsHelper(sourceFile, getSyntacticDiagnosticsForFile, cancellationToken); } @@ -1336,7 +1336,7 @@ namespace ts { return getDiagnosticsHelper(sourceFile, getSemanticDiagnosticsForFile, cancellationToken); } - function getDeclarationDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): ReadonlyArray { + function getDeclarationDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): ReadonlyArray { const options = program.getCompilerOptions(); // collect diagnostics from the program only once if either no source file was specified or out/outFile is set (bundled emit) if (!sourceFile || options.out || options.outFile) { @@ -1347,7 +1347,7 @@ namespace ts { } } - function getSyntacticDiagnosticsForFile(sourceFile: SourceFile): ReadonlyArray { + function getSyntacticDiagnosticsForFile(sourceFile: SourceFile): ReadonlyArray { // For JavaScript files, we report semantic errors for using TypeScript-only // constructs from within a JavaScript file as syntactic errors. if (isSourceFileJavaScript(sourceFile)) { @@ -1382,7 +1382,7 @@ namespace ts { } } - function getSemanticDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] { + function getSemanticDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): ReadonlyArray { return getAndCacheDiagnostics(sourceFile, cancellationToken, cachedSemanticDiagnosticsForFile, getSemanticDiagnosticsForFileNoCache); } @@ -1403,15 +1403,22 @@ namespace ts { // By default, only type-check .ts, .tsx, 'Deferred' and 'External' files (external files are added by plugins) const includeBindAndCheckDiagnostics = sourceFile.scriptKind === ScriptKind.TS || sourceFile.scriptKind === ScriptKind.TSX || sourceFile.scriptKind === ScriptKind.External || isCheckJs || sourceFile.scriptKind === ScriptKind.Deferred; - const bindDiagnostics = includeBindAndCheckDiagnostics ? sourceFile.bindDiagnostics : emptyArray; + const bindDiagnostics: ReadonlyArray = includeBindAndCheckDiagnostics ? sourceFile.bindDiagnostics : emptyArray; const checkDiagnostics = includeBindAndCheckDiagnostics ? typeChecker.getDiagnostics(sourceFile, cancellationToken) : emptyArray; const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName); const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName); - let diagnostics = bindDiagnostics.concat(checkDiagnostics, fileProcessingDiagnosticsInFile, programDiagnosticsInFile); - if (isCheckJs) { - diagnostics = concatenate(diagnostics, sourceFile.jsDocDiagnostics); + + let diagnostics: Diagnostic[] | undefined; + for (const diags of [bindDiagnostics, checkDiagnostics, fileProcessingDiagnosticsInFile, programDiagnosticsInFile, isCheckJs ? sourceFile.jsDocDiagnostics : undefined]) { + if (diags) { + for (const diag of diags) { + if (shouldReportDiagnostic(diag)) { + diagnostics = append(diagnostics, diag); + } + } + } } - return filter(diagnostics, shouldReportDiagnostic); + return diagnostics; }); } @@ -1440,9 +1447,9 @@ namespace ts { return true; } - function getJavaScriptSyntacticDiagnosticsForFile(sourceFile: SourceFile): Diagnostic[] { + function getJavaScriptSyntacticDiagnosticsForFile(sourceFile: SourceFile): DiagnosticWithLocation[] { return runWithCancellationToken(() => { - const diagnostics: Diagnostic[] = []; + const diagnostics: DiagnosticWithLocation[] = []; let parent: Node = sourceFile; walk(sourceFile); @@ -1610,20 +1617,20 @@ namespace ts { } } - function createDiagnosticForNodeArray(nodes: NodeArray, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): Diagnostic { + function createDiagnosticForNodeArray(nodes: NodeArray, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): DiagnosticWithLocation { const start = nodes.pos; return createFileDiagnostic(sourceFile, start, nodes.end - start, message, arg0, arg1, arg2); } // Since these are syntactic diagnostics, parent might not have been set // this means the sourceFile cannot be infered from the node - function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): Diagnostic { + function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): DiagnosticWithLocation { return createDiagnosticForNodeInSourceFile(sourceFile, node, message, arg0, arg1, arg2); } }); } - function getDeclarationDiagnosticsWorker(sourceFile: SourceFile | undefined, cancellationToken: CancellationToken): Diagnostic[] { + function getDeclarationDiagnosticsWorker(sourceFile: SourceFile, cancellationToken: CancellationToken): ReadonlyArray { return getAndCacheDiagnostics(sourceFile, cancellationToken, cachedDeclarationDiagnosticsForFile, getDeclarationDiagnosticsForFileNoCache); } @@ -1635,15 +1642,16 @@ namespace ts { }); } - function getAndCacheDiagnostics( + function getAndCacheDiagnostics( sourceFile: SourceFile | undefined, cancellationToken: CancellationToken, - cache: DiagnosticCache, - getDiagnostics: (sourceFile: SourceFile, cancellationToken: CancellationToken) => Diagnostic[]) { + cache: DiagnosticCache, + getDiagnostics: (sourceFile: SourceFile, cancellationToken: CancellationToken) => T[], + ): ReadonlyArray { const cachedResult = sourceFile ? cache.perFile && cache.perFile.get(sourceFile.path) - : cache.allDiagnostics; + : cache.allDiagnostics as T[]; if (cachedResult) { return cachedResult; @@ -1651,7 +1659,7 @@ namespace ts { const result = getDiagnostics(sourceFile, cancellationToken) || emptyArray; if (sourceFile) { if (!cache.perFile) { - cache.perFile = createMap(); + cache.perFile = createMap(); } cache.perFile.set(sourceFile.path, result); } @@ -1661,7 +1669,7 @@ namespace ts { return result; } - function getDeclarationDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] { + function getDeclarationDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): ReadonlyArray { return sourceFile.isDeclarationFile ? [] : getDeclarationDiagnosticsWorker(sourceFile, cancellationToken); } diff --git a/src/compiler/transformer.ts b/src/compiler/transformer.ts index 16a6912757f40..b661120b961a6 100644 --- a/src/compiler/transformer.ts +++ b/src/compiler/transformer.ts @@ -90,7 +90,7 @@ namespace ts { let onSubstituteNode: TransformationContext["onSubstituteNode"] = (_, node) => node; let onEmitNode: TransformationContext["onEmitNode"] = (hint, node, callback) => callback(hint, node); let state = TransformationState.Uninitialized; - const diagnostics: Diagnostic[] = []; + const diagnostics: DiagnosticWithLocation[] = []; // The transformation context is provided to each transformer as part of transformer // initialization. diff --git a/src/compiler/transformers/declarations.ts b/src/compiler/transformers/declarations.ts index 346f1343fabce..953f8279def76 100644 --- a/src/compiler/transformers/declarations.ts +++ b/src/compiler/transformers/declarations.ts @@ -1,6 +1,6 @@ /*@internal*/ namespace ts { - export function getDeclarationDiagnostics(host: EmitHost, resolver: EmitResolver, file: SourceFile | undefined): Diagnostic[] { + export function getDeclarationDiagnostics(host: EmitHost, resolver: EmitResolver, file: SourceFile | undefined): DiagnosticWithLocation[] { if (file && isSourceFileJavaScript(file)) { return []; // No declaration diagnostics for js for now } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 23e990d913e70..44c5ddb5d5034 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2599,17 +2599,17 @@ namespace ts { // File-level diagnostics reported by the parser (includes diagnostics about /// references // as well as code diagnostics). - /* @internal */ parseDiagnostics: Diagnostic[]; + /* @internal */ parseDiagnostics: DiagnosticWithLocation[]; // File-level diagnostics reported by the binder. - /* @internal */ bindDiagnostics: Diagnostic[]; - /* @internal */ bindSuggestionDiagnostics?: Diagnostic[]; + /* @internal */ bindDiagnostics: DiagnosticWithLocation[]; + /* @internal */ bindSuggestionDiagnostics?: DiagnosticWithLocation[]; // File-level JSDoc diagnostics reported by the JSDoc parser - /* @internal */ jsDocDiagnostics?: Diagnostic[]; + /* @internal */ jsDocDiagnostics?: DiagnosticWithLocation[]; // Stores additional file-level diagnostics reported by the program - /* @internal */ additionalSyntacticDiagnostics?: ReadonlyArray; + /* @internal */ additionalSyntacticDiagnostics?: ReadonlyArray; // Stores a line map for the file. // This field should never be used directly to obtain line map, use getLineMap function instead. @@ -2747,9 +2747,10 @@ namespace ts { getOptionsDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray; getGlobalDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray; - getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; + getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; + /** The first time this is called, it will return global diagnostics (no location). */ getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; - getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; + getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; getConfigFileParsingDiagnostics(): ReadonlyArray; /** @@ -3070,7 +3071,7 @@ namespace ts { * Does *not* get *all* suggestion diagnostics, just the ones that were convenient to report in the checker. * Others are added in computeSuggestionDiagnostics. */ - /* @internal */ getSuggestionDiagnostics(file: SourceFile): ReadonlyArray; + /* @internal */ getSuggestionDiagnostics(file: SourceFile): ReadonlyArray; /** * Depending on the operation performed, it may be appropriate to throw away the checker @@ -4228,6 +4229,11 @@ namespace ts { code: number; source?: string; } + export interface DiagnosticWithLocation extends Diagnostic { + file: SourceFile; + start: number; + length: number; + } export enum DiagnosticCategory { Warning, @@ -5084,7 +5090,7 @@ namespace ts { */ onEmitNode: (hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) => void; - /* @internal */ addDiagnostic(diag: Diagnostic): void; + /* @internal */ addDiagnostic(diag: DiagnosticWithLocation): void; } export interface TransformationResult { @@ -5092,7 +5098,7 @@ namespace ts { transformed: T[]; /** Gets diagnostics for the transformation. */ - diagnostics?: Diagnostic[]; + diagnostics?: DiagnosticWithLocation[]; /** * Gets a substitute for a node, if one is available; otherwise, returns the original node. @@ -5310,7 +5316,8 @@ namespace ts { // If fileName is provided, gets all the diagnostics associated with that file name. // Otherwise, returns all the diagnostics (global and file associated) in this collection. - getDiagnostics(fileName?: string): Diagnostic[]; + getDiagnostics(fileName: string): DiagnosticWithLocation[]; + getDiagnostics(): Diagnostic[]; reattachFileDiagnostics(newFile: SourceFile): void; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index df19a8948c722..64996153d23b6 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -603,7 +603,7 @@ namespace ts { } } - export function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): Diagnostic { + export function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): DiagnosticWithLocation { const sourceFile = getSourceFileOfNode(node); return createDiagnosticForNodeInSourceFile(sourceFile, node, message, arg0, arg1, arg2, arg3); } @@ -613,17 +613,17 @@ namespace ts { return createFileDiagnostic(sourceFile, start, nodes.end - start, message, arg0, arg1, arg2, arg3); } - export function createDiagnosticForNodeInSourceFile(sourceFile: SourceFile, node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): Diagnostic { + export function createDiagnosticForNodeInSourceFile(sourceFile: SourceFile, node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): DiagnosticWithLocation { const span = getErrorSpanForNode(sourceFile, node); return createFileDiagnostic(sourceFile, span.start, span.length, message, arg0, arg1, arg2, arg3); } - export function createDiagnosticForNodeSpan(sourceFile: SourceFile, startNode: Node, endNode: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): Diagnostic { + export function createDiagnosticForNodeSpan(sourceFile: SourceFile, startNode: Node, endNode: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): DiagnosticWithLocation { const start = skipTrivia(sourceFile.text, startNode.pos); return createFileDiagnostic(sourceFile, start, endNode.end - start, message, arg0, arg1, arg2, arg3); } - export function createDiagnosticForNodeFromMessageChain(node: Node, messageChain: DiagnosticMessageChain): Diagnostic { + export function createDiagnosticForNodeFromMessageChain(node: Node, messageChain: DiagnosticMessageChain): DiagnosticWithLocation { const sourceFile = getSourceFileOfNode(node); const span = getErrorSpanForNode(sourceFile, node); return { @@ -2645,7 +2645,7 @@ namespace ts { export function createDiagnosticCollection(): DiagnosticCollection { let nonFileDiagnostics = [] as SortedArray; const filesWithDiagnostics = [] as SortedArray; - const fileDiagnostics = createMap>(); + const fileDiagnostics = createMap>(); let hasReadNonFileDiagnostics = false; return { @@ -2664,8 +2664,8 @@ namespace ts { if (diagnostic.file) { diagnostics = fileDiagnostics.get(diagnostic.file.fileName); if (!diagnostics) { - diagnostics = [] as SortedArray; - fileDiagnostics.set(diagnostic.file.fileName, diagnostics); + diagnostics = [] as SortedArray; + fileDiagnostics.set(diagnostic.file.fileName, diagnostics as SortedArray); insertSorted(filesWithDiagnostics, diagnostic.file.fileName, compareStringsCaseSensitive); } } @@ -2687,12 +2687,14 @@ namespace ts { return nonFileDiagnostics; } + function getDiagnostics(fileName: string): DiagnosticWithLocation[]; + function getDiagnostics(): Diagnostic[]; function getDiagnostics(fileName?: string): Diagnostic[] { if (fileName) { return fileDiagnostics.get(fileName) || []; } - const fileDiags = flatMap(filesWithDiagnostics, f => fileDiagnostics.get(f)); + const fileDiags: Diagnostic[] = flatMap(filesWithDiagnostics, f => fileDiagnostics.get(f)); if (!nonFileDiagnostics.length) { return fileDiags; } diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 28233b797da47..96d7a01eaae68 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1406,7 +1406,7 @@ namespace Harness { const dupeCase = ts.createMap(); for (const inputFile of inputFiles.filter(f => f.content !== undefined)) { // Filter down to the errors in the file - const fileErrors = diagnostics.filter(e => { + const fileErrors = diagnostics.filter((e): e is ts.DiagnosticWithLocation => { const errFn = e.file; return errFn && utils.removeTestPathPrefixes(errFn.fileName) === utils.removeTestPathPrefixes(inputFile.unitName); }); diff --git a/src/harness/harnessLanguageService.ts b/src/harness/harnessLanguageService.ts index 34d6f775a74b4..8c30f9411e671 100644 --- a/src/harness/harnessLanguageService.ts +++ b/src/harness/harnessLanguageService.ts @@ -398,13 +398,13 @@ namespace Harness.LanguageService { cleanupSemanticCache(): void { this.shim.cleanupSemanticCache(); } - getSyntacticDiagnostics(fileName: string): ts.Diagnostic[] { + getSyntacticDiagnostics(fileName: string): ts.DiagnosticWithLocation[] { return unwrapJSONCallResult(this.shim.getSyntacticDiagnostics(fileName)); } - getSemanticDiagnostics(fileName: string): ts.Diagnostic[] { + getSemanticDiagnostics(fileName: string): ts.DiagnosticWithLocation[] { return unwrapJSONCallResult(this.shim.getSemanticDiagnostics(fileName)); } - getSuggestionDiagnostics(fileName: string): ts.Diagnostic[] { + getSuggestionDiagnostics(fileName: string): ts.DiagnosticWithLocation[] { return unwrapJSONCallResult(this.shim.getSuggestionDiagnostics(fileName)); } getCompilerOptionsDiagnostics(): ts.Diagnostic[] { diff --git a/src/harness/projectsRunner.ts b/src/harness/projectsRunner.ts index 08e92a73be235..9ef536ad0c8be 100644 --- a/src/harness/projectsRunner.ts +++ b/src/harness/projectsRunner.ts @@ -154,7 +154,7 @@ namespace project { const configParseResult = ts.parseJsonSourceFileConfigFileContent(result, configParseHost, ts.getDirectoryPath(configFileName), this.compilerOptions); inputFiles = configParseResult.fileNames; this.compilerOptions = configParseResult.options; - errors = result.parseDiagnostics.concat(configParseResult.errors); + errors = [...result.parseDiagnostics, ...configParseResult.errors]; } const compilerHost = new ProjectCompilerHost(this.sys, this.compilerOptions, this.testCaseJustName, this.testCase, moduleKind); diff --git a/src/server/client.ts b/src/server/client.ts index a07e793dcba55..2a97a54a18dcc 100644 --- a/src/server/client.ts +++ b/src/server/client.ts @@ -346,21 +346,21 @@ namespace ts.server { return notImplemented(); } - getSyntacticDiagnostics(file: string): Diagnostic[] { + getSyntacticDiagnostics(file: string): DiagnosticWithLocation[] { return this.getDiagnostics(file, CommandNames.SyntacticDiagnosticsSync); } - getSemanticDiagnostics(file: string): Diagnostic[] { + getSemanticDiagnostics(file: string): DiagnosticWithLocation[] { return this.getDiagnostics(file, CommandNames.SemanticDiagnosticsSync); } - getSuggestionDiagnostics(file: string): Diagnostic[] { + getSuggestionDiagnostics(file: string): DiagnosticWithLocation[] { return this.getDiagnostics(file, CommandNames.SuggestionDiagnosticsSync); } - private getDiagnostics(file: string, command: CommandNames): Diagnostic[] { + private getDiagnostics(file: string, command: CommandNames): DiagnosticWithLocation[] { const request = this.processRequest(command, { file, includeLinePosition: true }); const response = this.processResponse(request); - return (response.body).map((entry): Diagnostic => { + return (response.body).map((entry): DiagnosticWithLocation => { const category = firstDefined(Object.keys(DiagnosticCategory), id => isString(id) && entry.category === id.toLowerCase() ? (DiagnosticCategory)[id] : undefined); return { diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index bb3560cbde521..9f87bd150eccd 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1329,7 +1329,7 @@ namespace ts.server { if (!result.endOfFileToken) { result.endOfFileToken = { kind: SyntaxKind.EndOfFileToken }; } - const errors = result.parseDiagnostics; + const errors = result.parseDiagnostics as Diagnostic[]; const parsedCommandLine = parseJsonSourceFileConfigFileContent( result, cachedDirectoryStructureHost, diff --git a/src/services/codeFixProvider.ts b/src/services/codeFixProvider.ts index 39f760ba04cf1..31b766d3bf5a3 100644 --- a/src/services/codeFixProvider.ts +++ b/src/services/codeFixProvider.ts @@ -79,17 +79,17 @@ namespace ts { return { fileName, textChanges }; } - export function codeFixAll(context: CodeFixAllContext, errorCodes: number[], use: (changes: textChanges.ChangeTracker, error: Diagnostic, commands: Push) => void): CombinedCodeActions { + export function codeFixAll(context: CodeFixAllContext, errorCodes: number[], use: (changes: textChanges.ChangeTracker, error: DiagnosticWithLocation, commands: Push) => void): CombinedCodeActions { const commands: CodeActionCommand[] = []; const changes = textChanges.ChangeTracker.with(context, t => eachDiagnostic(context, errorCodes, diag => use(t, diag, commands))); return createCombinedCodeActions(changes, commands.length === 0 ? undefined : commands); } - function eachDiagnostic({ program, sourceFile }: CodeFixAllContext, errorCodes: number[], cb: (diag: Diagnostic) => void): void { + function eachDiagnostic({ program, sourceFile }: CodeFixAllContext, errorCodes: number[], cb: (diag: DiagnosticWithLocation) => void): void { for (const diag of program.getSemanticDiagnostics(sourceFile).concat(computeSuggestionDiagnostics(sourceFile, program))) { if (contains(errorCodes, diag.code)) { - cb(diag); + cb(diag as DiagnosticWithLocation); } } } diff --git a/src/services/codefixes/addMissingInvocationForDecorator.ts b/src/services/codefixes/addMissingInvocationForDecorator.ts index ead64f9a2ca9e..02b97eec37ebf 100644 --- a/src/services/codefixes/addMissingInvocationForDecorator.ts +++ b/src/services/codefixes/addMissingInvocationForDecorator.ts @@ -9,7 +9,7 @@ namespace ts.codefix { return [createCodeFixAction(fixId, changes, Diagnostics.Call_decorator_expression, fixId, Diagnostics.Add_to_all_uncalled_decorators)]; }, fixIds: [fixId], - getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file!, diag.start!)), + getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file, diag.start)), }); function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) { diff --git a/src/services/codefixes/annotateWithTypeFromJSDoc.ts b/src/services/codefixes/annotateWithTypeFromJSDoc.ts index 7b66467843650..b3fc09b7bc4b2 100644 --- a/src/services/codefixes/annotateWithTypeFromJSDoc.ts +++ b/src/services/codefixes/annotateWithTypeFromJSDoc.ts @@ -12,8 +12,8 @@ namespace ts.codefix { }, fixIds: [fixId], getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { - const decl = getDeclaration(diag.file!, diag.start!); - if (decl) doChange(changes, diag.file!, decl); + const decl = getDeclaration(diag.file, diag.start); + if (decl) doChange(changes, diag.file, decl); }), }); diff --git a/src/services/codefixes/disableJsDiagnostics.ts b/src/services/codefixes/disableJsDiagnostics.ts index b5d3059dd42cb..c81446152dd05 100644 --- a/src/services/codefixes/disableJsDiagnostics.ts +++ b/src/services/codefixes/disableJsDiagnostics.ts @@ -38,8 +38,8 @@ namespace ts.codefix { getAllCodeActions: context => { const seenLines = createMap(); return codeFixAll(context, errorCodes, (changes, diag) => { - if (textChanges.isValidLocationToAddComment(diag.file!, diag.start!)) { - makeChange(changes, diag.file!, diag.start!, seenLines); + if (textChanges.isValidLocationToAddComment(diag.file, diag.start)) { + makeChange(changes, diag.file, diag.start, seenLines); } }); }, diff --git a/src/services/codefixes/fixAddMissingMember.ts b/src/services/codefixes/fixAddMissingMember.ts index e53ce41ad8a75..c2a8316db379a 100644 --- a/src/services/codefixes/fixAddMissingMember.ts +++ b/src/services/codefixes/fixAddMissingMember.ts @@ -23,7 +23,7 @@ namespace ts.codefix { const seenNames = createMap(); return codeFixAll(context, errorCodes, (changes, diag) => { const { program, preferences } = context; - const info = getInfo(diag.file!, diag.start!, program.getTypeChecker()); + const info = getInfo(diag.file, diag.start, program.getTypeChecker()); if (!info) return; const { classDeclaration, classDeclarationSourceFile, inJs, makeStatic, token, call } = info; if (!addToSeen(seenNames, token.text)) { diff --git a/src/services/codefixes/fixClassDoesntImplementInheritedAbstractMember.ts b/src/services/codefixes/fixClassDoesntImplementInheritedAbstractMember.ts index a931f849cb235..d59df7a02b302 100644 --- a/src/services/codefixes/fixClassDoesntImplementInheritedAbstractMember.ts +++ b/src/services/codefixes/fixClassDoesntImplementInheritedAbstractMember.ts @@ -17,7 +17,7 @@ namespace ts.codefix { getAllCodeActions: context => { const seenClassDeclarations = createMap(); return codeFixAll(context, errorCodes, (changes, diag) => { - const classDeclaration = getClass(diag.file!, diag.start!); + const classDeclaration = getClass(diag.file, diag.start); if (addToSeen(seenClassDeclarations, getNodeId(classDeclaration))) { addMissingMembers(classDeclaration, context.sourceFile, context.program.getTypeChecker(), changes, context.preferences); } diff --git a/src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts b/src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts index 4e0b5745149b4..0da45c46873dd 100644 --- a/src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts +++ b/src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts @@ -18,10 +18,10 @@ namespace ts.codefix { getAllCodeActions(context) { const seenClassDeclarations = createMap(); return codeFixAll(context, errorCodes, (changes, diag) => { - const classDeclaration = getClass(diag.file!, diag.start!); + const classDeclaration = getClass(diag.file, diag.start); if (addToSeen(seenClassDeclarations, getNodeId(classDeclaration))) { for (const implementedTypeNode of getClassImplementsHeritageClauseElements(classDeclaration)) { - addMissingDeclarations(context.program.getTypeChecker(), implementedTypeNode, diag.file!, classDeclaration, changes, context.preferences); + addMissingDeclarations(context.program.getTypeChecker(), implementedTypeNode, diag.file, classDeclaration, changes, context.preferences); } } }); diff --git a/src/services/codefixes/fixClassSuperMustPrecedeThisAccess.ts b/src/services/codefixes/fixClassSuperMustPrecedeThisAccess.ts index af38b8e9025fd..2f7daf93dc735 100644 --- a/src/services/codefixes/fixClassSuperMustPrecedeThisAccess.ts +++ b/src/services/codefixes/fixClassSuperMustPrecedeThisAccess.ts @@ -17,7 +17,7 @@ namespace ts.codefix { const { sourceFile } = context; const seenClasses = createMap(); // Ensure we only do this once per class. return codeFixAll(context, errorCodes, (changes, diag) => { - const nodes = getNodes(diag.file!, diag.start!); + const nodes = getNodes(diag.file, diag.start); if (!nodes) return; const { constructor, superCall } = nodes; if (addToSeen(seenClasses, getNodeId(constructor.parent))) { diff --git a/src/services/codefixes/fixConstructorForDerivedNeedSuperCall.ts b/src/services/codefixes/fixConstructorForDerivedNeedSuperCall.ts index 2aed263ee833f..605bb660f0623 100644 --- a/src/services/codefixes/fixConstructorForDerivedNeedSuperCall.ts +++ b/src/services/codefixes/fixConstructorForDerivedNeedSuperCall.ts @@ -12,7 +12,7 @@ namespace ts.codefix { }, fixIds: [fixId], getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => - doChange(changes, context.sourceFile, getNode(diag.file, diag.start!))), + doChange(changes, context.sourceFile, getNode(diag.file, diag.start))), }); function getNode(sourceFile: SourceFile, pos: number): ConstructorDeclaration { diff --git a/src/services/codefixes/fixExtendsInterfaceBecomesImplements.ts b/src/services/codefixes/fixExtendsInterfaceBecomesImplements.ts index c3f406b847509..4320513fb617c 100644 --- a/src/services/codefixes/fixExtendsInterfaceBecomesImplements.ts +++ b/src/services/codefixes/fixExtendsInterfaceBecomesImplements.ts @@ -14,7 +14,7 @@ namespace ts.codefix { }, fixIds: [fixId], getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { - const nodes = getNodes(diag.file, diag.start!); + const nodes = getNodes(diag.file, diag.start); if (nodes) doChanges(changes, diag.file, nodes.extendsToken, nodes.heritageClauses); }), }); diff --git a/src/services/codefixes/fixForgottenThisPropertyAccess.ts b/src/services/codefixes/fixForgottenThisPropertyAccess.ts index 31c6128d003f4..ce066277a27e2 100644 --- a/src/services/codefixes/fixForgottenThisPropertyAccess.ts +++ b/src/services/codefixes/fixForgottenThisPropertyAccess.ts @@ -19,7 +19,7 @@ namespace ts.codefix { }, fixIds: [fixId], getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { - doChange(changes, context.sourceFile, getInfo(diag.file, diag.start!, diag.code)); + doChange(changes, context.sourceFile, getInfo(diag.file, diag.start, diag.code)); }), }); diff --git a/src/services/codefixes/fixSpelling.ts b/src/services/codefixes/fixSpelling.ts index 252b08b356af1..dbb23c3ac09dd 100644 --- a/src/services/codefixes/fixSpelling.ts +++ b/src/services/codefixes/fixSpelling.ts @@ -19,7 +19,7 @@ namespace ts.codefix { }, fixIds: [fixId], getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { - const info = getInfo(diag.file!, diag.start!, context); + const info = getInfo(diag.file, diag.start, context); const { target } = context.host.getCompilationSettings(); if (info) doChange(changes, context.sourceFile, info.node, info.suggestion, target); }), diff --git a/src/services/codefixes/fixUnusedIdentifier.ts b/src/services/codefixes/fixUnusedIdentifier.ts index 148b42b1c7e61..6ba592ca2b8b5 100644 --- a/src/services/codefixes/fixUnusedIdentifier.ts +++ b/src/services/codefixes/fixUnusedIdentifier.ts @@ -53,7 +53,7 @@ namespace ts.codefix { return codeFixAll(context, errorCodes, (changes, diag) => { const { sourceFile } = context; const startToken = getTokenAtPosition(sourceFile, diag.start, /*includeJsDocComment*/ false); - const token = findPrecedingToken(textSpanEnd(diag), diag.file!); + const token = findPrecedingToken(textSpanEnd(diag), diag.file); switch (context.fixId) { case fixIdPrefix: if (isIdentifier(token) && canPrefix(token)) { @@ -62,7 +62,7 @@ namespace ts.codefix { break; case fixIdDelete: // Ignore if this range was already deleted. - if (deleted.some(d => rangeContainsPosition(d, diag.start!))) break; + if (deleted.some(d => rangeContainsPosition(d, diag.start))) break; const importDecl = tryGetFullImport(startToken); if (importDecl) { diff --git a/src/services/codefixes/useDefaultImport.ts b/src/services/codefixes/useDefaultImport.ts index 99fc145b5630a..3fe451c41fb2a 100644 --- a/src/services/codefixes/useDefaultImport.ts +++ b/src/services/codefixes/useDefaultImport.ts @@ -13,8 +13,8 @@ namespace ts.codefix { }, fixIds: [fixId], getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { - const info = getInfo(diag.file!, diag.start!); - if (info) doChange(changes, diag.file!, info); + const info = getInfo(diag.file, diag.start); + if (info) doChange(changes, diag.file, info); }), }); diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 494a811c238de..b0273cc18e7a4 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -228,7 +228,7 @@ namespace ts.formatting { * This function will return a predicate that for a given text range will tell * if there are any parse errors that overlap with the range. */ - function prepareRangeContainsErrorFunction(errors: Diagnostic[], originalRange: TextRange): (r: TextRange) => boolean { + function prepareRangeContainsErrorFunction(errors: ReadonlyArray, originalRange: TextRange): (r: TextRange) => boolean { if (!errors.length) { return rangeHasNoErrors; } diff --git a/src/services/services.ts b/src/services/services.ts index 9b8d0e0464899..0375f6fcf38f7 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -545,10 +545,10 @@ namespace ts { public referencedFiles: FileReference[]; public typeReferenceDirectives: FileReference[]; - public syntacticDiagnostics: Diagnostic[]; - public parseDiagnostics: Diagnostic[]; - public bindDiagnostics: Diagnostic[]; - public bindSuggestionDiagnostics?: Diagnostic[]; + public syntacticDiagnostics: DiagnosticWithLocation[]; + public parseDiagnostics: DiagnosticWithLocation[]; + public bindDiagnostics: DiagnosticWithLocation[]; + public bindSuggestionDiagnostics?: DiagnosticWithLocation[]; public isDeclarationFile: boolean; public isDefaultLib: boolean; @@ -1377,7 +1377,7 @@ namespace ts { } /// Diagnostics - function getSyntacticDiagnostics(fileName: string): Diagnostic[] { + function getSyntacticDiagnostics(fileName: string): DiagnosticWithLocation[] { synchronizeHostData(); return program.getSyntacticDiagnostics(getValidSourceFile(fileName), cancellationToken).slice(); @@ -1405,7 +1405,7 @@ namespace ts { return [...semanticDiagnostics, ...declarationDiagnostics]; } - function getSuggestionDiagnostics(fileName: string): Diagnostic[] { + function getSuggestionDiagnostics(fileName: string): DiagnosticWithLocation[] { synchronizeHostData(); return computeSuggestionDiagnostics(getValidSourceFile(fileName), program); } diff --git a/src/services/shims.ts b/src/services/shims.ts index c371533da0c1f..808da2eb1ec9c 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -1147,7 +1147,7 @@ namespace ts { typeAcquisition: configFile.typeAcquisition, files: configFile.fileNames, raw: configFile.raw, - errors: realizeDiagnostics(result.parseDiagnostics.concat(configFile.errors), "\r\n") + errors: realizeDiagnostics([...result.parseDiagnostics, ...configFile.errors], "\r\n") }; }); } diff --git a/src/services/suggestionDiagnostics.ts b/src/services/suggestionDiagnostics.ts index 40ed7719c9fe4..c59c267d16448 100644 --- a/src/services/suggestionDiagnostics.ts +++ b/src/services/suggestionDiagnostics.ts @@ -1,9 +1,9 @@ /* @internal */ namespace ts { - export function computeSuggestionDiagnostics(sourceFile: SourceFile, program: Program): Diagnostic[] { + export function computeSuggestionDiagnostics(sourceFile: SourceFile, program: Program): DiagnosticWithLocation[] { program.getSemanticDiagnostics(sourceFile); const checker = program.getDiagnosticsProducingTypeChecker(); - const diags: Diagnostic[] = []; + const diags: DiagnosticWithLocation[] = []; if (sourceFile.commonJsModuleIndicator && (programContainsEs6Modules(program) || compilerOptionsIndicateEs6Modules(program.getCompilerOptions())) && diff --git a/src/services/transform.ts b/src/services/transform.ts index 200fb9aacb249..b6b2a35708104 100644 --- a/src/services/transform.ts +++ b/src/services/transform.ts @@ -6,7 +6,7 @@ namespace ts { * @param compilerOptions Optional compiler options. */ export function transform(source: T | T[], transformers: TransformerFactory[], compilerOptions?: CompilerOptions) { - const diagnostics: Diagnostic[] = []; + const diagnostics: DiagnosticWithLocation[] = []; compilerOptions = fixupCompilerOptions(compilerOptions, diagnostics); const nodes = isArray(source) ? source : [source]; const result = transformNodes(/*resolver*/ undefined, /*emitHost*/ undefined, compilerOptions, nodes, transformers, /*allowDtsFiles*/ true); diff --git a/src/services/types.ts b/src/services/types.ts index b8e2c488b17cb..b1b0ccd6de885 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -249,9 +249,10 @@ namespace ts { export interface LanguageService { cleanupSemanticCache(): void; - getSyntacticDiagnostics(fileName: string): Diagnostic[]; + getSyntacticDiagnostics(fileName: string): DiagnosticWithLocation[]; + /** The first time this is called, it will return global diagnostics (no location). */ getSemanticDiagnostics(fileName: string): Diagnostic[]; - getSuggestionDiagnostics(fileName: string): Diagnostic[]; + getSuggestionDiagnostics(fileName: string): DiagnosticWithLocation[]; // TODO: Rename this to getProgramDiagnostics to better indicate that these are any // diagnostics present for the program level, and not just 'options' diagnostics. diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 40c3abdbb9e88..d2c66aede48d6 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -1734,9 +1734,10 @@ declare namespace ts { emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult; getOptionsDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray; getGlobalDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray; - getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; + getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; + /** The first time this is called, it will return global diagnostics (no location). */ getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; - getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; + getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; getConfigFileParsingDiagnostics(): ReadonlyArray; /** * Gets a type checker that can be used to semantically analyze source files in the program. @@ -2359,6 +2360,11 @@ declare namespace ts { code: number; source?: string; } + interface DiagnosticWithLocation extends Diagnostic { + file: SourceFile; + start: number; + length: number; + } enum DiagnosticCategory { Warning = 0, Error = 1, @@ -2754,7 +2760,7 @@ declare namespace ts { /** Gets the transformed source files. */ transformed: T[]; /** Gets diagnostics for the transformation. */ - diagnostics?: Diagnostic[]; + diagnostics?: DiagnosticWithLocation[]; /** * Gets a substitute for a node, if one is available; otherwise, returns the original node. * @@ -2960,7 +2966,7 @@ declare namespace ts { } declare namespace ts { function isExternalModuleNameRelative(moduleName: string): boolean; - function sortAndDeduplicateDiagnostics(diagnostics: ReadonlyArray): Diagnostic[]; + function sortAndDeduplicateDiagnostics(diagnostics: ReadonlyArray): T[]; } declare function setTimeout(handler: (...args: any[]) => void, timeout: number): any; declare function clearTimeout(handle: any): void; @@ -4497,9 +4503,10 @@ declare namespace ts { } interface LanguageService { cleanupSemanticCache(): void; - getSyntacticDiagnostics(fileName: string): Diagnostic[]; + getSyntacticDiagnostics(fileName: string): DiagnosticWithLocation[]; + /** The first time this is called, it will return global diagnostics (no location). */ getSemanticDiagnostics(fileName: string): Diagnostic[]; - getSuggestionDiagnostics(fileName: string): Diagnostic[]; + getSuggestionDiagnostics(fileName: string): DiagnosticWithLocation[]; getCompilerOptionsDiagnostics(): Diagnostic[]; /** * @deprecated Use getEncodedSyntacticClassifications instead. diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 2324d24908bb1..71d598ca102e2 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -1734,9 +1734,10 @@ declare namespace ts { emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult; getOptionsDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray; getGlobalDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray; - getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; + getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; + /** The first time this is called, it will return global diagnostics (no location). */ getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; - getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; + getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; getConfigFileParsingDiagnostics(): ReadonlyArray; /** * Gets a type checker that can be used to semantically analyze source files in the program. @@ -2359,6 +2360,11 @@ declare namespace ts { code: number; source?: string; } + interface DiagnosticWithLocation extends Diagnostic { + file: SourceFile; + start: number; + length: number; + } enum DiagnosticCategory { Warning = 0, Error = 1, @@ -2754,7 +2760,7 @@ declare namespace ts { /** Gets the transformed source files. */ transformed: T[]; /** Gets diagnostics for the transformation. */ - diagnostics?: Diagnostic[]; + diagnostics?: DiagnosticWithLocation[]; /** * Gets a substitute for a node, if one is available; otherwise, returns the original node. * @@ -2960,7 +2966,7 @@ declare namespace ts { } declare namespace ts { function isExternalModuleNameRelative(moduleName: string): boolean; - function sortAndDeduplicateDiagnostics(diagnostics: ReadonlyArray): Diagnostic[]; + function sortAndDeduplicateDiagnostics(diagnostics: ReadonlyArray): T[]; } declare function setTimeout(handler: (...args: any[]) => void, timeout: number): any; declare function clearTimeout(handle: any): void; @@ -4497,9 +4503,10 @@ declare namespace ts { } interface LanguageService { cleanupSemanticCache(): void; - getSyntacticDiagnostics(fileName: string): Diagnostic[]; + getSyntacticDiagnostics(fileName: string): DiagnosticWithLocation[]; + /** The first time this is called, it will return global diagnostics (no location). */ getSemanticDiagnostics(fileName: string): Diagnostic[]; - getSuggestionDiagnostics(fileName: string): Diagnostic[]; + getSuggestionDiagnostics(fileName: string): DiagnosticWithLocation[]; getCompilerOptionsDiagnostics(): Diagnostic[]; /** * @deprecated Use getEncodedSyntacticClassifications instead.