Skip to content

Commit 694dbf4

Browse files
committed
Cover analysis pipeline with --performance timerify
1 parent d4b59d8 commit 694dbf4

10 files changed

Lines changed: 31 additions & 22 deletions

File tree

packages/knip/src/ConfigurationChief.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { _dirGlob, removeProductionSuffix } from './util/glob.ts';
2121
import { graphSequencer } from './util/graph-sequencer.ts';
2222
import mapWorkspaces from './util/map-workspaces.ts';
2323
import { join, relative } from './util/path.ts';
24+
import { timerify } from './util/Performance.ts';
2425
import { normalizePluginConfig } from './util/plugin.ts';
2526
import { toRegexOrString } from './util/regex.ts';
2627
import { ELLIPSIS } from './util/string.ts';
@@ -114,6 +115,7 @@ export class ConfigurationChief {
114115
this.workspaces = options.workspaces;
115116
this.rawConfig = options.parsedConfig;
116117
this.config = this.normalize(options.parsedConfig ?? {});
118+
this.findWorkspaceByFilePath = timerify(this.findWorkspaceByFilePath.bind(this), 'findWorkspaceByFilePath');
117119
}
118120

119121
public getConfigurationHints() {

packages/knip/src/ProjectPrincipal.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ParseResult, Visitor } from 'oxc-parser';
22
import { extractSpecifiers } from './typescript/follow-imports.ts';
3-
import { parseFile } from './typescript/ast-nodes.ts';
3+
import { _parseFile } from './typescript/ast-nodes.ts';
44
import { CacheConsultant } from './CacheConsultant.ts';
55
import { getCompilerExtensions } from './compilers/index.ts';
66
import type { AsyncCompilers, SyncCompilers } from './compilers/types.ts';
@@ -69,6 +69,7 @@ export class ProjectPrincipal {
6969
this.fileManager = new SourceFileManager({
7070
compilers: [this.syncCompilers, this.asyncCompilers],
7171
});
72+
this.walkAndAnalyze = timerify(this.walkAndAnalyze.bind(this), 'walkAndAnalyze');
7273
}
7374

