mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-11-30 01:24:24 +01:00
fixed undefined for type errors
This commit is contained in:
@@ -19,7 +19,7 @@ const mainBranchOrder = defaultConfig.gitGraph.mainBranchOrder;
|
|||||||
|
|
||||||
let commits = new Map<string, Commit>();
|
let commits = new Map<string, Commit>();
|
||||||
let head: Commit | null = null;
|
let head: Commit | null = null;
|
||||||
let branchesConfig = new Map<string, { name: string; order: number }>();
|
let branchesConfig = new Map<string, { name: string; order: number | undefined }>();
|
||||||
branchesConfig.set(mainBranchName, { name: mainBranchName, order: mainBranchOrder });
|
branchesConfig.set(mainBranchName, { name: mainBranchName, order: mainBranchOrder });
|
||||||
let branches = new Map<string, string | null>();
|
let branches = new Map<string, string | null>();
|
||||||
branches.set(mainBranchName, null);
|
branches.set(mainBranchName, null);
|
||||||
@@ -107,18 +107,19 @@ export const getOptions = function () {
|
|||||||
return options;
|
return options;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const commit = function (msg: string, id: string, type: number, tag: string) {
|
export const commit = function (msg: string, id: string, type: number, tags: string[] | undefined) {
|
||||||
log.info('commit', msg, id, type, tag);
|
log.info('commit', msg, id, type, tags);
|
||||||
log.debug('Entering commit:', msg, id, type, tag);
|
log.debug('Entering commit:', msg, id, type, tags);
|
||||||
id = common.sanitizeText(id, getConfig());
|
id = common.sanitizeText(id, getConfig());
|
||||||
msg = common.sanitizeText(msg, 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 = {
|
const newCommit: Commit = {
|
||||||
id: id ? id : seq + '-' + getId(),
|
id: id ? id : seq + '-' + getId(),
|
||||||
message: msg,
|
message: msg,
|
||||||
seq: seq++,
|
seq: seq++,
|
||||||
type: type,
|
type: type ? type : commitType.NORMAL,
|
||||||
tag: tag ? tag : '',
|
tags: tags ? tags : [],
|
||||||
parents: head == null ? [] : [head.id],
|
parents: head == null ? [] : [head.id],
|
||||||
branch: curBranch,
|
branch: curBranch,
|
||||||
};
|
};
|
||||||
@@ -129,7 +130,7 @@ export const commit = function (msg: string, id: string, type: number, tag: stri
|
|||||||
log.debug('in pushCommit ' + newCommit.id);
|
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());
|
name = common.sanitizeText(name, getConfig());
|
||||||
if (!branches.has(name)) {
|
if (!branches.has(name)) {
|
||||||
branches.set(name, head != null ? head.id : null);
|
branches.set(name, head != null ? head.id : null);
|
||||||
@@ -138,7 +139,7 @@ export const branch = function (name: string, order: number) {
|
|||||||
log.debug('in createBranch');
|
log.debug('in createBranch');
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
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,
|
otherBranch: string,
|
||||||
custom_id?: string,
|
custom_id?: string,
|
||||||
override_type?: number,
|
override_type?: number,
|
||||||
custom_tag?: string
|
custom_tags?: string[]
|
||||||
): void => {
|
): void => {
|
||||||
otherBranch = common.sanitizeText(otherBranch, getConfig());
|
otherBranch = common.sanitizeText(otherBranch, getConfig());
|
||||||
if (custom_id) {
|
if (custom_id) {
|
||||||
@@ -227,12 +228,19 @@ export const merge = (
|
|||||||
' already exists, use different custom Id'
|
' already exists, use different custom Id'
|
||||||
);
|
);
|
||||||
error.hash = {
|
error.hash = {
|
||||||
text: '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_tag,
|
token: 'merge ' + otherBranch + custom_id + override_type + custom_tags?.join(' '),
|
||||||
line: '1',
|
line: '1',
|
||||||
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
expected: [
|
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],
|
parents: [head == null ? null : head.id, verifiedBranch],
|
||||||
branch: curBranch,
|
branch: curBranch,
|
||||||
type: commitType.MERGE,
|
type: commitType.MERGE,
|
||||||
customType: override_type, //TODO - need to make customType optional
|
customType: override_type,
|
||||||
customId: custom_id, //TODO - need to make customId optional as well as tag
|
customId: custom_id ? true : false,
|
||||||
tag: custom_tag ? custom_tag : '',
|
tags: custom_tags ? custom_tags : [],
|
||||||
};
|
};
|
||||||
head = commit;
|
head = commit;
|
||||||
commits.set(commit.id, commit);
|
commits.set(commit.id, commit);
|
||||||
@@ -273,13 +281,14 @@ export const merge = (
|
|||||||
export const cherryPick = function (
|
export const cherryPick = function (
|
||||||
sourceId: string,
|
sourceId: string,
|
||||||
targetId: string,
|
targetId: string,
|
||||||
tag: string,
|
tags: string[],
|
||||||
parentCommitId: string
|
parentCommitId: string
|
||||||
) {
|
) {
|
||||||
log.debug('Entering cherryPick:', sourceId, targetId, tag);
|
log.debug('Entering cherryPick:', sourceId, targetId, tags);
|
||||||
sourceId = common.sanitizeText(sourceId, getConfig());
|
sourceId = common.sanitizeText(sourceId, getConfig());
|
||||||
targetId = common.sanitizeText(targetId, 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());
|
parentCommitId = common.sanitizeText(parentCommitId, getConfig());
|
||||||
|
|
||||||
if (!sourceId || !commits.has(sourceId)) {
|
if (!sourceId || !commits.has(sourceId)) {
|
||||||
@@ -367,11 +376,13 @@ export const cherryPick = function (
|
|||||||
parents: [head == null ? null : head.id, sourceCommit.id],
|
parents: [head == null ? null : head.id, sourceCommit.id],
|
||||||
branch: curBranch,
|
branch: curBranch,
|
||||||
type: commitType.CHERRY_PICK,
|
type: commitType.CHERRY_PICK,
|
||||||
tag:
|
tags: tags
|
||||||
tag ??
|
? tags.filter(Boolean)
|
||||||
`cherry-pick:${sourceCommit.id}${
|
: [
|
||||||
sourceCommit.type === commitType.MERGE ? `|parent:${parentCommitId}` : ''
|
`cherry-pick:${sourceCommit.id}${
|
||||||
}`,
|
sourceCommit.type === commitType.MERGE ? `|parent:${parentCommitId}` : ''
|
||||||
|
}`,
|
||||||
|
],
|
||||||
};
|
};
|
||||||
head = commit;
|
head = commit;
|
||||||
commits.set(commit.id, commit);
|
commits.set(commit.id, commit);
|
||||||
@@ -504,7 +515,7 @@ export const clear = function () {
|
|||||||
export const getBranchesAsObjArray = function () {
|
export const getBranchesAsObjArray = function () {
|
||||||
const branchesArray = [...branchesConfig.values()]
|
const branchesArray = [...branchesConfig.values()]
|
||||||
.map((branchConfig, i) => {
|
.map((branchConfig, i) => {
|
||||||
if (branchConfig.order !== null) {
|
if (branchConfig.order !== null && branchConfig.order !== undefined) {
|
||||||
return branchConfig;
|
return branchConfig;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
@@ -512,7 +523,7 @@ export const getBranchesAsObjArray = function () {
|
|||||||
order: parseFloat(`0.${i}`),
|
order: parseFloat(`0.${i}`),
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.sort((a, b) => a.order - b.order)
|
.sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
|
||||||
.map(({ name }) => ({ name }));
|
.map(({ name }) => ({ name }));
|
||||||
|
|
||||||
return branchesArray;
|
return branchesArray;
|
||||||
@@ -551,6 +562,7 @@ export const commitType = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
commitType,
|
||||||
getConfig: () => getConfig().gitGraph,
|
getConfig: () => getConfig().gitGraph,
|
||||||
setDirection,
|
setDirection,
|
||||||
setOptions,
|
setOptions,
|
||||||
|
|||||||
@@ -4,65 +4,14 @@ import type { ParserDefinition } from '../../diagram-api/types.js';
|
|||||||
import { log } from '../../logger.js';
|
import { log } from '../../logger.js';
|
||||||
import { populateCommonDb } from '../common/populateCommonDb.js';
|
import { populateCommonDb } from '../common/populateCommonDb.js';
|
||||||
import db from './gitGraphAst.js';
|
import db from './gitGraphAst.js';
|
||||||
import type {
|
|
||||||
Statement,
|
|
||||||
CommitAst,
|
|
||||||
Branch,
|
|
||||||
Merge,
|
|
||||||
Checkout,
|
|
||||||
CherryPicking,
|
|
||||||
} from './gitGraphTypes.js';
|
|
||||||
|
|
||||||
const populate = (ast: any) => {
|
const populate = (ast: any) => {
|
||||||
populateCommonDb(ast, db);
|
populateCommonDb(ast, db);
|
||||||
for (const statement of ast.statements) {
|
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 = {
|
export const parser: ParserDefinition = {
|
||||||
parse: async (input: string): Promise<void> => {
|
parse: async (input: string): Promise<void> => {
|
||||||
const ast: GitGraph = await parse('gitGraph', input);
|
const ast: GitGraph = await parse('gitGraph', input);
|
||||||
|
|||||||
@@ -5,11 +5,11 @@ export interface Commit {
|
|||||||
message: string;
|
message: string;
|
||||||
seq: number;
|
seq: number;
|
||||||
type: number;
|
type: number;
|
||||||
tag: string;
|
tags: string[] | undefined;
|
||||||
parents: (string | null)[];
|
parents: (string | null)[];
|
||||||
branch: string;
|
branch: string;
|
||||||
customType?: number;
|
customType?: number;
|
||||||
customId?: string;
|
customId?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface GitGraph {
|
export interface GitGraph {
|
||||||
|
|||||||
Reference in New Issue
Block a user