nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.77k stars 629 forks source link

Improve `conditionalAssignment` and `redundantClosure` rules by parsing full expressions #1539

Closed calda closed 10 months ago

calda commented 10 months ago

This PR improves the conditionalAssignment and redundantClosure rules by updating them to parse full expressions, rather than using a set of loose heuristics.

This PR adds a parseExpressionRange helper, which parses all of the expression types in the Swift grammar. Then we can simplify blockBodyHasSingleStatement to just to parse the expression in the body to verify that the entire body is a single expression.

Fixes #1538.

codecov[bot] commented 10 months ago

Codecov Report

Merging #1539 (2837d84) into develop (4382436) will increase coverage by 0.01%. The diff coverage is 95.33%.

:exclamation: Current head 2837d84 differs from pull request most recent head cc56e58. Consider uploading reports for the commit cc56e58 to get more accurate results

@@             Coverage Diff             @@
##           develop    #1539      +/-   ##
===========================================
+ Coverage    95.04%   95.05%   +0.01%     
===========================================
  Files           20       20              
  Lines        21478    21593     +115     
===========================================
+ Hits         20414    20526     +112     
- Misses        1064     1067       +3     
Files Coverage Δ
Sources/FormattingHelpers.swift 97.65% <94.11%> (-0.05%) :arrow_down:
Sources/ParsingHelpers.swift 96.65% <95.48%> (-0.08%) :arrow_down:

... and 2 files with indirect coverage changes