All files / lib/utils location.js

86.36% Statements 19/22
94.44% Branches 17/18
76.92% Functions 10/13
86.36% Lines 19/22
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 1091x     277x 277x 277x       69x                   27x                 605x                               567x                                 1023x 1023x 1023x       1209x 1209x 1209x 1209x 1209x       375x             758x           31x                                        
import { builders } from 'glimmer-engine/dist/node_modules/glimmer-syntax';
 
function posAfter(a, b) {
  const lineDiff = a.line - b.line;
  const colDiff = a.column - b.column;
  return lineDiff === 0 ? colDiff > 0 : lineDiff > 0;
}
 
function addOffsets(a, b) {
  return {
    column: a.column + b.column,
    line: a.line + b.line,
  };
}
 
// Start point diff and end point diff
// Useful to tell how a new location
// varies from old loation
function locDiff(a, b) {
  return builders.loc(
    b.start.line - a.start.line,
    b.start.column - a.start.column,
    b.end.line - a.end.line,
    b.end.column - a.end.column,
  );
}
 
function locAdd(a, b) {
  return builders.loc(
    b.start.line + a.start.line,
    b.start.column + a.start.column,
    b.end.line + a.end.line,
    b.end.column + a.end.column,
  );
}
 
function locOffset(a, b) {
  return {
    column: b.start.column - a.end.column,
    line: b.start.line - a.end.line,
  };
}
 
function locAppend(startOrEnd, offset) {
  return builders.loc(
    startOrEnd.line,
    startOrEnd.column,
    startOrEnd.line + (offset.line || 0),
    startOrEnd.column + (offset.column || 0),
  );
}
 
function locStartsAfter(a, b) {
  return posAfter(a.start, b);
}
 
function locEndsAfter(a, b) {
  return posAfter(a.end, b);
}
 
function posBefore(a, b) {
  const lineDiff = a.line - b.line;
  const colDiff = a.column - b.column;
  return lineDiff === 0 ? colDiff < 0 : lineDiff < 0;
}
 
function locContains(a, b) {
  const startCol = a.start.column;
  const endCol = a.end.column;
  const { column } = b;
  const lineDiff = a.start.line - b.line;
  return lineDiff === 0 && startCol < column && endCol >= column;
}
 
function locsEqual(a, b) {
  return a.start.line === b.start.line &&
         a.start.column === b.start.column &&
         a.end.line === b.end.line &&
         a.end.column === b.end.column;
}
 
function locsOverlap(a, b) {
  return locContains(a, b.start) ||
         locContains(a, b.end) ||
         locsEqual(a, b);
}
 
function locSpan(loc) {
  return {
    line: loc.end.line - loc.start.line,
    column: loc.end.column - loc.start.column,
  };
}
 
export {
  locDiff,
  locAdd,
  locOffset,
  locAppend,
  locStartsAfter,
  locEndsAfter,
  posAfter,
  posBefore,
  locContains,
  locSpan,
  addOffsets,
  locsOverlap,
};