diff --git a/CodeGeneration/Sources/generate-swiftsyntax/templates/basicformat/BasicFormatFile.swift b/CodeGeneration/Sources/generate-swiftsyntax/templates/basicformat/BasicFormatFile.swift index 5e8b738cfc9..719ab8f5d7e 100644 --- a/CodeGeneration/Sources/generate-swiftsyntax/templates/basicformat/BasicFormatFile.swift +++ b/CodeGeneration/Sources/generate-swiftsyntax/templates/basicformat/BasicFormatFile.swift @@ -192,6 +192,17 @@ let basicFormatFile = SourceFileSyntax { """ ) + StmtSyntax( + """ + switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) { + case (.leftParen, .leftBrace): // Ensures there is not a space in `.map({ $0.foo })` + return false + default: + break + } + """ + ) + try SwitchExprSyntax("switch token.tokenKind") { for token in SYNTAX_TOKENS { if token.requiresLeadingSpace { @@ -246,15 +257,15 @@ let basicFormatFile = SourceFileSyntax { StmtSyntax( """ switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) { - case (.exclamationMark, .leftParen), // Ensures there is not space in `myOptionalClosure!()` - (.exclamationMark, .period), // Ensures there is not space in `myOptionalBar!.foo()` - (.keyword(.as), .exclamationMark), // Ensures there is not space in `as!` - (.keyword(.as), .postfixQuestionMark), // Ensures there is not space in `as?` - (.keyword(.try), .exclamationMark), // Ensures there is not space in `try!` - (.keyword(.try), .postfixQuestionMark), // Ensures there is not space in `try?`: - (.postfixQuestionMark, .leftParen), // Ensures there is not space in `init?()` or `myOptionalClosure?()`s - (.postfixQuestionMark, .rightAngle), // Ensures there is not space in `ContiguousArray` - (.postfixQuestionMark, .rightParen): // Ensures there is not space in `myOptionalClosure?()` + case (.exclamationMark, .leftParen), // Ensures there is not a space in `myOptionalClosure!()` + (.exclamationMark, .period), // Ensures there is not a space in `myOptionalBar!.foo()` + (.keyword(.as), .exclamationMark), // Ensures there is not a space in `as!` + (.keyword(.as), .postfixQuestionMark), // Ensures there is not a space in `as?` + (.keyword(.try), .exclamationMark), // Ensures there is not a space in `try!` + (.keyword(.try), .postfixQuestionMark), // Ensures there is not a space in `try?`: + (.postfixQuestionMark, .leftParen), // Ensures there is not a space in `init?()` or `myOptionalClosure?()`s + (.postfixQuestionMark, .rightAngle), // Ensures there is not a space in `ContiguousArray` + (.postfixQuestionMark, .rightParen): // Ensures there is not a space in `myOptionalClosure?()` return false default: break diff --git a/Sources/SwiftBasicFormat/generated/BasicFormat.swift b/Sources/SwiftBasicFormat/generated/BasicFormat.swift index acfaec6bc63..5fb9f7ee581 100644 --- a/Sources/SwiftBasicFormat/generated/BasicFormat.swift +++ b/Sources/SwiftBasicFormat/generated/BasicFormat.swift @@ -152,6 +152,12 @@ open class BasicFormat: SyntaxRewriter { if let keyPath = getKeyPath(token), let requiresLeadingSpace = requiresLeadingSpace(keyPath) { return requiresLeadingSpace } + switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) { + case (.leftParen, .leftBrace): // Ensures there is not a space in `.map({ $0.foo })` + return false + default: + break + } switch token.tokenKind { case .leftBrace: return true @@ -202,15 +208,15 @@ open class BasicFormat: SyntaxRewriter { return requiresTrailingSpace } switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) { - case (.exclamationMark, .leftParen), // Ensures there is not space in `myOptionalClosure!()` - (.exclamationMark, .period), // Ensures there is not space in `myOptionalBar!.foo()` - (.keyword(.as), .exclamationMark), // Ensures there is not space in `as!` - (.keyword(.as), .postfixQuestionMark), // Ensures there is not space in `as?` - (.keyword(.try), .exclamationMark), // Ensures there is not space in `try!` - (.keyword(.try), .postfixQuestionMark), // Ensures there is not space in `try?`: - (.postfixQuestionMark, .leftParen), // Ensures there is not space in `init?()` or `myOptionalClosure?()`s - (.postfixQuestionMark, .rightAngle), // Ensures there is not space in `ContiguousArray` - (.postfixQuestionMark, .rightParen): // Ensures there is not space in `myOptionalClosure?()` + case (.exclamationMark, .leftParen), // Ensures there is not a space in `myOptionalClosure!()` + (.exclamationMark, .period), // Ensures there is not a space in `myOptionalBar!.foo()` + (.keyword(.as), .exclamationMark), // Ensures there is not a space in `as!` + (.keyword(.as), .postfixQuestionMark), // Ensures there is not a space in `as?` + (.keyword(.try), .exclamationMark), // Ensures there is not a space in `try!` + (.keyword(.try), .postfixQuestionMark), // Ensures there is not a space in `try?`: + (.postfixQuestionMark, .leftParen), // Ensures there is not a space in `init?()` or `myOptionalClosure?()`s + (.postfixQuestionMark, .rightAngle), // Ensures there is not a space in `ContiguousArray` + (.postfixQuestionMark, .rightParen): // Ensures there is not a space in `myOptionalClosure?()` return false default: break diff --git a/Tests/SwiftSyntaxBuilderTest/VariableTests.swift b/Tests/SwiftSyntaxBuilderTest/VariableTests.swift index 21d05e64597..bc0f8b602b2 100644 --- a/Tests/SwiftSyntaxBuilderTest/VariableTests.swift +++ b/Tests/SwiftSyntaxBuilderTest/VariableTests.swift @@ -105,6 +105,16 @@ final class VariableTests: XCTestCase { } """ ), + #line: ( + DeclSyntax("var bar: [String] { bar.map({ $0.description }) }"), + """ + var bar: [String] { + bar.map({ + $0.description + }) + } + """ + ), ] for (line, testCase) in testCases {