Skip to content

Commit

Permalink
feat: cache all source files and set up unique IDs for later dedup pu…
Browse files Browse the repository at this point in the history
…rposes
  • Loading branch information
sorccu committed Jan 17, 2025
1 parent fd3a92a commit 1b5e278
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path'

import { TSConfigFile, Options, Schema } from './tsconfig-json-file'
import { TSConfigFile, Schema } from './tsconfig-json-file'
import { JsonSourceFile } from './json-source-file'

/**
Expand All @@ -14,25 +14,14 @@ import { JsonSourceFile } from './json-source-file'
export class JSConfigFile extends TSConfigFile {
static FILENAME = 'jsconfig.json'

static #id = 0
readonly id = ++JSConfigFile.#id

static filePath (dirPath: string) {
return path.join(dirPath, JSConfigFile.FILENAME)
}

static loadFromJsonSourceFile (jsonFile: JsonSourceFile<Schema>): JSConfigFile | undefined {
return new JSConfigFile(jsonFile)
}

static loadFromFilePath (filePath: string, options?: Options): JSConfigFile | undefined {
const { jsonSourceFileLoader } = {
jsonSourceFileLoader: JsonSourceFile.loadFromFilePath<Schema>,
...options,
}

const jsonFile = jsonSourceFileLoader(filePath)
if (jsonFile === undefined) {
return
}

return new JSConfigFile(jsonFile)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { SourceFile } from './source-file'
import type { LoadFile } from './loader'

export type Options = {
sourceFileLoader?: LoadFile<SourceFile>,
}

export class JsonSourceFile<Schema> {
static #id = 0
readonly id = ++JsonSourceFile.#id

sourceFile: SourceFile
data: Schema

Expand All @@ -26,21 +24,4 @@ export class JsonSourceFile<Schema> {
} catch (err: any) {
}
}

static loadFromFilePath<Schema> (
filePath: string,
options?: Options,
): JsonSourceFile<Schema> | undefined {
const { sourceFileLoader } = {
sourceFileLoader: SourceFile.loadFromFilePath,
...options,
}

const sourceFile = sourceFileLoader(filePath)
if (sourceFile === undefined) {
return
}

return JsonSourceFile.loadFromSourceFile(sourceFile)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from 'node:path'

import { JsonSourceFile } from './json-source-file'
import type { LoadFile } from './loader'

type ExportCondition =
'node-addons' | 'node' | 'import' | 'require' | 'module-sync' | 'default'
Expand All @@ -11,13 +10,12 @@ type Schema = {
exports?: string | string[] | Record<string, string> | Record<ExportCondition, Record<string, string>>
}

export type Options = {
jsonSourceFileLoader?: LoadFile<JsonSourceFile<Schema>>,
}

export class PackageJsonFile {
static FILENAME = 'package.json'

static #id = 0
readonly id = ++PackageJsonFile.#id

jsonFile: JsonSourceFile<Schema>
basePath: string
mainPaths: string[]
Expand All @@ -41,20 +39,6 @@ export class PackageJsonFile {
return new PackageJsonFile(jsonFile)
}

static loadFromFilePath (filePath: string, options?: Options): PackageJsonFile | undefined {
const { jsonSourceFileLoader } = {
jsonSourceFileLoader: JsonSourceFile.loadFromFilePath<Schema>,
...options,
}

const jsonFile = jsonSourceFileLoader(filePath)
if (jsonFile === undefined) {
return
}

return new PackageJsonFile(jsonFile)
}

static filePath (dirPath: string) {
return path.join(dirPath, PackageJsonFile.FILENAME)
}
Expand Down
72 changes: 58 additions & 14 deletions packages/cli/src/services/check-parser/package-files/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,56 @@ import { PackageJsonFile } from './package-json-file'
import { TSConfigFile } from './tsconfig-json-file'
import { JSConfigFile } from './jsconfig-json-file'
import { isLocalPath, PathResult } from './paths'
import { FileLoader } from './loader'
import { FileLoader, LoadFile } from './loader'
import { JsonSourceFile } from './json-source-file'

class PackageFilesCache {
packageJsonCache = new FileLoader(PackageJsonFile.loadFromFilePath)
tsconfigJsonCache = new FileLoader(TSConfigFile.loadFromFilePath)
jsconfigJsonCache = new FileLoader(JSConfigFile.loadFromFilePath)
#sourceFileCache = new FileLoader(SourceFile.loadFromFilePath)

#jsonFileLoader<T, S> (load: (jsonFile: JsonSourceFile<S>) => T | undefined): LoadFile<T> {
return filePath => {
const sourceFile = this.#sourceFileCache.load(filePath)
if (sourceFile === undefined) {
return
}

const jsonFile = JsonSourceFile.loadFromSourceFile<S>(sourceFile)
if (jsonFile === undefined) {
return
}

return load(jsonFile)
}
}

#packageJsonCache = new FileLoader(this.#jsonFileLoader(PackageJsonFile.loadFromJsonSourceFile))
#tsconfigJsonCache = new FileLoader(this.#jsonFileLoader(TSConfigFile.loadFromJsonSourceFile))
#jsconfigJsonCache = new FileLoader(this.#jsonFileLoader(JSConfigFile.loadFromJsonSourceFile))

sourceFile (filePath: string, suffixes?: string[]) {
for (const suffix of ['', ...suffixes ?? []]) {
const suffixFilePath = filePath + suffix

const sourceFile = this.#sourceFileCache.load(suffixFilePath)
if (sourceFile === undefined) {
continue
}

return sourceFile
}
}

packageJson (filePath: string) {
return this.#packageJsonCache.load(filePath)
}

tsconfigJson (filePath: string) {
return this.#tsconfigJsonCache.load(filePath)
}

jsconfigJson (filePath: string) {
return this.#jsconfigJsonCache.load(filePath)
}
}

class PackageFiles {
Expand All @@ -22,15 +66,15 @@ class PackageFiles {

satisfyFromDirPath (dirPath: string, cache: PackageFilesCache): boolean {
if (this.packageJson === undefined) {
this.packageJson = cache.packageJsonCache.load(PackageJsonFile.filePath(dirPath))
this.packageJson = cache.packageJson(PackageJsonFile.filePath(dirPath))
}

if (this.tsconfigJson === undefined && this.jsconfigJson === undefined) {
this.tsconfigJson = cache.tsconfigJsonCache.load(TSConfigFile.filePath(dirPath))
this.tsconfigJson = cache.tsconfigJson(TSConfigFile.filePath(dirPath))
}

if (this.jsconfigJson === undefined && this.tsconfigJson === undefined) {
this.jsconfigJson = cache.jsconfigJsonCache.load(JSConfigFile.filePath(dirPath))
this.jsconfigJson = cache.jsconfigJson(JSConfigFile.filePath(dirPath))
}

return this.satisfied
Expand Down Expand Up @@ -143,7 +187,7 @@ export class PackageFilesResolver {

private resolveSourceFile (sourceFile: SourceFile): SourceFile[] {
if (sourceFile.meta.basename === PackageJsonFile.FILENAME) {
const packageJson = this.cache.packageJsonCache.load(sourceFile.meta.filePath)
const packageJson = this.cache.packageJson(sourceFile.meta.filePath)
if (packageJson === undefined) {
// This should never happen unless the package.json is invalid or
// something.
Expand All @@ -166,7 +210,7 @@ export class PackageFilesResolver {

const candidatePaths = configJson.collectLookupPaths(mainPath)
for (const candidatePath of candidatePaths) {
const mainSourceFile = SourceFile.loadFromFilePath(candidatePath)
const mainSourceFile = this.cache.sourceFile(candidatePath)
if (mainSourceFile === undefined) {
continue
}
Expand All @@ -177,7 +221,7 @@ export class PackageFilesResolver {
}
}

const mainSourceFile = SourceFile.loadFromFilePath(mainPath)
const mainSourceFile = this.cache.sourceFile(mainPath)
if (mainSourceFile === undefined) {
continue
}
Expand Down Expand Up @@ -211,7 +255,7 @@ export class PackageFilesResolver {
for (const importPath of dependencies) {
if (isLocalPath(importPath)) {
const relativeDepPath = path.resolve(dirname, importPath)
const sourceFile = SourceFile.loadFromFilePath(relativeDepPath, suffixes)
const sourceFile = this.cache.sourceFile(relativeDepPath, suffixes)
if (sourceFile !== undefined) {
const resolvedFiles = this.resolveSourceFile(sourceFile)
let found = false
Expand Down Expand Up @@ -245,7 +289,7 @@ export class PackageFilesResolver {
let found = false
for (const { source, target } of resolvedPaths) {
const relativePath = path.resolve(configJson.basePath, target.path)
const sourceFile = SourceFile.loadFromFilePath(relativePath, suffixes)
const sourceFile = this.cache.sourceFile(relativePath, suffixes)
if (sourceFile !== undefined) {
const resolvedFiles = this.resolveSourceFile(sourceFile)
for (const resolvedFile of resolvedFiles) {
Expand Down Expand Up @@ -282,7 +326,7 @@ export class PackageFilesResolver {

if (configJson.baseUrl !== undefined) {
const relativePath = path.resolve(configJson.basePath, configJson.baseUrl, importPath)
const sourceFile = SourceFile.loadFromFilePath(relativePath, suffixes)
const sourceFile = this.cache.sourceFile(relativePath, suffixes)
if (sourceFile !== undefined) {
const resolvedFiles = this.resolveSourceFile(sourceFile)
let found = false
Expand Down Expand Up @@ -312,7 +356,7 @@ export class PackageFilesResolver {
if (packageJson !== undefined) {
if (packageJson.supportsPackageRelativePaths()) {
const relativePath = path.resolve(packageJson.basePath, importPath)
const sourceFile = SourceFile.loadFromFilePath(relativePath, suffixes)
const sourceFile = this.cache.sourceFile(relativePath, suffixes)
if (sourceFile !== undefined) {
const resolvedFiles = this.resolveSourceFile(sourceFile)
let found = false
Expand Down
23 changes: 11 additions & 12 deletions packages/cli/src/services/check-parser/package-files/source-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export class FileMeta {
}

export class SourceFile {
static #id = 0
readonly id = ++SourceFile.#id

contents: string
meta: FileMeta

Expand All @@ -30,20 +33,16 @@ export class SourceFile {
this.contents = contents
}

static loadFromFilePath (filePath: string, suffixes?: string[]): SourceFile | undefined {
for (const suffix of ['', ...suffixes ?? []]) {
try {
const suffixFilePath = filePath + suffix

const contents = fs.readFileSync(suffixFilePath, {
encoding: 'utf8',
})
static loadFromFilePath (filePath: string): SourceFile | undefined {
try {
const contents = fs.readFileSync(filePath, {
encoding: 'utf8',
})

const meta = FileMeta.fromFilePath(suffixFilePath)
const meta = FileMeta.fromFilePath(filePath)

return new SourceFile(meta, contents)
} catch (err: any) {
}
return new SourceFile(meta, contents)
} catch (err: any) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import path from 'node:path'
import { SourceFile } from './source-file'
import { JsonSourceFile } from './json-source-file'
import { PathResolver, ResolveResult } from './paths'
import type { LoadFile } from './loader'

type Module =
'none' | 'commonjs' | 'amd' | 'system' | 'es6' | 'es2015' | 'es2020' |
Expand Down Expand Up @@ -61,10 +60,6 @@ export interface Schema {
compilerOptions?: CompilerOptions
}

export type Options = {
jsonSourceFileLoader?: LoadFile<JsonSourceFile<Schema>>,
}

type JSExtension = '.js' | '.mjs' | '.cjs'

const JSExtensions: JSExtension[] = ['.js', '.mjs', '.cjs']
Expand All @@ -85,6 +80,9 @@ const extensionMappings: JSExtensionMappings = {
export class TSConfigFile {
static FILENAME = 'tsconfig.json'

static #id = 0
readonly id = ++TSConfigFile.#id

jsonFile: JsonSourceFile<Schema>
basePath: string
moduleResolution: string
Expand Down Expand Up @@ -116,20 +114,6 @@ export class TSConfigFile {
return new TSConfigFile(jsonFile)
}

static loadFromFilePath (filePath: string, options?: Options): TSConfigFile | undefined {
const { jsonSourceFileLoader } = {
jsonSourceFileLoader: JsonSourceFile.loadFromFilePath<Schema>,
...options,
}

const jsonFile = jsonSourceFileLoader(filePath)
if (jsonFile === undefined) {
return
}

return new TSConfigFile(jsonFile)
}

static filePath (dirPath: string) {
return path.join(dirPath, TSConfigFile.FILENAME)
}
Expand Down

0 comments on commit 1b5e278

Please sign in to comment.