-
Notifications
You must be signed in to change notification settings - Fork 496
Feat(SwiftRefactor): Implement InvertIfCondition refactoring #3263
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?
Changes from all commits
624d0bf
ccc7b4d
08c1c8b
58c1796
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,82 @@ | ||||||||
| //===----------------------------------------------------------------------===// | ||||||||
| // | ||||||||
| // This source file is part of the Swift.org open source project | ||||||||
| // | ||||||||
| // Copyright (c) 2014 - 2026 Apple Inc. and the Swift project authors | ||||||||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||||||||
| // | ||||||||
| // See https://swift.org/LICENSE.txt for license information | ||||||||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||||||||
| // | ||||||||
| //===----------------------------------------------------------------------===// | ||||||||
|
|
||||||||
| #if compiler(>=6) | ||||||||
| public import SwiftSyntax | ||||||||
| #else | ||||||||
| import SwiftSyntax | ||||||||
| #endif | ||||||||
|
|
||||||||
| /// Inverts a negated `if` condition and swaps the branches. | ||||||||
| /// | ||||||||
| /// ## Before | ||||||||
| /// | ||||||||
| /// ```swift | ||||||||
| /// if !x { | ||||||||
| /// foo() | ||||||||
| /// } else { | ||||||||
| /// bar() | ||||||||
| /// } | ||||||||
| /// ``` | ||||||||
| /// | ||||||||
| /// ## After | ||||||||
| /// | ||||||||
| /// ```swift | ||||||||
| /// if x { | ||||||||
| /// bar() | ||||||||
| /// } else { | ||||||||
| /// foo() | ||||||||
| /// } | ||||||||
| /// ``` | ||||||||
| public struct InvertIfCondition: SyntaxRefactoringProvider { | ||||||||
| public static func refactor(syntax ifExpr: IfExprSyntax, in context: Void) -> IfExprSyntax { | ||||||||
| guard let elseBody = ifExpr.elseBody, case .codeBlock(let elseBlock) = elseBody else { | ||||||||
| return ifExpr | ||||||||
| } | ||||||||
|
|
||||||||
| guard ifExpr.conditions.count == 1, let condition = ifExpr.conditions.first else { | ||||||||
| return ifExpr | ||||||||
| } | ||||||||
|
|
||||||||
| guard case .expression(let expr) = condition.condition else { | ||||||||
| return ifExpr | ||||||||
| } | ||||||||
|
|
||||||||
| guard let prefixOpExpr = expr.as(PrefixOperatorExprSyntax.self), prefixOpExpr.operator.text == "!" else { | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also support adding the |
||||||||
| return ifExpr | ||||||||
| } | ||||||||
|
|
||||||||
| let innerExpr = prefixOpExpr.expression.with(\.leadingTrivia, prefixOpExpr.leadingTrivia.merging(triviaOf: prefixOpExpr.operator)) | ||||||||
|
|
||||||||
| let newCondition = condition.with(\.condition, .expression(innerExpr)) | ||||||||
| let newConditions = ifExpr.conditions.with(\.[ifExpr.conditions.startIndex], newCondition) | ||||||||
|
Comment on lines
+60
to
+61
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
|
||||||||
| let oldBody = ifExpr.body | ||||||||
| let oldElseBlock = elseBlock | ||||||||
|
|
||||||||
| let newBody = | ||||||||
| oldElseBlock | ||||||||
| .with(\.leadingTrivia, oldBody.leadingTrivia) | ||||||||
| .with(\.trailingTrivia, oldBody.trailingTrivia) | ||||||||
|
|
||||||||
| let newElseBody = | ||||||||
| oldBody | ||||||||
| .with(\.leadingTrivia, oldElseBlock.leadingTrivia) | ||||||||
| .with(\.trailingTrivia, oldElseBlock.trailingTrivia) | ||||||||
|
|
||||||||
| return | ||||||||
| ifExpr | ||||||||
| .with(\.conditions, newConditions) | ||||||||
| .with(\.body, newBody) | ||||||||
| .with(\.elseBody, .codeBlock(newElseBody)) | ||||||||
| } | ||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2014 - 2026 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import SwiftParser | ||
| import SwiftRefactor | ||
| import SwiftSyntax | ||
| import SwiftSyntaxBuilder | ||
| import XCTest | ||
| import _SwiftSyntaxTestSupport | ||
|
|
||
| final class InvertIfConditionTest: XCTestCase { | ||
| func testInvertIfCondition() throws { | ||
| let tests = [ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment about writing an
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment still applies |
||
| ( | ||
| """ | ||
| if !x { | ||
| foo() | ||
| } else { | ||
| bar() | ||
| } | ||
| """, | ||
| """ | ||
| if x { | ||
| bar() | ||
| } else { | ||
| foo() | ||
| } | ||
| """ | ||
| ), | ||
| ( | ||
| """ | ||
| if !(x == y) { | ||
| return | ||
| } else { | ||
| continue | ||
| } | ||
| """, | ||
| """ | ||
| if (x == y) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we strip the parentheses here since they are no longer necessary? |
||
| continue | ||
| } else { | ||
| return | ||
| } | ||
| """ | ||
| ), | ||
| // Trivia preservation | ||
| ( | ||
| """ | ||
| if /* comment */ !x { | ||
| a | ||
| } else { | ||
| b | ||
| } | ||
| """, | ||
| """ | ||
| if /* comment */ x { | ||
| b | ||
| } else { | ||
| a | ||
| } | ||
| """ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also add a test that has comments in the bodies? |
||
| ), | ||
| ] | ||
|
|
||
| for (input, expected) in tests { | ||
| try assertInvertIfCondition(input, expected: expected) | ||
| } | ||
| } | ||
|
|
||
| func testInvertIfConditionFails() throws { | ||
| let tests = [ | ||
| // Not negated | ||
| """ | ||
| if x { | ||
| a | ||
| } else { | ||
| b | ||
| } | ||
| """, | ||
| // No else | ||
| """ | ||
| if !x { | ||
| a | ||
| } | ||
| """, | ||
| // Else if (not a CodeBlock) | ||
| """ | ||
| if !x { | ||
| a | ||
| } else if y { | ||
| b | ||
| } | ||
| """, | ||
| // Multiple conditions | ||
| """ | ||
| if !x, !y { | ||
| a | ||
| } else { | ||
| b | ||
| } | ||
| """, | ||
| // Binding | ||
| """ | ||
| if let x = y { | ||
| a | ||
| } else { | ||
| b | ||
| } | ||
| """, | ||
| ] | ||
|
|
||
| for input in tests { | ||
| try assertInvertIfCondition(input, expected: input) | ||
| } | ||
| } | ||
|
|
||
| private func assertInvertIfCondition( | ||
| _ input: String, | ||
| expected: String, | ||
| file: StaticString = #filePath, | ||
| line: UInt = #line | ||
| ) throws { | ||
| let inputSyntax = try XCTUnwrap( | ||
| ExprSyntax.parse(from: input).as(IfExprSyntax.self), | ||
| "Failed validity check: \(input)", | ||
| file: file, | ||
| line: line | ||
| ) | ||
| let expectedSyntax = try XCTUnwrap( | ||
| ExprSyntax.parse(from: expected), | ||
| "Failed validity check: \(expected)", | ||
| file: file, | ||
| line: line | ||
| ) | ||
|
|
||
| try assertRefactor( | ||
| inputSyntax, | ||
| context: (), | ||
| provider: InvertIfCondition.self, | ||
| expected: expectedSyntax, | ||
| file: file, | ||
| line: line | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // Private helper to avoid redeclaration conflicts | ||
| private extension ExprSyntax { | ||
| static func parse(from source: String) -> ExprSyntax { | ||
| var parser = Parser(source) | ||
| return ExprSyntax.parse(from: &parser) | ||
| } | ||
| } | ||
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.
If there is nothing to refactor we should throw a
RefactoringNotApplicableError. This will prevent the refactoring from showing up in SourceKit-LSP when it doesn’t apply.