Skip to content

Commit 51d94a0

Browse files
committed
Fix spacings around !and ?
1 parent 8e6ae6e commit 51d94a0

File tree

7 files changed

+54
-22
lines changed

7 files changed

+54
-22
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/TokenSpec.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ public let SYNTAX_TOKENS: [TokenSpec] = [
278278
PunctuatorSpec(name: "Arrow", kind: "arrow", text: "->", requiresLeadingSpace: true, requiresTrailingSpace: true),
279279
PunctuatorSpec(name: "Backtick", kind: "backtick", text: "`"),
280280
PunctuatorSpec(name: "Backslash", kind: "backslash", text: "\\"),
281-
PunctuatorSpec(name: "ExclamationMark", kind: "exclaim_postfix", text: "!", requiresTrailingSpace: true),
282-
PunctuatorSpec(name: "PostfixQuestionMark", kind: "question_postfix", text: "?", requiresTrailingSpace: true),
281+
PunctuatorSpec(name: "ExclamationMark", kind: "exclaim_postfix", text: "!"),
282+
PunctuatorSpec(name: "PostfixQuestionMark", kind: "question_postfix", text: "?"),
283283
PunctuatorSpec(name: "InfixQuestionMark", kind: "question_infix", text: "?"),
284284
PunctuatorSpec(name: "StringQuote", kind: "string_quote", text: "\"", classification: "StringLiteral"),
285285
PunctuatorSpec(name: "SingleQuote", kind: "single_quote", text: "\'", classification: "StringLiteral"),

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,16 @@ let basicFormatFile = SourceFile {
157157
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
158158
case (.asKeyword, .exclamationMark), // Ensures there is not space in `as!`
159159
(.asKeyword, .postfixQuestionMark), // Ensures there is not space in `as?`
160-
(.exclamationMark, .leftParen), // Ensures there is not space in `myOptionalClosure!()`
161-
(.exclamationMark, .period), // Ensures there is not space in `myOptionalBar!.foo()`
162160
(.initKeyword, .leftParen), // Ensures there is not space in `init()`
163161
(.initKeyword, .postfixQuestionMark), // Ensures there is not space in `init?`
164-
(.postfixQuestionMark, .leftParen), // Ensures there is not space in `init?()`
165-
(.postfixQuestionMark, .rightAngle), // Ensures there is not space in `ContiguousArray<RawSyntax?>`
166-
(.postfixQuestionMark, .rightParen), // Ensures there is not space in `myOptionalClosure?()`
167162
(.tryKeyword, .exclamationMark), // Ensures there is not space in `try!`
168163
(.tryKeyword, .postfixQuestionMark): // Ensures there is not space in `try?`
169164
return false
170165
case (.spacedBinaryOperator, .comma):
171166
return false
167+
case (.exclamationMark, .identifier), // Ensures there is not space in `try! foo`
168+
(.postfixQuestionMark, .identifier): // Ensures there is not space in `try? foo`
169+
return true
172170
default:
173171
break
174172
}

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,16 @@ open class BasicFormat: SyntaxRewriter {
170170
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
171171
case (.asKeyword, .exclamationMark), // Ensures there is not space in `as!`
172172
(.asKeyword, .postfixQuestionMark), // Ensures there is not space in `as?`
173-
(.exclamationMark, .leftParen), // Ensures there is not space in `myOptionalClosure!()`
174-
(.exclamationMark, .period), // Ensures there is not space in `myOptionalBar!.foo()`
175173
(.initKeyword, .leftParen), // Ensures there is not space in `init()`
176174
(.initKeyword, .postfixQuestionMark), // Ensures there is not space in `init?`
177-
(.postfixQuestionMark, .leftParen), // Ensures there is not space in `init?()`
178-
(.postfixQuestionMark, .rightAngle), // Ensures there is not space in `ContiguousArray<RawSyntax?>`
179-
(.postfixQuestionMark, .rightParen), // Ensures there is not space in `myOptionalClosure?()`
180175
(.tryKeyword, .exclamationMark), // Ensures there is not space in `try!`
181176
(.tryKeyword, .postfixQuestionMark): // Ensures there is not space in `try?`
182177
return false
183178
case (.spacedBinaryOperator, .comma):
184179
return false
180+
case (.exclamationMark, .identifier), // Ensures there is not space in `try! foo`
181+
(.postfixQuestionMark, .identifier): // Ensures there is not space in `try? foo`
182+
return true
185183
default:
186184
break
187185
}
@@ -284,10 +282,6 @@ open class BasicFormat: SyntaxRewriter {
284282
return true
285283
case .arrow:
286284
return true
287-
case .exclamationMark:
288-
return true
289-
case .postfixQuestionMark:
290-
return true
291285
case .poundKeyPathKeyword:
292286
return true
293287
case .poundLineKeyword:

Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9166,7 +9166,7 @@ public enum SyntaxFactory {
91669166
@available(*, deprecated, message: "Use TokenSyntax.exclamationMarkToken instead")
91679167
public static func makeExclamationMarkToken(
91689168
leadingTrivia: Trivia = [],
9169-
trailingTrivia: Trivia = .space
9169+
trailingTrivia: Trivia = []
91709170
) -> TokenSyntax {
91719171
return makeToken(.exclamationMark, presence: .present,
91729172
leadingTrivia: leadingTrivia,
@@ -9175,7 +9175,7 @@ public enum SyntaxFactory {
91759175
@available(*, deprecated, message: "Use TokenSyntax.postfixQuestionMarkToken instead")
91769176
public static func makePostfixQuestionMarkToken(
91779177
leadingTrivia: Trivia = [],
9178-
trailingTrivia: Trivia = .space
9178+
trailingTrivia: Trivia = []
91799179
) -> TokenSyntax {
91809180
return makeToken(.postfixQuestionMark, presence: .present,
91819181
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: .public), DeclModifier(name: .static)],

Tests/SwiftSyntaxBuilderTest/VariableTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,36 @@ 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+
),
5080
]
5181

5282
for (line, testCase) in testCases {

gyb_syntax_support/Token.py

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

277277
Punctuator('Backslash', 'backslash', text='\\\\'),
278278

279-
Punctuator('ExclamationMark', 'exclaim_postfix', text='!',
280-
requires_trailing_space=True),
279+
Punctuator('ExclamationMark', 'exclaim_postfix', text='!'),
281280

282-
Punctuator('PostfixQuestionMark', 'question_postfix', text='?',
283-
requires_trailing_space=True),
281+
Punctuator('PostfixQuestionMark', 'question_postfix', text='?'),
284282
Punctuator('InfixQuestionMark', 'question_infix', text='?'),
285283

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

0 commit comments

Comments
 (0)