All files / lib/transforms fix-attr-mustache-loc.js

94.44% Statements 17/18
87.5% Branches 7/8
100% Functions 3/3
94.44% Lines 17/18
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 371x             94x   94x 353x 95x 64x 64x 64x 64x 64x       64x 64x       64x 19x 19x   64x         94x    
import {
  Walker,
  builders,
} from 'glimmer-engine/dist/node_modules/glimmer-syntax';
 
/* eslint no-param-reassign: [0, { "ignorePropertyModificationsFor": ["attr"] }] */
export default function fixAttrMustacheLoc(ast) {
  const walker = new Walker(ast);
 
  walker.visit(ast, (node) => {
    if (node.type === 'ElementNode') {
      node.attributes.forEach((attr) => {
        const attrLoc = attr.loc;
        const attrStart = attrLoc.start;
        const attrEnd = attrLoc.end;
        let valueLoc = attr.value.loc;
        const line = attrStart.line;
        //     attr="value"
        //     ^  ^^ +1   ^
        // start  length  end
        const column = attrStart.column + attr.name.length + 1;
        Iif (!valueLoc) {
          valueLoc = builders.loc(
            line, column, attrEnd.line, attrEnd.column,
          );
        } else if (valueLoc.start.line === null || valueLoc.start.column === null) {
          valueLoc.start.line = line;
          valueLoc.start.column = column;
        }
        attr.value.loc = valueLoc;
      });
    }
  });
 
  return ast;
}