mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-08-22 01:36:43 +02:00
Merge pull request #4338 from mermaid-js/sidv/fixClassGrammar
fix ClassGrammar
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "mermaid",
|
"name": "mermaid",
|
||||||
"version": "10.2.0",
|
"version": "10.2.0-rc.2",
|
||||||
"description": "Markdown-ish syntax for generating flowcharts, sequence diagrams, class diagrams, gantt charts and git graphs.",
|
"description": "Markdown-ish syntax for generating flowcharts, sequence diagrams, class diagrams, gantt charts and git graphs.",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"module": "./dist/mermaid.core.mjs",
|
"module": "./dist/mermaid.core.mjs",
|
||||||
|
@@ -1,13 +0,0 @@
|
|||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
||||||
const fs = require('fs');
|
|
||||||
|
|
||||||
import { LALRGenerator } from 'jison';
|
|
||||||
|
|
||||||
describe('class diagram grammar', function () {
|
|
||||||
it('should introduce no new conflicts', function () {
|
|
||||||
const file = require.resolve('./parser/classDiagram.jison');
|
|
||||||
const grammarSource = fs.readFileSync(file, 'utf8');
|
|
||||||
const grammarParser = new LALRGenerator(grammarSource, {});
|
|
||||||
expect(grammarParser.conflicts < 16).toBe(true);
|
|
||||||
});
|
|
||||||
});
|
|
@@ -0,0 +1,16 @@
|
|||||||
|
import { readFile } from 'node:fs/promises';
|
||||||
|
// @ts-ignore - no types
|
||||||
|
import { LALRGenerator } from 'jison';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
const getAbsolutePath = (relativePath: string) => {
|
||||||
|
return new URL(path.join(path.dirname(import.meta.url), relativePath)).pathname;
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('class diagram grammar', function () {
|
||||||
|
it('should have no conflicts', async function () {
|
||||||
|
const grammarSource = await readFile(getAbsolutePath('./parser/classDiagram.jison'), 'utf8');
|
||||||
|
const grammarParser = new LALRGenerator(grammarSource, {});
|
||||||
|
expect(grammarParser.conflicts).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
78
packages/mermaid/src/diagrams/class/classParser.spec.ts
Normal file
78
packages/mermaid/src/diagrams/class/classParser.spec.ts
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
import { setConfig } from '../../config.js';
|
||||||
|
import classDB from './classDb.js';
|
||||||
|
// @ts-ignore - no types in jison
|
||||||
|
import classDiagram from './parser/classDiagram.jison';
|
||||||
|
|
||||||
|
setConfig({
|
||||||
|
securityLevel: 'strict',
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when parsing class diagram', function () {
|
||||||
|
beforeEach(function () {
|
||||||
|
classDiagram.parser.yy = classDB;
|
||||||
|
classDiagram.parser.yy.clear();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should parse diagram with direction', () => {
|
||||||
|
classDiagram.parser.parse(`classDiagram
|
||||||
|
direction TB
|
||||||
|
class Student {
|
||||||
|
-idCard : IdCard
|
||||||
|
}
|
||||||
|
class IdCard{
|
||||||
|
-id : int
|
||||||
|
-name : string
|
||||||
|
}
|
||||||
|
class Bike{
|
||||||
|
-id : int
|
||||||
|
-name : string
|
||||||
|
}
|
||||||
|
Student "1" --o "1" IdCard : carries
|
||||||
|
Student "1" --o "1" Bike : rides`);
|
||||||
|
|
||||||
|
expect(Object.keys(classDB.getClasses()).length).toBe(3);
|
||||||
|
expect(classDB.getClasses().Student).toMatchInlineSnapshot(`
|
||||||
|
{
|
||||||
|
"annotations": [],
|
||||||
|
"cssClasses": [],
|
||||||
|
"domId": "classId-Student-0",
|
||||||
|
"id": "Student",
|
||||||
|
"label": "Student",
|
||||||
|
"members": [
|
||||||
|
"-idCard : IdCard",
|
||||||
|
],
|
||||||
|
"methods": [],
|
||||||
|
"type": "",
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
expect(classDB.getRelations().length).toBe(2);
|
||||||
|
expect(classDB.getRelations()).toMatchInlineSnapshot(`
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"id1": "Student",
|
||||||
|
"id2": "IdCard",
|
||||||
|
"relation": {
|
||||||
|
"lineType": 0,
|
||||||
|
"type1": "none",
|
||||||
|
"type2": 0,
|
||||||
|
},
|
||||||
|
"relationTitle1": "1",
|
||||||
|
"relationTitle2": "1",
|
||||||
|
"title": "carries",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id1": "Student",
|
||||||
|
"id2": "Bike",
|
||||||
|
"relation": {
|
||||||
|
"lineType": 0,
|
||||||
|
"type1": "none",
|
||||||
|
"type2": 0,
|
||||||
|
},
|
||||||
|
"relationTitle1": "1",
|
||||||
|
"relationTitle2": "1",
|
||||||
|
"title": "rides",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
});
|
@@ -200,9 +200,8 @@ Function arguments are optional: 'call <callback_name>()' simply executes 'callb
|
|||||||
|
|
||||||
start
|
start
|
||||||
: mermaidDoc
|
: mermaidDoc
|
||||||
| statements
|
|
||||||
| direction
|
|
||||||
| directive start
|
| directive start
|
||||||
|
| statements
|
||||||
;
|
;
|
||||||
|
|
||||||
direction
|
direction
|
||||||
@@ -272,7 +271,6 @@ statement
|
|||||||
| clickStatement
|
| clickStatement
|
||||||
| cssClassStatement
|
| cssClassStatement
|
||||||
| noteStatement
|
| noteStatement
|
||||||
| directive
|
|
||||||
| direction
|
| direction
|
||||||
| acc_title acc_title_value { $$=$2.trim();yy.setAccTitle($$); }
|
| acc_title acc_title_value { $$=$2.trim();yy.setAccTitle($$); }
|
||||||
| acc_descr acc_descr_value { $$=$2.trim();yy.setAccDescription($$); }
|
| acc_descr acc_descr_value { $$=$2.trim();yy.setAccDescription($$); }
|
||||||
|
@@ -14,12 +14,17 @@ const lint = async (file: string): Promise<boolean> => {
|
|||||||
console.log(`Linting ${file}`);
|
console.log(`Linting ${file}`);
|
||||||
const jisonCode = await readFile(file, 'utf8');
|
const jisonCode = await readFile(file, 'utf8');
|
||||||
// @ts-ignore no typings
|
// @ts-ignore no typings
|
||||||
const jsCode = new jison.Generator(jisonCode, { moduleType: 'amd' }).generate();
|
const generator = new jison.Generator(jisonCode, { moduleType: 'amd' });
|
||||||
|
const jsCode = generator.generate();
|
||||||
const [result] = await linter.lintText(jsCode);
|
const [result] = await linter.lintText(jsCode);
|
||||||
if (result.errorCount > 0) {
|
if (result.errorCount > 0) {
|
||||||
console.error(`Linting failed for ${file}`);
|
console.error(`Linting failed for ${file}`);
|
||||||
console.error(result.messages);
|
console.error(result.messages);
|
||||||
}
|
}
|
||||||
|
if (generator.conflicts > 0) {
|
||||||
|
console.error(`Linting failed for ${file}. Conflicts found in grammar`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return result.errorCount === 0;
|
return result.errorCount === 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user