All files / lib/formulas convert-each-in.js

100% Statements 17/17
100% Branches 11/11
100% Functions 3/3
100% Lines 17/17
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  1x     1x     1x         43x 43x 43x           4x   4x 43x 4x 4x 4x             4x 4x 4x 4x       4x    
/* eslint no-param-reassign: ["error", { "props": false }]*/
import {
  Walker,
} from 'glimmer-engine/dist/node_modules/glimmer-syntax';
import {
  locSpan,
} from '../utils/location';
import {
  offsetNode,
} from '../utils/node';
 
function isEachIn(node) {
  const parts = (node.path && node.path.parts) || [];
  const params = node.params || [];
  return node.type === 'BlockStatement' &&
    parts[0] === 'each' &&
    params[1] && params[1].parts[0] === 'in';
}
 
export default function convertEachIn(ast) {
  const walker = new Walker(ast);
 
  walker.visit(ast, (node) => {
    if (isEachIn(node)) {
      const item = node.params[0];
      const separator = node.params[1];
      const offset = locSpan({
        start: {
          column: separator.loc.end.column + 1,
          line: separator.loc.end.line,
        },
        end: item.loc.start,
      });
      offsetNode(ast, offset, { recursive: true, startingAt: item.loc.start });
      delete node.params[0]; // item
      delete node.params[1]; // in
      node.program.blockParams = [item.parts[0]];
    }
  });
 
  return ast;
}