Skip to content

Commit c1d9cc6

Browse files
authored
feat(check-types), introduce --json flag (#9614)
1 parent 0e67d6c commit c1d9cc6

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

scopes/typescript/ts-server/format-diagnostics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DiagnosticMessageChain, server } from 'typescript';
22

3-
type Diagnostic = server.protocol.Diagnostic;
3+
export type Diagnostic = server.protocol.Diagnostic;
44

55
/**
66
* mostly taken from ts repo, src/compiler/program.ts "formatDiagnosticsWithColorAndContext" method.

scopes/typescript/ts-server/ts-server-client.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,27 @@ import { findPathToModule } from './modules-resolver';
99
import { ProcessBasedTsServer } from './process-based-tsserver';
1010
import { CommandTypes, EventName } from './tsp-command-types';
1111
import { getTsserverExecutable } from './utils';
12-
import { formatDiagnostic } from './format-diagnostics';
12+
import { formatDiagnostic, Diagnostic } from './format-diagnostics';
1313

1414
export type TsserverClientOpts = {
1515
verbose?: boolean; // print tsserver events to the console.
1616
tsServerPath?: string; // if not provided, it'll use findTsserverPath() strategies.
1717
checkTypes?: CheckTypes; // whether errors/warnings are monitored and printed to the console.
1818
printTypeErrors?: boolean; // whether print typescript errors to the console.
19+
aggregateDiagnosticData?: boolean; // whether to aggregate diagnostic data instead of printing them to the console.
1920
};
2021

22+
export type DiagnosticData = {
23+
file: string;
24+
diagnostic: Diagnostic;
25+
formatted: string;
26+
}
27+
2128
export class TsserverClient {
2229
private tsServer: ProcessBasedTsServer | null;
2330
public lastDiagnostics: ts.server.protocol.DiagnosticEventBody[] = [];
2431
private serverRunning = false;
25-
32+
public diagnosticData: DiagnosticData[] = [];
2633
constructor(
2734
/**
2835
* absolute root path of the project.
@@ -306,12 +313,24 @@ export class TsserverClient {
306313
}
307314

308315
private publishDiagnostic(message: ts.server.protocol.DiagnosticEvent) {
309-
if (!message.body?.diagnostics.length || !this.options.printTypeErrors) {
316+
if (!message.body?.diagnostics.length || (!this.options.printTypeErrors && !this.options.aggregateDiagnosticData)) {
310317
return;
311318
}
312319
this.lastDiagnostics.push(message.body);
313320
const file = path.relative(this.projectPath, message.body.file);
314-
message.body.diagnostics.forEach((diag) => this.logger.console(formatDiagnostic(diag, file)));
321+
message.body.diagnostics.forEach((diag) => {
322+
const formatted = formatDiagnostic(diag, file);
323+
if (this.options.printTypeErrors) {
324+
this.logger.console(formatted);
325+
}
326+
if (this.options.aggregateDiagnosticData) {
327+
this.diagnosticData.push({
328+
file,
329+
diagnostic: diag,
330+
formatted,
331+
});
332+
}
333+
});
315334
}
316335

317336
/**

scopes/typescript/typescript/cmds/check-types.cmd.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class CheckTypesCmd implements Command {
1414
options = [
1515
['a', 'all', 'check-types for all components, not only modified and new'],
1616
['', 'strict', 'in case issues found, exit with code 1'],
17+
['j', 'json', 'return the output in json format'],
1718
] as CommandOptions;
1819

1920
constructor(
@@ -46,4 +47,26 @@ export class CheckTypesCmd implements Command {
4647
data: chalk.green(`${msg}. no errors were found.`),
4748
};
4849
}
50+
51+
async json([pattern]: [string], { all = false, strict = false }: { all: boolean; strict: boolean }) {
52+
if (!this.workspace) throw new OutsideWorkspaceError();
53+
const components = await this.workspace.getComponentsByUserInput(all, pattern);
54+
const files = this.typescript.getSupportedFilesForTsserver(components);
55+
await this.typescript.initTsserverClientFromWorkspace({ aggregateDiagnosticData: true }, files);
56+
const tsserver = this.typescript.getTsserverClient();
57+
if (!tsserver) throw new Error(`unable to start tsserver`);
58+
await tsserver.getDiagnostic(files);
59+
const diagData = tsserver.diagnosticData;
60+
tsserver.killTsServer();
61+
if (tsserver.lastDiagnostics.length) {
62+
return {
63+
code: strict ? 1 : 0,
64+
data: diagData,
65+
};
66+
}
67+
return {
68+
code: 0,
69+
data: diagData,
70+
};
71+
}
4972
}

0 commit comments

Comments
 (0)