Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Change SequenceDB to class based architecture. #6272

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

saurabhg772244
Copy link
Collaborator

📑 Summary

  • Refactors the existing sequenceDb implementation from a functional approach to a class-based architecture to improve code organization, maintainability, and extensibility.
  • Return a new DB object when diagramObject.db is called.
  • Introduces new SequenceDB class to encapsulate database operations and state
  • Migrates existing functions to class methods
  • Maintains existing API interface for backwards compatibility
  • Following functions still uses common DB:
    • apply
    • parseMessage
    • parseBoxData
    • LINETYPE
    • PLACEMENT
    • setDiagramTitle
    • setAccTitle
    • setAccDescription
  • Reason why above functions still uses common DB: Refactor: Change flowDB to class based architecture. #6161 (comment)
  • Added unique IDs for messages during parsing. Previously, IDs were assigned during rendering, which caused issues for diagrams since message operations relied on IDs.
  • Updated unit tests to support unique DB instances instead of shared DB.

Resolves #

📏 Design Decisions

  • The sequenceDb module previously relied on functions and global variables. Consequently, each call to mermaid.parse or mermaid.render resulted in changes to global variables.
  • This behavior caused issues when rendering multiple diagrams using the same Mermaid API, as the database object (DB) for the diagrams would get mixed up.
  • To address this, sequenceDb was refactored into a class-based structure. This change ensures that each diagramObject.db call creates a new instance of the database (DB), preventing conflicts between diagrams.

📋 Tasks

Make sure you

  • 📖 have read the contribution guidelines
  • 💻 have added necessary unit/e2e tests.
  • 📓 have added documentation. Make sure MERMAID_RELEASE_VERSION is used for all new features.
  • 🦋 If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Copy link

changeset-bot bot commented Feb 12, 2025

🦋 Changeset detected

Latest commit: 451c886

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
mermaid Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the Type: Other Not an enhancement or a bug label Feb 12, 2025
Copy link

netlify bot commented Feb 12, 2025

Deploy Preview for mermaid-js ready!

Name Link
🔨 Latest commit 451c886
🔍 Latest deploy log https://app.netlify.com/sites/mermaid-js/deploys/67af496ee39300000844e3ea
😎 Deploy Preview https://deploy-preview-6272--mermaid-js.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

pkg-pr-new bot commented Feb 12, 2025

Open in Stackblitz

npm i https://pkg.pr.new/mermaid-js/mermaid@6272
npm i https://pkg.pr.new/mermaid-js/mermaid/@mermaid-js/layout-elk@6272
npm i https://pkg.pr.new/mermaid-js/mermaid/@mermaid-js/parser@6272
npm i https://pkg.pr.new/mermaid-js/mermaid/@mermaid-js/mermaid-zenuml@6272

commit: 451c886

