Skip to content

Commit

Permalink
Merge pull request #5518 from aloisklink/5468/refactor/remove-TypeScr…
Browse files Browse the repository at this point in the history
…ipt-non-null-assertion-when-possible
  • Loading branch information
Yash-Singh1 authored May 15, 2024
2 parents dd92aec + 730fa89 commit 00eaebe
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 104 deletions.
12 changes: 6 additions & 6 deletions docs/config/setup/modules/mermaidAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ mermaid.initialize(config);

#### Defined in

[mermaidAPI.ts:635](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L635)
[mermaidAPI.ts:634](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L634)

## Functions

Expand Down Expand Up @@ -129,7 +129,7 @@ Return the last node appended

#### Defined in

[mermaidAPI.ts:277](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L277)
[mermaidAPI.ts:276](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L276)

---

Expand All @@ -155,7 +155,7 @@ the cleaned up svgCode

#### Defined in

[mermaidAPI.ts:223](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L223)
[mermaidAPI.ts:222](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L222)

---

Expand Down Expand Up @@ -203,7 +203,7 @@ the string with all the user styles

#### Defined in

[mermaidAPI.ts:200](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L200)
[mermaidAPI.ts:199](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L199)

---

Expand Down Expand Up @@ -256,7 +256,7 @@ Put the svgCode into an iFrame. Return the iFrame code

#### Defined in

[mermaidAPI.ts:254](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L254)
[mermaidAPI.ts:253](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L253)

---

Expand All @@ -281,4 +281,4 @@ Remove any existing elements from the given document

#### Defined in

