All files / lib/utils whitespace-diff.js

91.67% Statements 22/24
83.33% Branches 10/12
100% Functions 2/2
91.67% Lines 22/24
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 381x 1x     758x 758x     758x 396x   362x 362x 362x 22x   340x 340x   362x       88x 88x     88x 88x 88x 4x   84x 84x   88x    
import repeat from './repeat';
import { locsOverlap } from './location';
 
export default function whitespaceDiff(a, b) {
  const whitespace = [];
  Iif (!a || !b) {
    throw Error('Must provide two locations to whitespaceDiff');
  }
  if (locsOverlap(a, b)) {
    return '';
  }
  const rowDiff = b.start.line - a.end.line;
  whitespace.push(repeat('\n', rowDiff));
  if (rowDiff > 0) {
    whitespace.push(repeat(' ', b.start.column));
  } else {
    const colDiff = b.start.column - a.end.column;
    whitespace.push(repeat(' ', colDiff));
  }
  return whitespace.join('');
}
 
export function locToWhitespace(loc) {
  const whitespace = [];
  Iif (!loc) {
    throw Error('Must provide a location to locToWhitespace');
  }
  const rowDiff = loc.end.line - loc.start.line;
  whitespace.push(repeat('\n', rowDiff));
  if (rowDiff > 0) {
    whitespace.push(repeat(' ', loc.end.column));
  } else {
    const colDiff = loc.end.column - loc.start.column;
    whitespace.push(repeat(' ', colDiff));
  }
  return whitespace.join('');
}