From f48e5c5c1db9a88019f97d6fac5fda7558cfb903 Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Thu, 20 Feb 2025 16:29:45 +0530 Subject: [PATCH] chore: Rearrange extract, to match previous location. Might make it easier for reviewers --- .../mermaid/src/diagrams/state/stateDb.ts | 138 +++++++++--------- 1 file changed, 69 insertions(+), 69 deletions(-) diff --git a/packages/mermaid/src/diagrams/state/stateDb.ts b/packages/mermaid/src/diagrams/state/stateDb.ts index 853a0e22f6..baca48c8fa 100644 --- a/packages/mermaid/src/diagrams/state/stateDb.ts +++ b/packages/mermaid/src/diagrams/state/stateDb.ts @@ -202,6 +202,75 @@ export class StateDB { this.trimColon = this.trimColon.bind(this); } + setRootDoc(o: Stmt[]) { + log.info('Setting root doc', o); + this.rootDoc = o; + if (this.version === 1) { + this.extract(o); + } else { + this.extract(this.getRootDocV2()); + } + } + + docTranslator(parent: RootStmt | StateStmt, node: Stmt, first: boolean) { + if (node.stmt === STMT_RELATION) { + this.docTranslator(parent, node.state1, true); + this.docTranslator(parent, node.state2, false); + return; + } + + if (node.stmt === STMT_STATE) { + if (node.id === CONSTANTS.START_NODE) { + node.id = parent.id + (first ? '_start' : '_end'); + node.start = first; + } else { + // This is just a plain state, not a start or end + node.id = node.id.trim(); + } + } + + if ((node.stmt !== STMT_ROOT && node.stmt !== STMT_STATE) || !node.doc) { + return; + } + + const doc = []; + // Check for concurrency + let currentDoc = []; + for (const stmt of node.doc) { + if ((stmt as StateStmt).type === DIVIDER_TYPE) { + const newNode = clone(stmt as StateStmt); + newNode.doc = clone(currentDoc); + doc.push(newNode); + currentDoc = []; + } else { + currentDoc.push(stmt); + } + } + + // If any divider was encountered + if (doc.length > 0 && currentDoc.length > 0) { + const newNode = { + stmt: STMT_STATE, + id: generateId(), + type: 'divider', + doc: clone(currentDoc), + } satisfies StateStmt; + doc.push(clone(newNode)); + node.doc = doc; + } + + node.doc.forEach((docNode) => this.docTranslator(node, docNode, true)); + } + + private getRootDocV2() { + this.docTranslator( + { id: STMT_ROOT, stmt: STMT_ROOT }, + { id: STMT_ROOT, stmt: STMT_ROOT, doc: this.rootDoc }, + true + ); + return { id: STMT_ROOT, doc: this.rootDoc }; + } + /** * Convert all of the statements (stmts) that were parsed into states and relationships. * This is done because a state diagram may have nested sections, @@ -280,75 +349,6 @@ export class StateDB { } } - setRootDoc(o: Stmt[]) { - log.info('Setting root doc', o); - this.rootDoc = o; - if (this.version === 1) { - this.extract(o); - } else { - this.extract(this.getRootDocV2()); - } - } - - docTranslator(parent: RootStmt | StateStmt, node: Stmt, first: boolean) { - if (node.stmt === STMT_RELATION) { - this.docTranslator(parent, node.state1, true); - this.docTranslator(parent, node.state2, false); - return; - } - - if (node.stmt === STMT_STATE) { - if (node.id === CONSTANTS.START_NODE) { - node.id = parent.id + (first ? '_start' : '_end'); - node.start = first; - } else { - // This is just a plain state, not a start or end - node.id = node.id.trim(); - } - } - - if ((node.stmt !== STMT_ROOT && node.stmt !== STMT_STATE) || !node.doc) { - return; - } - - const doc = []; - // Check for concurrency - let currentDoc = []; - for (const stmt of node.doc) { - if ((stmt as StateStmt).type === DIVIDER_TYPE) { - const newNode = clone(stmt as StateStmt); - newNode.doc = clone(currentDoc); - doc.push(newNode); - currentDoc = []; - } else { - currentDoc.push(stmt); - } - } - - // If any divider was encountered - if (doc.length > 0 && currentDoc.length > 0) { - const newNode = { - stmt: STMT_STATE, - id: generateId(), - type: 'divider', - doc: clone(currentDoc), - } satisfies StateStmt; - doc.push(clone(newNode)); - node.doc = doc; - } - - node.doc.forEach((docNode) => this.docTranslator(node, docNode, true)); - } - - private getRootDocV2() { - this.docTranslator( - { id: STMT_ROOT, stmt: STMT_ROOT }, - { id: STMT_ROOT, stmt: STMT_ROOT, doc: this.rootDoc }, - true - ); - return { id: STMT_ROOT, doc: this.rootDoc }; - } - /** * Function called by parser when a node definition has been found. *