Skip to content

Commit

Permalink
Move toSourcePath to ProjectPrincipal constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jul 14, 2024
1 parent d7285c1 commit ef1ab49
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/knip/src/PrincipalFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ProjectPrincipal } from './ProjectPrincipal.js';
import type { AsyncCompilers, SyncCompilers } from './compilers/types.js';
import { debugLog } from './util/debug.js';
import { toAbsolute, toRelative } from './util/path.js';
import type { ToSourceFilePath } from './util/to-source-path.js';

type Paths = ts.CompilerOptions['paths'];

Expand All @@ -19,6 +20,7 @@ export type PrincipalOptions = {
isIsolateWorkspaces: boolean;
isSkipLibs: boolean;
isWatch: boolean;
toSourceFilePath: ToSourceFilePath;
};

const mapToAbsolutePaths = (paths: NonNullable<Paths>, cwd: string): Paths =>
Expand Down
11 changes: 7 additions & 4 deletions packages/knip/src/ProjectPrincipal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { DependencyGraph, Export, ExportMember, FileNode, UnresolvedImport
import type { BoundSourceFile } from './typescript/SourceFile.js';
import type { SourceFileManager } from './typescript/SourceFileManager.js';
import { createHosts } from './typescript/createHosts.js';
import { type GetImportsAndExportsOptions, _getImportsAndExports } from './typescript/getImportsAndExports.js';
import { type GetImportsAndExportsOptions, _getImportsAndExports } from './typescript/get-imports-and-exports.js';
import type { ResolveModuleNames } from './typescript/resolveModuleNames.js';
import { timerify } from './util/Performance.js';
import { compact } from './util/array.js';
Expand Down Expand Up @@ -69,6 +69,8 @@ export class ProjectPrincipal {

cache: CacheConsultant<FileNode>;

toSourceFilePath: ToSourceFilePath;

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

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

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

this.compilerOptions = {
Expand All @@ -98,16 +100,17 @@ export class ProjectPrincipal {
this.isSkipLibs = isSkipLibs;
this.isWatch = isWatch;
this.cache = new CacheConsultant(pkgName || ANONYMOUS);
this.toSourceFilePath = toSourceFilePath;
}

init(toSourceFilePath: ToSourceFilePath) {
init() {
const { fileManager, compilerHost, resolveModuleNames, languageServiceHost } = createHosts({
cwd: this.cwd,
compilerOptions: this.compilerOptions,
entryPaths: this.entryPaths,
compilers: [this.syncCompilers, this.asyncCompilers],
isSkipLibs: this.isSkipLibs,
toSourceFilePath,
toSourceFilePath: this.toSourceFilePath,
useResolverCache: !this.isWatch,
});

Expand Down
3 changes: 2 additions & 1 deletion packages/knip/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
isIsolateWorkspaces,
isSkipLibs,
isWatch,
toSourceFilePath,
});

const worker = new WorkspaceWorker({
Expand Down Expand Up @@ -331,7 +332,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
};

for (const principal of principals) {
principal.init(toSourceFilePath);
principal.init();

for (const [containingFilePath, specifier, workspaceName] of principal.referencedDependencies) {
const workspace = chief.findWorkspaceByName(workspaceName);
Expand Down
6 changes: 3 additions & 3 deletions packages/knip/src/util/to-source-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { debugLog } from './debug.js';
import { isDirectory } from './fs.js';
import { isInternal, join, toRelative } from './path.js';

export type ToSourceFilePath = (filePath: string) => undefined | string;

const defaultExtensions = `.{${DEFAULT_EXTENSIONS.map(ext => ext.slice(1)).join(',')}}`;
const hasJSExt = /\.(m|c)js$/;
const hasTSExt = /(?<!\.d)\.(m|c)?tsx?$/;
Expand All @@ -22,7 +20,7 @@ export const augmentWorkspace = (workspace: Workspace, dir: string, compilerOpti
export const getToSourcePathHandler = (chief: ConfigurationChief) => {
const toSourceMapCache = new Map<string, string>();

const toSourcePath: ToSourceFilePath = (filePath: string) => {
const toSourcePath = (filePath: string) => {
if (!isInternal(filePath) || hasJSExt.test(filePath) || hasTSExt.test(filePath)) return;
if (toSourceMapCache.has(filePath)) return toSourceMapCache.get(filePath);
const workspace = chief.findWorkspaceByFilePath(filePath);
Expand All @@ -43,3 +41,5 @@ export const getToSourcePathHandler = (chief: ConfigurationChief) => {

return toSourcePath;
};

export type ToSourceFilePath = ReturnType<typeof getToSourcePathHandler>;

0 comments on commit ef1ab49

Please sign in to comment.