Skip to content

Commit db621a2

Browse files
committed
Fix trailing space on as keyword
1 parent 819a40d commit db621a2

File tree

6 files changed

+109
-13
lines changed

6 files changed

+109
-13
lines changed

CodeGeneration/Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ let package = Package(
6262
name: "SyntaxSupport",
6363
exclude: [
6464
"gyb_helpers",
65+
"AttributeKinds.swift.gyb",
6566
"AttributeNodes.swift.gyb",
6667
"AvailabilityNodes.swift.gyb",
6768
"BuilderInitializableTypes.swift.gyb",

CodeGeneration/Sources/generate-swiftbasicformat/BasicFormatFile.swift

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ let basicFormatFile = SourceFile {
5252
open override func visit(_ node: TokenSyntax) -> TokenSyntax {
5353
var leadingTrivia = node.leadingTrivia
5454
var trailingTrivia = node.trailingTrivia
55-
if requiresLeadingSpace(node.tokenKind) && leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
55+
if requiresLeadingSpace(node) && leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
5656
leadingTrivia += .space
5757
}
58-
if requiresTrailingSpace(node.tokenKind) && trailingTrivia.isEmpty {
58+
if requiresTrailingSpace(node) && trailingTrivia.isEmpty {
5959
trailingTrivia += .space
6060
}
6161
if let keyPath = getKeyPath(Syntax(node)), requiresLeadingNewline(keyPath), !(leadingTrivia.first?.isNewline ?? false) {
@@ -121,8 +121,8 @@ let basicFormatFile = SourceFile {
121121
}
122122
}
123123

124-
FunctionDecl("open func requiresLeadingSpace(_ tokenKind: TokenKind) -> Bool") {
125-
SwitchStmt(expression: Expr("tokenKind")) {
124+
FunctionDecl("open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool") {
125+
SwitchStmt(expression: Expr("token.tokenKind")) {
126126
for token in SYNTAX_TOKENS {
127127
if token.requiresLeadingSpace {
128128
SwitchCase("case .\(raw: token.swiftKind):") {
@@ -136,8 +136,41 @@ let basicFormatFile = SourceFile {
136136
}
137137
}
138138

139-
FunctionDecl("open func requiresTrailingSpace(_ tokenKind: TokenKind) -> Bool") {
140-
SwitchStmt(expression: Expr("tokenKind")) {
139+
FunctionDecl("open func requiresTrailingSpace(_ token: TokenSyntax) -> Bool") {
140+
IfStmt(
141+
conditions: ConditionElementList {
142+
Expr("[.asKeyword, .initKeyword].contains(token.tokenKind)")
143+
OptionalBindingCondition(
144+
letOrVarKeyword: .let,
145+
pattern: Pattern("nextTokenKind"),
146+
initializer: InitializerClause(value: Expr("token.nextToken(viewMode: .sourceAccurate)?.tokenKind"))
147+
)
148+
}
149+
) {
150+
SwitchStmt(expression: Expr("nextTokenKind")) {
151+
SwitchCase(
152+
label: .case(SwitchCaseLabel {
153+
CaseItem(
154+
pattern: EnumCasePattern(
155+
period: .period,
156+
caseName: .identifier("exclamationMark")),
157+
trailingComma: .comma)
158+
CaseItem(
159+
pattern: EnumCasePattern(
160+
period: .period,
161+
caseName: .identifier("postfixQuestionMark"))
162+
)
163+
})) {
164+
ReturnStmt("return false")
165+
}
166+
167+
SwitchCase("default:") {
168+
BreakStmt()
169+
}
170+
}
171+
}
172+
173+
SwitchStmt(expression: Expr("token.tokenKind")) {
141174
for token in SYNTAX_TOKENS {
142175
if token.requiresTrailingSpace {
143176
SwitchCase("case .\(raw: token.swiftKind):") {

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ open class BasicFormat: SyntaxRewriter {
4848
open override func visit(_ node: TokenSyntax) -> TokenSyntax {
4949
var leadingTrivia = node.leadingTrivia
5050
var trailingTrivia = node.trailingTrivia
51-
if requiresLeadingSpace(node.tokenKind) && leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
51+
if requiresLeadingSpace(node) && leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
5252
leadingTrivia += .space
5353
}
54-
if requiresTrailingSpace(node.tokenKind) && trailingTrivia.isEmpty {
54+
if requiresTrailingSpace(node) && trailingTrivia.isEmpty {
5555
trailingTrivia += .space
5656
}
5757
if let keyPath = getKeyPath(Syntax(node)), requiresLeadingNewline(keyPath), !(leadingTrivia.first?.isNewline ?? false) {
@@ -113,8 +113,8 @@ open class BasicFormat: SyntaxRewriter {
113113
}
114114
}
115115

116-
open func requiresLeadingSpace(_ tokenKind: TokenKind) -> Bool {
117-
switch tokenKind {
116+
open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool {
117+
switch token.tokenKind {
118118
case .whereKeyword:
119119
return true
120120
case .catchKeyword:
@@ -136,8 +136,16 @@ open class BasicFormat: SyntaxRewriter {
136136
}
137137
}
138138

139-
open func requiresTrailingSpace(_ tokenKind: TokenKind) -> Bool {
140-
switch tokenKind {
139+
open func requiresTrailingSpace(_ token: TokenSyntax) -> Bool {
140+
if [.asKeyword, .initKeyword].contains(token.tokenKind), let nextTokenKind = token.nextToken(viewMode: .sourceAccurate)?.tokenKind {
141+
switch nextTokenKind {
142+
case .exclamationMark, .postfixQuestionMark:
143+
return false
144+
default:
145+
break
146+
}
147+
}
148+
switch token.tokenKind {
141149
case .associatedtypeKeyword:
142150
return true
143151
case .classKeyword:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import XCTest
14+
import SwiftSyntax
15+
import SwiftSyntaxBuilder
16+
17+
final class InitializerDeclTests: XCTestCase {
18+
func testInitializerDecl() {
19+
let builder = InitializerDecl("""
20+
public init?(errorCode: Int) {
21+
guard errorCode > 0 else { return nil }
22+
self.code = errorCode
23+
}
24+
""")
25+
26+
print(builder.formatted().description)
27+
28+
AssertBuildResult(builder, """
29+
public init?(errorCode: Int) {
30+
guard errorCode > 0 else {
31+
return nil
32+
}
33+
self.code = errorCode
34+
}
35+
""")
36+
}
37+
}

Tests/SwiftSyntaxBuilderTest/MemberAccessTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import SwiftSyntaxBuilder
1616

1717
final class MemberAccessTests: XCTestCase {
1818
func testMemberAccessExprConvenienceInitializers() {
19-
let builder = MemberAccessExpr( base: "Foo", name: "bar")
19+
let builder = MemberAccessExpr(base: "Foo", name: "bar")
2020
AssertBuildResult(builder, "Foo.bar")
2121
}
2222
}

Tests/SwiftSyntaxBuilderTest/ReturnStmsTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,21 @@ final class ReturnStmtTests: XCTestCase {
2424
""")
2525
}
2626

27+
func testReturnStmtwithAsKeyword() {
28+
let testCases: [UInt: (ReturnStmt, String)] = [
29+
#line: (ReturnStmt("return self.asProtocol(SyntaxProtocol.self) as? DeclSyntaxProtocol"),
30+
"return self.asProtocol(SyntaxProtocol.self) as? DeclSyntaxProtocol"),
31+
#line: (ReturnStmt("return 0 as! String"),
32+
"return 0 as! String"),
33+
#line: (ReturnStmt("return 0 as Double"),
34+
"return 0 as Double"),
35+
#line: (ReturnStmt("return !myBool"),
36+
"return !myBool")
37+
]
38+
39+
for (line, testCase) in testCases {
40+
let (builder, expected) = testCase
41+
AssertBuildResult(builder, expected, line: line)
42+
}
43+
}
2744
}

0 commit comments

Comments
 (0)