Skip to content

Commit 8721dd0

Browse files
authored
Add type brands for fileId and fileIdListId (microsoft#44280)
1 parent 6329a0d commit 8721dd0

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

src/compiler/builder.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -705,9 +705,11 @@ namespace ts {
705705
return filterSemanticDiagnotics(diagnostics, state.compilerOptions);
706706
}
707707

708-
export type ProgramBuildInfoDiagnostic = number | [fileId: number, diagnostics: readonly ReusableDiagnostic[]];
709-
export type ProgramBuilderInfoFilePendingEmit = [fileId: number, emitKind: BuilderFileEmit];
710-
export type ProgramBuildInfoReferencedMap = [fileId: number, fileIdListId: number][];
708+
export type ProgramBuildInfoFileId = number & { __programBuildInfoFileIdBrand: any };
709+
export type ProgramBuildInfoFileIdListId = number & { __programBuildInfoFileIdListIdBrand: any };
710+
export type ProgramBuildInfoDiagnostic = ProgramBuildInfoFileId | [fileId: ProgramBuildInfoFileId, diagnostics: readonly ReusableDiagnostic[]];
711+
export type ProgramBuilderInfoFilePendingEmit = [fileId: ProgramBuildInfoFileId, emitKind: BuilderFileEmit];
712+
export type ProgramBuildInfoReferencedMap = [fileId: ProgramBuildInfoFileId, fileIdListId: ProgramBuildInfoFileIdListId][];
711713
export type ProgramBuildInfoBuilderStateFileInfo = Omit<BuilderState.FileInfo, "signature"> & {
712714
/**
713715
* Signature is
@@ -725,7 +727,7 @@ namespace ts {
725727
fileNames: readonly string[];
726728
fileInfos: readonly ProgramBuildInfoFileInfo[];
727729
options: CompilerOptions | undefined;
728-
fileIdsList?: readonly (readonly number[])[];
730+
fileIdsList?: readonly (readonly ProgramBuildInfoFileId[])[];
729731
referencedMap?: ProgramBuildInfoReferencedMap;
730732
exportedModulesMap?: ProgramBuildInfoReferencedMap;
731733
semanticDiagnosticsPerFile?: ProgramBuildInfoDiagnostic[];
@@ -740,9 +742,9 @@ namespace ts {
740742
const currentDirectory = Debug.checkDefined(state.program).getCurrentDirectory();
741743
const buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(getTsBuildInfoEmitOutputFilePath(state.compilerOptions)!, currentDirectory));
742744
const fileNames: string[] = [];
743-
const fileNameToFileId = new Map<string, number>();
744-
let fileIdsList: (readonly number[])[] | undefined;
745-
let fileNamesToFileIdListId: ESMap<string, number> | undefined;
745+
const fileNameToFileId = new Map<string, ProgramBuildInfoFileId>();
746+
let fileIdsList: (readonly ProgramBuildInfoFileId[])[] | undefined;
747+
let fileNamesToFileIdListId: ESMap<string, ProgramBuildInfoFileIdListId> | undefined;
746748
const fileInfos = arrayFrom(state.fileInfos.entries(), ([key, value]): ProgramBuildInfoFileInfo => {
747749
// Ensure fileId
748750
const fileId = toFileId(key);
@@ -825,22 +827,22 @@ namespace ts {
825827
return ensurePathIsNonModuleName(getRelativePathFromDirectory(buildInfoDirectory, path, getCanonicalFileName));
826828
}
827829

828-
function toFileId(path: Path): number {
830+
function toFileId(path: Path): ProgramBuildInfoFileId {
829831
let fileId = fileNameToFileId.get(path);
830832
if (fileId === undefined) {
831833
fileNames.push(relativeToBuildInfo(path));
832-
fileNameToFileId.set(path, fileId = fileNames.length);
834+
fileNameToFileId.set(path, fileId = fileNames.length as ProgramBuildInfoFileId);
833835
}
834836
return fileId;
835837
}
836838

837-
function toFileIdListId(set: ReadonlySet<Path>): number {
839+
function toFileIdListId(set: ReadonlySet<Path>): ProgramBuildInfoFileIdListId {
838840
const fileIds = arrayFrom(set.keys(), toFileId).sort(compareValues);
839841
const key = fileIds.join();
840842
let fileIdListId = fileNamesToFileIdListId?.get(key);
841843
if (fileIdListId === undefined) {
842844
(fileIdsList ||= []).push(fileIds);
843-
(fileNamesToFileIdListId ||= new Map()).set(key, fileIdListId = fileIdsList.length);
845+
(fileNamesToFileIdListId ||= new Map()).set(key, fileIdListId = fileIdsList.length as ProgramBuildInfoFileIdListId);
844846
}
845847
return fileIdListId;
846848
}
@@ -1249,7 +1251,7 @@ namespace ts {
12491251
const filePaths = program.fileNames.map(toPath);
12501252
const filePathsSetList = program.fileIdsList?.map(fileIds => new Set(fileIds.map(toFilePath)));
12511253
const fileInfos = new Map<Path, BuilderState.FileInfo>();
1252-
program.fileInfos.forEach((fileInfo, index) => fileInfos.set(toFilePath(index + 1), toBuilderStateFileInfo(fileInfo)));
1254+
program.fileInfos.forEach((fileInfo, index) => fileInfos.set(toFilePath(index + 1 as ProgramBuildInfoFileId), toBuilderStateFileInfo(fileInfo)));
12531255
const state: ReusableBuilderProgramState = {
12541256
fileInfos,
12551257
compilerOptions: program.options ? convertToOptionsWithAbsolutePaths(program.options, toAbsolutePath) : {},
@@ -1294,11 +1296,11 @@ namespace ts {
12941296
return getNormalizedAbsolutePath(path, buildInfoDirectory);
12951297
}
12961298

1297-
function toFilePath(fileId: number) {
1299+
function toFilePath(fileId: ProgramBuildInfoFileId) {
12981300
return filePaths[fileId - 1];
12991301
}
13001302

1301-
function toFilePathsSet(fileIdsListId: number) {
1303+
function toFilePathsSet(fileIdsListId: ProgramBuildInfoFileIdListId) {
13021304
return filePathsSetList![fileIdsListId - 1];
13031305
}
13041306

src/testRunner/unittests/tsbuild/helpers.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,24 +236,24 @@ interface Symbol {
236236
}
237237
}
238238

239-
type ProgramBuildInfoDiagnostic = string | [string, readonly ReusableDiagnostic[]];
240-
type ProgramBuilderInfoFilePendingEmit = [string, "DtsOnly" | "Full"];
241-
interface ProgramBuildInfo {
239+
type ReadableProgramBuildInfoDiagnostic = string | [string, readonly ReusableDiagnostic[]];
240+
type ReadableProgramBuilderInfoFilePendingEmit = [string, "DtsOnly" | "Full"];
241+
interface ReadableProgramBuildInfo {
242242
fileNames: readonly string[];
243243
fileNamesList: readonly (readonly string[])[] | undefined;
244244
fileInfos: MapLike<BuilderState.FileInfo>;
245245
options: CompilerOptions | undefined;
246246
referencedMap?: MapLike<string[]>;
247247
exportedModulesMap?: MapLike<string[]>;
248-
semanticDiagnosticsPerFile?: readonly ProgramBuildInfoDiagnostic[];
249-
affectedFilesPendingEmit?: readonly ProgramBuilderInfoFilePendingEmit[];
248+
semanticDiagnosticsPerFile?: readonly ReadableProgramBuildInfoDiagnostic[];
249+
affectedFilesPendingEmit?: readonly ReadableProgramBuilderInfoFilePendingEmit[];
250250
}
251-
type ReadableBuildInfo = Omit<BuildInfo, "program"> & { program: ProgramBuildInfo | undefined; size: number; };
251+
type ReadableBuildInfo = Omit<BuildInfo, "program"> & { program: ReadableProgramBuildInfo | undefined; size: number; };
252252
function generateBuildInfoProgramBaseline(sys: System, originalWriteFile: System["writeFile"], buildInfoPath: string, buildInfo: BuildInfo) {
253-
const fileInfos: ProgramBuildInfo["fileInfos"] = {};
254-
buildInfo.program?.fileInfos.forEach((fileInfo, index) => fileInfos[toFileName(index + 1)] = toBuilderStateFileInfo(fileInfo));
253+
const fileInfos: ReadableProgramBuildInfo["fileInfos"] = {};
254+
buildInfo.program?.fileInfos.forEach((fileInfo, index) => fileInfos[toFileName(index + 1 as ProgramBuildInfoFileId)] = toBuilderStateFileInfo(fileInfo));
255255
const fileNamesList = buildInfo.program?.fileIdsList?.map(fileIdsListId => fileIdsListId.map(toFileName));
256-
const program: ProgramBuildInfo | undefined = buildInfo.program && {
256+
const program: ReadableProgramBuildInfo | undefined = buildInfo.program && {
257257
fileNames: buildInfo.program.fileNames,
258258
fileNamesList,
259259
fileInfos,
@@ -282,11 +282,11 @@ interface Symbol {
282282
// For now its just JSON.stringify
283283
originalWriteFile.call(sys, `${buildInfoPath}.readable.baseline.txt`, JSON.stringify(result, /*replacer*/ undefined, 2));
284284

285-
function toFileName(fileId: number) {
285+
function toFileName(fileId: ProgramBuildInfoFileId) {
286286
return buildInfo.program!.fileNames[fileId - 1];
287287
}
288288

289-
function toFileNames(fileIdsListId: number) {
289+
function toFileNames(fileIdsListId: ProgramBuildInfoFileIdListId) {
290290
return fileNamesList![fileIdsListId - 1];
291291
}
292292

0 commit comments

Comments
 (0)