Skip to content

Commit

Permalink
chore: Rearrange extract, to match previous location.
Browse files Browse the repository at this point in the history
Might make it easier for reviewers
  • Loading branch information
sidharthv96 committed Feb 20, 2025
1 parent bb4b92a commit f48e5c5
Showing 1 changed file with 69 additions and 69 deletions.
138 changes: 69 additions & 69 deletions packages/mermaid/src/diagrams/state/stateDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit f48e5c5

Please sign in to comment.