From d2e2017907913210e5b4c5ff39f518926dc71df1 Mon Sep 17 00:00:00 2001 From: Austin Fulbright Date: Wed, 24 Jul 2024 09:48:46 -0400 Subject: [PATCH] fixed undefined for type errors --- .../mermaid/src/diagrams/git/gitGraphAst.ts | 64 +++++++++++-------- .../src/diagrams/git/gitGraphParser.ts | 53 +-------------- .../mermaid/src/diagrams/git/gitGraphTypes.ts | 4 +- 3 files changed, 41 insertions(+), 80 deletions(-) diff --git a/packages/mermaid/src/diagrams/git/gitGraphAst.ts b/packages/mermaid/src/diagrams/git/gitGraphAst.ts index 9a26d32f7..8148e0249 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphAst.ts +++ b/packages/mermaid/src/diagrams/git/gitGraphAst.ts @@ -19,7 +19,7 @@ const mainBranchOrder = defaultConfig.gitGraph.mainBranchOrder; let commits = new Map(); let head: Commit | null = null; -let branchesConfig = new Map(); +let branchesConfig = new Map(); branchesConfig.set(mainBranchName, { name: mainBranchName, order: mainBranchOrder }); let branches = new Map(); branches.set(mainBranchName, null); @@ -107,18 +107,19 @@ export const getOptions = function () { return options; }; -export const commit = function (msg: string, id: string, type: number, tag: string) { - log.info('commit', msg, id, type, tag); - log.debug('Entering commit:', msg, id, type, tag); +export const commit = function (msg: string, id: string, type: number, tags: string[] | undefined) { + log.info('commit', msg, id, type, tags); + log.debug('Entering commit:', msg, id, type, tags); id = common.sanitizeText(id, getConfig()); msg = common.sanitizeText(msg, getConfig()); - tag = common.sanitizeText(tag, getConfig()); + const config = getConfig(); + tags = tags?.map((tag) => common.sanitizeText(tag, config)); const newCommit: Commit = { id: id ? id : seq + '-' + getId(), message: msg, seq: seq++, - type: type, - tag: tag ? tag : '', + type: type ? type : commitType.NORMAL, + tags: tags ? tags : [], parents: head == null ? [] : [head.id], branch: curBranch, }; @@ -129,7 +130,7 @@ export const commit = function (msg: string, id: string, type: number, tag: stri log.debug('in pushCommit ' + newCommit.id); }; -export const branch = function (name: string, order: number) { +export const branch = function (name: string, order: number | undefined) { name = common.sanitizeText(name, getConfig()); if (!branches.has(name)) { branches.set(name, head != null ? head.id : null); @@ -138,7 +139,7 @@ export const branch = function (name: string, order: number) { log.debug('in createBranch'); } else { throw new Error( - `Trying to create an existing branch: ${name}. Use 'checkout ${name}' instead.` + `Trying to create an existing branch. (Help: Either use a new name if you want create a new branch or try using "checkout ${name}" to switch to an existing branch)` ); } }; @@ -147,7 +148,7 @@ export const merge = ( otherBranch: string, custom_id?: string, override_type?: number, - custom_tag?: string + custom_tags?: string[] ): void => { otherBranch = common.sanitizeText(otherBranch, getConfig()); if (custom_id) { @@ -227,12 +228,19 @@ export const merge = ( ' already exists, use different custom Id' ); error.hash = { - text: 'merge ' + otherBranch + custom_id + override_type + custom_tag, - token: 'merge ' + otherBranch + custom_id + override_type + custom_tag, + text: 'merge ' + otherBranch + custom_id + override_type + custom_tags?.join(' '), + token: 'merge ' + otherBranch + custom_id + override_type + custom_tags?.join(' '), line: '1', loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, expected: [ - 'merge ' + otherBranch + ' ' + custom_id + '_UNIQUE ' + override_type + ' ' + custom_tag, + 'merge ' + + otherBranch + + ' ' + + custom_id + + '_UNIQUE ' + + override_type + + ' ' + + custom_tags?.join(' '), ], }; @@ -258,9 +266,9 @@ export const merge = ( parents: [head == null ? null : head.id, verifiedBranch], branch: curBranch, type: commitType.MERGE, - customType: override_type, //TODO - need to make customType optional - customId: custom_id, //TODO - need to make customId optional as well as tag - tag: custom_tag ? custom_tag : '', + customType: override_type, + customId: custom_id ? true : false, + tags: custom_tags ? custom_tags : [], }; head = commit; commits.set(commit.id, commit); @@ -273,13 +281,14 @@ export const merge = ( export const cherryPick = function ( sourceId: string, targetId: string, - tag: string, + tags: string[], parentCommitId: string ) { - log.debug('Entering cherryPick:', sourceId, targetId, tag); + log.debug('Entering cherryPick:', sourceId, targetId, tags); sourceId = common.sanitizeText(sourceId, getConfig()); targetId = common.sanitizeText(targetId, getConfig()); - tag = common.sanitizeText(tag, getConfig()); + const config = getConfig(); + tags = tags?.map((tag) => common.sanitizeText(tag, config)); parentCommitId = common.sanitizeText(parentCommitId, getConfig()); if (!sourceId || !commits.has(sourceId)) { @@ -367,11 +376,13 @@ export const cherryPick = function ( parents: [head == null ? null : head.id, sourceCommit.id], branch: curBranch, type: commitType.CHERRY_PICK, - tag: - tag ?? - `cherry-pick:${sourceCommit.id}${ - sourceCommit.type === commitType.MERGE ? `|parent:${parentCommitId}` : '' - }`, + tags: tags + ? tags.filter(Boolean) + : [ + `cherry-pick:${sourceCommit.id}${ + sourceCommit.type === commitType.MERGE ? `|parent:${parentCommitId}` : '' + }`, + ], }; head = commit; commits.set(commit.id, commit); @@ -504,7 +515,7 @@ export const clear = function () { export const getBranchesAsObjArray = function () { const branchesArray = [...branchesConfig.values()] .map((branchConfig, i) => { - if (branchConfig.order !== null) { + if (branchConfig.order !== null && branchConfig.order !== undefined) { return branchConfig; } return { @@ -512,7 +523,7 @@ export const getBranchesAsObjArray = function () { order: parseFloat(`0.${i}`), }; }) - .sort((a, b) => a.order - b.order) + .sort((a, b) => (a.order ?? 0) - (b.order ?? 0)) .map(({ name }) => ({ name })); return branchesArray; @@ -551,6 +562,7 @@ export const commitType = { }; export default { + commitType, getConfig: () => getConfig().gitGraph, setDirection, setOptions, diff --git a/packages/mermaid/src/diagrams/git/gitGraphParser.ts b/packages/mermaid/src/diagrams/git/gitGraphParser.ts index 713992e0f..d193109b4 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphParser.ts +++ b/packages/mermaid/src/diagrams/git/gitGraphParser.ts @@ -4,65 +4,14 @@ import type { ParserDefinition } from '../../diagram-api/types.js'; import { log } from '../../logger.js'; import { populateCommonDb } from '../common/populateCommonDb.js'; import db from './gitGraphAst.js'; -import type { - Statement, - CommitAst, - Branch, - Merge, - Checkout, - CherryPicking, -} from './gitGraphTypes.js'; const populate = (ast: any) => { populateCommonDb(ast, db); for (const statement of ast.statements) { - parseStatement(statement); + log.debug(statement); } }; -const parseStatement = (statement: Statement) => { - switch (statement.$type) { - case 'Commit': - parseCommit(statement); - break; - case 'Branch': - parseBranch(statement); - break; - case 'Merge': - parseMerge(statement); - break; - case 'Checkout': - parseCheckout(statement); - break; - case 'CherryPicking': - parseCherryPicking(statement); - break; - default: - throw new Error(`Unknown statement type: ${(statement as any).$type}`); - } -}; - -function parseCommit(commit: CommitAst) { - const message = commit.message ?? ''; - db.commit(message, commit.id, commit.tags, commit.type); -} - -function parseBranch(branch: Branch) { - db.branch(branch.name, branch.order); -} - -function parseMerge(merge: Merge) { - db.merge(merge.branch, merge.id, merge.tags, merge.type); -} - -function parseCheckout(checkout: Checkout) { - db.checkout(checkout.branch); -} - -function parseCherryPicking(cherryPicking: CherryPicking) { - db.cherryPick(cherryPicking.id, cherryPicking.tags, cherryPicking.parent); -} - export const parser: ParserDefinition = { parse: async (input: string): Promise => { const ast: GitGraph = await parse('gitGraph', input); diff --git a/packages/mermaid/src/diagrams/git/gitGraphTypes.ts b/packages/mermaid/src/diagrams/git/gitGraphTypes.ts index da7567b8d..92bc3617a 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphTypes.ts +++ b/packages/mermaid/src/diagrams/git/gitGraphTypes.ts @@ -5,11 +5,11 @@ export interface Commit { message: string; seq: number; type: number; - tag: string; + tags: string[] | undefined; parents: (string | null)[]; branch: string; customType?: number; - customId?: string; + customId?: boolean; } export interface GitGraph {