Skip to content

Commit 88baad6

Browse files
authored
Merge pull request #1511 from kimdv/kimdv/add-spacing-between-attributes
Add test cases for spaces between attributes
2 parents 61cc73e + 44d7521 commit 88baad6

11 files changed

+215
-17
lines changed

CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public let COMMON_NODES: [Node] = [
1616
name: "CodeBlockItemList",
1717
nameForDiagnostics: nil,
1818
kind: "SyntaxCollection",
19-
element: "CodeBlockItem",
20-
elementsSeparatedByNewline: true
19+
element: "CodeBlockItem"
2120
),
2221

2322
// code-block-item = (decl | stmt | expr) ';'?

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ public let DECL_NODES: [Node] = [
111111
name: "AccessorList",
112112
nameForDiagnostics: nil,
113113
kind: "SyntaxCollection",
114-
element: "AccessorDecl",
115-
elementsSeparatedByNewline: true
114+
element: "AccessorDecl"
116115
),
117116

118117
// (value)
@@ -1296,8 +1295,7 @@ public let DECL_NODES: [Node] = [
12961295
name: "MemberDeclList",
12971296
nameForDiagnostics: nil,
12981297
kind: "SyntaxCollection",
1299-
element: "MemberDeclListItem",
1300-
elementsSeparatedByNewline: true
1298+
element: "MemberDeclListItem"
13011299
),
13021300

13031301
// declaration-modifier -> access-level-modifier

CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,8 +1481,7 @@ public let EXPR_NODES: [Node] = [
14811481
kind: "SyntaxCollection",
14821482
element: "Syntax",
14831483
elementName: "SwitchCase",
1484-
elementChoices: ["SwitchCase", "IfConfigDecl"],
1485-
elementsSeparatedByNewline: true
1484+
elementChoices: ["SwitchCase", "IfConfigDecl"]
14861485
),
14871486

14881487
// switch-case -> unknown-attr? switch-case-label stmt-list

