Skip to content
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

[dev-tool] add command to type check files not being built by tshy #30810

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions common/tools/dev-tool/src/commands/run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default subCommand(commandInfo, {
"extract-api": () => import("./extract-api"),
bundle: () => import("./bundle"),
"build-test": () => import("./build-test"),
typecheck: () => import("./typecheck"),
"start-browser-relay": () => import("./startBrowserRelay"),

// "vendored" is a special command that passes through execution to dev-tool's own commands
Expand Down
52 changes: 52 additions & 0 deletions common/tools/dev-tool/src/commands/run/typecheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as path from "node:path";
import { leafCommand, makeCommandInfo } from "../../framework/command";
import { createPrinter } from "../../util/printer";
import { resolveProject } from "../../util/resolveProject";
import { Project } from "ts-morph";
import { DiagnosticCategory } from "typescript";

const log = createPrinter("typecheck");
jeremymeng marked this conversation as resolved.
Show resolved Hide resolved

export const commandInfo = makeCommandInfo(
"typecheck",
"typecheck typescript code files that are not part of tshy build",
{
paths: {
kind: "string",
description:
"comma-separated relative path patterns of additional files not included in tsconfig.json to check for compilation errors. Examples: '--paths samples-dev/**/*.ts,test/**/*.ts'.",
},
},
);

export default leafCommand(commandInfo, async (options) => {
log.info(`type-checking...`);
const { path: projPath } = await resolveProject(process.cwd());

const project = new Project({
tsConfigFilePath: path.join(projPath, "tsconfig.json"),
});

if (options.paths) {
jeremymeng marked this conversation as resolved.
Show resolved Hide resolved
const patterns = options.paths.split(",");
for (const pattern of patterns) {
const fullPaths = `${projPath}/${pattern}`;
log.info(` adding additional files at ${fullPaths}`);
project.addSourceFilesAtPaths(fullPaths);
}
}

const diagnostics = project.getPreEmitDiagnostics();
const hasError = diagnostics.some((d) => d.getCategory() === DiagnosticCategory.Error);
if (hasError) {
log.error(
project.formatDiagnosticsWithColorAndContext(
diagnostics.filter((d) => d.getCategory() === DiagnosticCategory.Error),
),
);
}
return !hasError;
MaryGao marked this conversation as resolved.
Show resolved Hide resolved
});