Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<RawSyntax?>`
(.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<RawSyntax?>`
(.postfixQuestionMark, .rightParen): // Ensures there is not a space in `myOptionalClosure?()`
return false
default:
break
Expand Down
24 changes: 15 additions & 9 deletions Sources/SwiftBasicFormat/generated/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<RawSyntax?>`
(.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<RawSyntax?>`
(.postfixQuestionMark, .rightParen): // Ensures there is not a space in `myOptionalClosure?()`
return false
default:
break
Expand Down
10 changes: 10 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/VariableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down