From 8b08e67fdc9a3cd29c4b68ba914754040238a761 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 22 Mar 2025 01:24:41 +0200 Subject: [PATCH] feat(linter/mermaid): better handling of zero position --- .../widgets/type_widgets/linters/mermaid.spec.ts | 15 +++++++++------ .../app/widgets/type_widgets/linters/mermaid.ts | 10 +++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/public/app/widgets/type_widgets/linters/mermaid.spec.ts b/src/public/app/widgets/type_widgets/linters/mermaid.spec.ts index 1d0808f30..458d1649a 100644 --- a/src/public/app/widgets/type_widgets/linters/mermaid.spec.ts +++ b/src/public/app/widgets/type_widgets/linters/mermaid.spec.ts @@ -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 } }); }); }); diff --git a/src/public/app/widgets/type_widgets/linters/mermaid.ts b/src/public/app/widgets/type_widgets/linters/mermaid.ts index 0df2ee824..34048ba60 100644 --- a/src/public/app/widgets/type_widgets/linters/mermaid.ts +++ b/src/public/app/widgets/type_widgets/linters/mermaid.ts @@ -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 [