Skip to content

Fix trailing space on as, init and try keyword #1137

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
Dec 14, 2022
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 @@ -52,10 +52,10 @@ let basicFormatFile = SourceFile {
open override func visit(_ node: TokenSyntax) -> TokenSyntax {
var leadingTrivia = node.leadingTrivia
var trailingTrivia = node.trailingTrivia
if requiresLeadingSpace(node.tokenKind) && leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
if requiresLeadingSpace(node) && leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
leadingTrivia += .space
}
if requiresTrailingSpace(node.tokenKind) && trailingTrivia.isEmpty {
if requiresTrailingSpace(node) && trailingTrivia.isEmpty {
trailingTrivia += .space
}
if let keyPath = getKeyPath(Syntax(node)), requiresLeadingNewline(keyPath), !(leadingTrivia.first?.isNewline ?? false) {
Expand Down Expand Up @@ -121,8 +121,8 @@ let basicFormatFile = SourceFile {
}
}

FunctionDecl("open func requiresLeadingSpace(_ tokenKind: TokenKind) -> Bool") {
SwitchStmt(expression: Expr("tokenKind")) {
FunctionDecl("open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool") {
SwitchStmt(expression: Expr("token.tokenKind")) {
for token in SYNTAX_TOKENS {
if token.requiresLeadingSpace {
SwitchCase("case .\(raw: token.swiftKind):") {
Expand All @@ -136,8 +136,21 @@ let basicFormatFile = SourceFile {
}
}

FunctionDecl("open func requiresTrailingSpace(_ tokenKind: TokenKind) -> Bool") {
SwitchStmt(expression: Expr("tokenKind")) {
FunctionDecl("open func requiresTrailingSpace(_ token: TokenSyntax) -> Bool") {
SwitchStmt("""
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
case (.asKeyword, .exclamationMark),
(.asKeyword, .postfixQuestionMark),
(.initKeyword, .postfixQuestionMark),
(.tryKeyword, .exclamationMark),
(.tryKeyword, .postfixQuestionMark):
return false
default:
break
}
""")

SwitchStmt(expression: Expr("token.tokenKind")) {
for token in SYNTAX_TOKENS {
if token.requiresTrailingSpace {
SwitchCase("case .\(raw: token.swiftKind):") {
Expand Down
22 changes: 16 additions & 6 deletions Sources/SwiftBasicFormat/generated/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ open class BasicFormat: SyntaxRewriter {
open override func visit(_ node: TokenSyntax) -> TokenSyntax {
var leadingTrivia = node.leadingTrivia
var trailingTrivia = node.trailingTrivia
if requiresLeadingSpace(node.tokenKind) && leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
if requiresLeadingSpace(node) && leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
leadingTrivia += .space
}
if requiresTrailingSpace(node.tokenKind) && trailingTrivia.isEmpty {
if requiresTrailingSpace(node) && trailingTrivia.isEmpty {
trailingTrivia += .space
}
if let keyPath = getKeyPath(Syntax(node)), requiresLeadingNewline(keyPath), !(leadingTrivia.first?.isNewline ?? false) {
Expand Down Expand Up @@ -113,8 +113,8 @@ open class BasicFormat: SyntaxRewriter {
}
}

open func requiresLeadingSpace(_ tokenKind: TokenKind) -> Bool {
switch tokenKind {
open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool {
switch token.tokenKind {
case .whereKeyword:
return true
case .catchKeyword:
Expand All @@ -136,8 +136,18 @@ open class BasicFormat: SyntaxRewriter {
}
}

open func requiresTrailingSpace(_ tokenKind: TokenKind) -> Bool {
switch tokenKind {
open func requiresTrailingSpace(_ token: TokenSyntax) -> Bool {
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
case (.asKeyword, .exclamationMark),
(.asKeyword, .postfixQuestionMark),
(.initKeyword, .postfixQuestionMark),
(.tryKeyword, .exclamationMark),
(.tryKeyword, .postfixQuestionMark):
return false
default:
break
}
switch token.tokenKind {
case .associatedtypeKeyword:
return true
case .classKeyword:
Expand Down
37 changes: 37 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/InitializerDeclTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import XCTest
import SwiftSyntax
import SwiftSyntaxBuilder

final class InitializerDeclTests: XCTestCase {
func testInitializerDecl() {
let builder = InitializerDecl("""
public init?(errorCode: Int) {
guard errorCode > 0 else { return nil }
self.code = errorCode
}
""")

print(builder.formatted().description)

AssertBuildResult(builder, """
public init?(errorCode: Int) {
guard errorCode > 0 else {
return nil
}
self.code = errorCode
}
""")
}
}
2 changes: 1 addition & 1 deletion Tests/SwiftSyntaxBuilderTest/MemberAccessTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import SwiftSyntaxBuilder

final class MemberAccessTests: XCTestCase {
func testMemberAccessExprConvenienceInitializers() {
let builder = MemberAccessExpr( base: "Foo", name: "bar")
let builder = MemberAccessExpr(base: "Foo", name: "bar")
AssertBuildResult(builder, "Foo.bar")
}
}
17 changes: 17 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/ReturnStmsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,21 @@ final class ReturnStmtTests: XCTestCase {
""")
}

func testReturnStmtwithAsKeyword() {
let testCases: [UInt: (ReturnStmt, String)] = [
#line: (ReturnStmt("return self.asProtocol(SyntaxProtocol.self) as? DeclSyntaxProtocol"),
"return self.asProtocol(SyntaxProtocol.self) as? DeclSyntaxProtocol"),
#line: (ReturnStmt("return 0 as! String"),
"return 0 as! String"),
#line: (ReturnStmt("return 0 as Double"),
"return 0 as Double"),
#line: (ReturnStmt("return !myBool"),
"return !myBool")
]

for (line, testCase) in testCases {
let (builder, expected) = testCase
AssertBuildResult(builder, expected, line: line)
}
}
}
14 changes: 14 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/VariableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ final class VariableTests: XCTestCase {
AssertBuildResult(buildable, "␣let a: [Int]")
}

func testVariableDeclWithTry() {
let testCases: [UInt: (VariableDecl, String)] = [
#line: (VariableDecl("let content = try? String(contentsOf: url)"),
"let content = try? String(contentsOf: url)"),
#line: (VariableDecl("let content = try! String(contentsOf: url)"),
"let content = try! String(contentsOf: url)"),
]

for (line, testCase) in testCases {
let (builder, expected) = testCase
AssertBuildResult(builder, expected, line: line)
}
}

func testVariableDeclWithValue() {
let leadingTrivia = Trivia.unexpectedText("␣")

Expand Down