Skip to content

Commit 4e425ca

Browse files
committed
fix(mobile): keep shared diff parsing free of browser globals
1 parent 7224019 commit 4e425ca

7 files changed

Lines changed: 101 additions & 17 deletions

File tree

‎apps/mobile/src/features/review/workingChangesProjection.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { parseDiffFromFile, parsePatchFiles, type FileDiffMetadata } from "@pierre/diffs";
1+
import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
2+
import type { FileDiffMetadata } from "@pierre/diffs/types";
3+
import { parseReviewDiffFromFiles } from "@t3tools/client-runtime/review-diff-metadata";
24
import type {
35
WorkingChangesSnapshot,
46
WorkingChangesState,
57
} from "@t3tools/client-runtime/review-working-changes-state";
68
import { reviewDiffEdits } from "@t3tools/client-runtime/review-diff-context";
7-
import { mapRenderableFile, type ReviewRenderableFile } from "./reviewModel";
9+
import { mapRenderableFile, type ReviewRenderableFile } from "./reviewModel.ts";
810

911
/** Native and desktop readers project the same patch model instead of separate staging arithmetic. */
1012
export function nativeWorkingChangesSnapshot(
@@ -21,7 +23,10 @@ export function nativeWorkingChangesSnapshot(
2123
const parsed = patch ? parsePatchFiles(patch)[0]?.files[0] : undefined;
2224
if (parsed) return { ...parsed, cacheKey: file.path };
2325
return {
24-
...parseDiffFromFile({ name: file.path, contents: "" }, { name: file.path, contents: "" }),
26+
...parseReviewDiffFromFiles(
27+
{ name: file.path, contents: "" },
28+
{ name: file.path, contents: "" },
29+
),
2530
type: file.changeType,
2631
...(file.previousPath ? { prevName: file.previousPath } : {}),
2732
cacheKey: `${file.path}:pending`,

‎packages/client-runtime/package.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@
478478
"./review-file-progress": {
479479
"types": "./src/useReviewFileProgress.ts",
480480
"default": "./src/useReviewFileProgress.ts"
481+
},
482+
"./review-diff-metadata": {
483+
"types": "./src/reviewDiffMetadata.ts",
484+
"default": "./src/reviewDiffMetadata.ts"
481485
}
482486
},
483487
"scripts": {
@@ -495,6 +499,7 @@
495499
"@t3tools/source-control-gitcafe": "workspace:*",
496500
"@t3tools/source-control-github": "workspace:*",
497501
"@t3tools/source-control-gitlab": "workspace:*",
502+
"diff": "9.0.0",
498503
"effect": "catalog:",
499504
"three": "0.180.0"
500505
},

‎packages/client-runtime/src/reviewDiffContext.ts‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import {
2-
hydratePartialDiff,
3-
parseDiffFromFile,
4-
parsePatchFiles,
5-
type FileDiffMetadata,
6-
} from "@pierre/diffs";
1+
import { processFile, parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
2+
import type { FileDiffMetadata } from "@pierre/diffs/types";
3+
import { parseReviewDiffFromFiles } from "./reviewDiffMetadata.ts";
74
import * as Hash from "effect/Hash";
85

96
export const UNKNOWN_REVIEW_LINE = "\u0000unknown:";
@@ -15,7 +12,7 @@ export function projectReviewDiffContext(
1512
previous: FileDiffMetadata,
1613
anchorSide: "additions" | "deletions",
1714
): FileDiffMetadata {
18-
const generated = parseDiffFromFile(
15+
const generated = parseReviewDiffFromFiles(
1916
{ name: previous.name, contents: before.join("") },
2017
{ name: previous.name, contents: after.join("") },
2118
{ context: Infinity },
@@ -90,7 +87,7 @@ export function projectReviewDiffContext(
9087
const oldPath = JSON.stringify(`a/${previous.name}`);
9188
const newPath = JSON.stringify(`b/${previous.name}`);
9289
const header = `diff --git ${oldPath} ${newPath}\n--- ${oldPath}\n+++ ${newPath}\n`;
93-
const file = parsePatchFiles(header + hunks.join(""))[0]?.files[0];
90+
let file = parsePatchFiles(header + hunks.join(""))[0]?.files[0];
9491
if (!file) return { ...previous, hunks: [], splitLineCount: 0, unifiedLineCount: 0 };
9592
// Full contents let the renderer retain its existing expansion map. The hunks still
9693
// describe the original open regions, including ones that now contain only context.
@@ -101,10 +98,12 @@ export function projectReviewDiffContext(
10198
if (complete) {
10299
// Complete metadata uses document coordinates, including collapsed gaps and trailing
103100
// context, for virtualized expansion. Compact offsets are only valid for partial patches.
104-
hydratePartialDiff("merge", file, {
105-
oldFile: { name: previous.name, contents: before.join("") },
106-
newFile: { name: previous.name, contents: after.join("") },
107-
});
101+
file =
102+
processFile(header + hunks.join(""), {
103+
oldFile: { name: previous.name, contents: before.join("") },
104+
newFile: { name: previous.name, contents: after.join("") },
105+
throwOnError: true,
106+
}) ?? file;
108107
} else {
109108
let splitStart = 0;
110109
let unifiedStart = 0;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { processFile } from "@pierre/diffs/utils/parsePatchFiles";
2+
import type { FileContents } from "@pierre/diffs/types";
3+
import { createTwoFilesPatch } from "diff";
4+
5+
/** Keep shared diff arithmetic usable in native clients without loading web components. */
6+
export function parseReviewDiffFromFiles(
7+
oldFile: FileContents,
8+
newFile: FileContents,
9+
options?: { context?: number },
10+
) {
11+
const patch = createTwoFilesPatch(
12+
oldFile.name,
13+
newFile.name,
14+
oldFile.contents,
15+
newFile.contents,
16+
oldFile.header,
17+
newFile.header,
18+
options,
19+
);
20+
const file = processFile(patch, { oldFile, newFile, throwOnError: true });
21+
if (!file) throw new Error("Could not parse the working file comparison");
22+
return file;
23+
}

‎packages/client-runtime/src/reviewWorkingChangesState.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { parseDiffFromFile, parsePatchFiles, type FileDiffMetadata } from "@pierre/diffs";
1+
import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
2+
import type { FileDiffMetadata } from "@pierre/diffs/types";
3+
import { parseReviewDiffFromFiles } from "./reviewDiffMetadata.ts";
24
import * as Hash from "effect/Hash";
35
import { orderReviewFiles } from "./reviewWorkspace.ts";
46
import { projectReviewDiffContext, UNKNOWN_REVIEW_LINE } from "./reviewDiffContext.ts";
@@ -70,7 +72,7 @@ function optimisticFile(
7072
if (before.join("") === after.join("")) return { file: null, hunks: [], contextFile };
7173
// Unknown unchanged lines establish coordinates only. Zero context prevents them from
7274
// entering the optimistic render or a staging patch; Git supplies real context on refresh.
73-
const generated = parseDiffFromFile(
75+
const generated = parseReviewDiffFromFiles(
7476
{ name: fallback.name, contents: before.join("") },
7577
{ name: fallback.name, contents: after.join("") },
7678
{ context: 0 },

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Exercises real native startup imports in a process without DOM globals.
2+
import * as NodeChildProcess from "node:child_process";
3+
import * as NodeURL from "node:url";
4+
import { describe, expect, it } from "vite-plus/test";
5+
6+
// Vitest's DOM setup can hide this native startup crash; load the actual modules
7+
// in a fresh process with no browser globals or web-component registration.
8+
describe("native diff startup", () => {
9+
it("loads shared staging arithmetic and parses full file data without a DOM", () => {
10+
const entry = new URL("../packages/client-runtime/src/reviewDiffMetadata.ts", import.meta.url)
11+
.href;
12+
const staging = new URL(
13+
"../packages/client-runtime/src/reviewWorkingChangesState.ts",
14+
import.meta.url,
15+
).href;
16+
const native = new URL(
17+
"../apps/mobile/src/features/review/workingChangesProjection.ts",
18+
import.meta.url,
19+
).href;
20+
const result = NodeChildProcess.execFileSync(
21+
process.execPath,
22+
[
23+
"--input-type=module",
24+
"--eval",
25+
`
26+
import assert from 'node:assert/strict';
27+
for (const name of ['window', 'document', 'HTMLElement', 'customElements']) {
28+
assert.equal(name in globalThis, false);
29+
}
30+
const { parseReviewDiffFromFiles } = await import(${JSON.stringify(entry)});
31+
await import(${JSON.stringify(staging)});
32+
await import(${JSON.stringify(native)});
33+
const file = parseReviewDiffFromFiles(
34+
{ name: 'file.txt', contents: 'old\\n' },
35+
{ name: 'file.txt', contents: 'new\\n' },
36+
);
37+
assert.equal(file.isPartial, false);
38+
assert.equal(file.hunks.length, 1);
39+
assert.equal(file.additionLines[0], 'new\\n');
40+
console.log('native startup passed');
41+
`,
42+
],
43+
{ cwd: NodeURL.fileURLToPath(new URL("..", import.meta.url)), encoding: "utf8" },
44+
);
45+
expect(result.trim()).toBe("native startup passed");
46+
});
47+
});

0 commit comments

Comments
 (0)