Skip to content

Commit

Permalink
feat: #337 - Add DiagnosticWithLocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jun 2, 2018
1 parent a20cd4e commit f1f700e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 17 deletions.
25 changes: 20 additions & 5 deletions lib/ts-simple-ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8030,7 +8030,7 @@ export declare class Program {
* Gets the syntactic diagnostics.
* @param sourceFile - Optional source file.
*/
getSyntacticDiagnostics(sourceFile?: SourceFile): Diagnostic[];
getSyntacticDiagnostics(sourceFile?: SourceFile): DiagnosticWithLocation[];
/**
* Gets the semantic diagnostics.
* @param sourceFile - Optional source file.
Expand All @@ -8040,7 +8040,7 @@ export declare class Program {
* Gets the declaration diagnostics.
* @param sourceFile - Optional source file.
*/
getDeclarationDiagnostics(sourceFile?: SourceFile): Diagnostic[];
getDeclarationDiagnostics(sourceFile?: SourceFile): DiagnosticWithLocation[];
/**
* Gets the pre-emit diagnostics.
* @param sourceFile - Source file.
Expand Down Expand Up @@ -8193,11 +8193,11 @@ export declare class DefinitionInfo<TCompilerObject extends ts.DefinitionInfo =
/**
* Diagnostic.
*/
export declare class Diagnostic {
export declare class Diagnostic<TCompilerObject extends ts.Diagnostic = ts.Diagnostic> {
/**
* Gets the underlying compiler diagnostic.
*/
readonly compilerObject: ts.Diagnostic;
readonly compilerObject: TCompilerObject;
/**
* Gets the source file.
*/
Expand Down Expand Up @@ -8228,6 +8228,21 @@ export declare class Diagnostic {
getSource(): string | undefined;
}

export declare class DiagnosticWithLocation extends Diagnostic<ts.DiagnosticWithLocation> {
/**
* Gets the start.
*/
getStart(): number;
/**
* Gets the length
*/
getLength(): number;
/**
* Gets the source file.
*/
getSourceFile(): SourceFile;
}

/**
* Diagnostic message chain.
*/
Expand Down Expand Up @@ -8310,7 +8325,7 @@ export declare class EmitResult {
/**
* Contains declaration emit diagnostics.
*/
getDiagnostics(): Diagnostic[];
getDiagnostics(): Diagnostic<ts.Diagnostic>[];
}

export declare class FileTextChanges {
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/tools/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as tsInternal from "../../typescript/tsInternal";
import { GlobalContainer } from "../../GlobalContainer";
import { TypeChecker } from "./TypeChecker";
import { SourceFile } from "../file";
import { EmitResult, Diagnostic } from "./results";
import { EmitResult, Diagnostic, DiagnosticWithLocation } from "./results";

/**
* Options for emitting.
Expand Down Expand Up @@ -89,9 +89,9 @@ export class Program {
* Gets the syntactic diagnostics.
* @param sourceFile - Optional source file.
*/
getSyntacticDiagnostics(sourceFile?: SourceFile): Diagnostic[] {
getSyntacticDiagnostics(sourceFile?: SourceFile): DiagnosticWithLocation[] {
const compilerDiagnostics = this.compilerObject.getSyntacticDiagnostics(sourceFile == null ? undefined : sourceFile.compilerNode);
return compilerDiagnostics.map(d => this.global.compilerFactory.getDiagnostic(d));
return compilerDiagnostics.map(d => this.global.compilerFactory.getDiagnosticWithLocation(d));
}

/**
Expand All @@ -107,9 +107,9 @@ export class Program {
* Gets the declaration diagnostics.
* @param sourceFile - Optional source file.
*/
getDeclarationDiagnostics(sourceFile?: SourceFile): Diagnostic[] {
getDeclarationDiagnostics(sourceFile?: SourceFile): DiagnosticWithLocation[] {
const compilerDiagnostics = this.compilerObject.getDeclarationDiagnostics(sourceFile == null ? undefined : sourceFile.compilerNode);
return compilerDiagnostics.map(d => this.global.compilerFactory.getDiagnostic(d));
return compilerDiagnostics.map(d => this.global.compilerFactory.getDiagnosticWithLocation(d));
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/tools/results/Diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import { DiagnosticMessageChain } from "./DiagnosticMessageChain";
/**
* Diagnostic.
*/
export class Diagnostic {
export class Diagnostic<TCompilerObject extends ts.Diagnostic = ts.Diagnostic> {
/** @internal */
readonly global: GlobalContainer | undefined;
/** @internal */
readonly _compilerObject: ts.Diagnostic;
readonly _compilerObject: TCompilerObject;

/** @internal */
constructor(global: GlobalContainer | undefined, compilerObject: ts.Diagnostic) {
constructor(global: GlobalContainer | undefined, compilerObject: TCompilerObject) {
this.global = global;
this._compilerObject = compilerObject;
}

/**
* Gets the underlying compiler diagnostic.
*/
get compilerObject(): ts.Diagnostic {
get compilerObject(): TCompilerObject {
return this._compilerObject;
}

Expand Down
31 changes: 31 additions & 0 deletions src/compiler/tools/results/DiagnosticWithLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Diagnostic } from "./Diagnostic";
import { ts } from "../../../typescript";
import { GlobalContainer } from "../../../GlobalContainer";

export class DiagnosticWithLocation extends Diagnostic<ts.DiagnosticWithLocation> {
/** @internal */
constructor(global: GlobalContainer | undefined, compilerObject: ts.DiagnosticWithLocation) {
super(global, compilerObject);
}

/**
* Gets the start.
*/
getStart() {
return super.getStart()!;
}

/**
* Gets the length
*/
getLength() {
return super.getLength()!;
}

/**
* Gets the source file.
*/
getSourceFile() {
return super.getSourceFile()!;
}
}
1 change: 1 addition & 0 deletions src/compiler/tools/results/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./DefinitionInfo";
export * from "./Diagnostic";
export * from "./DiagnosticWithLocation";
export * from "./DiagnosticMessageChain";
export * from "./DocumentSpan";
export * from "./EmitOutput";
Expand Down
19 changes: 16 additions & 3 deletions src/factories/CompilerFactory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ts, SyntaxKind, TypeFlags } from "../typescript";
import { SourceFile, Node, SymbolDisplayPart, Symbol, Type, TypeParameter, Signature, DefinitionInfo, Diagnostic, DiagnosticMessageChain,
JSDocTagInfo, ReferencedSymbol, ReferencedSymbolDefinitionInfo, DocumentSpan, ReferenceEntry } from "../compiler";
import { SourceFile, Node, SymbolDisplayPart, Symbol, Type, TypeParameter, Signature, DefinitionInfo, Diagnostic, DiagnosticWithLocation,
DiagnosticMessageChain, JSDocTagInfo, ReferencedSymbol, ReferencedSymbolDefinitionInfo, DocumentSpan,
ReferenceEntry } from "../compiler";
import * as errors from "../errors";
import { SourceFileStructure } from "../structures";
import { SourceFileStructurePrinter } from "../structurePrinters";
Expand Down Expand Up @@ -434,7 +435,19 @@ export class CompilerFactory {
* @param diagnostic - Compiler diagnostic.
*/
getDiagnostic(diagnostic: ts.Diagnostic): Diagnostic {
return this.diagnosticCache.getOrCreate(diagnostic, () => new Diagnostic(this.global, diagnostic));
return this.diagnosticCache.getOrCreate(diagnostic, () => {
if (diagnostic.start != null)
return new DiagnosticWithLocation(this.global, diagnostic as ts.DiagnosticWithLocation);
return new Diagnostic(this.global, diagnostic);
});
}

/**
* Gets a wrapped diagnostic with location from a compiler diagnostic.
* @param diagnostic - Compiler diagnostic.
*/
getDiagnosticWithLocation(diagnostic: ts.DiagnosticWithLocation): DiagnosticWithLocation {
return this.diagnosticCache.getOrCreate(diagnostic, () => new DiagnosticWithLocation(this.global, diagnostic));
}

/**
Expand Down

0 comments on commit f1f700e

Please sign in to comment.