Skip to content

Commit

Permalink
feat: [classDiagram] allow to parse more characters in class member l…
Browse files Browse the repository at this point in the history
…abel (#270)
  • Loading branch information
hikerpig authored Mar 13, 2024
1 parent 1d45081 commit d635362
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 37 deletions.
7 changes: 7 additions & 0 deletions .changeset/spotty-roses-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@pintora/diagrams': patch
'@pintora/cli': patch
'@pintora/standalone': patch
---

feat: [classDiagram] allow to parse more characters in class member label
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 1 addition & 22 deletions packages/pintora-diagrams/src/class/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -92,7 +90,6 @@ export type ClassIR = BaseDiagramIR & {
}

const NAMESPACE_SEP = '.'
const FIELD_SEP = ':'

export class ClassDb extends BaseDb {
protected classes: Record<string, TClass> = {}
Expand Down Expand Up @@ -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'
Expand All @@ -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,
Expand Down
5 changes: 0 additions & 5 deletions packages/pintora-diagrams/src/class/parser/classDiagram.ne
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -77,7 +73,6 @@ let lexer = moo.states({
},
_PLACEMENT,
NL: MOO_NEWLINE,
COMMA: /,/,
VALID_TEXT: { match: VALID_TEXT_REGEXP, fallback: true },
}
})
Expand Down

0 comments on commit d635362

Please sign in to comment.