// JSON.parse the text
try {
let sanitizedText = sanitizeText(text.text, getConfig());
sanitizedText = sanitizedText.replace(/&/g, '&');

Check failure

Code scanning / CodeQL

Double escaping or unescaping High

This replacement may produce '&' characters that are double-unescaped
here
.

Copilot Autofix AI about 9 hours ago

To fix the problem, we should remove the unnecessary replacements of & and = after the sanitizeText function call. This ensures that the text remains properly sanitized and prevents any double unescaping issues.

Suggested changeset 1
packages/mermaid/src/diagrams/sequence/sequenceDb.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/mermaid/src/diagrams/sequence/sequenceDb.ts b/packages/mermaid/src/diagrams/sequence/sequenceDb.ts
--- a/packages/mermaid/src/diagrams/sequence/sequenceDb.ts
+++ b/packages/mermaid/src/diagrams/sequence/sequenceDb.ts
@@ -398,5 +398,3 @@
     try {
-      let sanitizedText = sanitizeText(text.text, getConfig());
-      sanitizedText = sanitizedText.replace(/&amp;/g, '&');
-      sanitizedText = sanitizedText.replace(/&equals;/g, '=');
+      const sanitizedText = sanitizeText(text.text, getConfig());
       const links = JSON.parse(sanitizedText);
@@ -414,6 +412,4 @@
       const links: Record<string, string> = {};
-      let sanitizedText = sanitizeText(text.text, getConfig());
+      const sanitizedText = sanitizeText(text.text, getConfig());
       const sep = sanitizedText.indexOf('@');
-      sanitizedText = sanitizedText.replace(/&amp;/g, '&');
-      sanitizedText = sanitizedText.replace(/&equals;/g, '=');
       const label = sanitizedText.slice(0, sep - 1).trim();
EOF
@@ -398,5 +398,3 @@
try {
let sanitizedText = sanitizeText(text.text, getConfig());
sanitizedText = sanitizedText.replace(/&amp;/g, '&');
sanitizedText = sanitizedText.replace(/&equals;/g, '=');
const sanitizedText = sanitizeText(text.text, getConfig());
const links = JSON.parse(sanitizedText);
@@ -414,6 +412,4 @@
const links: Record<string, string> = {};
let sanitizedText = sanitizeText(text.text, getConfig());
const sanitizedText = sanitizeText(text.text, getConfig());
const sep = sanitizedText.indexOf('@');
sanitizedText = sanitizedText.replace(/&amp;/g, '&');
sanitizedText = sanitizedText.replace(/&equals;/g, '=');
const label = sanitizedText.slice(0, sep - 1).trim();
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
const links: Record<string, string> = {};
let sanitizedText = sanitizeText(text.text, getConfig());
const sep = sanitizedText.indexOf('@');
sanitizedText = sanitizedText.replace(/&amp;/g, '&');

Check failure

Code scanning / CodeQL

Double escaping or unescaping High

This replacement may produce '&' characters that are double-unescaped
here
.

Copilot Autofix AI about 9 hours ago

To fix the problem, we need to ensure that unescaping operations are not performed redundantly. The best way to achieve this is to avoid manual unescaping and rely on a well-tested library for sanitization and unescaping. This will help prevent double unescaping and ensure that the data is processed correctly.

We will remove the manual unescaping operations and rely on the sanitizeText function to handle any necessary unescaping. If sanitizeText does not perform unescaping, we should update it to do so.

Suggested changeset 1
packages/mermaid/src/diagrams/sequence/sequenceDb.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/mermaid/src/diagrams/sequence/sequenceDb.ts b/packages/mermaid/src/diagrams/sequence/sequenceDb.ts
--- a/packages/mermaid/src/diagrams/sequence/sequenceDb.ts
+++ b/packages/mermaid/src/diagrams/sequence/sequenceDb.ts
@@ -398,5 +398,3 @@
     try {
-      let sanitizedText = sanitizeText(text.text, getConfig());
-      sanitizedText = sanitizedText.replace(/&amp;/g, '&');
-      sanitizedText = sanitizedText.replace(/&equals;/g, '=');
+      const sanitizedText = sanitizeText(text.text, getConfig());
       const links = JSON.parse(sanitizedText);
@@ -414,6 +412,4 @@
       const links: Record<string, string> = {};
-      let sanitizedText = sanitizeText(text.text, getConfig());
+      const sanitizedText = sanitizeText(text.text, getConfig());
       const sep = sanitizedText.indexOf('@');
-      sanitizedText = sanitizedText.replace(/&amp;/g, '&');
-      sanitizedText = sanitizedText.replace(/&equals;/g, '=');
       const label = sanitizedText.slice(0, sep - 1).trim();
EOF
@@ -398,5 +398,3 @@
try {
let sanitizedText = sanitizeText(text.text, getConfig());
sanitizedText = sanitizedText.replace(/&amp;/g, '&');
sanitizedText = sanitizedText.replace(/&equals;/g, '=');
const sanitizedText = sanitizeText(text.text, getConfig());
const links = JSON.parse(sanitizedText);
@@ -414,6 +412,4 @@
const links: Record<string, string> = {};
let sanitizedText = sanitizeText(text.text, getConfig());
const sanitizedText = sanitizeText(text.text, getConfig());
const sep = sanitizedText.indexOf('@');
sanitizedText = sanitizedText.replace(/&amp;/g, '&');
sanitizedText = sanitizedText.replace(/&equals;/g, '=');
const label = sanitizedText.slice(0, sep - 1).trim();
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Copy link

codecov bot commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 0% with 581 lines in your changes missing coverage. Please review.

Project coverage is 4.47%. Comparing base (8dd0e7a) to head (451c886).
Report is 11 commits behind head on develop.

Files with missing lines Patch % Lines
...ckages/mermaid/src/diagrams/sequence/sequenceDb.ts 0.00% 567 Missing ⚠️
...s/mermaid/src/diagrams/sequence/sequenceDiagram.ts 0.00% 14 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           develop   #6272   +/-   ##
=======================================
  Coverage     4.47%   4.47%           
=======================================
  Files          385     384    -1     
  Lines        54191   54179   -12     
  Branches       598     623   +25     
=======================================
  Hits          2425    2425           
+ Misses       51766   51754   -12     
Flag Coverage Δ
unit 4.47% <0.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
.../mermaid/src/diagrams/sequence/sequenceRenderer.ts 0.00% <ø> (ø)
...s/mermaid/src/diagrams/sequence/sequenceDiagram.ts 0.00% <0.00%> (ø)
...ckages/mermaid/src/diagrams/sequence/sequenceDb.ts 0.00% <0.00%> (ø)

... and 1 file with indirect coverage changes

Copy link
Member

@aloisklink aloisklink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The this.setWrap(getConfig().wrap); in the constructor seems to be the only thing I'm a bit nervous about, the rest of the PR seems good to me (as long as the visual regression tests come back green ✔️)

Comment on lines +230 to +233
const diagram1 = await Diagram.fromText(str);

await diagram1.renderer.draw(str, 'tst', '1.2.3', diagram1); // needs to be rendered for the correct value of visibility auto numbers
expect(diagram1.db.showSequenceNumbers()).toBe(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion(super non-blocking): I wonder if it's easier to just delete the let diagram at

// const parser = sequence.parser;
let diagram;

Because then the test would be:

    const diagram = await Diagram.fromText(str);
    await diagram.renderer.draw(str, 'tst', '1.2.3', diagram); // needs to be rendered for the correct value of visibility auto numbers
    expect(diagram.db.showSequenceNumbers()).toBe(true);

So you'll be changing much less lines of code in this PR.

@@ -1917,10 +1847,11 @@ Note left of Alice: Bob thinks
Bob->>Alice: Fine!`;

await mermaidAPI.parse(str);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: There's a couple of mermaidAPI.parse calls you can delete from these unit tests, since they don't do anything.

Suggested change
await mermaidAPI.parse(str);

packages/mermaid/src/diagrams/sequence/sequenceDb.ts Outdated Show resolved Hide resolved
packages/mermaid/src/diagrams/sequence/sequenceDb.ts Outdated Show resolved Hide resolved
@saurabhg772244
Copy link
Collaborator Author

@aloisklink i think we can refactor it into separate PR

@saurabhg772244 saurabhg772244 marked this pull request as ready for review February 14, 2025 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Other Not an enhancement or a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants