-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Make conditional_returns_on_newline rule autocorrectable #6489
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
base: main
Are you sure you want to change the base?
Conversation
Added a Rewriter class to automatically fix violations where return statements are on the same line as conditional keywords (if/guard). The autocorrection: - Adds a newline before the return statement - Preserves proper indentation (4 spaces from the conditional's indentation) - Handles both if/else and guard statements - Preserves preceding statements on the same line (e.g., XCTFail()) - Respects the if_only configuration option Added test for guard statement corrections. Co-authored-by: Shelley <shelley@exe.dev>
Here's an example of your CHANGELOG entry: * Make conditional_returns_on_newline rule autocorrectable.
[ldct](https://github.com/ldct)
[#issue_number](https://github.com/realm/SwiftLint/issues/issue_number)note: There are two invisible spaces after the entry's text. Generated by 🚫 Danger |
Co-authored-by: Shelley <shelley@exe.dev>
…orrectable Co-authored-by: Shelley <shelley@exe.dev>
Co-authored-by: Shelley <shelley@exe.dev>
SimplyDanny
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a highly appreciated enhancement. Thanks for the contribution!
| Example("↓if true { break } else { return }"): | ||
| Example("if true { break } else {\n return\n}"), | ||
| Example("↓if true { return \"YES\" } else { return \"NO\" }"): | ||
| Example("if true {\n return \"YES\"\n} else { return \"NO\" }"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add another example where the if is indented, e.g.
func f() {
if true { return }
}|
|
||
| func testGuardCorrection() { | ||
| // Test guard correction with default configuration | ||
| let corrections = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be part of the example list in the rule itself. Make sure, that violating guard examples don't get fixed when the if_only option is set.
Co-authored-by: Shelley <shelley@exe.dev>
- Added guard correction examples to the rule's corrections array - Updated if_only test to override corrections (excluding guard) - Removed separate testGuardCorrection since it's now tested via RuleDescription Co-authored-by: Shelley <shelley@exe.dev>
Summary
This PR adds autocorrection support to the
conditional_returns_on_newlinerule.Changes
Rewriterclass to automatically fix violations where return statements are on the same line as conditional keywords (if/guard)if/elseandguardstatementsXCTFail();)if_onlyconfiguration optionExample Corrections