feat(linter/mermaid): better handling of zero position

This commit is contained in:
Elian Doran
2025-03-22 01:24:41 +02:00
parent 5d8ac3b9f7
commit 8b08e67fdc
2 changed files with 14 additions and 11 deletions

View File

@@ -21,22 +21,25 @@ describe("Mermaid linter", () => {
expect(result[0].message).toSatisfy((v: string) => v.includes("Expecting 'SPACE'"));
expect(result[0]).toMatchObject({
from: { line: 0, col: 0 },
to: { line: 0, col: 0 }
to: { line: 0, col: 1 }
});
});
it("reports correctly basic arrow missing in diagram", async () => {
const input = trimIndentation`\
stateDiagram-v2
[*] -> Still
xychart-beta horizontal
title "Percentage usge"
x-axis [data, sys, usr, var]
y-axis 0--->100
bar [20, 70, 0, 0]
`;
const result = await validateMermaid(input);
expect(result.length).toBe(1);
expect(result[0].message).toSatisfy((v: string) => v.includes("Expecting 'SPACE'"));
expect(result[0].message).toSatisfy((v: string) => v.includes("Expecting 'ARROW_DELIMITER'"));
expect(result[0]).toMatchObject({
from: { line: 1, col: 0 },
to: { line: 1, col: 3 }
from: { line: 3, col: 8 },
to: { line: 3, col: 9 }
});
});
});

View File

@@ -31,12 +31,12 @@ export async function validateMermaid(text: string) {
const mermaidError = (e as MermaidParseError);
const loc = mermaidError.hash.loc;
let firstCol = loc.first_column;
let lastCol = loc.last_column;
if (firstCol !== 0 && lastCol !== 0) {
firstCol = lastCol + 1;
lastCol = firstCol + 1;
let firstCol = loc.first_column + 1;
let lastCol = loc.last_column + 1;
if (firstCol === 1 && lastCol === 1) {
firstCol = 0;
}
return [