[mermaidAPI.ts:327](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L327)
[mermaidAPI.ts:326](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L326)
36 changes: 17 additions & 19 deletions packages/mermaid/src/diagrams/block/blockDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ let classes: Map<string, ClassDef> = new Map();
*/
export const addStyleClass = function (id: string, styleAttributes = '') {
// create a new style class object with this id
if (!classes.has(id)) {
classes.set(id, { id: id, styles: [], textStyles: [] }); // This is a classDef
let foundClass = classes.get(id);
if (!foundClass) {
foundClass = { id: id, styles: [], textStyles: [] };
classes.set(id, foundClass); // This is a classDef
}
const foundClass = classes.get(id)!;
if (styleAttributes !== undefined && styleAttributes !== null) {
styleAttributes.split(STYLECLASS_SEP).forEach((attrib) => {
// remove any trailing ;
Expand Down Expand Up @@ -73,13 +74,13 @@ export const setCssClass = function (itemIds: string, cssClassName: string) {
let foundBlock = blockDatabase.get(id);
if (foundBlock === undefined) {
const trimmedId = id.trim();
blockDatabase.set(trimmedId, { id: trimmedId, type: 'na', children: [] } as Block);
foundBlock = blockDatabase.get(trimmedId);
foundBlock = { id: trimmedId, type: 'na', children: [] } as Block;
blockDatabase.set(trimmedId, foundBlock);
}
if (!foundBlock!.classes) {
foundBlock!.classes = [];
if (!foundBlock.classes) {
foundBlock.classes = [];
}
foundBlock!.classes.push(cssClassName);
foundBlock.classes.push(cssClassName);
});
};

Expand All @@ -104,12 +105,9 @@ const populateBlockDatabase = (_blockList: Block[] | Block[][], parent: Block):
if (block.type === 'column-setting') {
parent.columns = block.columns || -1;
} else if (block.type === 'edge') {
if (edgeCount.has(block.id)) {
edgeCount.set(block.id, edgeCount.get(block.id)! + 1);
} else {
edgeCount.set(block.id, 1);
}
block.id = edgeCount.get(block.id)! + '-' + block.id;
const count = (edgeCount.get(block.id) ?? 0) + 1;
edgeCount.set(block.id, count);
block.id = count + '-' + block.id;
edgeList.push(block);
} else {
if (!block.label) {
Expand All @@ -120,17 +118,17 @@ const populateBlockDatabase = (_blockList: Block[] | Block[][], parent: Block):
block.label = block.id;
}
}
const isCreatingBlock = !blockDatabase.has(block.id);
const existingBlock = blockDatabase.get(block.id);

if (isCreatingBlock) {
if (existingBlock === undefined) {
blockDatabase.set(block.id, block);
} else {
// Add newer relevant data to aggregated node
if (block.type !== 'na') {
blockDatabase.get(block.id)!.type = block.type;
existingBlock.type = block.type;
}
if (block.label !== block.id) {
blockDatabase.get(block.id)!.label = block.label;
existingBlock.label = block.label;
}
}

Expand All @@ -146,7 +144,7 @@ const populateBlockDatabase = (_blockList: Block[] | Block[][], parent: Block):
blockDatabase.set(newBlock.id, newBlock);
children.push(newBlock);
}
} else if (isCreatingBlock) {
} else if (existingBlock === undefined) {
children.push(block);
}
}
Expand Down
9 changes: 5 additions & 4 deletions packages/mermaid/src/diagrams/class/classDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,9 @@ export const setCssClass = function (ids: string, className: string) {
if (_id[0].match(/\d/)) {
id = MERMAID_DOM_ID_PREFIX + id;
}
if (classes.has(id)) {
classes.get(id)!.cssClasses.push(className);
const classNode = classes.get(id);
if (classNode) {
classNode.cssClasses.push(className);
}
});
};
Expand Down Expand Up @@ -268,8 +269,8 @@ export const setLink = function (ids: string, linkStr: string, target: string) {
if (_id[0].match(/\d/)) {
id = MERMAID_DOM_ID_PREFIX + id;
}
if (classes.has(id)) {
const theClass = classes.get(id)!;
const theClass = classes.get(id);
if (theClass) {
theClass.link = utils.formatUrl(linkStr, config);
if (config.securityLevel === 'sandbox') {
theClass.linkTarget = '_top';
Expand Down
18 changes: 6 additions & 12 deletions packages/mermaid/src/diagrams/class/classRenderer-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@ export const addNamespaces = function (
_id: string,
diagObj: any
) {
const keys = [...namespaces.keys()];
log.info('keys:', keys);
log.info('keys:', [...namespaces.keys()]);
log.info(namespaces);

// Iterate through each item in the vertex object (containing all the vertices found) in the graph definition
keys.forEach(function (id) {
const vertex = namespaces.get(id)!;

namespaces.forEach(function (vertex) {
// parent node must be one of [rect, roundedWithTitle, noteGroup, divider]
const shape = 'rect';

Expand Down Expand Up @@ -89,16 +86,13 @@ export const addClasses = function (
diagObj: any,
parent?: string
) {
const keys = [...classes.keys()];
log.info('keys:', keys);
log.info('keys:', [...classes.keys()]);
log.info(classes);

// Iterate through each item in the vertex object (containing all the vertices found) in the graph definition
keys
.filter((id) => classes.get(id)!.parent == parent)
.forEach(function (id) {
const vertex = classes.get(id)!;

[...classes.values()]
.filter((vertex) => vertex.parent === parent)
.forEach(function (vertex) {
/**
* Variable for storing the classes for the vertex
*/
Expand Down
48 changes: 25 additions & 23 deletions packages/mermaid/src/diagrams/flowchart/flowDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ const sanitizeText = (txt: string) => common.sanitizeText(txt, config);
* @param id - id of the node
*/
export const lookUpDomId = function (id: string) {
const vertexKeys = vertices.keys();
for (const vertexKey of vertexKeys) {
if (vertices.get(vertexKey)!.id === id) {
return vertices.get(vertexKey)!.domId;
for (const vertex of vertices.values()) {
if (vertex.id === id) {
return vertex.domId;
}
}
return id;
Expand All @@ -67,17 +66,18 @@ export const addVertex = function (
}
let txt;

if (!vertices.has(id)) {
vertices.set(id, {
let vertex = vertices.get(id);
if (vertex === undefined) {
vertex = {
id,
labelType: 'text',
domId: MERMAID_DOM_ID_PREFIX + id + '-' + vertexCounter,
styles: [],
classes: [],
});
};
vertices.set(id, vertex);
}
vertexCounter++;
const vertex = vertices.get(id)!;

if (textObj !== undefined) {
config = getConfig();
Expand Down Expand Up @@ -210,17 +210,19 @@ export const updateLink = function (positions: ('default' | number)[], style: st

export const addClass = function (ids: string, style: string[]) {
ids.split(',').forEach(function (id) {
if (!classes.has(id)) {
classes.set(id, { id, styles: [], textStyles: [] });
let classNode = classes.get(id);
if (classNode === undefined) {
classNode = { id, styles: [], textStyles: [] };
classes.set(id, classNode);
}

if (style !== undefined && style !== null) {
style.forEach(function (s) {
if (s.match('color')) {
const newStyle = s.replace('fill', 'bgFill').replace('color', 'fill');
classes.get(id)!.textStyles.push(newStyle);
classNode.textStyles.push(newStyle);
}
classes.get(id)!.styles.push(s);
classNode.styles.push(s);
});
}
});
Expand Down Expand Up @@ -257,11 +259,13 @@ export const setDirection = function (dir: string) {
*/
export const setClass = function (ids: string, className: string) {
for (const id of ids.split(',')) {
if (vertices.has(id)) {
vertices.get(id)!.classes.push(className);
const vertex = vertices.get(id);
if (vertex) {
vertex.classes.push(className);
}
if (subGraphLookup.has(id)) {
subGraphLookup.get(id)!.classes.push(className);
const subGraph = subGraphLookup.get(id);
if (subGraph) {
subGraph.classes.push(className);
}
}
};
Expand Down Expand Up @@ -305,8 +309,9 @@ const setClickFun = function (id: string, functionName: string, functionArgs: st
argList.push(id);
}

if (vertices.has(id)) {
vertices.get(id)!.haveCallback = true;
const vertex = vertices.get(id);
if (vertex) {
vertex.haveCallback = true;
funs.push(function () {
const elem = document.querySelector(`[id="${domId}"]`);
if (elem !== null) {
Expand All @@ -331,7 +336,7 @@ const setClickFun = function (id: string, functionName: string, functionArgs: st
*/
export const setLink = function (ids: string, linkStr: string, target: string) {
ids.split(',').forEach(function (id) {
const vertex = vertices.get(id)!;
const vertex = vertices.get(id);
if (vertex !== undefined) {
vertex.link = utils.formatUrl(linkStr, config);
vertex.linkTarget = target;
Expand All @@ -341,10 +346,7 @@ export const setLink = function (ids: string, linkStr: string, target: string) {
};

export const getTooltip = function (id: string) {
if (tooltips.has(id)) {
return tooltips.get(id)!;
}
return undefined;
return tooltips.get(id);
};

/**
Expand Down
4 changes: 1 addition & 3 deletions packages/mermaid/src/diagrams/git/gitGraphAst.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,7 @@ export const getCommits = function () {
return commits;
};
export const getCommitsArray = function () {
const commitArr = [...commits.keys()].map(function (key) {
return commits.get(key);
});
const commitArr = [...commits.values()];
commitArr.forEach(function (o) {
log.debug(o.id);
});
Expand Down
25 changes: 6 additions & 19 deletions packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,7 @@ describe('when parsing a gitGraph', function () {
expect(parser.yy.getCurrentBranch()).toBe('main');
expect(parser.yy.getDirection()).toBe('LR');
expect(parser.yy.getBranches().size).toBe(2);
const commit1 = commits.keys().next().value;
const commit2 = [...commits.keys()][1];
const commit3 = [...commits.keys()][2];
const [commit1, commit2, commit3] = commits.keys();
expect(commits.get(commit1).branch).toBe('main');
expect(commits.get(commit2).branch).toBe('branch');
expect(commits.get(commit3).branch).toBe('main');
Expand Down Expand Up @@ -477,8 +475,7 @@ describe('when parsing a gitGraph', function () {
expect(parser.yy.getCurrentBranch()).toBe('testBranch');
expect(parser.yy.getDirection()).toBe('LR');
expect(parser.yy.getBranches().size).toBe(2);
const commit1 = commits.keys().next().value;
const commit2 = [...commits.keys()][1];
const [commit1, commit2] = commits.keys();
expect(commits.get(commit1).branch).toBe('main');
expect(commits.get(commit1).parents).toStrictEqual([]);
expect(commits.get(commit2).branch).toBe('testBranch');
Expand All @@ -502,10 +499,7 @@ describe('when parsing a gitGraph', function () {
expect(parser.yy.getCurrentBranch()).toBe('main');
expect(parser.yy.getDirection()).toBe('LR');
expect(parser.yy.getBranches().size).toBe(2);
const commit1 = commits.keys().next().value;
const commit2 = [...commits.keys()][1];
const commit3 = [...commits.keys()][2];
const commit4 = [...commits.keys()][3];
const [commit1, commit2, commit3, commit4] = commits.keys();
expect(commits.get(commit1).branch).toBe('main');
expect(commits.get(commit1).parents).toStrictEqual([]);
expect(commits.get(commit2).branch).toBe('testBranch');
Expand Down Expand Up @@ -552,8 +546,7 @@ describe('when parsing a gitGraph', function () {
expect(parser.yy.getCurrentBranch()).toBe('testBranch');
expect(parser.yy.getDirection()).toBe('LR');
expect(parser.yy.getBranches().size).toBe(2);
const commit1 = commits.keys().next().value;
const commit2 = [...commits.keys()][1];
const [commit1, commit2] = commits.keys();
expect(commits.get(commit1).branch).toBe('main');
expect(commits.get(commit1).parents).toStrictEqual([]);
expect(commits.get(commit2).branch).toBe('testBranch');
Expand All @@ -577,10 +570,7 @@ describe('when parsing a gitGraph', function () {
expect(parser.yy.getCurrentBranch()).toBe('main');
expect(parser.yy.getDirection()).toBe('LR');
expect(parser.yy.getBranches().size).toBe(2);
const commit1 = commits.keys().next().value;
const commit2 = [...commits.keys()][1];
const commit3 = [...commits.keys()][2];
const commit4 = [...commits.keys()][3];
const [commit1, commit2, commit3, commit4] = commits.keys();
expect(commits.get(commit1).branch).toBe('main');
expect(commits.get(commit1).parents).toStrictEqual([]);
expect(commits.get(commit2).branch).toBe('testBranch');
Expand Down Expand Up @@ -614,10 +604,7 @@ describe('when parsing a gitGraph', function () {
expect(parser.yy.getCurrentBranch()).toBe('main');
expect(parser.yy.getDirection()).toBe('LR');
expect(parser.yy.getBranches().size).toBe(2);
const commit1 = commits.keys().next().value;
const commit2 = [...commits.keys()][1];
const commit3 = [...commits.keys()][2];

const [commit1, commit2, commit3] = commits.keys();
expect(commits.get(commit1).branch).toBe('main');
expect(commits.get(commit1).parents).toStrictEqual([]);

Expand Down
4 changes: 2 additions & 2 deletions packages/mermaid/src/diagrams/pie/pieRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ export const draw: DrawDefinition = (text, id, _version, diagObj) => {
.attr('class', 'pieCircle');

let sum = 0;
[...sections.keys()].forEach((key: string): void => {
sum += sections.get(key)!;
sections.forEach((section) => {
sum += section;
});
// Now add the percentage.
// Use the centroid method to get the best coordinates.
Expand Down
Loading

0 comments on commit 00eaebe

Please sign in to comment.