diff --git a/packages/core/src/parser.ts b/packages/core/src/parser.ts index 70ec19d..a1b2b8e 100644 --- a/packages/core/src/parser.ts +++ b/packages/core/src/parser.ts @@ -46,6 +46,16 @@ function attributes(text: string, error: (message: string) => void): Map { + if (i === 0 || i >= end || !/^\s/.test(line)) return false; + const rest = line.trimStart(); + return rest.startsWith(name) && /^\s*:/.test(rest.slice(name.length)); + }); + return index > 0 ? index + 1 : 2; +} + export function parseNotebook(source: string): Notebook { const notebook: Notebook = { title: "Untitled", params: [], blocks: [], diagnostics: [] }; const rawLines = source.split("\n"); @@ -70,22 +80,26 @@ export function parseNotebook(source: string): Notebook { else if (data.title !== undefined) error("title must be a non-empty string", 2); if (data.params !== undefined && !record(data.params)) error("params must be a mapping", 2); if (record(data.params)) for (const [name, raw] of Object.entries(data.params)) { - if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) error(`Invalid parameter name: ${name}`, 2); - if (!record(raw)) { error(`Parameter ${name} must be a mapping`, 2); continue; } - if (!paramTypes.has(String(raw.type))) { error(`Unknown param type for ${name}`, 2); continue; } + // Point at the parameter itself, not just the frontmatter start. + const at = paramLine(lines, end, name); + if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) error(`Invalid parameter name: ${name}`, at); + if (!record(raw)) { error(`Parameter ${name} must be a mapping`, at); continue; } + if (!paramTypes.has(String(raw.type))) { error(`Unknown param type for ${name}`, at); continue; } const def: ParamDef = { name, type: raw.type as ParamDef["type"], required: raw.required === true }; - for (const key of Object.keys(raw)) if (!["type", "required", "default", "label", "options"].includes(key)) error(`Unknown parameter field ${key} for ${name}`, 2); - if (raw.required !== undefined && typeof raw.required !== "boolean") error(`required must be boolean for ${name}`, 2); + for (const key of Object.keys(raw)) if (!["type", "required", "default", "label", "options"].includes(key)) + // `{ label: a (b, c) }` splits at the comma and turns "c)" into a key without a value. + error(`Unknown parameter field "${key}" for ${name}` + (raw[key] === null ? '. A value containing a comma needs quotes, e.g. label: "a, b"' : ""), at); + if (raw.required !== undefined && typeof raw.required !== "boolean") error(`required must be boolean for ${name}`, at); if (raw.default !== undefined) { - if (def.type === "secret") error(`Secret ${name} cannot have a default`, 2); + if (def.type === "secret") error(`Secret ${name} cannot have a default`, at); else if (typeof raw.default === "string" || typeof raw.default === "number" || typeof raw.default === "boolean") def.default = String(raw.default); - else error(`Invalid default for ${name}`, 2); + else error(`Invalid default for ${name}`, at); } if (typeof raw.label === "string") def.label = raw.label; - else if (raw.label !== undefined) error(`Invalid label for ${name}`, 2); + else if (raw.label !== undefined) error(`Invalid label for ${name}`, at); if (Array.isArray(raw.options) && raw.options.every(x => typeof x === "string")) def.options = raw.options; - else if (raw.options !== undefined) error(`Invalid options for ${name}`, 2); - if (def.type === "choice" && !def.options?.length) error(`Choice ${name} needs options`, 2); + else if (raw.options !== undefined) error(`Invalid options for ${name}`, at); + if (def.type === "choice" && !def.options?.length) error(`Choice ${name} needs options`, at); notebook.params.push(def); } } diff --git a/packages/core/test/core.test.ts b/packages/core/test/core.test.ts index 38d0d80..c3f0207 100644 --- a/packages/core/test/core.test.ts +++ b/packages/core/test/core.test.ts @@ -80,6 +80,14 @@ describe("parser", () => { ])("reports malformed input without throwing: %s", (source, message) => { expect(parseNotebook(source).diagnostics.some(d => d.message.includes(message))).toBe(true); }); + it("points an unquoted comma in a flow-mapping label at the parameter line with a fix", () => { + const n = parseNotebook("---\ntitle: T\nparams:\n host: { type: host }\n key: { type: secret, label: Auth key (optional, else log in) }\n---\n# T\n"); + expect(n.diagnostics).toEqual([{ + severity: "error", + line: 5, + message: 'Unknown parameter field "else log in)" for key. A value containing a comma needs quotes, e.g. label: "a, b"', + }]); + }); it("uses title fallback, numeric step ids, CRLF ranges and exact script whitespace", () => { expect(parseNotebook("").title).toBe("Untitled"); const n = parseNotebook("---\r\ntitle: Front\r\n---\r\n# Heading\r\n```sh {run}\r\n echo x \r\n\r\n```\r\n");