Skip to content

Default init style from SwiftBasicFormat looks unfamiliar #1115

@stevapple

Description

@stevapple

Description

Currently SwiftBasicFormat defaults to format initializer declaration as init (…) and init ?(…).

This is diverged from the common practice as well as the Swift book.

eg. https://github.com/apple/swift-syntax/blob/aeb2ff511cd0a4e37f794a93004f55fb03be2633/Sources/SwiftSyntaxBuilder/generated/ResultBuilders.swift#L93-L95

Steps to Reproduce

import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftBasicFormat

class CodeGenerationFormat: BasicFormat {
  override var indentation: TriviaPiece { .spaces(indentationLevel * 2) }
}

let sourceFile = SourceFile {
  StructDecl("public struct SimpleError: Error") {
    VariableDecl("private let code: Int")
    InitializerDecl("""
      public init?(errorCode: Int) {
        guard errorCode > 0 else { return nil }
        self.code = errorCode
      }
      """)
  }
}

print(sourceFile.formatted(using: CodeGenerationFormat()))

Output:

public struct SimpleError: Error {
  private let code: Int
  public init ?(errorCode: Int) {
    guard errorCode > 0 else { 
      return nil 
    }
    self.code = errorCode
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    SwiftSyntaxBuilderIssues about the SwiftSyntaxBuilder code generation library

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions