fix(core): report parameter errors at their line with a quoting hint (#13)
An unquoted comma in a flow-mapping label splits it into a valueless key. Parameter diagnostics now point at the parameter's own frontmatter line, and a valueless unknown field says that the value needs quotes.
This commit is contained in:
+24
-10
@@ -46,6 +46,16 @@ function attributes(text: string, error: (message: string) => void): Map<string,
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 1-based source line of a parameter's key inside the frontmatter, or the first frontmatter line. */
|
||||||
|
function paramLine(lines: string[], end: number, name: string): number {
|
||||||
|
const index = lines.findIndex((line, i) => {
|
||||||
|
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 {
|
export function parseNotebook(source: string): Notebook {
|
||||||
const notebook: Notebook = { title: "Untitled", params: [], blocks: [], diagnostics: [] };
|
const notebook: Notebook = { title: "Untitled", params: [], blocks: [], diagnostics: [] };
|
||||||
const rawLines = source.split("\n");
|
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);
|
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 (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 (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);
|
// Point at the parameter itself, not just the frontmatter start.
|
||||||
if (!record(raw)) { error(`Parameter ${name} must be a mapping`, 2); continue; }
|
const at = paramLine(lines, end, name);
|
||||||
if (!paramTypes.has(String(raw.type))) { error(`Unknown param type for ${name}`, 2); continue; }
|
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 };
|
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);
|
for (const key of Object.keys(raw)) if (!["type", "required", "default", "label", "options"].includes(key))
|
||||||
if (raw.required !== undefined && typeof raw.required !== "boolean") error(`required must be boolean for ${name}`, 2);
|
// `{ 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 (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 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;
|
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;
|
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);
|
else if (raw.options !== undefined) error(`Invalid options for ${name}`, at);
|
||||||
if (def.type === "choice" && !def.options?.length) error(`Choice ${name} needs options`, 2);
|
if (def.type === "choice" && !def.options?.length) error(`Choice ${name} needs options`, at);
|
||||||
notebook.params.push(def);
|
notebook.params.push(def);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,6 +80,14 @@ describe("parser", () => {
|
|||||||
])("reports malformed input without throwing: %s", (source, message) => {
|
])("reports malformed input without throwing: %s", (source, message) => {
|
||||||
expect(parseNotebook(source).diagnostics.some(d => d.message.includes(message))).toBe(true);
|
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", () => {
|
it("uses title fallback, numeric step ids, CRLF ranges and exact script whitespace", () => {
|
||||||
expect(parseNotebook("").title).toBe("Untitled");
|
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");
|
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");
|
||||||
|
|||||||
Reference in New Issue
Block a user