Skip to content

Commit

Permalink
refactor: use branded types for IDs (#8105)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan authored May 20, 2024
1 parent c649954 commit f0b51bb
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 106 deletions.
2 changes: 1 addition & 1 deletion deps/graph-builder/src/lockfileToDepGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function getChildrenPaths (
registries: Registries
virtualStoreDir: string
storeDir: string
skipped: Set<string>
skipped: Set<DepPath>
pkgSnapshotsByDepPaths: Record<DepPath, PackageSnapshot>
lockfileDir: string
sideEffectsCacheRead: boolean
Expand Down
12 changes: 6 additions & 6 deletions exec/build-modules/src/buildSequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export type DependenciesGraph<T extends string> = Record<T, DependenciesGraphNod

export function buildSequence<T extends string> (
depGraph: Record<string, Pick<DependenciesGraphNode<T>, 'children' | 'requiresBuild'>>,
rootDepPaths: string[]
rootDepPaths: T[]
): T[][] {
const nodesToBuild = new Set<string>()
getSubgraphToBuild(depGraph, rootDepPaths, nodesToBuild, new Set<string>())
const onlyFromBuildGraph = filter((depPath: string) => nodesToBuild.has(depPath))
getSubgraphToBuild(depGraph, rootDepPaths, nodesToBuild, new Set<T>())
const onlyFromBuildGraph = filter((depPath: T) => nodesToBuild.has(depPath))
const nodesToBuildArray = Array.from(nodesToBuild)
const graph = new Map(
nodesToBuildArray
Expand All @@ -42,9 +42,9 @@ export function buildSequence<T extends string> (

function getSubgraphToBuild<T extends string> (
graph: Record<string, Pick<DependenciesGraphNode<T>, 'children' | 'requiresBuild' | 'patchFile'>>,
entryNodes: string[],
nodesToBuild: Set<string>,
walked: Set<string>
entryNodes: T[],
nodesToBuild: Set<T>,
walked: Set<T>
): boolean {
let currentShouldBeBuilt = false
for (const depPath of entryNodes) {
Expand Down
2 changes: 1 addition & 1 deletion exec/build-modules/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type { DepsStateCache }

export async function buildModules<T extends string> (
depGraph: DependenciesGraph<T>,
rootDepPaths: string[],
rootDepPaths: T[],
opts: {
allowBuild?: (pkgName: string) => boolean
childConcurrency?: number
Expand Down
4 changes: 2 additions & 2 deletions lockfile/filter-lockfile/src/filterLockfile.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { type Lockfile } from '@pnpm/lockfile-types'
import { type DependenciesField } from '@pnpm/types'
import { type DependenciesField, type DepPath } from '@pnpm/types'
import { filterLockfileByImporters } from './filterLockfileByImporters'

export function filterLockfile (
lockfile: Lockfile,
opts: {
include: { [dependenciesField in DependenciesField]: boolean }
skipped: Set<string>
skipped: Set<DepPath>
}
): Lockfile {
return filterLockfileByImporters(lockfile, Object.keys(lockfile.importers), {
Expand Down
4 changes: 2 additions & 2 deletions lockfile/filter-lockfile/src/filterLockfileByImporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@pnpm/lockfile-types'
import { lockfileWalker, type LockfileWalkerStep } from '@pnpm/lockfile-walker'
import { logger } from '@pnpm/logger'
import { type DependenciesField } from '@pnpm/types'
import { type DependenciesField, type DepPath } from '@pnpm/types'
import { filterImporter } from './filterImporter'

const lockfileLogger = logger('lockfile')
Expand All @@ -16,7 +16,7 @@ export function filterLockfileByImporters (
importerIds: string[],
opts: {
include: { [dependenciesField in DependenciesField]: boolean }
skipped: Set<string>
skipped: Set<DepPath>
failOnMissingDependencies: boolean
}
): Lockfile {
Expand Down
10 changes: 5 additions & 5 deletions lockfile/filter-lockfile/test/filterByImporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ test('filterByImporters(): only prod dependencies of one importer', () => {
devDependencies: false,
optionalDependencies: false,
},
skipped: new Set<string>(),
skipped: new Set<DepPath>(),
}
)

Expand Down Expand Up @@ -149,7 +149,7 @@ test('filterByImporters(): fail on missing packages when failOnMissingDependenci
devDependencies: false,
optionalDependencies: false,
},
skipped: new Set<string>(),
skipped: new Set<DepPath>(),
}
)
} catch (_: any) { // eslint-disable-line
Expand Down Expand Up @@ -195,7 +195,7 @@ test('filterByImporters(): do not fail on missing packages when failOnMissingDep
devDependencies: false,
optionalDependencies: false,
},
skipped: new Set<string>(),
skipped: new Set<DepPath>(),
}
)

Expand Down Expand Up @@ -290,7 +290,7 @@ test('filterByImporters(): do not include skipped packages', () => {
devDependencies: true,
optionalDependencies: true,
},
skipped: new Set<string>(['optional-dep@1.0.0']),
skipped: new Set<DepPath>(['optional-dep@1.0.0' as DepPath]),
}
)

Expand Down Expand Up @@ -390,7 +390,7 @@ test('filterByImporters(): exclude orphan packages', () => {
devDependencies: true,
optionalDependencies: true,
},
skipped: new Set<string>(),
skipped: new Set<DepPath>(),
}
)

Expand Down
10 changes: 5 additions & 5 deletions lockfile/lockfile-walker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export function lockfileWalkerGroupImporterSteps (
importerIds: string[],
opts?: {
include?: { [dependenciesField in DependenciesField]: boolean }
skipped?: Set<string>
skipped?: Set<DepPath>
}
): Array<{ importerId: string, step: LockfileWalkerStep }> {
const walked = new Set<string>(((opts?.skipped) != null) ? Array.from(opts?.skipped) : [])
const walked = new Set<DepPath>(((opts?.skipped) != null) ? Array.from(opts?.skipped) : [])

return importerIds.map((importerId) => {
const projectSnapshot = lockfile.importers[importerId]
Expand Down Expand Up @@ -57,10 +57,10 @@ export function lockfileWalker (
importerIds: string[],
opts?: {
include?: { [dependenciesField in DependenciesField]: boolean }
skipped?: Set<string>
skipped?: Set<DepPath>
}
): LockfileWalker {
const walked = new Set<string>(((opts?.skipped) != null) ? Array.from(opts?.skipped) : [])
const walked = new Set<DepPath>(((opts?.skipped) != null) ? Array.from(opts?.skipped) : [])
const entryNodes = [] as DepPath[]
const directDeps = [] as Array<{ alias: string, depPath: DepPath }>

Expand Down Expand Up @@ -92,7 +92,7 @@ function step (
ctx: {
includeOptionalDependencies: boolean
lockfile: Lockfile
walked: Set<string>
walked: Set<DepPath>
},
nextDepPaths: DepPath[]
): LockfileWalkerStep {
Expand Down
6 changes: 3 additions & 3 deletions pkg-manager/modules-cleaner/src/prune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function prune (
currentLockfile: Lockfile
pruneStore?: boolean
pruneVirtualStore?: boolean
skipped: Set<string>
skipped: Set<DepPath>
virtualStoreDir: string
virtualStoreDirMaxLength: number
lockfileDir: string
Expand Down Expand Up @@ -176,7 +176,7 @@ export async function prune (
)
const neededPkgs = new Set<string>(['node_modules'])
for (const depPath of Object.keys(opts.wantedLockfile.packages ?? {})) {
if (opts.skipped.has(depPath)) continue
if (opts.skipped.has(depPath as DepPath)) continue
neededPkgs.add(depPathToFilename(depPath, opts.virtualStoreDirMaxLength))
}
const availablePkgs = await readVirtualStoreDir(opts.virtualStoreDir, opts.lockfileDir)
Expand Down Expand Up @@ -248,7 +248,7 @@ function getPkgsDepPathsOwnedOnlyByImporters (
importerIds: string[],
lockfile: Lockfile,
include: { [dependenciesField in DependenciesField]: boolean },
skipped: Set<string>
skipped: Set<DepPath>
): Record<string, string> {
const selected = filterLockfileByImporters(lockfile,
importerIds,
Expand Down
6 changes: 2 additions & 4 deletions pkg-manager/resolve-dependencies/src/dedupeInjectedDeps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import normalize from 'normalize-path'
import { type DepPath } from '@pnpm/types'
import { type PkgResolutionId, type DepPath } from '@pnpm/types'
import { type ResolvedDirectDependency, type ResolvedImporters } from './resolveDependencyTree'
import { type NodeId } from './nextNodeId'
import { type LinkedDependency } from './resolveDependencies'
Expand Down Expand Up @@ -80,12 +80,10 @@ function applyDedupeMap<T extends PartialResolvedPackage> (
opts.dependenciesByProjectId[id].delete(alias)
const index = opts.resolvedImporters[id].directDependencies.findIndex((dep) => dep.alias === alias)
const prev = opts.resolvedImporters[id].directDependencies[index]
const depPath = `link:${normalize(path.relative(id, dedupedProjectId))}`
const linkedDep: LinkedDependency & ResolvedDirectDependency = {
...prev,
isLinkedDependency: true,
depPath,
pkgId: depPath,
pkgId: `link:${normalize(path.relative(id, dedupedProjectId))}` as PkgResolutionId,
resolution: {
type: 'directory',
directory: path.join(opts.lockfileDir, dedupedProjectId),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function parentIdsContainSequence (pkgIds: string[], pkgId1: string, pkgId2: string): boolean {
import { type PkgResolutionId } from '@pnpm/types'

export function parentIdsContainSequence (pkgIds: PkgResolutionId[], pkgId1: PkgResolutionId, pkgId2: PkgResolutionId): boolean {
const pkg1Index = pkgIds.indexOf(pkgId1)
if (pkg1Index === -1 || pkg1Index === pkgIds.length - 1) {
return false
Expand Down
Loading

0 comments on commit f0b51bb

Please sign in to comment.