Skip to content

Commit ef8269f

Browse files
committed
Add code comments for certain castings
1 parent 9159361 commit ef8269f

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

client/src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type NormalizedPath = string & { __brand: "NormalizedPath" };
2626
* @returns The normalized path, or null if input was null
2727
*/
2828
export function normalizePath(filePath: string | null): NormalizedPath | null {
29+
// `path.normalize` ensures we can assume string is now NormalizedPath
2930
return filePath != null ? (path.normalize(filePath) as NormalizedPath) : null;
3031
}
3132

server/src/find-runtime.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { readdir, stat as statAsync, readFile } from "fs/promises";
22
import { join, resolve } from "path";
33
import { compilerInfoPartialPath } from "./constants";
4-
import { NormalizedPath } from "./utils";
4+
import { NormalizedPath, normalizePath } from "./utils";
55

66
// Efficient parallel folder traversal to find node_modules directories
77
async function findNodeModulesDirs(
@@ -102,7 +102,9 @@ async function findRuntimePath(
102102
const contents = await readFile(compilerInfo, "utf8");
103103
const compileInfo: { runtime_path?: string } = JSON.parse(contents);
104104
if (compileInfo && compileInfo.runtime_path) {
105-
return [compileInfo.runtime_path as NormalizedPath];
105+
// We somewhat assume the user to pass down a normalized path, but we cannot be sure of this.
106+
const normalizedRuntimePath = normalizePath(compileInfo.runtime_path);
107+
return normalizedRuntimePath ? [normalizedRuntimePath] : [];
106108
}
107109
} catch {
108110
// Ignore errors, fallback to node_modules search
@@ -150,6 +152,7 @@ async function findRuntimePath(
150152
).then((results) => results.flatMap((x) => x));
151153

152154
return rescriptRuntimeDirs.map(
155+
// `resolve` ensures we can assume string is now NormalizedPath
153156
(runtime) => resolve(runtime) as NormalizedPath,
154157
);
155158
}

server/src/incrementalCompilation.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,13 @@ function triggerIncrementalCompilationOfFile(
302302
? `${moduleName}-${project.namespaceName}`
303303
: moduleName;
304304

305+
// projectRootPath is already NormalizedPath, appending a constant string still makes it a NormalizedPath
305306
const incrementalFolderPath: NormalizedPath = path.join(
306307
projectRootPath,
307308
INCREMENTAL_FILE_FOLDER_LOCATION,
308309
) as NormalizedPath;
309310

311+
// projectRootPath is already NormalizedPath, appending a constant string still makes it a NormalizedPath
310312
let originalTypeFileLocation = path.resolve(
311313
projectRootPath,
312314
c.compilerDirPartialPath,
@@ -316,6 +318,7 @@ function triggerIncrementalCompilationOfFile(
316318
const parsed = path.parse(originalTypeFileLocation);
317319
parsed.ext = ext === ".res" ? ".cmt" : ".cmti";
318320
parsed.base = "";
321+
// As originalTypeFileLocation was a NormalizedPath, path.format ensures we can assume string is now NormalizedPath
319322
originalTypeFileLocation = path.format(parsed) as NormalizedPath;
320323

321324
incrementalFileCacheEntry = {
@@ -326,6 +329,7 @@ function triggerIncrementalCompilationOfFile(
326329
moduleNameNamespaced,
327330
sourceFileName: moduleName + ext,
328331
sourceFilePath: filePath,
332+
// As incrementalFolderPath was a NormalizedPath, path.join ensures we can assume string is now NormalizedPath
329333
incrementalFilePath: path.join(
330334
incrementalFolderPath,
331335
moduleName + ext,

server/src/utils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export type FileURI = string & { __brand: "FileURI" };
5151
* @returns The normalized path, or null if input was null
5252
*/
5353
export function normalizePath(filePath: string | null): NormalizedPath | null {
54+
// `path.normalize` ensures we can assume string is now NormalizedPath
5455
return filePath != null ? (path.normalize(filePath) as NormalizedPath) : null;
5556
}
5657

@@ -77,6 +78,7 @@ let tempFileId = 0;
7778
export let createFileInTempDir = (extension = ""): NormalizedPath => {
7879
let tempFileName = tempFilePrefix + tempFileId + extension;
7980
tempFileId = tempFileId + 1;
81+
// `os.tmpdir` returns an absolute path, so `path.join` ensures we can assume string is now NormalizedPath
8082
return path.join(os.tmpdir(), tempFileName) as NormalizedPath;
8183
};
8284

@@ -715,6 +717,7 @@ export let runBuildWatcherUsingValidBuildPath = (
715717

716718
// parser helpers
717719
export let pathToURI = (file: NormalizedPath): FileURI => {
720+
// `pathToFileURL` ensures we can assume string is now FileURI
718721
return pathToFileURL(file).toString() as FileURI;
719722
};
720723
let parseFileAndRange = (fileAndRange: string) => {
@@ -953,10 +956,9 @@ export let parseCompilerLogOutput = async (
953956
for (const parsedDiagnostic of parsedDiagnostics) {
954957
let [fileAndRangeLine, ...diagnosticMessage] = parsedDiagnostic.content;
955958
let { file, range } = parseFileAndRange(fileAndRangeLine);
956-
const fileUri = file as FileURI;
957959

958-
if (result[fileUri] == null) {
959-
result[fileUri] = [];
960+
if (result[file] == null) {
961+
result[file] = [];
960962
}
961963

962964
// remove start and end whitespaces/newlines
@@ -982,7 +984,7 @@ export let parseCompilerLogOutput = async (
982984
range,
983985
});
984986

985-
result[fileUri].push(diagnostic);
987+
result[file].push(diagnostic);
986988
}
987989
}
988990

0 commit comments

Comments
 (0)