core/refactor/array.js
function removeFromArrayByNode(node, eleNode) {
  const elements = node.elements;

  if (!elements.includes(eleNode)) {
    logger.warn('Failed to find element when trying to remove element from array.');
    return [];
  }

  if (!node._filePath) {
    throw new Error('No _filePath property found on node when removing element from array');
  }

  const content = vio.getContent(node._filePath);

  let startPos = nearestCharBefore(',', content, eleNode.start);
  let isFirstElement = false;
  if (startPos < 0) {
    // it's the first element
    isFirstElement = true;
    startPos = node.start + 1; // start from the char just after the '['
  }

  let endPos = eleNode.end;

  if (elements.length === 1 || isFirstElement) {
    // if the element is the only element, try to remove the trailing comma if exists
    const nextComma = nearestCharAfter(',', content, endPos - 1);
    if (nextComma >= 0) endPos = nextComma + 1;
  }

  return [
    {
      start: startPos,
      end: endPos,
      replacement: '',
    },
  ];
}
function removeFromArrayByNode(node, eleNode) {
  const elements = node.elements;

  if (!elements.includes(eleNode)) {
    logger.warn('Failed to find element when trying to remove element from array.');
    return [];
  }

  if (!node._filePath) {
    throw new Error('No _filePath property found on node when removing element from array');
  }

  const content = vio.getContent(node._filePath);

  let startPos = nearestCharBefore(',', content, eleNode.start);
  let isFirstElement = false;
  if (startPos < 0) {
    // it's the first element
    isFirstElement = true;
    startPos = node.start + 1; // start from the char just after the '['
  }

  let endPos = eleNode.end;

  if (elements.length === 1 || isFirstElement) {
    // if the element is the only element, try to remove the trailing comma if exists
    const nextComma = nearestCharAfter(',', content, endPos - 1);
    if (nextComma >= 0) endPos = nextComma + 1;
  }

  return [
    {
      start: startPos,
      end: endPos,
      replacement: '',
    },
  ];
}
function removeFromArrayByNode ( node , eleNode ) { const elements = node . elements ; if ( ! elements . includes ( eleNode ) ) { logger . warn ( 'Failed to find element when trying to remove element from array.' ) ; return [ ] ; } if ( ! node . _filePath ) { throw new Error ( 'No _filePath property found on node when removing element from array' ) ; } const content = vio . getContent ( node . _filePath ) ; let startPos = nearestCharBefore ( ',' , content , eleNode . start ) ; let isFirstElement = false ; if ( startPos < 0 ) { // it's the first element isFirstElement = true ; startPos = node . start + 1 ; // start from the char just after the '[' } let endPos = eleNode . end ; if ( elements . length === 1 || isFirstElement ) { // if the element is the only element, try to remove the trailing comma if exists const nextComma = nearestCharAfter ( ',' , content , endPos - 1 ) ; if ( nextComma >= 0 ) endPos = nextComma + 1 ; } return [ { start : startPos , end : endPos , replacement : '' , } , ] ; }
Remove an element from an array definition.
@param {object} node - The ast node of the array definition.
@param {object} eleNode - The ast node to be removed.
@alias module:refactor.removeFromArrayByNode
@
Remove an element from an array definition .