All files / lib/utils node.js

96.4% Statements 134/139
93.55% Branches 87/93
100% Functions 23/23
95.97% Lines 119/124
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243      1x 1x 1x             443x 443x 180x 208x 208x 208x       43x 31x 31x     58x 58x 58x     1x 1x 1x 1x 1x 1x 1x 3x 3x 2x   1x     1x     58x 58x 57x 57x 57x 57x 57x 61x 61x 60x   1x     57x       859x     859x 654x   11055x 3685x 350x 3335x 626x 626x         859x     738x 738x     738x 738x   738x 117x     738x             738x   507x 133x       507x 165x 165x     507x 207x     19x     188x         507x 135x 135x       738x 133x     605x 605x 605x 605x       1026x     1026x 736x   1026x 4453x 354x 4099x 959x     1026x       116x 1x   115x     115x 113x   2x       1564x 391x 27x 27x             27x 27x 27x 27x 27x 27x 27x 27x       27x       50x 50x 50x 50x 50x 1242x                   207x 207x 50x   207x 50x   207x 50x   207x 58x       50x                          
/* eslint-disable max-len */
/* eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsFor": ["parent", "offset", "node"] }] */
/* eslint-enable max-len */
import _ from 'underscore';
import { builders } from 'glimmer-engine/dist/node_modules/glimmer-syntax';
import {
  posAfter,
  posBefore,
  locSpan,
  locAdd,
} from './location';
 
function sortNodes(...nodes) {
  const allNodes = nodes.reduce((acc, n) => acc.concat(n), []);
  return _.uniq(allNodes).sort((a, b) => {
    const lineDiff = a.loc.start.line - b.loc.start.line;
    const colDiff = a.loc.start.column - b.loc.start.column;
    return (lineDiff === 0 && colDiff > 0) || lineDiff > 0;
  });
}
 
function nodeIndex(node, ...lists) {
  const nodes = sortNodes(node, ...lists);
  return nodes.indexOf(node);
}
 
function sameLine(node, ...nodes) {
  const allNodes = nodes.reduce((acc, n) => acc.concat(n), []);
  return sortNodes([node], allNodes);
}
 
function nodesBefore(before, ...nodes) {
  const allNodes = nodes.reduce((acc, n) => acc.concat(n), []);
  const sorted = sameLine(before, allNodes);
  const index = sorted.indexOf(before);
  let start = index;
  const currentLine = before.loc.start.line;
  for (let i = start; i >= 0; i -= 1) {
    const endLine = sorted[i].loc.end.line;
    if (endLine === currentLine) {
      start = i;
    } else {
      break;
    }
  }
  return sorted.slice(start, index);
}
 
function nodesAfter(after, ...nodes) {
  const allNodes = nodes.reduce((acc, n) => acc.concat(n), []);
  const sorted = sameLine(after, allNodes);
  const index = sorted.indexOf(after);
  let stop = index;
  const currentLine = after.loc.end.line;
  for (let i = stop; i < sorted.length; i += 1) {
    const endLine = sorted[i].loc.end.line;
    if (endLine === currentLine) {
      stop = i;
    } else {
      break;
    }
  }
  return sorted.slice(index + 1, stop + 1);
}
 
function enumerate(node, func, options) {
  Iif (!node) {
    return node;
  }
  if (node.type) {
    func(node, options || {});
  }
  Object.entries(node).forEach(([key, val]) => {
    if (val && val.type) {
      enumerate(val, func, { parent: node, property: key, isArray: false });
    } else if (Array.isArray(val)) {
      const nodes = _.clone(val);
      nodes.forEach((n, index) => enumerate(n, func, {
        parent: node, property: key, isArray: true, index,
      }));
    }
  });
  return node;
}
 
function performOffset(node, offset, options = {}) {
  Iif (!node || !node.loc) {
    throw new Error('performOffset must be given a node with a loc');
  }
  let { loc } = node;
  const { startingAt } = options;
 
  if (offset.line && !options.both) {
    offset.column = 0;
  }
 
  let diff = builders.loc(
    offset.line,
    offset.column,
    offset.line,
    offset.column,
  );
 
  if (startingAt) {
    // entire node is before start
    if (posBefore(loc.end, startingAt)) {
      diff = null;
    }
 
    // on line below start
    if (loc.start.line > startingAt.line) {
      diff.start.column = 0;
      diff.end.column = 0;
    }
 
    if (loc.end.line > startingAt.line) {
      if (loc.start.line === startingAt.line) {
        // starts on same line after but ends on lower line
        // shift start but not end
        diff.end.column = 0;
      } else {
        // node ends below line than starting line
        diff.end.column = 0;
      }
    }
 
    // node starts before start but ends after
    if (posBefore(loc.start, startingAt) && posAfter(loc.end, startingAt)) {
      diff.start.line = 0;
      diff.start.column = 0;
    }
  }
 
  if (!diff) {
    return node;
  }
 
  loc = builders.loc(loc);
  loc = locAdd(loc, diff);
  node.loc = loc;
  return node;
}
 
function offsetNodeRecursive(node, offset, options) {
  Iif (!node) {
    return node;
  }
  if (node.loc) {
    performOffset(node, offset, options);
  }
  Object.values(node).forEach((val) => {
    if (val && val.type) {
      offsetNodeRecursive(val, offset, options);
    } else if (Array.isArray(val)) {
      val.forEach(v => offsetNodeRecursive(v, offset, options));
    }
  });
  return node;
}
 
function offsetNode(node, offset, options) {
  if (Array.isArray(node)) {
    return node.map(n => offsetNode(n, offset, options));
  }
  Iif (!node || !node.loc) {
    throw new Error('offsetNode must be given a node with a loc');
  }
  if (options && options.recursive) {
    return offsetNodeRecursive(node, offset, options);
  }
  return performOffset(node, offset, options);
}
 
function removeNode(target, ast) {
  enumerate(ast, (node, { parent, property, isArray, index }) => {
    if (target === node) {
      Eif (isArray) {
        parent[property].splice(index, 1);
      } else {
        delete parent[property];
      }
    }
  });
 
  const startingAt = target.loc.start;
  const span = locSpan(target.loc);
  span.line = -span.line;
  span.column = -span.column;
  const after = nodesAfter(target, ast);
  const before = nodesAfter(target, ast);
  offsetNode(ast, span, { recursive: true, startingAt });
  Eif (after.length === 0 && before.length === 0) {
    // TODO: delete empty line
  }
 
  return ast;
}
 
function sizeOfNodes(nodes) {
  let minLine = null;
  let maxLine = null;
  let minCol = null;
  let maxCol = null;
  enumerate({ nodes }, (n) => {
    if (n.loc) {
      const {
        start: {
          line: startLine,
          column: startCol,
        },
        end: {
          line: endLine,
          column: endCol,
        },
      } = n.loc;
      if (minLine === null || minLine > startLine) {
        minLine = startLine;
      }
      if (maxLine === null || maxLine < endLine) {
        maxLine = endLine;
      }
      if (minCol === null || minCol > startCol) {
        minCol = startCol;
      }
      if (maxCol === null || maxCol < endCol) {
        maxCol = endCol;
      }
    }
  }, { recursive: true });
  return builders.loc(0, 0, maxLine - minLine, maxCol - minCol).end;
}
 
export {
  sortNodes,
  nodesBefore,
  nodesAfter,
  nodeIndex,
  offsetNode,
  removeNode,
  enumerate,
  sizeOfNodes,
};