Skip to content

Commit

Permalink
Merge pull request mermaid-js#3921 from tomperr/fix/3795_class_tilde_…
Browse files Browse the repository at this point in the history
…visibility

fix(generic): fix generic type detection
  • Loading branch information
sidharthv96 authored Dec 23, 2022
2 parents 8ed1ad5 + df42f96 commit 2c88c6b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
13 changes: 13 additions & 0 deletions demos/classchart.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,20 @@ <h1>Class diagram demos</h1>
Pineapple : -int leafCount()
Pineapple : -int spikeCount()
</pre>
<hr />

<pre class="mermaid">
classDiagram
class Person {
+Id : Guid
+FirstName : string
+LastName : string
-privateProperty : string
#ProtectedProperty : string
~InternalProperty : string
~AnotherInternalProperty : List~List~string~~
}
</pre>
<hr />

<script src="./mermaid.js"></script>
Expand Down
4 changes: 3 additions & 1 deletion docs/syntax/classDiagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class BankAccount{

#### Generic Types

Members can be defined using generic types, such as `List<int>`, for fields, parameters, and return types by enclosing the type within `~` (**tilde**). Note: **nested** type declarations such as `List<List<int>>` are not currently supported.
Members can be defined using generic types, such as `List<int>`, for fields, parameters, and return types by enclosing the type within `~` (**tilde**). **Nested** type declarations such as `List<List<int>>` are supported.

Generics can be represented as part of a class definition and also in the parameters or the return value of a method/function:

Expand All @@ -222,6 +222,7 @@ class Square~Shape~{
Square : -List~string~ messages
Square : +setMessages(List~string~ messages)
Square : +getMessages() List~string~
Square : +getDistanceMatrix() List~List~int~~
```

```mermaid
Expand All @@ -236,6 +237,7 @@ class Square~Shape~{
Square : -List~string~ messages
Square : +setMessages(List~string~ messages)
Square : +getMessages() List~string~
Square : +getDistanceMatrix() List~List~int~~
```

#### Visibility
Expand Down
2 changes: 2 additions & 0 deletions packages/mermaid/src/diagrams/common/common.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,7 @@ describe('generic parser', function () {
expect(parseGenericTypes('test ~Array~Array~string[]~~~')).toEqual(
'test <Array<Array<string[]>>>'
);
expect(parseGenericTypes('~test')).toEqual('~test');
expect(parseGenericTypes('~test Array~string~')).toEqual('~test Array<string>');
});
});
14 changes: 10 additions & 4 deletions packages/mermaid/src/diagrams/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,17 @@ export const evaluate = (val?: string | boolean): boolean =>
export const parseGenericTypes = function (text: string): string {
let cleanedText = text;

if (text.includes('~')) {
cleanedText = cleanedText.replace(/~([^~].*)/, '<$1');
cleanedText = cleanedText.replace(/~([^~]*)$/, '>$1');
if (text.split('~').length - 1 >= 2) {
let newCleanedText = cleanedText;

return parseGenericTypes(cleanedText);
// use a do...while loop instead of replaceAll to detect recursion
// e.g. Array~Array~T~~
do {
cleanedText = newCleanedText;
newCleanedText = cleanedText.replace(/~([^\s,:;]+)~/, '<$1>');
} while (newCleanedText != cleanedText);

return parseGenericTypes(newCleanedText);
} else {
return cleanedText;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/mermaid/src/docs/syntax/classDiagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class BankAccount{

#### Generic Types

Members can be defined using generic types, such as `List<int>`, for fields, parameters, and return types by enclosing the type within `~` (**tilde**). Note: **nested** type declarations such as `List<List<int>>` are not currently supported.
Members can be defined using generic types, such as `List<int>`, for fields, parameters, and return types by enclosing the type within `~` (**tilde**). **Nested** type declarations such as `List<List<int>>` are supported.

Generics can be represented as part of a class definition and also in the parameters or the return value of a method/function:

Expand All @@ -139,6 +139,7 @@ class Square~Shape~{
Square : -List~string~ messages
Square : +setMessages(List~string~ messages)
Square : +getMessages() List~string~
Square : +getDistanceMatrix() List~List~int~~
```

#### Visibility
Expand Down

0 comments on commit 2c88c6b

Please sign in to comment.