From d6353624d7ad23b851da8109dac801b0af8c2521 Mon Sep 17 00:00:00 2001 From: hikerpig Date: Wed, 13 Mar 2024 23:22:38 +0800 Subject: [PATCH] feat: [classDiagram] allow to parse more characters in class member label (#270) --- .changeset/spotty-roses-suffer.md | 7 ++++++ .../__snapshots__/class-parser.spec.ts.snap | 22 ++++++++++-------- .../src/class/__tests__/class-parser.spec.ts | 2 ++ packages/pintora-diagrams/src/class/db.ts | 23 +------------------ .../src/class/parser/classDiagram.ne | 5 ---- 5 files changed, 22 insertions(+), 37 deletions(-) create mode 100644 .changeset/spotty-roses-suffer.md diff --git a/.changeset/spotty-roses-suffer.md b/.changeset/spotty-roses-suffer.md new file mode 100644 index 00000000..5bb74764 --- /dev/null +++ b/.changeset/spotty-roses-suffer.md @@ -0,0 +1,7 @@ +--- +'@pintora/diagrams': patch +'@pintora/cli': patch +'@pintora/standalone': patch +--- + +feat: [classDiagram] allow to parse more characters in class member label diff --git a/packages/pintora-diagrams/src/class/__tests__/__snapshots__/class-parser.spec.ts.snap b/packages/pintora-diagrams/src/class/__tests__/__snapshots__/class-parser.spec.ts.snap index 266edec2..c032d4d9 100644 --- a/packages/pintora-diagrams/src/class/__tests__/__snapshots__/class-parser.spec.ts.snap +++ b/packages/pintora-diagrams/src/class/__tests__/__snapshots__/class-parser.spec.ts.snap @@ -11,9 +11,19 @@ exports[`class parser can parse class label 1`] = ` "access": "public", "isMethod": false, "modifier": null, - "name": "RED", "raw": "RED", - "typeName": "", + }, + { + "access": "public", + "isMethod": false, + "modifier": null, + "raw": "Optional[Foo] foo", + }, + { + "access": "public", + "isMethod": true, + "modifier": null, + "raw": "void foo(a: int, b: int)", }, ], "name": "class1", @@ -34,32 +44,24 @@ exports[`class parser can parse methods and fields 1`] = ` "access": "public", "isMethod": false, "modifier": null, - "name": "field1", "raw": "string field1", - "typeName": "string", }, { "access": "public", "isMethod": true, "modifier": null, - "name": "method1()", "raw": "int method1()", - "typeName": "int", }, { "access": "public", "isMethod": false, "modifier": null, - "name": "field2", "raw": "field2: number", - "typeName": "number", }, { "access": "public", "isMethod": true, - "name": "method2()", "raw": "method2()", - "typeName": "", }, ], "name": "C1", diff --git a/packages/pintora-diagrams/src/class/__tests__/class-parser.spec.ts b/packages/pintora-diagrams/src/class/__tests__/class-parser.spec.ts index 62924217..09466399 100644 --- a/packages/pintora-diagrams/src/class/__tests__/class-parser.spec.ts +++ b/packages/pintora-diagrams/src/class/__tests__/class-parser.spec.ts @@ -126,6 +126,8 @@ describe('class parser', () => { class "This is class label" as class1 class class1 { RED + Optional[Foo] foo + void foo(a: int, b: int) } `) parse(example) diff --git a/packages/pintora-diagrams/src/class/db.ts b/packages/pintora-diagrams/src/class/db.ts index 5f79eae8..5abe16f8 100644 --- a/packages/pintora-diagrams/src/class/db.ts +++ b/packages/pintora-diagrams/src/class/db.ts @@ -34,8 +34,6 @@ type Access = 'public' | 'private' | 'protected' type Modifier = 'abstract' | 'static' | null export type TClassMember = { - name: string - typeName: string raw: string access?: Access isMethod?: boolean @@ -92,7 +90,6 @@ export type ClassIR = BaseDiagramIR & { } const NAMESPACE_SEP = '.' -const FIELD_SEP = ':' export class ClassDb extends BaseDb { protected classes: Record = {} @@ -196,8 +193,6 @@ export class ClassDb extends BaseDb { } protected parseMemberLabel(raw: string) { - let name: string - let typeName = '' let temp = raw const firstChar = temp[0] let access: Access = 'public' @@ -210,25 +205,9 @@ export class ClassDb extends BaseDb { if (isPrivate || isProtected || isPublic) temp = temp.slice(1) - if (temp.includes(FIELD_SEP)) { - const pos = temp.indexOf(FIELD_SEP) - name = temp.slice(0, pos) - typeName = temp.slice(pos + 1, temp.length).trim() - } else { - const spacePos = temp.indexOf(' ') - if (spacePos === -1) { - name = temp.trim() - } else { - typeName = temp.slice(0, spacePos) - name = temp.slice(spacePos + 1, temp.length).trim() - } - } - - const isMethod = /\(.*\)/.test(name) + const isMethod = /\(.*\)/.test(temp) const member: TClassMember = { - name, - typeName, access, raw, isMethod, diff --git a/packages/pintora-diagrams/src/class/parser/classDiagram.ne b/packages/pintora-diagrams/src/class/parser/classDiagram.ne index e8810bea..d6036971 100644 --- a/packages/pintora-diagrams/src/class/parser/classDiagram.ne +++ b/packages/pintora-diagrams/src/class/parser/classDiagram.ne @@ -44,17 +44,13 @@ let lexer = moo.states({ COLOR: COLOR_REGEXP, SEMICOLON: /;/, COLON: /:/, - COMMA: /,/, CLASS_DIAGRAM: /classDiagram/, L_PAREN: L_PAREN_REGEXP, R_PAREN: R_PAREN_REGEXP, L_BRACKET: { match: /\{/ }, R_BRACKET: { match: /\}/ }, - L_SQ_BRACKET: { match: /\[/ }, - R_SQ_BRACKET: { match: /\]/ }, TEXT_WITH_ANGLE_BRACKETS: { match: /\<\<(?:.*)\>\>/ }, EQ: { match: /=/ }, - // RELATION_INHERITANCE: { match: /\<\|\-\-/ }, SUBGRAPH: { match: /subgraph/ }, NOTE: textToCaseInsensitiveRegex('@note'), START_NOTE: { @@ -77,7 +73,6 @@ let lexer = moo.states({ }, _PLACEMENT, NL: MOO_NEWLINE, - COMMA: /,/, VALID_TEXT: { match: VALID_TEXT_REGEXP, fallback: true }, } })