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 RemoveImportsVisitor crash when ImportAlias is inserted without comma #397

Merged
merged 3 commits into from
Oct 1, 2020

Conversation

zsol
Copy link
Member

@zsol zsol commented Sep 29, 2020

Summary

The comment preserving logic introduced in #392 assumed that in an ImportFrom node, ImportAliases have a comma property (except for the last one). That's only true if the ImportFrom node is parsed from actual source, but isn't necessarily true if it's constructed manually.

This PR adds a defensive check for the existence of a proper Comma node, and in case it doesn't exist (because it's a MaybeSentinel), the logic now just moves over the Comma node from the alias that's about to be removed.

Test Plan

Added a test case that adds a new ImportAlias node manually (with comma=MaybeSentinel.DEFAULT), then removes the next alias.

@zsol zsol requested a review from jimmylai September 29, 2020 10:48
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 29, 2020
Copy link
Contributor

@jimmylai jimmylai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the comment, LGTM.

Comment on lines 378 to 383
prev = prev.with_deep_changes(
whitespace_after=_merge_whitespace_after(
prev.comma.whitespace_after,
comma.whitespace_after,
)
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codecov-commenter
Copy link

Codecov Report

Merging #397 into master will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #397      +/-   ##
==========================================
+ Coverage   94.08%   94.09%   +0.01%     
==========================================
  Files         231      231              
  Lines       22348    22360      +12     
==========================================
+ Hits        21026    21040      +14     
+ Misses       1322     1320       -2     
Impacted Files Coverage Δ
libcst/codemod/visitors/_remove_imports.py 93.52% <100.00%> (+1.31%) ⬆️
...bcst/codemod/visitors/tests/test_remove_imports.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update efe0fdb...a2a1947. Read the comment docs.

@zsol zsol merged commit 1d22a29 into Instagram:master Oct 1, 2020
@zsol zsol deleted the invalid-aliases branch October 1, 2020 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants