Skip to content

SwiftBasicFormat isn't formatting try? correctly #1141

@stevapple

Description

@stevapple

Description

Currently SwiftBasicFormat will format try? as try ?, which is a false syntax rejected by the Swift compiler.

Steps to Reproduce

import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftBasicFormat

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

let sourceFile = SourceFile {
    ImportDecl("import Foundation")
    VariableDecl("let url = URL(string: \"https://www.swift.org\")")
    VariableDecl("let content = try? String(contentsOf: url)")
}

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

Output:

import Foundation
let url = URL(string: "https://www.swift.org")
let content = try ? String(contentsOf: url)
// main.swift:3:18 Consecutive statements on a line must be separated by ';'
// main.swift:3:19 Expected initial value after '='
// main.swift:3:19 Expected expression

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions