Skip to content

refactor(check-types), remove duplications between reporter/json #9619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Changes from all commits
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
31 changes: 16 additions & 15 deletions scopes/typescript/typescript/cmds/check-types.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,8 @@ export class CheckTypesCmd implements Command {
) {}

async report([pattern]: [string], { all = false, strict = false }: { all: boolean; strict: boolean }) {
if (!this.workspace) throw new OutsideWorkspaceError();
const components = await this.workspace.getComponentsByUserInput(all, pattern);
this.logger.setStatusLine(`checking types for ${components.length} components`);
const files = this.typescript.getSupportedFilesForTsserver(components);
await this.typescript.initTsserverClientFromWorkspace({ printTypeErrors: true }, files);
const tsserver = this.typescript.getTsserverClient();
if (!tsserver) throw new Error(`unable to start tsserver`);
const start = Date.now();
await tsserver.getDiagnostic(files);
const tsserver = await this.runDiagnosticOnTsServer(false, pattern, all);
const end = Date.now() - start;
const msg = `completed type checking (${end / 1000} sec)`;
tsserver.killTsServer();
Expand All @@ -49,13 +42,7 @@ export class CheckTypesCmd implements Command {
}

async json([pattern]: [string], { all = false, strict = false }: { all: boolean; strict: boolean }) {
if (!this.workspace) throw new OutsideWorkspaceError();
const components = await this.workspace.getComponentsByUserInput(all, pattern);
const files = this.typescript.getSupportedFilesForTsserver(components);
await this.typescript.initTsserverClientFromWorkspace({ aggregateDiagnosticData: true }, files);
const tsserver = this.typescript.getTsserverClient();
if (!tsserver) throw new Error(`unable to start tsserver`);
await tsserver.getDiagnostic(files);
const tsserver = await this.runDiagnosticOnTsServer(true, pattern, all);
const diagData = tsserver.diagnosticData;
tsserver.killTsServer();
if (tsserver.lastDiagnostics.length) {
Expand All @@ -69,4 +56,18 @@ export class CheckTypesCmd implements Command {
data: diagData,
};
}

private async runDiagnosticOnTsServer(isJson: boolean, pattern: string, all: boolean) {
if (!this.workspace) throw new OutsideWorkspaceError();
const components = await this.workspace.getComponentsByUserInput(all, pattern);
const files = this.typescript.getSupportedFilesForTsserver(components);
await this.typescript.initTsserverClientFromWorkspace({
aggregateDiagnosticData: isJson,
printTypeErrors: !isJson
}, files);
const tsserver = this.typescript.getTsserverClient();
if (!tsserver) throw new Error(`unable to start tsserver`);
await tsserver.getDiagnostic(files);
return tsserver;
}
}