Repository: mermaid-js/mermaid
File: packages/mermaid/src/diagrams/git/gitGraphRenderer.js (JavaScript)

Code before:
  for (const row of rows) {
    const tspan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
    tspan.setAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:space', 'preserve');
    tspan.setAttribute('dy', '1em');
    tspan.setAttribute('x', '0');
    tspan.setAttribute('class', 'row');
    tspan.textContent = row.trim();
    svgLabel.appendChild(tspan);
  }
  /**
   * @param svg
   * @param selector
   */
  return svgLabel;
};

/**
 * Searches for the closest parent from the parents list passed as argument.
 * The parents list comes from an individual commit. The closest parent is actually
 * the one farther down the graph, since that means it is closer to its child.
 *
 * @param {string[]} parents
 * @returns {string}
 */
const findClosestParent = (parents) => {
  let closestParent = '';
  let maxPosition = 0;

  parents.forEach((parent) => {
    const parentPosition = dir === 'TB' ? commitPos[parent].y : commitPos[parent].x;
    if (parentPosition >= maxPosition) {
      closestParent = parent;
      maxPosition = parentPosition;
    }
  });

  return closestParent;
};

/**
 * Draws the commits with its symbol and labels. The function has two modes, one which only
 * calculates the positions and one that does the actual drawing. This for a simple way getting the
 * vertical layering correct in the graph.
 *
 * @param {any} svg
 * @param {any} commits
 * @param {any} modifyGraph
 */
const drawCommits = (svg, commits, modifyGraph) => {
  const gitGraphConfig = getConfig().gitGraph;
  const gBullets = svg.append('g').attr('class', 'commit-bullets');

Review comment (suggestion):
Maybe it is better to return undefined when parent is missing

Code after:
  for (const row of rows) {
    const tspan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
    tspan.setAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:space', 'preserve');
    tspan.setAttribute('dy', '1em');
    tspan.setAttribute('x', '0');
    tspan.setAttribute('class', 'row');
    tspan.textContent = row.trim();
    svgLabel.appendChild(tspan);
  }
  /**
   * @param svg
   * @param selector
   */
  return svgLabel;
};

/**
 * Searches for the closest parent from the parents list passed as argument.
 * The parents list comes from an individual commit. The closest parent is actually
 * the one farther down the graph, since that means it is closer to its child.
 *
 * @param {string[]} parents
 * @returns {string | undefined}
 */
const findClosestParent = (parents) => {
  let closestParent = '';
  let maxPosition = 0;

  parents.forEach((parent) => {
    const parentPosition = dir === 'TB' ? commitPos[parent].y : commitPos[parent].x;
    if (parentPosition >= maxPosition) {
      closestParent = parent;
      maxPosition = parentPosition;
    }
  });

  return closestParent || undefined;
};

/**
 * Draws the commits with its symbol and labels. The function has two modes, one which only
 * calculates the positions and one that does the actual drawing. This for a simple way getting the
 * vertical layering correct in the graph.
 *
 * @param {any} svg
 * @param {any} commits
 * @param {any} modifyGraph
 */
const drawCommits = (svg, commits, modifyGraph) => {
  const gitGraphConfig = getConfig().gitGraph;
  const gBullets = svg.append('g').attr('class', 'commit-bullets');