Skip to content

Fix spacings around !and ? #1218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2023
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 @@ -190,8 +190,8 @@ public let SYNTAX_TOKENS: [TokenSpec] = [
PunctuatorSpec(name: "Arrow", kind: "arrow", text: "->", requiresLeadingSpace: true, requiresTrailingSpace: true),
PunctuatorSpec(name: "Backtick", kind: "backtick", text: "`"),
PunctuatorSpec(name: "Backslash", kind: "backslash", text: "\\"),
PunctuatorSpec(name: "ExclamationMark", kind: "exclaim_postfix", text: "!", requiresTrailingSpace: true),
PunctuatorSpec(name: "PostfixQuestionMark", kind: "question_postfix", text: "?", requiresTrailingSpace: true),
PunctuatorSpec(name: "ExclamationMark", kind: "exclaim_postfix", text: "!"),
PunctuatorSpec(name: "PostfixQuestionMark", kind: "question_postfix", text: "?"),
PunctuatorSpec(name: "InfixQuestionMark", kind: "question_infix", text: "?"),
PunctuatorSpec(name: "StringQuote", kind: "string_quote", text: "\"", classification: "StringLiteral"),
PunctuatorSpec(name: "SingleQuote", kind: "single_quote", text: "\'", classification: "StringLiteral"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,19 @@ let basicFormatFile = SourceFile {
}

FunctionDecl("open func requiresTrailingSpace(_ token: TokenSyntax) -> Bool") {
IfStmt("""
// Format `[:]` as-is.
if token.tokenKind == .colon && token.parent?.kind == .dictionaryExpr {
SwitchStmtSyntax("""
switch (token.tokenKind, token.parent?.kind) {
case (.colon, .dictionaryExpr): // Ensures there is not space in `[:]`
return false
case (.exclamationMark, .tryExpr), // Ensures there is a space in `try! foo`
(.postfixQuestionMark, .tryExpr): // Ensures there is a space in `try? foo`
return true
default:
break
}
""")

SwitchStmt("""
SwitchStmtSyntax("""
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
case (.keyword(.as), .exclamationMark), // Ensures there is not space in `as!`
(.keyword(.as), .postfixQuestionMark), // Ensures there is not space in `as?`
Expand All @@ -171,7 +176,7 @@ let basicFormatFile = SourceFile {
(.postfixQuestionMark, .rightParen), // Ensures there is not space in `myOptionalClosure?()`
(.keyword(.try), .exclamationMark), // Ensures there is not space in `try!`
(.keyword(.try), .postfixQuestionMark), // Ensures there is not space in `try?`
(.binaryOperator, .comma): // Ensures there is no space in @available(*, deprecated)
(.binaryOperator, .comma): // Ensures there is no space in `@available(*, deprecated)`
return false
default:
break
Expand Down
15 changes: 8 additions & 7 deletions Sources/SwiftBasicFormat/generated/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,14 @@ open class BasicFormat: SyntaxRewriter {
}

open func requiresTrailingSpace(_ token: TokenSyntax) -> Bool {
// Format `[:]` as-is.
if token.tokenKind == .colon && token.parent?.kind == .dictionaryExpr {
switch (token.tokenKind, token.parent?.kind) {
case (.colon, .dictionaryExpr): // Ensures there is not space in `[:]`
return false
case (.exclamationMark, .tryExpr), // Ensures there is a space in `try! foo`
(.postfixQuestionMark, .tryExpr): // Ensures there is a space in `try? foo`
return true
default:
break
}
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
case (.keyword(.as), .exclamationMark), // Ensures there is not space in `as!`
Expand All @@ -179,7 +184,7 @@ open class BasicFormat: SyntaxRewriter {
(.postfixQuestionMark, .rightParen), // Ensures there is not space in `myOptionalClosure?()`
(.keyword(.try), .exclamationMark), // Ensures there is not space in `try!`
(.keyword(.try), .postfixQuestionMark), // Ensures there is not space in `try?`
(.binaryOperator, .comma): // Ensures there is no space in @available(*, deprecated)
(.binaryOperator, .comma): // Ensures there is no space in `@available(*, deprecated)`
return false
default:
break
Expand All @@ -193,10 +198,6 @@ open class BasicFormat: SyntaxRewriter {
return true
case .arrow:
return true
case .exclamationMark:
return true
case .postfixQuestionMark:
return true
case .poundKeyPathKeyword:
return true
case .poundLineKeyword:
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8689,7 +8689,7 @@ public enum SyntaxFactory {
@available(*, deprecated, message: "Use TokenSyntax.exclamationMarkToken instead")
public static func makeExclamationMarkToken(
leadingTrivia: Trivia = [],
trailingTrivia: Trivia = .space
trailingTrivia: Trivia = []
) -> TokenSyntax {
return makeToken(.exclamationMark, presence: .present,
leadingTrivia: leadingTrivia,
Expand All @@ -8698,7 +8698,7 @@ public enum SyntaxFactory {
@available(*, deprecated, message: "Use TokenSyntax.postfixQuestionMarkToken instead")
public static func makePostfixQuestionMarkToken(
leadingTrivia: Trivia = [],
trailingTrivia: Trivia = .space
trailingTrivia: Trivia = []
) -> TokenSyntax {
return makeToken(.postfixQuestionMark, presence: .present,
leadingTrivia: leadingTrivia,
Expand Down
12 changes: 12 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/FunctionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ final class FunctionTests: XCTestCase {
}
"""
),
#line: (
FunctionDecl(
"""
public func foo(myOptionalValue: String?, myOtherOptionalValue: [String?]) {
}
"""
),
"""
public func foo(myOptionalValue: String?, myOtherOptionalValue: [String?]) {
}
"""
),
#line: (
FunctionDeclSyntax(
modifiers: [DeclModifier(name: .keyword(.public)), DeclModifier(name: .keyword(.static))],
Expand Down
42 changes: 42 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/VariableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,48 @@ final class VariableTests: XCTestCase {
}
"""
),
#line: (
VariableDecl("var foo: String? { myOptional?.someProperty }"),
"""
var foo: String? {
myOptional?.someProperty
}
"""
),
#line: (
VariableDecl("let absoluteRaw = AbsoluteRawSyntax(raw: raw!, info: info)"),
"""
let absoluteRaw = AbsoluteRawSyntax(raw: raw!, info: info)
"""
),
#line: (
VariableDecl("var foo: String { bar(baz!) }"),
"""
var foo: String {
bar(baz!)
}
"""
),
#line: (
VariableDecl(#"var foo: String { bar ?? "" }"#),
#"""
var foo: String {
bar ?? ""
}
"""#
),
#line: (
VariableDecl("let bar = try! (foo())"),
"""
let bar = try! (foo())
"""
),
#line: (
VariableDecl("let bar = try! !functionThatThrows()"),
"""
let bar = try! !functionThatThrows()
"""
),
]

for (line, testCase) in testCases {
Expand Down
6 changes: 2 additions & 4 deletions gyb_syntax_support/Token.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,9 @@ def macro_name(self):

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

Punctuator('ExclamationMark', 'exclaim_postfix', text='!',
requires_trailing_space=True),
Punctuator('ExclamationMark', 'exclaim_postfix', text='!'),

Punctuator('PostfixQuestionMark', 'question_postfix', text='?',
requires_trailing_space=True),
Punctuator('PostfixQuestionMark', 'question_postfix', text='?'),
Punctuator('InfixQuestionMark', 'question_infix', text='?'),

Punctuator('StringQuote', 'string_quote', text='\\\"',
Expand Down