Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/integrations/omp-yaml-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,24 @@ function isComment(line: string): boolean {
}

function hasInlineComment(line: string): boolean {
return line.includes("#");
let quote: "'" | "\"" | null = null;
for (let index = 0; index < line.length; index += 1) {
const character = line[index]!;
if (quote === "\"") {
if (character === "\\") index += 1;
else if (character === quote) quote = null;
continue;
}
if (quote === "'") {
if (character !== quote) continue;
if (line[index + 1] === quote) index += 1;
else quote = null;
continue;
}
if (character === "'" || character === "\"") quote = character;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Restrict quote tracking to quoted-scalar starts

When an OMP-managed plain scalar contains an apostrophe or double quote before a real inline comment—for example, name: user's model # keep this note, which is valid YAML—hasInlineComment enters quote mode even though YAML treats that quote as ordinary plain-scalar content. The whitespace-separated # is therefore missed, so refresh or disable proceeds and silently deletes the user-authored comment instead of returning null. Only enter quote state where a quoted scalar can syntactically begin (or use a YAML lexer/CST), and add regression coverage for quoted characters inside plain scalars.

AGENTS.md reference: src/AGENTS.md:L10-L10

Useful? React with 👍 / 👎.

else if (character === "#" && /\s/u.test(line[index - 1] ?? "")) return true;
}
return false;
}

function isPlainBlockKey(line: string, indent: number, key: string): boolean {
Expand Down
32 changes: 32 additions & 0 deletions tests/omp-yaml-source-inline-comments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ const CURRENT_VALUE = {
api: "openai-completions",
};

const SOURCE_WITH_QUOTED_HASH = [
"providers:",
" opencodex:",
" models:",
" - id: \"provider/model#variant\"",
" name: 'model#variant (provider)'",
"",
].join("\n");

const VALUE_WITH_QUOTED_HASH = {
models: [{
id: "provider/model#variant",
name: "model#variant (provider)",
}],
};

describe("OMP managed YAML inline comments", () => {
test("refresh refuses to replace a managed block containing a nested inline comment", () => {
const nextValue = {
Expand All @@ -35,4 +51,20 @@ describe("OMP managed YAML inline comments", () => {
{},
)).toBeNull();
});

test("refresh accepts hash characters inside quoted model scalars", () => {
expect(patchOmpYamlSource(
SOURCE_WITH_QUOTED_HASH,
{ kind: "upsert", value: VALUE_WITH_QUOTED_HASH },
{ providers: { opencodex: VALUE_WITH_QUOTED_HASH } },
)).not.toBeNull();
});

test("disable accepts hash characters inside quoted model scalars", () => {
expect(patchOmpYamlSource(
SOURCE_WITH_QUOTED_HASH,
{ kind: "remove", removeEmptyProviders: true },
{},
)).toBe("");
});
});
Loading