core/refactor/array.js
function addToArrayByNode(node, code) {
  // node: the arr expression node
  // code: added as the last element of the array

  const multilines = node.loc.start.line !== node.loc.end.line;
  let insertPos = node.start + 1; // insert after '['

  if (node.elements.length) {
    const ele = _.last(node.elements);
    insertPos = ele.end;
  }

  let replacement;
  if (multilines) {
    const indent = _.repeat(' ', node.loc.end.column - 1);
    replacement = `\n${indent}  ${code}`;
    if (node.elements.length) {
      replacement = `,${replacement}`;
    } else {
      replacement = `${replacement},`;
    }
  } else {
    replacement = code;
    if (node.elements.length > 0) {
      replacement = `, ${code}`;
    }
  }
  return [
    {
      start: insertPos,
      end: insertPos,
      replacement,
    },
  ];
}
function addToArrayByNode(node, code) {
  // node: the arr expression node
  // code: added as the last element of the array

  const multilines = node.loc.start.line !== node.loc.end.line;
  let insertPos = node.start + 1; // insert after '['

  if (node.elements.length) {
    const ele = _.last(node.elements);
    insertPos = ele.end;
  }

  let replacement;
  if (multilines) {
    const indent = _.repeat(' ', node.loc.end.column - 1);
    replacement = `\n${indent}  ${code}`;
    if (node.elements.length) {
      replacement = `,${replacement}`;
    } else {
      replacement = `${replacement},`;
    }
  } else {
    replacement = code;
    if (node.elements.length > 0) {
      replacement = `, ${code}`;
    }
  }
  return [
    {
      start: insertPos,
      end: insertPos,
      replacement,
    },
  ];
}
function addToArrayByNode ( node , code ) { // node: the arr expression node // code: added as the last element of the array const multilines = node . loc . start . line !== node . loc . end . line ; let insertPos = node . start + 1 ; // insert after '[' if ( node . elements . length ) { const ele = _ . last ( node . elements ) ; insertPos = ele . end ; } let replacement ; if ( multilines ) { const indent = _ . repeat ( ' ' , node . loc . end . column - 1 ) ; replacement = ` \n ${ indent } ${ code } ` ; if ( node . elements . length ) { replacement = ` ${ replacement } ` ; } else { replacement = ` ${ replacement } ` ; } } else { replacement = code ; if ( node . elements . length > 0 ) { replacement = ` ${ code } ` ; } } return [ { start : insertPos , end : insertPos , replacement , } , ] ; }
Add an element to an array definition.
@param {object} node - The ast node of the array definition.
@param {string} code - The code to append to the array.
@alias module:refactor.addToArrayByNode
@
Add an element to an array definition .