mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-28 07:03:17 +08:00
fix: Throw correct errors when parsing diagrams with errors
This commit is contained in:
parent
6c2647e8cf
commit
d7610dda8f
@ -14,6 +14,7 @@
|
|||||||
"bilkent",
|
"bilkent",
|
||||||
"bisheng",
|
"bisheng",
|
||||||
"braintree",
|
"braintree",
|
||||||
|
"brkt",
|
||||||
"brolin",
|
"brolin",
|
||||||
"brotli",
|
"brotli",
|
||||||
"classdef",
|
"classdef",
|
||||||
@ -60,12 +61,14 @@
|
|||||||
"mindmaps",
|
"mindmaps",
|
||||||
"mitigations",
|
"mitigations",
|
||||||
"mkdocs",
|
"mkdocs",
|
||||||
|
"mult",
|
||||||
"orlandoni",
|
"orlandoni",
|
||||||
"phpbb",
|
"phpbb",
|
||||||
"plantuml",
|
"plantuml",
|
||||||
"playfair",
|
"playfair",
|
||||||
"pnpm",
|
"pnpm",
|
||||||
"podlite",
|
"podlite",
|
||||||
|
"quence",
|
||||||
"ranksep",
|
"ranksep",
|
||||||
"rect",
|
"rect",
|
||||||
"rects",
|
"rects",
|
||||||
@ -81,6 +84,7 @@
|
|||||||
"substate",
|
"substate",
|
||||||
"sveidqvist",
|
"sveidqvist",
|
||||||
"techn",
|
"techn",
|
||||||
|
"teststr",
|
||||||
"treemap",
|
"treemap",
|
||||||
"ts-nocheck",
|
"ts-nocheck",
|
||||||
"tuleap",
|
"tuleap",
|
||||||
|
@ -102,7 +102,6 @@ export const getDiagramFromText = (
|
|||||||
try {
|
try {
|
||||||
// Trying to find the diagram
|
// Trying to find the diagram
|
||||||
getDiagram(type);
|
getDiagram(type);
|
||||||
return new Diagram(txt, parseError);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const loader = getDiagramLoader(type);
|
const loader = getDiagramLoader(type);
|
||||||
if (!loader) {
|
if (!loader) {
|
||||||
@ -118,6 +117,7 @@ export const getDiagramFromText = (
|
|||||||
return new Diagram(txt, parseError);
|
return new Diagram(txt, parseError);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
return new Diagram(txt, parseError);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Diagram;
|
export default Diagram;
|
||||||
|
67
packages/mermaid/src/diagram.spec.ts
Normal file
67
packages/mermaid/src/diagram.spec.ts
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
import { describe, test, expect } from 'vitest';
|
||||||
|
import Diagram, { getDiagramFromText } from './Diagram';
|
||||||
|
import { addDetector } from './diagram-api/detectType';
|
||||||
|
import { addDiagrams } from './diagram-api/diagram-orchestration';
|
||||||
|
|
||||||
|
addDiagrams();
|
||||||
|
|
||||||
|
describe('diagram detection', () => {
|
||||||
|
test('should detect inbuilt diagrams', () => {
|
||||||
|
const graph = getDiagramFromText('graph TD; A-->B') as Diagram;
|
||||||
|
expect(graph).toBeInstanceOf(Diagram);
|
||||||
|
expect(graph.type).toBe('flowchart-v2');
|
||||||
|
const sequence = getDiagramFromText(
|
||||||
|
'sequenceDiagram; Alice->>+John: Hello John, how are you?'
|
||||||
|
) as Diagram;
|
||||||
|
expect(sequence).toBeInstanceOf(Diagram);
|
||||||
|
expect(sequence.type).toBe('sequence');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should detect external diagrams', async () => {
|
||||||
|
addDetector(
|
||||||
|
'loki',
|
||||||
|
(str) => str.startsWith('loki'),
|
||||||
|
() =>
|
||||||
|
Promise.resolve({
|
||||||
|
id: 'loki',
|
||||||
|
diagram: {
|
||||||
|
db: {},
|
||||||
|
parser: {
|
||||||
|
parse: () => {
|
||||||
|
// no-op
|
||||||
|
},
|
||||||
|
parser: {
|
||||||
|
yy: {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
renderer: {},
|
||||||
|
styles: {},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
const diagram = (await getDiagramFromText('loki TD; A-->B')) as Diagram;
|
||||||
|
expect(diagram).toBeInstanceOf(Diagram);
|
||||||
|
expect(diagram.type).toBe('loki');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should throw the right error for incorrect diagram', () => {
|
||||||
|
expect(() => getDiagramFromText('graph TD; A-->')).toThrowErrorMatchingInlineSnapshot(`
|
||||||
|
"Parse error on line 3:
|
||||||
|
graph TD; A-->
|
||||||
|
--------------^
|
||||||
|
Expecting 'AMP', 'ALPHA', 'COLON', 'PIPE', 'TESTSTR', 'DOWN', 'DEFAULT', 'NUM', 'COMMA', 'MINUS', 'BRKT', 'DOT', 'PUNCTUATION', 'UNICODE_TEXT', 'PLUS', 'EQUALS', 'MULT', 'UNDERSCORE', got 'EOF'"
|
||||||
|
`);
|
||||||
|
expect(() => getDiagramFromText('sequenceDiagram; A-->B')).toThrowErrorMatchingInlineSnapshot(`
|
||||||
|
"Parse error on line 1:
|
||||||
|
...quenceDiagram; A-->B
|
||||||
|
-----------------------^
|
||||||
|
Expecting 'TXT', got 'NEWLINE'"
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should throw the right error for unregistered diagrams', () => {
|
||||||
|
expect(() => getDiagramFromText('thor TD; A-->B')).toThrowError(
|
||||||
|
'No diagram type detected for text: thor TD; A-->B'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user