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(core): avoid duplicating comments in TestBed teardown migration #43776

Closed

Conversation

crisbeto
Copy link
Member

@crisbeto crisbeto commented Oct 8, 2021

Currently the TestBed teardown migration is set up in a similar way to all other migrations where we take a CallExpression, add a parameter to it, print it, and replace the existing call. The problem is that doing so while preserving the expression of the original CallExpression can cause comments to be duplicated. This can happen quite frequently, because by default the CLI generates comments before initTestModule calls.

To work around it, these changes make the migration more precise by inserting a new parameter or replacing and existing one using string manipulation. This requires a bit more code, but it's more reliable than the following alternatives:

  1. Using getFullStart and getFullWidth to replace the node. This would work with our current setup, but the problem is that getFullStart also includes whitespace and newlines before the leading comment. This can cause us to mess up the user's formatting and figuring out which whitespace to keep and which one to remove is tricky.
  2. Recreating the CallExpression.expression when constructing the new node. This would also work since it'll drop any existing comments, but the problem is that CallExpression.expression can be a wide variety of nodes which we would have to account for. We can't use getMutableClone, because it preserves the comments.

Fixes #43739.

Currently the TestBed teardown migration is set up in a similar way to all other migrations where we take a `CallExpression`, add a parameter to it, print it, and replace the existing call. The problem is that doing so while preserving the `expression` of the original `CallExpression` can cause comments to be duplicated. This can happen quite frequently, because by default the CLI generates comments before `initTestModule` calls.

To work around it, these changes make the migration more precise by inserting a new parameter or replacing and existing one using string manipulation.

This requires a bit more code, but it's more reliable than the following alternatives:
1. Using `getFullStart` and `getFullWidth` to replace the node. This would work with our current setup, but the problem is that `getFullStart` also includes whitespace and newlines before the leading comment. This can cause us to mess up the user's formatting and figuring out which whitespace to keep and which one to remove is tricky.
2. Recreating the `CallExpression.expression` when constructing the new node. This would also work since it'll drop any existing comments, but the problem is that `CallExpression.expression` can be a wide variety of nodes which we would have to account for. We can't use `getMutableClone`, because it preserves the comments.

Fixes angular#43739.
@google-cla google-cla bot added the cla: yes label Oct 8, 2021
@crisbeto crisbeto marked this pull request as ready for review October 8, 2021 09:04
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer area: core Issues related to the framework runtime target: rc This PR is targeted for the next release-candidate labels Oct 8, 2021
@ngbot ngbot bot modified the milestone: Backlog Oct 8, 2021
@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Oct 8, 2021
@crisbeto
Copy link
Member Author

crisbeto commented Oct 8, 2021

Merge assistance: this PR only touches migration code so it doesn't need a presubmit.

@atscott atscott removed the merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note label Oct 11, 2021
@atscott
Copy link
Contributor

atscott commented Oct 11, 2021

This PR was merged into the repository by commit 08caead.

atscott pushed a commit that referenced this pull request Oct 11, 2021
…43776)

Currently the TestBed teardown migration is set up in a similar way to all other migrations where we take a `CallExpression`, add a parameter to it, print it, and replace the existing call. The problem is that doing so while preserving the `expression` of the original `CallExpression` can cause comments to be duplicated. This can happen quite frequently, because by default the CLI generates comments before `initTestModule` calls.

To work around it, these changes make the migration more precise by inserting a new parameter or replacing and existing one using string manipulation.

This requires a bit more code, but it's more reliable than the following alternatives:
1. Using `getFullStart` and `getFullWidth` to replace the node. This would work with our current setup, but the problem is that `getFullStart` also includes whitespace and newlines before the leading comment. This can cause us to mess up the user's formatting and figuring out which whitespace to keep and which one to remove is tricky.
2. Recreating the `CallExpression.expression` when constructing the new node. This would also work since it'll drop any existing comments, but the problem is that `CallExpression.expression` can be a wide variety of nodes which we would have to account for. We can't use `getMutableClone`, because it preserves the comments.

Fixes #43739.

PR Close #43776
@atscott atscott closed this in 08caead Oct 11, 2021
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime cla: yes target: rc This PR is targeted for the next release-candidate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

testbed-teardown migration adds duplicate comment
3 participants