Make conditional_returns_on_newline rule autocorrectable#6489
Make conditional_returns_on_newline rule autocorrectable#6489ldct wants to merge 7 commits intorealm:mainfrom
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.
That's a highly appreciated enhancement. Thanks for the contribution!
Source/SwiftLintBuiltInRules/Rules/Style/ConditionalReturnsOnNewlineRule.swift
Show resolved
Hide resolved
|
|
||
| func testGuardCorrection() { | ||
| // Test guard correction with default configuration | ||
| let corrections = [ |
There was a problem hiding this comment.
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>
Co-authored-by: Shelley <shelley@exe.dev>
| return super.visit(fixedNode) | ||
| } | ||
|
|
||
| private func isReturn(_ returnStmt: ReturnStmtSyntax?, onTheSameLineAs token: TokenSyntax) -> Bool { |
There was a problem hiding this comment.
Please don't duplicate this method.
| Example(""" | ||
| ↓guard condition else { XCTFail(); return } | ||
| """): Example(""" | ||
| guard condition else { XCTFail(); |
There was a problem hiding this comment.
Not sure this is the expected fix here ... and not sure if there is any reasonable fix for that. We should probably restrict fixing to single return statements. That would also simplify the implementation a little.
Alternatively, we could move both statements onto the next line but keeping them together (there is another rule to violate on multiple statements on one line).
| private func fixCodeBlock(_ block: CodeBlockSyntax, baseToken: TokenSyntax) -> CodeBlockSyntax { | ||
| // Get the indentation of the base token (e.g., `if` or `guard`) | ||
| let baseIndentation = baseToken.leadingTrivia.indentation(isOnNewline: true) ?? Trivia() | ||
| let innerIndentation = Trivia(pieces: baseIndentation.pieces + [.spaces(4)]) |
There was a problem hiding this comment.
You can add Trivia objects directly.
| guard condition else { XCTFail(); | ||
| return | ||
| } | ||
| """), |
There was a problem hiding this comment.
Make sure to keep leading an trailing comments as well, e.g.
guard true else { /* leading comment */ return /* trailing comment */ }
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