From 7539a62ff5f20c6e80e03eb49d1d0b2e2b425b67 Mon Sep 17 00:00:00 2001 From: Elizabeth Craig Date: Mon, 9 Sep 2024 22:04:05 -0700 Subject: [PATCH] start using deep readonly --- src/bump/setDependentVersions.ts | 2 +- src/changefile/unlinkChangeFiles.ts | 11 +++++------ src/changelog/getPackageChangelogs.ts | 3 ++- src/changelog/writeChangelog.ts | 3 ++- src/types/BumpInfo.ts | 7 ++++--- src/types/DeepReadonly.ts | 17 +++++++++++++++++ 6 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 src/types/DeepReadonly.ts diff --git a/src/bump/setDependentVersions.ts b/src/bump/setDependentVersions.ts index 9c51f75c4..6104c41d8 100644 --- a/src/bump/setDependentVersions.ts +++ b/src/bump/setDependentVersions.ts @@ -10,7 +10,7 @@ import { bumpMinSemverRange } from './bumpMinSemverRange'; */ export function setDependentVersions( packageInfos: PackageInfos, - scopedPackages: Set, + scopedPackages: ReadonlySet, options: Pick ): BumpInfo['dependentChangedBy'] { const { verbose } = options; diff --git a/src/changefile/unlinkChangeFiles.ts b/src/changefile/unlinkChangeFiles.ts index 0d06807a6..42d22af6c 100644 --- a/src/changefile/unlinkChangeFiles.ts +++ b/src/changefile/unlinkChangeFiles.ts @@ -2,8 +2,9 @@ import { ChangeSet } from '../types/ChangeInfo'; import { getChangePath } from '../paths'; import fs from 'fs-extra'; import path from 'path'; -import { PackageInfo } from '../types/PackageInfo'; +import { PackageInfos } from '../types/PackageInfo'; import type { BeachballOptions } from '../types/BeachballOptions'; +import { DeepReadonly } from '../types/DeepReadonly'; /** * Unlink only change files that are specified in the changes param @@ -11,14 +12,12 @@ import type { BeachballOptions } from '../types/BeachballOptions'; * @param changes existing change files to be removed */ export function unlinkChangeFiles( - changeSet: ChangeSet, - packageInfos: { - [pkg: string]: PackageInfo; - }, + changeSet: DeepReadonly, + packageInfos: PackageInfos, options: Pick ): void { const changePath = getChangePath(options); - if (!changeSet || !changeSet.length) { + if (!changeSet?.length) { return; } console.log('Removing change files:'); diff --git a/src/changelog/getPackageChangelogs.ts b/src/changelog/getPackageChangelogs.ts index 2061c586a..920060bd9 100644 --- a/src/changelog/getPackageChangelogs.ts +++ b/src/changelog/getPackageChangelogs.ts @@ -7,6 +7,7 @@ import { getChangePath } from '../paths'; import { getFileAddedHash } from 'workspace-tools'; import { ChangeSet } from '../types/ChangeInfo'; import type { BeachballOptions } from '../types/BeachballOptions'; +import { DeepReadonly } from '../types/DeepReadonly'; /** * Used for `ChangelogEntry.commit` if the commit hash is not available. @@ -18,7 +19,7 @@ const commitNotAvailable = 'not available'; * @returns Mapping from package name to package changelog. */ export function getPackageChangelogs(params: { - changeFileChangeInfos: ChangeSet; + changeFileChangeInfos: DeepReadonly; calculatedChangeTypes: BumpInfo['calculatedChangeTypes']; dependentChangedBy?: BumpInfo['dependentChangedBy']; packageInfos: PackageInfos; diff --git a/src/changelog/writeChangelog.ts b/src/changelog/writeChangelog.ts index 36dfdbb23..7d24c34b9 100644 --- a/src/changelog/writeChangelog.ts +++ b/src/changelog/writeChangelog.ts @@ -11,6 +11,7 @@ import { isPathIncluded } from '../monorepo/isPathIncluded'; import { PackageChangelog, ChangelogJson } from '../types/ChangeLog'; import { mergeChangelogs } from './mergeChangelogs'; import { ChangeSet } from '../types/ChangeInfo'; +import { DeepReadonly } from '../types/DeepReadonly'; export async function writeChangelog( bumpInfo: Pick, @@ -50,7 +51,7 @@ export async function writeChangelog( */ async function writeGroupedChangelog( options: BeachballOptions, - changeFileChangeInfos: ChangeSet, + changeFileChangeInfos: DeepReadonly, calculatedChangeTypes: BumpInfo['calculatedChangeTypes'], packageInfos: PackageInfos ): Promise { diff --git a/src/types/BumpInfo.ts b/src/types/BumpInfo.ts index a3880088f..40fbbb114 100644 --- a/src/types/BumpInfo.ts +++ b/src/types/BumpInfo.ts @@ -1,9 +1,10 @@ import { ChangeSet, ChangeType } from './ChangeInfo'; +import { DeepReadonly } from './DeepReadonly'; import { PackageInfos, PackageGroups } from './PackageInfo'; export type BumpInfo = { /** Changes coming from the change files */ - changeFileChangeInfos: ChangeSet; + changeFileChangeInfos: DeepReadonly; /** * Cached version of package info (e.g. package.json, package path). @@ -15,7 +16,7 @@ export type BumpInfo = { calculatedChangeTypes: { [pkgName: string]: ChangeType }; /** Package grouping */ - packageGroups: PackageGroups; + packageGroups: DeepReadonly; /** Set of packages that had been modified */ modifiedPackages: Set; @@ -27,7 +28,7 @@ export type BumpInfo = { dependentChangedBy: { [pkgName: string]: Set }; /** Set of packages that are in scope for this bump */ - scopedPackages: Set; + scopedPackages: ReadonlySet; }; /** Dependents cache (child points to parents): if A depends on B, then `{ B: [A] }` */ diff --git a/src/types/DeepReadonly.ts b/src/types/DeepReadonly.ts new file mode 100644 index 000000000..9beda7f6c --- /dev/null +++ b/src/types/DeepReadonly.ts @@ -0,0 +1,17 @@ +export type DeepReadonly = T extends (infer R)[] + ? DeepReadonlyArray + : T extends Function + ? T + : T extends Set + ? ReadonlySet> + : T extends Map + ? ReadonlyMap, DeepReadonly> + : T extends object + ? DeepReadonlyObject + : T; + +interface DeepReadonlyArray extends ReadonlyArray> {} + +type DeepReadonlyObject = { + readonly [P in keyof T]: DeepReadonly; +};