mirror of https://github.com/TriliumNext/Notes
Merge pull request #2029 from TriliumNext/refactor/typecheck_errors
Solve typecheck errorspull/2037/head
commit
63ea9104c6
@ -1,43 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { trimIndentation } from "@triliumnext/commons";
|
||||
import { validateMermaid } from "./mermaid.js";
|
||||
|
||||
describe("Mermaid linter", () => {
|
||||
|
||||
(global as any).CodeMirror = {
|
||||
Pos(line: number, col: number) {
|
||||
return { line, col };
|
||||
}
|
||||
};
|
||||
|
||||
it("reports correctly bad diagram type", async () => {
|
||||
const input = trimIndentation`\
|
||||
stateDiagram-v23
|
||||
[*] -> Still
|
||||
`;
|
||||
|
||||
const result = await validateMermaid(input);
|
||||
expect(result).toMatchObject([{
|
||||
message: "Expecting 'SPACE', 'NL', 'SD', got 'ID'",
|
||||
from: { line: 0, col: 0 },
|
||||
to: { line: 0, col: 1 }
|
||||
}]);
|
||||
});
|
||||
|
||||
it("reports correctly basic arrow missing in diagram", async () => {
|
||||
const input = trimIndentation`\
|
||||
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).toMatchObject([{
|
||||
message: "Expecting 'ARROW_DELIMITER', got 'MINUS'",
|
||||
from: { line: 3, col: 8 },
|
||||
to: { line: 3, col: 9 }
|
||||
}]);
|
||||
});
|
||||
});
|
||||
@ -1,58 +0,0 @@
|
||||
import mermaid from "mermaid";
|
||||
|
||||
interface MermaidParseError extends Error {
|
||||
hash: {
|
||||
text: string;
|
||||
token: string;
|
||||
line: number;
|
||||
loc: {
|
||||
first_line: number;
|
||||
first_column: number;
|
||||
last_line: number;
|
||||
last_column: number;
|
||||
};
|
||||
expected: string[]
|
||||
}
|
||||
}
|
||||
|
||||
export default function registerErrorReporter() {
|
||||
CodeMirror.registerHelper("lint", null, validateMermaid);
|
||||
}
|
||||
|
||||
export async function validateMermaid(text: string) {
|
||||
if (!text.trim()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
await mermaid.parse(text);
|
||||
} catch (e: unknown) {
|
||||
console.warn("Got validation error", JSON.stringify(e));
|
||||
|
||||
const mermaidError = (e as MermaidParseError);
|
||||
const loc = mermaidError.hash.loc;
|
||||
|
||||
let firstCol = loc.first_column + 1;
|
||||
let lastCol = loc.last_column + 1;
|
||||
|
||||
if (firstCol === 1 && lastCol === 1) {
|
||||
firstCol = 0;
|
||||
}
|
||||
|
||||
let messageLines = mermaidError.message.split("\n");
|
||||
if (messageLines.length >= 4) {
|
||||
messageLines = messageLines.slice(3);
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
message: messageLines.join("\n"),
|
||||
severity: "error",
|
||||
from: CodeMirror.Pos(loc.first_line - 1, firstCol),
|
||||
to: CodeMirror.Pos(loc.last_line - 1, lastCol)
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
{
|
||||
"jsc": {
|
||||
"target": "es2017",
|
||||
"parser": {
|
||||
"syntax": "typescript",
|
||||
"decorators": true,
|
||||
"dynamicImport": true
|
||||
},
|
||||
"transform": {
|
||||
"decoratorMetadata": true,
|
||||
"legacyDecorator": true
|
||||
},
|
||||
"keepClassNames": true,
|
||||
"externalHelpers": true,
|
||||
"loose": true
|
||||
},
|
||||
"module": {
|
||||
"type": "es6"
|
||||
},
|
||||
"sourceMaps": true,
|
||||
"exclude": ["jest.config.ts",".*\\.spec.tsx?$",".*\\.test.tsx?$","./src/jest-setup.ts$","./**/jest-setup.ts$"]
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue