Skip to content

Commit 9603e67

Browse files
author
Gio Della-Libera
committed
fix(path): plumb fileNameForGuard through internal rebuild + fix yaml type inference (#78678)
CI on the post-merge head surfaced 2 issues my local lint missed: - TS7022 in `src/oc-path/yaml/edit.ts:167` — `'pair' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer'. Fixed by adding explicit `Pair | undefined` / `readonly Pair[]` / `readonly string[]` type annotations on the local bindings — breaks the circular-inference path that the bare `Pair` cast triggered. Same fix at positionalForYamlNode. - CLI-S03 in `src/commands/path.test.ts` failed asserting `gateway.jsonc` appeared in the sentinel error. Root cause: the F13 plumbing landed at emitForKind (the OUTER emit) but the sentinel throw originates INSIDE the per-kind setter via rebuildRaw → emitJsonc({mode:'render'}), which doesn't see the outer fileName. Threaded `path.file` through setJsoncOcPath → rebuildRaw → emitJsonc and through setJsonlOcPath → finalize → emitJsonl so internal render-mode emits also surface the file context (`oc://gateway.jsonc/[path]` instead of `oc:///[path]`). Yaml's setIn uses the yaml-lib serializer (not our emit), so no plumbing needed there. The jsonl finalize change also propagates `ast.lineEnding` through the synthetic `next` AST so CRLF preservation (F10) still holds after a structural edit. Local: vitest path.test + yaml/jsonc/jsonl edit tests 70 passed; oxlint --type-aware on changed files: 0 warnings, 0 errors.
1 parent 8829bcb commit 9603e67

3 files changed

Lines changed: 33 additions & 13 deletions

File tree

‎src/oc-path/jsonc/edit.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ export function setJsoncOcPath(
6868
// Empty path — replace the root.
6969
if (segments.length === 0) {
7070
const next = { ...ast, root: newValue };
71-
return { ok: true, ast: rebuildRaw(next) };
71+
return { ok: true, ast: rebuildRaw(next, path.file) };
7272
}
7373

7474
const replaced = replaceAt(ast.root, segments, 0, newValue);
7575
if (replaced === null) {return { ok: false, reason: 'unresolved' };}
7676
const next = { ...ast, root: replaced };
77-
return { ok: true, ast: rebuildRaw(next) };
77+
return { ok: true, ast: rebuildRaw(next, path.file) };
7878
}
7979

8080
function replaceAt(
@@ -168,8 +168,17 @@ function replaceAt(
168168
* `setJsoncOcPath` does a `raw.slice(0,start) + newBytes + raw.slice(end)`
169169
* splice, leaving trivia untouched. Tracked as PR follow-up.
170170
*/
171-
function rebuildRaw(ast: JsoncAst): JsoncAst {
171+
function rebuildRaw(ast: JsoncAst, fileName?: string): JsoncAst {
172+
// Plumb fileName so render-mode emit's sentinel guard reports the
173+
// file context (`oc://gateway.jsonc/[path]`) instead of the empty
174+
// fallback (`oc:///[path]`). The throw originates here when a
175+
// caller-injected sentinel reaches a leaf — without the file
176+
// context, forensics + audit pipelines see "rejected somewhere"
177+
// with no way to identify the file.
178+
const opts = fileName !== undefined
179+
? { mode: 'render' as const, fileNameForGuard: fileName }
180+
: { mode: 'render' as const };
172181
const next: JsoncAst = { kind: 'jsonc', raw: '', root: ast.root };
173-
const rendered = emitJsonc(next, { mode: 'render' });
182+
const rendered = emitJsonc(next, opts);
174183
return { ...ast, raw: rendered };
175184
}

‎src/oc-path/jsonl/edit.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function setJsonlOcPath(
5454
value: newValue,
5555
raw: target.raw,
5656
};
57-
return finalize(ast, lineIdx, newLine);
57+
return finalize(ast, lineIdx, newLine, path.file);
5858
}
5959

6060
if (target.kind !== 'value') {return { ok: false, reason: 'not-a-value-line' };}
@@ -75,7 +75,7 @@ export function setJsonlOcPath(
7575
value: replaced,
7676
raw: target.raw,
7777
};
78-
return finalize(ast, lineIdx, newLine);
78+
return finalize(ast, lineIdx, newLine, path.file);
7979
}
8080

8181
function replaceAt(
@@ -191,11 +191,19 @@ function pickLineIndex(ast: JsonlAst, addr: string): number {
191191
return ast.lines.findIndex((l) => l.line === target);
192192
}
193193

194-
function finalize(ast: JsonlAst, lineIdx: number, newLine: JsonlLine): JsonlEditResult {
194+
function finalize(ast: JsonlAst, lineIdx: number, newLine: JsonlLine, fileName?: string): JsonlEditResult {
195195
const newLines = ast.lines.slice();
196196
newLines[lineIdx] = newLine;
197-
const next: JsonlAst = { kind: 'jsonl', raw: '', lines: newLines };
198-
const rendered = emitJsonl(next, { mode: 'render' });
197+
const next: JsonlAst = {
198+
kind: 'jsonl',
199+
raw: '',
200+
lines: newLines,
201+
...(ast.lineEnding !== undefined ? { lineEnding: ast.lineEnding } : {}),
202+
};
203+
const opts = fileName !== undefined
204+
? { mode: 'render' as const, fileNameForGuard: fileName }
205+
: { mode: 'render' as const };
206+
const rendered = emitJsonl(next, opts);
199207
return { ok: true, ast: { ...next, raw: rendered } };
200208
}
201209

‎src/oc-path/yaml/edit.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ function resolvePositionalSegments(
164164
}
165165
out.push(segNorm);
166166
if (isMap(node)) {
167-
const pair = (node as { items: Pair[] }).items.find((p) => {
167+
const pairs: readonly Pair[] = (node as { items: readonly Pair[] }).items;
168+
const pair: Pair | undefined = pairs.find((p) => {
168169
const k = isScalar(p.key) ? p.key.value : p.key;
169170
return String(k) === segNorm;
170171
});
@@ -187,12 +188,14 @@ function resolvePositionalSegments(
187188

188189
function positionalForYamlNode(node: Node, seg: string): string | null {
189190
if (isMap(node)) {
190-
const pairs = (node as { items: Pair[] }).items;
191-
const keys = pairs.map((p) => String(isScalar(p.key) ? p.key.value : p.key));
191+
const pairs: readonly Pair[] = (node as { items: readonly Pair[] }).items;
192+
const keys: readonly string[] = pairs.map((p) =>
193+
String(isScalar(p.key) ? p.key.value : p.key),
194+
);
192195
return resolvePositionalSeg(seg, { indexable: false, size: keys.length, keys });
193196
}
194197
if (isSeq(node)) {
195-
const items = (node as { items: Node[] }).items;
198+
const items: readonly Node[] = (node as { items: readonly Node[] }).items;
196199
return resolvePositionalSeg(seg, { indexable: true, size: items.length });
197200
}
198201
return null;

0 commit comments

Comments
 (0)