diff --git a/packages/mermaid/src/diagrams/class/classDb.ts b/packages/mermaid/src/diagrams/class/classDb.ts index 7f233f3bf..68d5ec852 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 7816345c8..405e411fa 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';