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 bugs in the organizeDeclarations rule and remove rewritesEntireFile flag #703

Merged
merged 2 commits into from
Aug 19, 2020

Conversation

calda
Copy link
Collaborator

@calda calda commented Aug 19, 2020

This PR fixes some issues with the parseDeclarations method (added in #701), which was causing bugs in the organizeDeclarations rule.

  1. When parseDeclarations finds a startOfScope token, it skips to the corresponding endOfScope.

    • Comments have a startOfScope("//") token but no corresponding endOfScope token, so this was inadvertently skipping to the end of the scope.
    • This was causing parseDeclarations to accidentally chunk all of the code after a // MARK: comment into the same declaration.
  2. When parseDeclaration finds an endOfScope token, it was always skipping to the corresponding startOfScope.

    • This was meant to support skipping through annotation bodies, but was also inadvertently skipping through function bodies.
    • This was causing parseDeclarations to accidentally include multiple declarations in a single Declaration object.

This PR also updates organizeDeclarations to only mutate the formatter when it actually needs to make changes. This change, combined with the other fixes, means we no longer need the rewritesEntireFile flag!

// The `name` property on individual rules is not populated until the first call into `rulesByName`,
// so we have to make sure to trigger this before checking the names of the given rules.
if rules.contains(where: { $0.name.isEmpty }) {
_ = FormatRules.all
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

All of the organizeDeclaration tests were mysteriously failing when ran individually, but succeeding when ran as a part of the entire unit test suite.

It turns out the rules.first?.name == "organizeDeclarations" check below was failing because the name fields hadn't been populated yet. I think this may be a recent change / regression (I wasn't observing this behavior on my work branch last weekend).

@nicklockwood
Copy link
Owner

Awesome, thanks 👍

@nicklockwood nicklockwood merged commit 1bcf14f into nicklockwood:develop Aug 19, 2020
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.

2 participants