Skip to content

Conversation

@rickhohler
Copy link

Description

This PR implements the InvertIfCondition refactoring action, which supports SourceKit-LSP #2408.

Transformation:

if !x {
  foo()
} else {
  bar()
}
// becomes:
if x {
  bar()
} else {
  foo()
}

Detailed Design

  • Adds InvertIfCondition provider to SwiftRefactor.
  • Criteria (Strict Scope):
    • Input must be an IfExprSyntax.
    • Must have an else block (CodeBlock).
    • Must have exactly one condition.
    • Condition must be negated (PrefixOperatorExpr with !).
  • Logic:
    • Unwraps the negation (!condition -> condition).
    • Swaps the body and elseBody.
    • Preserves trivia (comments/whitespace) by swapping leading/trailing trivia between the blocks.

Fixes

Addresses logic for swiftlang/sourcekit-lsp#2408.

Pre-PR Checklist

  • Code builds and passes tests.
  • Ran swift-format.
  • Added unit tests in InvertIfConditionTest.swift.

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.

1 participant