Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: ts-morph incorrectly transforms nodes which begin with whitespace #1592

Open
wants to merge 2 commits into
base: latest
Choose a base branch
from

Conversation

ofersadgat
Copy link

@ofersadgat ofersadgat commented Dec 10, 2024

Fixes: #1591

Note that using .pos matches what typescript is doing as well:

function visitNodes2(nodes, visitor, test, start, count) {
  if (nodes === void 0) {
    return nodes;
  }
  const length2 = nodes.length;
  if (start === void 0 || start < 0) {
    start = 0;
  }
  if (count === void 0 || count > length2 - start) {
    count = length2 - start;
  }
  let hasTrailingComma;
  let pos = -1;
  let end = -1;
  if (start > 0 || count < length2) {
    hasTrailingComma = nodes.hasTrailingComma && start + count === length2;
  } else {
    pos = nodes.pos;
    end = nodes.end;
    hasTrailingComma = nodes.hasTrailingComma;
  }
  const updated = visitArrayWorker(nodes, visitor, test, start, count); 
  if (updated !== nodes) {
    const updatedArray = factory.createNodeArray(updated, hasTrailingComma);
    setTextRangePosEnd(updatedArray, pos, end);
    return updatedArray;
  }
  return nodes;
}

The line:
const updated = visitArrayWorker(nodes, visitor, test, start, count);
is calling visitArrayWorker which calls innerVisit which first calls the transform function followed by the handleTransformation function. So, on the way out typescript is using .pos as well.

@dsherret
Copy link
Owner

I'm not sure this is correct and it seems to cause some tests to fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ts-morph incorrectly transforms nodes which begin with whitespace
2 participants