Skip to content

swift-format bug formatting nested #if #else #endif nodes in switch statement #779

Closed
@dduan

Description

@dduan

Swift format can’t properly format multiple ifconfig statements in switch statements. To reproduce, paste the following as a unit test in the swift-format unit tests:

  func testNestedPoundIfInSwitchStatement() {
    let input =
      """
      switch self {
      #if os(iOS) || os(tvOS) || os(watchOS)
      case .a:
        return 40
      #if os(iOS) || os(tvOS)
      case .e:
        return 30
      #endif
      #if os(iOS)
      case .g:
        return 2
      #endif
      #endif
      default:
        return nil
      }

      """
    var configuration = Configuration.forTesting
    configuration.indentConditionalCompilationBlocks = false
    assertPrettyPrintEqual(input: input, expected: input, linelength: 45, configuration: configuration)
  }

The test fails with:

failed - Pretty-printed result was not what was expected - Actual output (+) differed from expected output (-):
 switch self {
 #if os(iOS) || os(tvOS) || os(watchOS)
 case .a:
   return 40
 #if os(iOS) || os(tvOS)
 case .e:
   return 30
-#endif
-#if os(iOS)
+#endif#if os(iOS)
 case .g:
   return 2
 #endif
 #endif
 default:
   return nil
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions