From eca0cea33912410a2ec049d75776c6ac8321e009 Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Mon, 28 Aug 2023 18:54:30 +0530 Subject: [PATCH 1/3] refactor: Fir classDB types --- .../mermaid/src/diagrams/class/classDb.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/mermaid/src/diagrams/class/classDb.ts b/packages/mermaid/src/diagrams/class/classDb.ts index b14b1d07a1..7f233f3bfb 100644 --- a/packages/mermaid/src/diagrams/class/classDb.ts +++ b/packages/mermaid/src/diagrams/class/classDb.ts @@ -1,4 +1,3 @@ -// @ts-nocheck - don't check until handle it import type { Selection } from 'd3'; import { select } from 'd3'; import { log } from '../../logger.js'; @@ -71,21 +70,21 @@ export const setClassLabel = function (id: string, label: string) { * @public */ export const addClass = function (id: string) { - const classId = splitClassNameAndType(id); + const { className, type } = splitClassNameAndType(id); // Only add class if not exists - if (classes[classId.className] !== undefined) { + if (Object.hasOwn(classes, className)) { return; } - classes[classId.className] = { - id: classId.className, - type: classId.type, - label: classId.className, + classes[className] = { + id: className, + type: type, + label: className, cssClasses: [], methods: [], members: [], annotations: [], - domId: MERMAID_DOM_ID_PREFIX + classId.className + '-' + classCounter, + domId: MERMAID_DOM_ID_PREFIX + className + '-' + classCounter, } as ClassNode; classCounter++; @@ -368,6 +367,7 @@ export const relationType = { const setupToolTips = function (element: Element) { let tooltipElem: Selection = select('.mermaidTooltip'); + // @ts-expect-error - Incorrect types if ((tooltipElem._groups || tooltipElem)[0][0] === null) { tooltipElem = select('body').append('div').attr('class', 'mermaidTooltip').style('opacity', 0); } @@ -377,7 +377,6 @@ const setupToolTips = function (element: Element) { const nodes = svg.selectAll('g.node'); nodes .on('mouseover', function () { - // @ts-expect-error - select is not part of the d3 type definition const el = select(this); const title = el.attr('title'); // Don't try to draw a tooltip if no data is provided @@ -387,6 +386,7 @@ const setupToolTips = function (element: Element) { // @ts-ignore - getBoundingClientRect is not part of the d3 type definition const rect = this.getBoundingClientRect(); + // @ts-expect-error - Incorrect types tooltipElem.transition().duration(200).style('opacity', '.9'); tooltipElem .text(el.attr('title')) @@ -396,8 +396,8 @@ const setupToolTips = function (element: Element) { el.classed('hover', true); }) .on('mouseout', function () { + // @ts-expect-error - Incorrect types tooltipElem.transition().duration(500).style('opacity', 0); - // @ts-expect-error - select is not part of the d3 type definition const el = select(this); el.classed('hover', false); }); From c3df18fc5bdef8112ba3a91e67c143c8449ecc9f Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Mon, 28 Aug 2023 18:57:34 +0530 Subject: [PATCH 2/3] feat: #2685 Support class member initialization by member definition --- packages/mermaid/src/diagrams/class/classDb.ts | 2 ++ .../src/diagrams/class/classDiagram.spec.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/packages/mermaid/src/diagrams/class/classDb.ts b/packages/mermaid/src/diagrams/class/classDb.ts index 7f233f3bfb..68d5ec852a 100644 --- a/packages/mermaid/src/diagrams/class/classDb.ts +++ b/packages/mermaid/src/diagrams/class/classDb.ts @@ -174,6 +174,8 @@ export const addAnnotation = function (className: string, annotation: string) { * @public */ export const addMember = function (className: string, member: string) { + addClass(className); + const validatedClassName = splitClassNameAndType(className).className; const theClass = classes[validatedClassName]; diff --git a/packages/mermaid/src/diagrams/class/classDiagram.spec.ts b/packages/mermaid/src/diagrams/class/classDiagram.spec.ts index 7816345c80..405e411fab 100644 --- a/packages/mermaid/src/diagrams/class/classDiagram.spec.ts +++ b/packages/mermaid/src/diagrams/class/classDiagram.spec.ts @@ -742,6 +742,22 @@ describe('given a class diagram with members and methods ', function () { parser.parse(str); }); + it('should handle direct member declaration', function () { + const str = 'classDiagram\n' + 'Car : wheels'; + + parser.parse(str); + expect(classDb.getClasses()).toHaveProperty('Car'); + expect(classDb.getClasses()['Car']['members']).toContain('wheels'); + }); + + it('should handle direct member declaration with type', function () { + const str = 'classDiagram\n' + 'Car : int wheels'; + + parser.parse(str); + expect(classDb.getClasses()).toHaveProperty('Car'); + expect(classDb.getClasses()['Car']['members']).toContain('int wheels'); + }); + it('should handle simple member declaration with type', function () { const str = 'classDiagram\n' + 'class Car\n' + 'Car : int wheels'; From fd7406b94aab05db8775c1573b773d234a1a0e5c Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Tue, 5 Sep 2023 21:41:49 +0530 Subject: [PATCH 3/3] chore: Fix unit tests --- .../src/diagrams/class/classDiagram.spec.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/mermaid/src/diagrams/class/classDiagram.spec.ts b/packages/mermaid/src/diagrams/class/classDiagram.spec.ts index 19e31e6d23..532c8aaa7a 100644 --- a/packages/mermaid/src/diagrams/class/classDiagram.spec.ts +++ b/packages/mermaid/src/diagrams/class/classDiagram.spec.ts @@ -814,19 +814,17 @@ describe('given a class diagram with members and methods ', function () { }); it('should handle direct member declaration', function () { - const str = 'classDiagram\n' + 'Car : wheels'; - - parser.parse(str); - expect(classDb.getClasses()).toHaveProperty('Car'); - expect(classDb.getClasses()['Car']['members']).toContain('wheels'); + parser.parse('classDiagram\n' + 'Car : wheels'); + const car = classDb.getClass('Car'); + expect(car.members.length).toBe(1); + expect(car.members[0].id).toBe('wheels'); }); it('should handle direct member declaration with type', function () { - const str = 'classDiagram\n' + 'Car : int wheels'; - - parser.parse(str); - expect(classDb.getClasses()).toHaveProperty('Car'); - expect(classDb.getClasses()['Car']['members']).toContain('int wheels'); + parser.parse('classDiagram\n' + 'Car : int wheels'); + const car = classDb.getClass('Car'); + expect(car.members.length).toBe(1); + expect(car.members[0].id).toBe('int wheels'); }); it('should handle simple member declaration with type', function () {