CodeGeneration/Sources/SyntaxSupport/Node.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public class Node {
3030
public let collectionElementName: String?
3131
public let collectionElementChoices: [String]?
3232
public let omitWhenEmpty: Bool
33-
public let elementsSeparatedByNewline: Bool
3433
public let collectionElement: String
3534

3635
/// Returns `true` if this node declares one of the base syntax kinds.
@@ -98,8 +97,7 @@ public class Node {
9897
element: String = "",
9998
elementName: String? = nil,
10099
elementChoices: [String]? = nil,
101-
omitWhenEmpty: Bool = false,
102-
elementsSeparatedByNewline: Bool = false
100+
omitWhenEmpty: Bool = false
103101
) {
104102
self.syntaxKind = name
105103
self.swiftSyntaxKind = lowercaseFirstWord(name: name)
@@ -159,7 +157,6 @@ public class Node {
159157
// from its supertype, use that.
160158
self.collectionElementName = elementName ?? self.collectionElement
161159
self.collectionElementChoices = elementChoices ?? []
162-
self.elementsSeparatedByNewline = elementsSeparatedByNewline
163160

164161
// For SyntaxCollections make sure that the elementName is set.
165162
precondition(!isSyntaxCollection || elementName != nil || element != "")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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 AttributeListSyntaxTests: XCTestCase {
18+
func testAttributeListSyntaxSpacing() {
19+
let buildable = AttributeListSyntax {
20+
AttributeSyntax("@inlinable")
21+
AttributeSyntax("@discardableResult")
22+
}
23+
assertBuildResult(
24+
buildable,
25+
"""
26+
@inlinable @discardableResult
27+
"""
28+
)
29+
}
30+
}

Tests/SwiftSyntaxBuilderTest/ClosureExprTests.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,46 @@ final class ClosureExprTests: XCTestCase {
3434
"""
3535
)
3636
}
37+
func testClosureExprWithAsync() {
38+
let buildable = ClosureExprSyntax(
39+
signature: ClosureSignatureSyntax(
40+
input: .simpleInput(
41+
ClosureParamListSyntax {
42+
ClosureParamSyntax(name: .identifier("area"))
43+
}
44+
),
45+
effectSpecifiers: TypeEffectSpecifiersSyntax(
46+
asyncSpecifier: .keyword(.async),
47+
throwsSpecifier: .keyword(.throws)
48+
)
49+
)
50+
) {}
51+
52+
assertBuildResult(
53+
buildable,
54+
"""
55+
{area async throws in
56+
}
57+
"""
58+
)
59+
}
60+
61+
func testMultiTrailingClosure() {
62+
let buildable = ExprSyntax(
63+
"""
64+
foo { _ in
65+
}anotherClosure: { _ in
66+
}
67+
"""
68+
)
69+
70+
assertBuildResult(
71+
buildable,
72+
"""
73+
foo { _ in
74+
} anotherClosure: { _ in
75+
}
76+
"""
77+
)
78+
}
3779
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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 FunctionSignatureSyntaxTests: XCTestCase {
18+
func testFunctionEffectSpecifiersSyntax() throws {
19+
let functionEffects = FunctionEffectSpecifiersSyntax(asyncSpecifier: .keyword(.async), throwsSpecifier: .keyword(.rethrows))
20+
let buildable = FunctionSignatureSyntax(input: .init(parameterList: []), effectSpecifiers: functionEffects, output: .init(returnType: TypeSyntax("String")))
21+
22+
assertBuildResult(
23+
buildable,
24+
"""
25+
() async rethrows -> String
26+
"""
27+
)
28+
}
29+
}

Tests/SwiftSyntaxBuilderTest/FunctionTests.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,47 @@ final class FunctionTests: XCTestCase {
163163
}
164164
"""
165165
),
166+
#line: (
167+
DeclSyntax(
168+
"""
169+
@discardableResult
170+
public func foo() -> String {
171+
return "foo"
172+
}
173+
"""
174+
),
175+
"""
176+
@discardableResult
177+
public func foo() -> String {
178+
return "foo"
179+
}
180+
"""
181+
),
182+
#line: (
183+
DeclSyntax(
184+
FunctionDeclSyntax(
185+
attributes: [.attribute(AttributeSyntax("@inline(__always)")), .attribute(AttributeSyntax("@discardableResult"))],
186+
modifiers: [DeclModifierSyntax(name: .keyword(.public))],
187+
identifier: TokenSyntax.identifier("foo"),
188+
signature: FunctionSignatureSyntax(
189+
input: ParameterClauseSyntax(
190+
parameterList: FunctionParameterListSyntax {}
191+
),
192+
output: ReturnClauseSyntax(
193+
returnType: SimpleTypeIdentifierSyntax(name: .identifier("String"))
194+
)
195+
),
196+
bodyBuilder: {
197+
StmtSyntax(#"return "foo""#)
198+
}
199+
)
200+
),
201+
"""
202+
@inline(__always) @discardableResult public func foo() -> String {
203+
return "foo"
204+
}
205+
"""
206+
),
166207
#line: (
167208
DeclSyntax(
168209
FunctionDeclSyntax(
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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 FunctionTypeSyntaxTests: XCTestCase {
18+
func testFunctionEffectSpecifiersSyntax() throws {
19+
let typeEffects = TypeEffectSpecifiersSyntax(asyncSpecifier: .keyword(.async), throwsSpecifier: .keyword(.throws))
20+
let buildable = FunctionTypeSyntax(arguments: [], effectSpecifiers: typeEffects, output: .init(returnType: TypeSyntax("String")))
21+
22+
assertBuildResult(
23+
buildable,
24+
"""
25+
() async throws -> String
26+
"""
27+
)
28+
}
29+
}

Tests/SwiftSyntaxBuilderTest/ImportTests.swift renamed to Tests/SwiftSyntaxBuilderTest/ImportDeclSyntaxTests.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,28 @@ import XCTest
1414
import SwiftSyntax
1515
import SwiftSyntaxBuilder
1616

17-
final class ImportTests: XCTestCase {
18-
func testImport() {
19-
let leadingTrivia = Trivia.unexpectedText("")
17+
final class ImportDeclSyntaxTests: XCTestCase {
18+
func testImportDeclSyntax() {
2019
let identifier = TokenSyntax.identifier("SwiftSyntax")
2120

2221
let importDecl = ImportDeclSyntax(
23-
leadingTrivia: leadingTrivia,
2422
path: AccessPathSyntax([AccessPathComponentSyntax(name: identifier)])
2523
)
2624

27-
assertBuildResult(importDecl, "␣import SwiftSyntax")
25+
assertBuildResult(importDecl, "import SwiftSyntax")
26+
}
27+
28+
func testImportWithAttribute() {
29+
let buildable = ImportDeclSyntax(
30+
attributes: [.attribute("@_exported")],
31+
path: [AccessPathComponentSyntax(name: "SwiftSyntax")]
32+
)
33+
34+
assertBuildResult(
35+
buildable,
36+
"""
37+
@_exported import SwiftSyntax
38+
"""
39+
)
2840
}
2941
}

Tests/SwiftSyntaxBuilderTest/VariableTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,28 @@ final class VariableTests: XCTestCase {
293293
)
294294
}
295295

296+
func testAccessorWithEffectSpecifier() throws {
297+
let buildable = try VariableDeclSyntax("var test: Int") {
298+
AccessorDeclSyntax(
299+
accessorKind: .keyword(.get),
300+
effectSpecifiers: AccessorEffectSpecifiersSyntax(
301+
asyncSpecifier: .keyword(.async),
302+
throwsSpecifier: .keyword(.throws)
303+
)
304+
) {}
305+
}
306+
307+
assertBuildResult(
308+
buildable,
309+
"""
310+
var test: Int {
311+
get async throws {
312+
}
313+
}
314+
"""
315+
)
316+
}
317+
296318
func testAttributedVariables() throws {
297319
let testCases: [UInt: (VariableDeclSyntax, String)] = try [
298320
#line: (

0 commit comments

Comments
 (0)