Skip to content

Commit

Permalink
refactor: Add more silly/perf logging
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Sep 13, 2024
1 parent d1ab9bc commit ff90643
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
18 changes: 18 additions & 0 deletions src/linter/ui5Types/TypeLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default class TypeChecker {
}

async lint() {
const silly = log.isLevelEnabled("silly");
const files: FileContents = new Map();
const sourceMaps = new Map<string, string>(); // Maps a source path to source map content
let lazyFileLoading = true;
Expand All @@ -72,6 +73,14 @@ export default class TypeChecker {
lazyFileLoading = false;
pathsToLint = resources.map((resource) => resource.getPath());
}

// Sort paths to ensure consistent order (helps with debugging and comparing verbose/silly logs)
pathsToLint.sort((a, b) => a.localeCompare(b));

if (silly) {
log.silly(`pathsToLint: ${pathsToLint.join(", ")}`);
}

for (const resource of resources) {
const resourcePath = resource.getPath();
if (resourcePath.endsWith(".js.map")) {
Expand All @@ -91,8 +100,14 @@ export default class TypeChecker {
}

const host = await createVirtualCompilerHost(this.#compilerOptions, files, sourceMaps);

const createProgramDone = taskStart("ts.createProgram", undefined, true);
const program = ts.createProgram(pathsToLint, this.#compilerOptions, host);
createProgramDone();

const getTypeCheckerDone = taskStart("program.getTypeChecker", undefined, true);
const checker = program.getTypeChecker();
getTypeCheckerDone();

const dataTypesFile = await fs.readFile(
new URL("../../../resources/dataTypes.json", import.meta.url),
Expand All @@ -116,6 +131,9 @@ export default class TypeChecker {
manifestContent = await res.getString();
}
}
if (silly) {
log.silly(`Linting ${sourceFile.fileName}`);
}
const linterDone = taskStart("Type-check resource", sourceFile.fileName, true);
const linter = new SourceFileLinter(
this.#context, sourceFile.fileName,
Expand Down
40 changes: 35 additions & 5 deletions src/linter/ui5Types/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import fs from "node:fs/promises";
import {createRequire} from "node:module";
import transpileAmdToEsm from "./amdTranspiler/transpiler.js";
import {ResourcePath} from "../LinterContext.js";
import {getLogger} from "@ui5/logger";
const log = getLogger("linter:ui5Types:host");
const require = createRequire(import.meta.url);

interface PackageJson {
Expand Down Expand Up @@ -65,6 +67,8 @@ export async function createVirtualCompilerHost(
options: ts.CompilerOptions,
files: FileContents, sourceMaps: FileContents
): Promise<ts.CompilerHost> {
const silly = log.isLevelEnabled("silly");

const typePathMappings = new Map<string, string>();
addPathMappingForPackage("typescript", typePathMappings);

Expand Down Expand Up @@ -155,9 +159,17 @@ export async function createVirtualCompilerHost(
}
}

if (silly) {
log.silly(`compilerOptions: ${JSON.stringify(options, null, 2)}`);
}

const sourceFileCache = new Map<string, ts.SourceFile>();
return {
directoryExists: (directory) => {
if (silly) {
log.silly(`directoryExists: ${directory}`);
}

if (directories.has(directory)) {
return true;
}
Expand All @@ -179,6 +191,9 @@ export async function createVirtualCompilerHost(
},
fileExists: (fileName) => {
// NOTE: This function should be kept in sync with "getFile"
if (silly) {
log.silly(`fileExists: ${fileName}`);
}

if (files.has(fileName)) {
return true;
Expand All @@ -191,18 +206,29 @@ export async function createVirtualCompilerHost(
}
return false;
},
getCurrentDirectory: () => options.rootDir ?? "/",
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getCurrentDirectory: () => {
if (silly) {
log.silly(`getCurrentDirectory`);
}
return options.rootDir ?? "/";
},

getDirectories: (directory: string) => {
// This function seems to be called only if the "types" option is not set
// console.log(`getDirectories: ${directory}`);
if (silly) {
log.silly(`getDirectories: ${directory}`);
}
return [];
},
readDirectory: (
dirPath: string, extensions?: readonly string[],
exclude?: readonly string[], include?: readonly string[],
depth?: number
): string[] => {
if (silly) {
log.silly(`readDirectory: ${dirPath}`);
}

// This function doesn't seem to be called during normal operations
// console.log(`readDirectory: ${dirPath}`);
return Array.from(files.keys()).filter((filePath) => {
Expand All @@ -215,7 +241,9 @@ export async function createVirtualCompilerHost(
getSourceFile: (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
fileName: string, languageVersion: ts.ScriptTarget, onError?: (message: string) => void) => {
// console.log(`getSourceFile ${fileName}`);
if (silly) {
log.silly(`getSourceFile: ${fileName}`);
}
if (sourceFileCache.has(fileName)) {
return sourceFileCache.get(fileName);
}
Expand All @@ -229,7 +257,9 @@ export async function createVirtualCompilerHost(
return sourceFile;
},
readFile: (fileName) => {
// console.log(`readFile ${fileName}`);
if (silly) {
log.silly(`readFile: ${fileName}`);
}
return getFile(fileName);
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down

0 comments on commit ff90643

Please sign in to comment.