Skip to content

Commit cf413a6

Browse files
authored
Merge pull request #1338 from kimdv/kimdv/fix-spacing-between-left-brace-and-paren
2 parents 30acdc1 + b542daf commit cf413a6

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

CodeGeneration/Sources/generate-swiftsyntax/templates/basicformat/BasicFormatFile.swift

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,17 @@ let basicFormatFile = SourceFileSyntax {
192192
"""
193193
)
194194

195+
StmtSyntax(
196+
"""
197+
switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) {
198+
case (.leftParen, .leftBrace): // Ensures there is not a space in `.map({ $0.foo })`
199+
return false
200+
default:
201+
break
202+
}
203+
"""
204+
)
205+
195206
try SwitchExprSyntax("switch token.tokenKind") {
196207
for token in SYNTAX_TOKENS {
197208
if token.requiresLeadingSpace {
@@ -246,15 +257,15 @@ let basicFormatFile = SourceFileSyntax {
246257
StmtSyntax(
247258
"""
248259
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
249-
case (.exclamationMark, .leftParen), // Ensures there is not space in `myOptionalClosure!()`
250-
(.exclamationMark, .period), // Ensures there is not space in `myOptionalBar!.foo()`
251-
(.keyword(.as), .exclamationMark), // Ensures there is not space in `as!`
252-
(.keyword(.as), .postfixQuestionMark), // Ensures there is not space in `as?`
253-
(.keyword(.try), .exclamationMark), // Ensures there is not space in `try!`
254-
(.keyword(.try), .postfixQuestionMark), // Ensures there is not space in `try?`:
255-
(.postfixQuestionMark, .leftParen), // Ensures there is not space in `init?()` or `myOptionalClosure?()`s
256-
(.postfixQuestionMark, .rightAngle), // Ensures there is not space in `ContiguousArray<RawSyntax?>`
257-
(.postfixQuestionMark, .rightParen): // Ensures there is not space in `myOptionalClosure?()`
260+
case (.exclamationMark, .leftParen), // Ensures there is not a space in `myOptionalClosure!()`
261+
(.exclamationMark, .period), // Ensures there is not a space in `myOptionalBar!.foo()`
262+
(.keyword(.as), .exclamationMark), // Ensures there is not a space in `as!`
263+
(.keyword(.as), .postfixQuestionMark), // Ensures there is not a space in `as?`
264+
(.keyword(.try), .exclamationMark), // Ensures there is not a space in `try!`
265+
(.keyword(.try), .postfixQuestionMark), // Ensures there is not a space in `try?`:
266+
(.postfixQuestionMark, .leftParen), // Ensures there is not a space in `init?()` or `myOptionalClosure?()`s
267+
(.postfixQuestionMark, .rightAngle), // Ensures there is not a space in `ContiguousArray<RawSyntax?>`
268+
(.postfixQuestionMark, .rightParen): // Ensures there is not a space in `myOptionalClosure?()`
258269
return false
259270
default:
260271
break

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ open class BasicFormat: SyntaxRewriter {
152152
if let keyPath = getKeyPath(token), let requiresLeadingSpace = requiresLeadingSpace(keyPath) {
153153
return requiresLeadingSpace
154154
}
155+
switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) {
156+
case (.leftParen, .leftBrace): // Ensures there is not a space in `.map({ $0.foo })`
157+
return false
158+
default:
159+
break
160+
}
155161
switch token.tokenKind {
156162
case .leftBrace:
157163
return true
@@ -202,15 +208,15 @@ open class BasicFormat: SyntaxRewriter {
202208
return requiresTrailingSpace
203209
}
204210
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
205-
case (.exclamationMark, .leftParen), // Ensures there is not space in `myOptionalClosure!()`
206-
(.exclamationMark, .period), // Ensures there is not space in `myOptionalBar!.foo()`
207-
(.keyword(.as), .exclamationMark), // Ensures there is not space in `as!`
208-
(.keyword(.as), .postfixQuestionMark), // Ensures there is not space in `as?`
209-
(.keyword(.try), .exclamationMark), // Ensures there is not space in `try!`
210-
(.keyword(.try), .postfixQuestionMark), // Ensures there is not space in `try?`:
211-
(.postfixQuestionMark, .leftParen), // Ensures there is not space in `init?()` or `myOptionalClosure?()`s
212-
(.postfixQuestionMark, .rightAngle), // Ensures there is not space in `ContiguousArray<RawSyntax?>`
213-
(.postfixQuestionMark, .rightParen): // Ensures there is not space in `myOptionalClosure?()`
211+
case (.exclamationMark, .leftParen), // Ensures there is not a space in `myOptionalClosure!()`
212+
(.exclamationMark, .period), // Ensures there is not a space in `myOptionalBar!.foo()`
213+
(.keyword(.as), .exclamationMark), // Ensures there is not a space in `as!`
214+
(.keyword(.as), .postfixQuestionMark), // Ensures there is not a space in `as?`
215+
(.keyword(.try), .exclamationMark), // Ensures there is not a space in `try!`
216+
(.keyword(.try), .postfixQuestionMark), // Ensures there is not a space in `try?`:
217+
(.postfixQuestionMark, .leftParen), // Ensures there is not a space in `init?()` or `myOptionalClosure?()`s
218+
(.postfixQuestionMark, .rightAngle), // Ensures there is not a space in `ContiguousArray<RawSyntax?>`
219+
(.postfixQuestionMark, .rightParen): // Ensures there is not a space in `myOptionalClosure?()`
214220
return false
215221
default:
216222
break

Tests/SwiftSyntaxBuilderTest/VariableTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ final class VariableTests: XCTestCase {
105105
}
106106
"""
107107
),
108+
#line: (
109+
DeclSyntax("var bar: [String] { bar.map({ $0.description }) }"),
110+
"""
111+
var bar: [String] {
112+
bar.map({
113+
$0.description
114+
})
115+
}
116+
"""
117+
),
108118
]
109119

110120
for (line, testCase) in testCases {

0 commit comments

Comments
 (0)