Skip to content

Conversation

@rickhohler
Copy link

Description

This PR implements the CollapseNestedIfStmt refactoring action, which merges nested if statements into a single if statement with a combined condition list.

Transformation:

if a {
  if b {
    print("hello")
  }
}
// becomes:
if a, b {
  print("hello")
}

Detailed Design

  • Adds CollapseNestedIfStmt provider to SwiftRefactor.
  • Validation Logic:
    • Ensures outer if has exactly one statement (the inner if).
    • Ensures neither if has an else block (to avoid ambiguity).
    • Checks for IfExprSyntax inside the block.
  • Transformation Logic:
    • Appends inner conditions to outer conditions.
    • Carefully handles comma insertion and whitespace trimming for the merged condition list.

Fixes

Addresses one part of swiftlang/sourcekit-lsp#2424 (Porting legacy refactoring actions).

Pre-PR Checklist

  • Code builds and passes tests.
  • Ran swift-format.
  • Added unit tests in CollapseNestedIfStmtTest.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