fix: handle leading trivia when replacing a range in a syntax list by kerdofficial · Pull Request #1694 · dsherret/ts-morph · GitHub
Skip to content

fix: handle leading trivia when replacing a range in a syntax list - #1694

Open
kerdofficial wants to merge 1 commit into
dsherret:latestfrom
kerdofficial:fix/613-fix-missing-imports-trivia
Open

kerdofficial wants to merge 1 commit into
dsherret:latestfrom
kerdofficial:fix/613-fix-missing-imports-trivia

Conversation

@kerdofficial

Copy link
Copy Markdown

Fixes #613

After removing the only import with replaceWithText(""), the file starts with whitespace ( console.log(qux);). fixMissingImports() then inserts an import at position 0 and fails with "Manipulation error: Error replacing tree: The children of the old and new trees were expected to have the same count (1:2)".

RangeHandler looks for the node that contains the changed range by comparing getStart(), which skips leading trivia. The statement list's start is after the whitespace, so it was treated as an unchanged sibling instead of the container. It now also matches a SyntaxList by its full start.

Added a test for the case in the issue. It fails without the change and also checks that the existing statement and syntax list nodes are kept. deno task test, deno task code-verification and deno task type-check pass.

This branch has not been deployed

No deployments
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.

fixMissingImports breaks when adding to empty named imports

1 participant