Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Move lint diagnostics to their own calls and add cancellation token
  • Loading branch information
weswigham committed Aug 5, 2016
commit 0ca2ff97956c60d36442473097f954cb985e2831
4 changes: 2 additions & 2 deletions src/compiler/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ namespace ts {

export interface SyntacticLintProviderStatic extends BaseProviderStatic {
readonly ["extension-kind"]: ExtensionKind.SyntacticLint;
new (state: {ts: typeof ts, args: any, host: CompilerHost, program: Program}): LintWalker;
new (state: {ts: typeof ts, args: any, host: CompilerHost, program: Program, token: CancellationToken}): LintWalker;
}

export interface SemanticLintProviderStatic extends BaseProviderStatic {
readonly ["extension-kind"]: ExtensionKind.SemanticLint;
new (state: {ts: typeof ts, args: any, host: CompilerHost, program: Program, checker: TypeChecker}): LintWalker;
new (state: {ts: typeof ts, args: any, host: CompilerHost, program: Program, token: CancellationToken, checker: TypeChecker}): LintWalker;
}

export namespace ExtensionKind {
Expand Down
47 changes: 39 additions & 8 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1191,9 +1191,11 @@ namespace ts {
getSourceFiles: () => files,
getCompilerOptions: () => options,
getSyntacticDiagnostics,
getSyntacticLintDiagnostics,
getOptionsDiagnostics,
getGlobalDiagnostics,
getSemanticDiagnostics,
getSemanticLintDiagnostics,
getDeclarationDiagnostics,
getTypeChecker,
getClassifiableNames,
Expand Down Expand Up @@ -1502,10 +1504,18 @@ namespace ts {
return getDiagnosticsHelper(sourceFile, getSyntacticDiagnosticsForFile, cancellationToken);
}

function getSyntacticLintDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
return getDiagnosticsHelper(sourceFile, getSyntacticLintDiagnosticsForFile, cancellationToken);
}

function getSemanticDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
return getDiagnosticsHelper(sourceFile, getSemanticDiagnosticsForFile, cancellationToken);
}

function getSemanticLintDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
return getDiagnosticsHelper(sourceFile, getSemanticLintDiagnosticsForFile, cancellationToken);
}

function getDeclarationDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
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)
Expand All @@ -1517,7 +1527,7 @@ namespace ts {
}
}

function performLintPassOnFile(sourceFile: SourceFile, kind: ExtensionKind.SyntacticLint | ExtensionKind.SemanticLint): Diagnostic[] | undefined {
function performLintPassOnFile(sourceFile: SourceFile, token: CancellationToken, kind: ExtensionKind.SyntacticLint | ExtensionKind.SemanticLint): Diagnostic[] | undefined {
const lints = extensionCache.getCompilerExtensions()[kind];
if (!lints || !lints.length) {
return;
Expand All @@ -1534,19 +1544,25 @@ namespace ts {
const checker = getTypeChecker();
startExtensionProfile(profilingEnabled, name, "construct");
try {
walker = new (ctor as SemanticLintProviderStatic)({ ts, checker, args, host, program });
walker = new (ctor as SemanticLintProviderStatic)({ ts, args, host, program, token, checker });
}
catch (e) {
if (e instanceof OperationCanceledException) {
throw e;
}
diagnostics.push(createExtensionDiagnostic(name, `Lint construction failed: ${(e as Error).message}`, sourceFile, /*start*/undefined, /*length*/undefined, DiagnosticCategory.Error));
}
completeExtensionProfile(profilingEnabled, name, "construct");
}
else if (kind === ExtensionKind.SyntacticLint) {
startExtensionProfile(profilingEnabled, name, "construct");
try {
walker = new (ctor as SyntacticLintProviderStatic)({ ts, args, host, program });
walker = new (ctor as SyntacticLintProviderStatic)({ ts, args, host, program, token });
}
catch (e) {
if (e instanceof OperationCanceledException) {
throw e;
}
diagnostics.push(createExtensionDiagnostic(name, `Lint construction failed: ${(e as Error).message}`, /*sourceFile*/undefined, /*start*/undefined, /*length*/undefined, DiagnosticCategory.Error));
}
completeExtensionProfile(profilingEnabled, name, "construct");
Expand All @@ -1573,6 +1589,9 @@ namespace ts {
activeLint.accepted = !activeLint.walker.visit(node, error);
}
catch (e) {
if (e instanceof OperationCanceledException) {
throw e;
}
activeLint.errored = true;
diagnostics.push(createExtensionDiagnostic(errorQualifiedName("!!!"), `visit failed with error: ${e}`, /*sourceFile*/undefined, /*start*/undefined, /*length*/undefined, DiagnosticCategory.Error));
}
Expand Down Expand Up @@ -1614,6 +1633,9 @@ namespace ts {
activeLint.walker.afterVisit(node, error);
}
catch (e) {
if (e instanceof OperationCanceledException) {
throw e;
}
activeLint.errored = true;
diagnostics.push(createExtensionDiagnostic(errorQualifiedName("!!!"), `afterVisit failed with error: ${e}`, /*sourceFile*/undefined, /*start*/undefined, /*length*/undefined, DiagnosticCategory.Error));
}
Expand Down Expand Up @@ -1766,13 +1788,17 @@ namespace ts {
}

function getSyntacticDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
return sourceFile.parseDiagnostics;
}

function getSyntacticLintDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
if (!sourceFile.isDeclarationFile) {
const lintDiagnostics = performLintPassOnFile(sourceFile, ExtensionKind.SyntacticLint);
const lintDiagnostics = performLintPassOnFile(sourceFile, cancellationToken, ExtensionKind.SyntacticLint);
if (lintDiagnostics && lintDiagnostics.length) {
return sourceFile.parseDiagnostics.concat(lintDiagnostics);
return lintDiagnostics;
}
}
return sourceFile.parseDiagnostics;
return [];
}

function runWithCancellationToken<T>(func: () => T): T {
Expand Down Expand Up @@ -1812,9 +1838,14 @@ namespace ts {
typeChecker.getDiagnostics(sourceFile, cancellationToken);
const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName);
const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName);
const lintDiagnostics = (!sourceFile.isDeclarationFile) ? (performLintPassOnFile(sourceFile, ExtensionKind.SemanticLint) || []) : [];

return bindDiagnostics.concat(checkDiagnostics).concat(fileProcessingDiagnosticsInFile).concat(programDiagnosticsInFile).concat(lintDiagnostics);
return bindDiagnostics.concat(checkDiagnostics).concat(fileProcessingDiagnosticsInFile).concat(programDiagnosticsInFile);
});
}

function getSemanticLintDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
return runWithCancellationToken(() => {
return (!sourceFile.isDeclarationFile) ? (performLintPassOnFile(sourceFile, cancellationToken, ExtensionKind.SemanticLint) || []) : [];
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ namespace ts {
let diagnostics: Diagnostic[];

// First get and report any syntactic errors.
diagnostics = program.getSyntacticDiagnostics();
diagnostics = program.getSyntacticDiagnostics().concat(program.getSyntacticLintDiagnostics());

// Count warnings/messages and ignore them for determining continued error reporting
const nonErrorCount = countWhere(diagnostics, d => d.category !== DiagnosticCategory.Error);
Expand All @@ -618,7 +618,7 @@ namespace ts {
const nonErrorCount = countWhere(diagnostics, d => d.category !== DiagnosticCategory.Error);

if (diagnostics.length === nonErrorCount) {
diagnostics = diagnostics.concat(program.getSemanticDiagnostics());
diagnostics = diagnostics.concat(program.getSemanticDiagnostics()).concat(program.getSemanticLintDiagnostics());
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,8 @@ namespace ts {
getGlobalDiagnostics(cancellationToken?: CancellationToken): Diagnostic[];
getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
getSyntacticLintDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
getSemanticLintDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];

/**
Expand Down
8 changes: 5 additions & 3 deletions src/harness/extensionRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,12 @@ class ExtensionRunner extends RunnerBase {
const service = ts.createLanguageService(host);
const fileResults: Harness.Compiler.GeneratedFile[] = [];

const diagnostics = ts.concatenate(ts.concatenate(
const diagnostics = ts.concatenate(ts.concatenate(ts.concatenate(ts.concatenate(
service.getProgramDiagnostics(),
ts.flatten(ts.map(typescriptFiles, fileName => service.getSyntacticDiagnostics(this.getCanonicalFileName(fileName))))),
ts.flatten(ts.map(typescriptFiles, fileName => service.getSemanticDiagnostics(this.getCanonicalFileName(fileName)))));
ts.flatten(ts.map(typescriptFiles, fileName => service.getSyntacticLintDiagnostics(this.getCanonicalFileName(fileName))))),
ts.flatten(ts.map(typescriptFiles, fileName => service.getSemanticDiagnostics(this.getCanonicalFileName(fileName))))),
ts.flatten(ts.map(typescriptFiles, fileName => service.getSemanticLintDiagnostics(this.getCanonicalFileName(fileName)))));

const emitResult = service.getProgram().emit(/*targetSourceFile*/undefined, writeFile);

Expand All @@ -178,7 +180,7 @@ class ExtensionRunner extends RunnerBase {
const self = this;
const program = ts.createProgram(typescriptFiles, options, this.mockHost);
const fileResults: Harness.Compiler.GeneratedFile[] = [];
const diagnostics = ts.getPreEmitDiagnostics(program);
const diagnostics = ts.concatenate(ts.getPreEmitDiagnostics(program), ts.concatenate(program.getSyntacticLintDiagnostics(), program.getSemanticLintDiagnostics()));
const emitResult = program.emit(/*targetSourceFile*/undefined, writeFile);

const allDiagnostics = ts.sortAndDeduplicateDiagnostics(ts.concatenate(diagnostics, emitResult.diagnostics));
Expand Down
6 changes: 6 additions & 0 deletions src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,15 @@ namespace Harness.LanguageService {
getSyntacticDiagnostics(fileName: string): ts.Diagnostic[] {
return unwrapJSONCallResult(this.shim.getSyntacticDiagnostics(fileName));
}
getSyntacticLintDiagnostics(fileName: string): ts.Diagnostic[] {
return unwrapJSONCallResult(this.shim.getSyntacticLintDiagnostics(fileName));
}
getSemanticDiagnostics(fileName: string): ts.Diagnostic[] {
return unwrapJSONCallResult(this.shim.getSemanticDiagnostics(fileName));
}
getSemanticLintDiagnostics(fileName: string): ts.Diagnostic[] {
return unwrapJSONCallResult(this.shim.getSemanticLintDiagnostics(fileName));
}
getCompilerOptionsDiagnostics(): ts.Diagnostic[] {
return unwrapJSONCallResult(this.shim.getCompilerOptionsDiagnostics());
}
Expand Down
8 changes: 8 additions & 0 deletions src/server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,18 @@ namespace ts.server {
throw new Error("Not Implemented Yet.");
}

getSyntacticLintDiagnostics(fileName: string): Diagnostic[] {
throw new Error("Not Implemented Yet.");
}

getSemanticDiagnostics(fileName: string): Diagnostic[] {
throw new Error("Not Implemented Yet.");
}

getSemanticLintDiagnostics(fileName: string): Diagnostic[] {
throw new Error("Not Implemented Yet.");
}

getCompilerOptionsDiagnostics(): Diagnostic[] {
return this.getProgramDiagnostics();
}
Expand Down
16 changes: 16 additions & 0 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ namespace ts.server {
export const Geterr = "geterr";
export const GeterrForProject = "geterrForProject";
export const SemanticDiagnosticsSync = "semanticDiagnosticsSync";
export const SemanticLintDiagnosticsSync = "semanticLintDiagnosticsSync";
export const SyntacticDiagnosticsSync = "syntacticDiagnosticsSync";
export const SyntacticLintDiagnosticsSync = "syntacticLintDiagnosticsSync";
export const NavBar = "navbar";
export const Navto = "navto";
export const Occurrences = "occurrences";
Expand Down Expand Up @@ -404,10 +406,18 @@ namespace ts.server {
return this.getDiagnosticsWorker(args, (project, file) => project.compilerService.languageService.getSyntacticDiagnostics(file));
}

private getSyntacticLintDiagnosticsSync(args: protocol.FileRequestArgs): protocol.Diagnostic[] {
return this.getDiagnosticsWorker(args, (project, file) => project.compilerService.languageService.getSyntacticLintDiagnostics(file));
}

private getSemanticDiagnosticsSync(args: protocol.FileRequestArgs): protocol.Diagnostic[] {
return this.getDiagnosticsWorker(args, (project, file) => project.compilerService.languageService.getSemanticDiagnostics(file));
}

private getSemanticLintDiagnosticsSync(args: protocol.FileRequestArgs): protocol.Diagnostic[] {
return this.getDiagnosticsWorker(args, (project, file) => project.compilerService.languageService.getSemanticLintDiagnostics(file));
}

private getDocumentHighlights(line: number, offset: number, fileName: string, filesToSearch: string[]): protocol.DocumentHighlightsItem[] {
fileName = ts.normalizePath(fileName);
const project = this.projectService.getProjectForFile(fileName);
Expand Down Expand Up @@ -1132,9 +1142,15 @@ namespace ts.server {
[CommandNames.SemanticDiagnosticsSync]: (request: protocol.FileRequest) => {
return this.requiredResponse(this.getSemanticDiagnosticsSync(request.arguments));
},
[CommandNames.SemanticLintDiagnosticsSync]: (request: protocol.FileRequest) => {
return this.requiredResponse(this.getSemanticLintDiagnosticsSync(request.arguments));
},
[CommandNames.SyntacticDiagnosticsSync]: (request: protocol.FileRequest) => {
return this.requiredResponse(this.getSyntacticDiagnosticsSync(request.arguments));
},
[CommandNames.SyntacticLintDiagnosticsSync]: (request: protocol.FileRequest) => {
return this.requiredResponse(this.getSyntacticLintDiagnosticsSync(request.arguments));
},
[CommandNames.Geterr]: (request: protocol.Request) => {
const geterrArgs = <protocol.GeterrRequestArgs>request.arguments;
return { response: this.getDiagnostics(geterrArgs.delay, geterrArgs.files), responseRequired: false };
Expand Down
16 changes: 16 additions & 0 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,9 @@ namespace ts {
cleanupSemanticCache(): void;

getSyntacticDiagnostics(fileName: string): Diagnostic[];
getSyntacticLintDiagnostics(fileName: string): Diagnostic[];
getSemanticDiagnostics(fileName: string): Diagnostic[];
getSemanticLintDiagnostics(fileName: string): Diagnostic[];

/**
* @deprecated Use getProgramDiagnostics instead.
Expand Down Expand Up @@ -3300,6 +3302,12 @@ namespace ts {
return program.getSyntacticDiagnostics(getValidSourceFile(fileName), cancellationToken);
}

function getSyntacticLintDiagnostics(fileName: string) {
synchronizeHostData();

return program.getSyntacticLintDiagnostics(getValidSourceFile(fileName), cancellationToken);
}

/**
* getSemanticDiagnostics return array of Diagnostics. If '-d' is not enabled, only report semantic errors
* If '-d' enabled, report both semantic and emitter errors
Expand All @@ -3322,6 +3330,12 @@ namespace ts {
return concatenate(semanticDiagnostics, declarationDiagnostics);
}

function getSemanticLintDiagnostics(fileName: string) {
synchronizeHostData();

return program.getSemanticLintDiagnostics(getValidSourceFile(fileName), cancellationToken);
}

function getProgramDiagnostics() {
synchronizeHostData();
return program.getOptionsDiagnostics(cancellationToken).concat(
Expand Down Expand Up @@ -8282,7 +8296,9 @@ namespace ts {
dispose,
cleanupSemanticCache,
getSyntacticDiagnostics,
getSyntacticLintDiagnostics,
getSemanticDiagnostics,
getSemanticLintDiagnostics,
getCompilerOptionsDiagnostics: getProgramDiagnostics,
getProgramDiagnostics,
getSyntacticClassifications,
Expand Down
Loading