7475
addCompilers(compilers: [SyncCompilers, AsyncCompilers]) {
@@ -203,7 +204,7 @@ export class ProjectPrincipal {
203204
}
204205

205206
try {
206-
const result = parseFile(filePath, sourceText);
207+
const result = _parseFile(filePath, sourceText);
207208
this.fileManager.sourceTextCache.delete(filePath);
208209

209210
if (isProjectPath) {
@@ -235,7 +236,7 @@ export class ProjectPrincipal {
235236
if (!sourceText) continue;
236237

237238
try {
238-
const result = parseFile(filePath, sourceText);
239+
const result = _parseFile(filePath, sourceText);
239240
for (const specifier of extractSpecifiers(result, sourceText, filePath)) {
240241
const resolved = this.resolveSpecifier(specifier, filePath);
241242
if (resolved && !isInNodeModules(resolved)) visited.add(resolved);

packages/knip/src/WorkspaceWorker.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type { PackageJson } from './types/package-json.ts';
2121
import type { DependencySet } from './types/workspace.ts';
2222
import { createManifest, type Manifest } from './util/package-json.ts';
2323
import { collectStringLiterals, isExternalReExportsOnly } from './typescript/ast-helpers.ts';
24-
import { parseFile } from './typescript/ast-nodes.ts';
24+
import { _parseFile } from './typescript/ast-nodes.ts';
2525
import { compact } from './util/array.ts';
2626
import type { MainOptions } from './util/create-options.ts';
2727
import { debugLogArray, debugLogObject } from './util/debug.ts';
@@ -137,7 +137,7 @@ export class WorkspaceWorker {
137137

138138
this.cache = new CacheConsultant(`plugins-${name}`, options);
139139

140-
this.getConfigurationHints = timerify(this.getConfigurationHints.bind(this), 'worker.getConfigurationHints');
140+
this.getConfigurationHints = timerify(this.getConfigurationHints.bind(this), 'getConfigurationHints');
141141
}
142142

143143
public async init() {
@@ -322,7 +322,7 @@ export class WorkspaceWorker {
322322
const configFilesMap = this.configFilesMap;
323323
const configFiles = this.configFilesMap.get(wsName);
324324
const seen = new Map<string, Set<string>>();
325-
const parsedConfigCache = new Map<string, ReturnType<typeof parseFile> | undefined>();
325+
const parsedConfigCache = new Map<string, ReturnType<typeof _parseFile> | undefined>();
326326

327327
const storeConfigFilePath = (pluginName: PluginName, input: ConfigInput) => {
328328
const configFilePath = this.handleInput(input);
@@ -400,13 +400,13 @@ export class WorkspaceWorker {
400400
continue;
401401
}
402402

403-
let parsed: ReturnType<typeof parseFile> | undefined;
403+
let parsed: ReturnType<typeof _parseFile> | undefined;
404404
if (!isManifest) {
405405
if (parsedConfigCache.has(configFilePath)) {
406406
parsed = parsedConfigCache.get(configFilePath);
407407
} else {
408408
const sourceText = this.readFile(configFilePath);
409-
parsed = sourceText ? parseFile(configFilePath, sourceText) : undefined;
409+
parsed = sourceText ? _parseFile(configFilePath, sourceText) : undefined;
410410
parsedConfigCache.set(configFilePath, parsed);
411411
}
412412
}

packages/knip/src/plugins/nuxt/helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { readFileSync } from 'node:fs';
22
import { createRequire } from 'node:module';
33
import { Visitor, type ParseResult } from 'oxc-parser';
44
import { scriptBodies } from '../../compilers/compilers.ts';
5-
import { parseFile } from '../../typescript/ast-nodes.ts';
5+
import { _parseFile } from '../../typescript/ast-nodes.ts';
66
import { basename, dirname, isInNodeModules, join } from '../../util/path.ts';
77
import type { TemplateAstNode, VueSfc } from './types.ts';
88

@@ -25,7 +25,7 @@ const readFile = (filePath: string): string => {
2525
}
2626
};
2727

28-
export const readAndParseFile = (filePath: string) => parseFile(filePath, readFile(filePath));
28+
export const readAndParseFile = (filePath: string) => _parseFile(filePath, readFile(filePath));
2929

3030
export const collectIdentifiers = (source: string, fileName: string) => {
3131
const identifiers = new Set<string>();
@@ -34,7 +34,7 @@ export const collectIdentifiers = (source: string, fileName: string) => {
3434
identifiers.add(node.name);
3535
},
3636
});
37-
visitor.visit(parseFile(fileName, source).program);
37+
visitor.visit(_parseFile(fileName, source).program);
3838
return identifiers;
3939
};
4040

packages/knip/src/plugins/sst/resolveFromAST.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Visitor } from 'oxc-parser';
22
import type { ResolveFromAST } from '../../types/config.ts';
33
import { collectPropertyValues, getImportMap } from '../../typescript/ast-helpers.ts';
4-
import { parseFile } from '../../typescript/ast-nodes.ts';
4+
import { _parseFile } from '../../typescript/ast-nodes.ts';
55
import { toDeferResolveProductionEntry } from '../../util/input.ts';
66
import { dirname } from '../../util/path.ts';
77
import { _resolveSync } from '../../util/resolve.ts';
@@ -31,7 +31,7 @@ export const getInputsFromHandlers: ResolveFromAST = (program, options) => {
3131
if (resolvedPath) {
3232
const stackText = options.readFile(resolvedPath);
3333
if (stackText) {
34-
const stackResult = parseFile('stack.ts', stackText);
34+
const stackResult = _parseFile('stack.ts', stackText);
3535
addHandlers(collectPropertyValues(stackResult.program, 'handler'));
3636
}
3737
}

packages/knip/src/typescript/ast-helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ParseResult, Program } from 'oxc-parser';
22
import { Visitor } from 'oxc-parser';
33
import stripJsonComments from 'strip-json-comments';
44
import { extname, isInternal } from '../util/path.ts';
5-
import { getStringValue, isStringLiteral, parseFile } from './ast-nodes.ts';
5+
import { _parseFile, getStringValue, isStringLiteral } from './ast-nodes.ts';
66

77
export const getPropertyKey = (prop: any): string | undefined =>
88
prop?.key?.type === 'Identifier' ? prop.key.name : getStringValue(prop?.key);
@@ -189,7 +189,7 @@ export const collectStringLiterals = (sourceText: string, filePath: string): Set
189189
collectJsonStringLiterals(JSON.parse(stripJsonComments(sourceText, { trailingCommas: true })), literals);
190190
return literals;
191191
}
192-
const result = parseFile(filePath, sourceText);
192+
const result = _parseFile(filePath, sourceText);
193193
const visitor = new Visitor({
194194
Literal(node: any) {
195195
if (typeof node.value === 'string') literals.add(node.value);

packages/knip/src/typescript/ast-nodes.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { DEFAULT_EXTENSIONS, FIX_FLAGS, SYMBOL_TYPE } from '../constants.ts';
99
import { extname } from '../util/path.ts';
1010
import type { GetImportsAndExportsOptions, IgnoreExportsUsedInFile } from '../types/config.ts';
11+
import { timerify } from '../util/Performance.ts';
1112
import { EMPTY_TAGS } from './visitors/jsdoc.ts';
1213
import type { Fix } from '../types/exports.ts';
1314
import type { SymbolType } from '../types/issues.ts';
@@ -18,12 +19,14 @@ const defaultParseOptions = {
1819
experimentalRawTransfer: rawTransferSupported(),
1920
};
2021

21-
export const parseFile = (filePath: string, sourceText: string) => {
22+
const parseFile = (filePath: string, sourceText: string) => {
2223
const ext = extname(filePath);
2324
const parseFileName = DEFAULT_EXTENSIONS.has(ext) ? filePath : `${filePath}.ts`;
2425
return parseSync(parseFileName, sourceText, defaultParseOptions);
2526
};
2627

28+
export const _parseFile = timerify(parseFile);
29+
2730
export type ResolveModule = (specifier: string, containingFile: string) => ResolvedModule | undefined;
2831

2932
export interface ResolvedModule {

packages/knip/src/typescript/get-imports-and-exports.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import { dirname, isInNodeModules, resolve } from '../util/path.ts';
1111
import { shouldIgnore } from '../util/tag.ts';
1212
import { extractImportsFromComments } from './comments.ts';
1313
import {
14+
_parseFile,
1415
buildLineStarts,
1516
getLineAndCol,
16-
parseFile,
1717
shouldCountRefs,
1818
type ResolveModule,
1919
type ResolvedModule,
2020
} from './ast-nodes.ts';
2121
import { buildJSDocTagLookup } from './visitors/jsdoc.ts';
22-
import { walkAST } from './visitors/walk.ts';
22+
import { _walkAST } from './visitors/walk.ts';
2323

2424
interface AddInternalImportOptions {
2525
specifier: string;
@@ -209,7 +209,7 @@ const getImportsAndExports = (
209209
}
210210
};
211211

212-
const result = cachedParseResult ?? parseFile(filePath, sourceText);
212+
const result = cachedParseResult ?? _parseFile(filePath, sourceText);
213213
const lineStarts = buildLineStarts(sourceText);
214214
const getJSDocTags = buildJSDocTagLookup(result.comments, sourceText);
215215

@@ -346,7 +346,7 @@ const getImportsAndExports = (
346346
addImport(spec, undefined, undefined, undefined, pos, mod);
347347
}
348348

349-
const localRefs = walkAST(result.program, sourceText, filePath, {
349+
const localRefs = _walkAST(result.program, sourceText, filePath, {
350350
lineStarts,
351351
skipExports,
352352
options,

packages/knip/src/typescript/visitors/walk.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { IssueSymbol, SymbolType } from '../../types/issues.ts';
1717
import type { Export, ExportMember, ImportMap, ImportMaps } from '../../types/module-graph.ts';
1818
import { addValue } from '../../util/module-graph.ts';
1919
import { isInNodeModules } from '../../util/path.ts';
20+
import { timerify } from '../../util/Performance.ts';
2021
import { getLineAndCol, getStringValue, isStringLiteral, type ResolveModule } from '../ast-nodes.ts';
2122
import { EMPTY_TAGS } from './jsdoc.ts';
2223
import { handleCallExpression, handleNewExpression } from './calls.ts';
@@ -711,7 +712,7 @@ export function buildVisitor(pluginVisitorObjects: PluginVisitorObject[], includ
711712
return new Visitor(merged as VisitorObject);
712713
}
713714

714-
export function walkAST(program: Program, sourceText: string, filePath: string, ctx: WalkContext) {
715+
function walkAST(program: Program, sourceText: string, filePath: string, ctx: WalkContext) {
715716
const isJS =
716717
filePath.endsWith('.js') || filePath.endsWith('.mjs') || filePath.endsWith('.cjs') || filePath.endsWith('.jsx');
717718

@@ -821,3 +822,5 @@ export function walkAST(program: Program, sourceText: string, filePath: string,
821822
state = undefined!;
822823
return localRefs;
823824
}
825+
826+
export const _walkAST = timerify(walkAST);

packages/knip/src/util/glob-core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export const findAndParseGitignores = async (cwd: string, workspaceDirs?: Set<st
196196
.withPromise();
197197
};
198198

199-
await timerify(walkGitignores)();
199+
await walkGitignores();
200200

201201
// tinyglobby's `ignore` can't express unignores (see tinyglobby/fast-glob #86). Drop cached
202202
// ignore patterns shadowed by any unignore path (and its ancestor dirs) so glob() sees a

0 commit comments

Comments
 (0)