Skip to content

Commit

Permalink
Skip work, gc file manager
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Mar 15, 2024
1 parent 70e0900 commit 7dd0651
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/knip/src/PrincipalFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type PrincipalOptions = {
pkgName: string;
isGitIgnored: (path: string) => boolean;
isIsolateWorkspaces: boolean;
isSkipLibs: boolean;
};

const mapToAbsolutePaths = (paths: NonNullable<Paths>, cwd: string): Paths =>
Expand Down
5 changes: 4 additions & 1 deletion packages/knip/src/ProjectPrincipal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class ProjectPrincipal {
extensions: Set<string>;
syncCompilers: SyncCompilers;
asyncCompilers: AsyncCompilers;
isSkipLibs: boolean;

// @ts-expect-error Don't want to ignore this, but we're not touching this until after init()
backend: {
Expand All @@ -83,7 +84,7 @@ export class ProjectPrincipal {

findReferences?: ts.LanguageService['findReferences'];

constructor({ compilerOptions, cwd, compilers, isGitIgnored }: PrincipalOptions) {
constructor({ compilerOptions, cwd, compilers, isGitIgnored, isSkipLibs }: PrincipalOptions) {
this.cwd = cwd;

this.isGitIgnored = isGitIgnored;
Expand All @@ -99,6 +100,7 @@ export class ProjectPrincipal {
this.extensions = new Set([...DEFAULT_EXTENSIONS, ...getCompilerExtensions(compilers)]);
this.syncCompilers = syncCompilers;
this.asyncCompilers = asyncCompilers;
this.isSkipLibs = isSkipLibs;
}

init() {
Expand All @@ -109,6 +111,7 @@ export class ProjectPrincipal {
compilerOptions: this.compilerOptions,
entryPaths: this.entryPaths,
compilers: [this.syncCompilers, this.asyncCompilers],
isSkipLibs: this.isSkipLibs,
});

this.backend = {
Expand Down
1 change: 1 addition & 0 deletions packages/knip/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
pkgName,
isGitIgnored,
isIsolateWorkspaces,
isSkipLibs: !isReportClassMembers,
});

const worker = new WorkspaceWorker({
Expand Down
16 changes: 12 additions & 4 deletions packages/knip/src/typescript/SourceFileManager.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import ts from 'typescript';
import { debugLog } from '../util/debug.js';
import { extname, isInternal } from '../util/path.js';
import { extname, isInNodeModules, isInternal } from '../util/path.js';
import type { SyncCompilers, AsyncCompilers } from '../compilers/types.js';

interface SourceFileManagerOptions {
isSkipLibs: boolean;
compilers: [SyncCompilers, AsyncCompilers];
}

export class SourceFileManager {
isSkipLibs: boolean;
sourceFileCache = new Map<string, ts.SourceFile | undefined>();
snapshotCache = new Map<string, ts.IScriptSnapshot | undefined>();
syncCompilers?: SyncCompilers;
asyncCompilers?: AsyncCompilers;
syncCompilers: SyncCompilers;
asyncCompilers: AsyncCompilers;

installCompilers(compilers: [SyncCompilers, AsyncCompilers]) {
constructor({ compilers, isSkipLibs }: SourceFileManagerOptions) {
this.isSkipLibs = isSkipLibs;
this.syncCompilers = compilers[0];
this.asyncCompilers = compilers[1];
}
Expand All @@ -22,6 +29,7 @@ export class SourceFileManager {
}

getSourceFile(filePath: string) {
if (this.isSkipLibs && isInNodeModules(filePath)) return undefined;
if (this.sourceFileCache.has(filePath)) return this.sourceFileCache.get(filePath);
const contents = ts.sys.readFile(filePath);
if (typeof contents !== 'string') {
Expand Down
7 changes: 3 additions & 4 deletions packages/knip/src/typescript/createHosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ type CreateHostsOptions = {
compilerOptions: ts.CompilerOptions;
entryPaths: Set<string>;
compilers: [SyncCompilers, AsyncCompilers];
isSkipLibs: boolean;
};

const fileManager = new SourceFileManager();

export const createHosts = ({ cwd, compilerOptions, entryPaths, compilers }: CreateHostsOptions) => {
fileManager.installCompilers(compilers);
export const createHosts = ({ cwd, compilerOptions, entryPaths, compilers, isSkipLibs }: CreateHostsOptions) => {
const fileManager = new SourceFileManager({ compilers, isSkipLibs });
const virtualFileExtensions = getCompilerExtensions(compilers);
const sys = createCustomSys(cwd, virtualFileExtensions);
const resolveModuleNames = createCustomModuleResolver(sys, compilerOptions, virtualFileExtensions);
Expand Down
3 changes: 2 additions & 1 deletion packages/knip/src/typescript/resolveModuleNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from 'node:fs';
import { isBuiltin } from 'node:module';
import ts from 'typescript';
import { sanitizeSpecifier } from '../util/modules.js';
import { basename, dirname, extname, format, isAbsolute, isInternal, join } from '../util/path.js';
import { basename, dirname, extname, format, isAbsolute, isInNodeModules, isInternal, join } from '../util/path.js';
import { isDeclarationFileExtension } from './ast-helpers.js';
import { ensureRealFilePath, isVirtualFilePath } from './utils.js';

Expand Down Expand Up @@ -66,6 +66,7 @@ export function createCustomModuleResolver(

// No need to try and resolve builtins, bail out
if (isBuiltin(sanitizedSpecifier)) return undefined;
if (isInNodeModules(name)) return undefined;

const tsResolvedModule = ts.resolveModuleName(
sanitizedSpecifier,
Expand Down

0 comments on commit 7dd0651

Please sign in to comment.