Skip to content

Commit 0aa125b

Browse files
authored
Merge pull request #1218 from kimdv/kimdv/fix-spacing-pt-2
Fix spacings around `!`and `?`
2 parents 1e61cc3 + c96d707 commit 0aa125b

File tree

7 files changed

+78
-20
lines changed

7 files changed

+78
-20
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/TokenSpec.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ public let SYNTAX_TOKENS: [TokenSpec] = [
190190
PunctuatorSpec(name: "Arrow", kind: "arrow", text: "->", requiresLeadingSpace: true, requiresTrailingSpace: true),
191191
PunctuatorSpec(name: "Backtick", kind: "backtick", text: "`"),
192192
PunctuatorSpec(name: "Backslash", kind: "backslash", text: "\\"),
193-
PunctuatorSpec(name: "ExclamationMark", kind: "exclaim_postfix", text: "!", requiresTrailingSpace: true),
194-
PunctuatorSpec(name: "PostfixQuestionMark", kind: "question_postfix", text: "?", requiresTrailingSpace: true),
193+
PunctuatorSpec(name: "ExclamationMark", kind: "exclaim_postfix", text: "!"),
194+
PunctuatorSpec(name: "PostfixQuestionMark", kind: "question_postfix", text: "?"),
195195
PunctuatorSpec(name: "InfixQuestionMark", kind: "question_infix", text: "?"),
196196
PunctuatorSpec(name: "StringQuote", kind: "string_quote", text: "\"", classification: "StringLiteral"),
197197
PunctuatorSpec(name: "SingleQuote", kind: "single_quote", text: "\'", classification: "StringLiteral"),

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,19 @@ let basicFormatFile = SourceFile {
151151
}
152152

153153
FunctionDecl("open func requiresTrailingSpace(_ token: TokenSyntax) -> Bool") {
154-
IfStmt("""
155-
// Format `[:]` as-is.
156-
if token.tokenKind == .colon && token.parent?.kind == .dictionaryExpr {
154+
SwitchStmtSyntax("""
155+
switch (token.tokenKind, token.parent?.kind) {
156+
case (.colon, .dictionaryExpr): // Ensures there is not space in `[:]`
157157
return false
158+
case (.exclamationMark, .tryExpr), // Ensures there is a space in `try! foo`
159+
(.postfixQuestionMark, .tryExpr): // Ensures there is a space in `try? foo`
160+
return true
161+
default:
162+
break
158163
}
159164
""")
160165

161-
SwitchStmt("""
166+
SwitchStmtSyntax("""
162167
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
163168
case (.keyword(.as), .exclamationMark), // Ensures there is not space in `as!`
164169
(.keyword(.as), .postfixQuestionMark), // Ensures there is not space in `as?`
@@ -171,7 +176,7 @@ let basicFormatFile = SourceFile {
171176
(.postfixQuestionMark, .rightParen), // Ensures there is not space in `myOptionalClosure?()`
172177
(.keyword(.try), .exclamationMark), // Ensures there is not space in `try!`
173178
(.keyword(.try), .postfixQuestionMark), // Ensures there is not space in `try?`
174-
(.binaryOperator, .comma): // Ensures there is no space in @available(*, deprecated)
179+
(.binaryOperator, .comma): // Ensures there is no space in `@available(*, deprecated)`
175180
return false
176181
default:
177182
break

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,14 @@ open class BasicFormat: SyntaxRewriter {
163163
}
164164

165165
open func requiresTrailingSpace(_ token: TokenSyntax) -> Bool {
166-
// Format `[:]` as-is.
167-
if token.tokenKind == .colon && token.parent?.kind == .dictionaryExpr {
166+
switch (token.tokenKind, token.parent?.kind) {
167+
case (.colon, .dictionaryExpr): // Ensures there is not space in `[:]`
168168
return false
169+
case (.exclamationMark, .tryExpr), // Ensures there is a space in `try! foo`
170+
(.postfixQuestionMark, .tryExpr): // Ensures there is a space in `try? foo`
171+
return true
172+
default:
173+
break
169174
}
170175
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
171176
case (.keyword(.as), .exclamationMark), // Ensures there is not space in `as!`
@@ -179,7 +184,7 @@ open class BasicFormat: SyntaxRewriter {
179184
(.postfixQuestionMark, .rightParen), // Ensures there is not space in `myOptionalClosure?()`
180185
(.keyword(.try), .exclamationMark), // Ensures there is not space in `try!`
181186
(.keyword(.try), .postfixQuestionMark), // Ensures there is not space in `try?`
182-
(.binaryOperator, .comma): // Ensures there is no space in @available(*, deprecated)
187+
(.binaryOperator, .comma): // Ensures there is no space in `@available(*, deprecated)`
183188
return false
184189
default:
185190
break
@@ -193,10 +198,6 @@ open class BasicFormat: SyntaxRewriter {
193198
return true
194199
case .arrow:
195200
return true
196-
case .exclamationMark:
197-
return true
198-
case .postfixQuestionMark:
199-
return true
200201
case .poundKeyPathKeyword:
201202
return true
202203
case .poundLineKeyword:

Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8830,7 +8830,7 @@ public enum SyntaxFactory {
88308830
@available(*, deprecated, message: "Use TokenSyntax.exclamationMarkToken instead")
88318831
public static func makeExclamationMarkToken(
88328832
leadingTrivia: Trivia = [],
8833-
trailingTrivia: Trivia = .space
8833+
trailingTrivia: Trivia = []
88348834
) -> TokenSyntax {
88358835
return makeToken(.exclamationMark, presence: .present,
88368836
leadingTrivia: leadingTrivia,
@@ -8839,7 +8839,7 @@ public enum SyntaxFactory {
88398839
@available(*, deprecated, message: "Use TokenSyntax.postfixQuestionMarkToken instead")
88408840
public static func makePostfixQuestionMarkToken(
88418841
leadingTrivia: Trivia = [],
8842-
trailingTrivia: Trivia = .space
8842+
trailingTrivia: Trivia = []
88438843
) -> TokenSyntax {
88448844
return makeToken(.postfixQuestionMark, presence: .present,
88458845
leadingTrivia: leadingTrivia,

Tests/SwiftSyntaxBuilderTest/FunctionTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ final class FunctionTests: XCTestCase {
151151
}
152152
"""
153153
),
154+
#line: (
155+
FunctionDecl(
156+
"""
157+
public func foo(myOptionalValue: String?, myOtherOptionalValue: [String?]) {
158+
}
159+
"""
160+
),
161+
"""
162+
public func foo(myOptionalValue: String?, myOtherOptionalValue: [String?]) {
163+
}
164+
"""
165+
),
154166
#line: (
155167
FunctionDeclSyntax(
156168
modifiers: [DeclModifier(name: .keyword(.public)), DeclModifier(name: .keyword(.static))],

Tests/SwiftSyntaxBuilderTest/VariableTests.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,48 @@ final class VariableTests: XCTestCase {
4747
}
4848
"""
4949
),
50+
#line: (
51+
VariableDecl("var foo: String? { myOptional?.someProperty }"),
52+
"""
53+
var foo: String? {
54+
myOptional?.someProperty
55+
}
56+
"""
57+
),
58+
#line: (
59+
VariableDecl("let absoluteRaw = AbsoluteRawSyntax(raw: raw!, info: info)"),
60+
"""
61+
let absoluteRaw = AbsoluteRawSyntax(raw: raw!, info: info)
62+
"""
63+
),
64+
#line: (
65+
VariableDecl("var foo: String { bar(baz!) }"),
66+
"""
67+
var foo: String {
68+
bar(baz!)
69+
}
70+
"""
71+
),
72+
#line: (
73+
VariableDecl(#"var foo: String { bar ?? "" }"#),
74+
#"""
75+
var foo: String {
76+
bar ?? ""
77+
}
78+
"""#
79+
),
80+
#line: (
81+
VariableDecl("let bar = try! (foo())"),
82+
"""
83+
let bar = try! (foo())
84+
"""
85+
),
86+
#line: (
87+
VariableDecl("let bar = try! !functionThatThrows()"),
88+
"""
89+
let bar = try! !functionThatThrows()
90+
"""
91+
),
5092
]
5193

5294
for (line, testCase) in testCases {

gyb_syntax_support/Token.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,9 @@ def macro_name(self):
215215

216216
Punctuator('Backslash', 'backslash', text='\\\\'),
217217

218-
Punctuator('ExclamationMark', 'exclaim_postfix', text='!',
219-
requires_trailing_space=True),
218+
Punctuator('ExclamationMark', 'exclaim_postfix', text='!'),
220219

221-
Punctuator('PostfixQuestionMark', 'question_postfix', text='?',
222-
requires_trailing_space=True),
220+
Punctuator('PostfixQuestionMark', 'question_postfix', text='?'),
223221
Punctuator('InfixQuestionMark', 'question_infix', text='?'),
224222

225223
Punctuator('StringQuote', 'string_quote', text='\\\"',

0 commit comments

Comments
 (0)