Skip to content

Commit 69c0be3

Browse files
authored
Merge pull request #1879 from ahoppen/ahoppen/syntax-collection-manipulation
Deprecate manipulation functions on SyntaxCollection
2 parents 0b80433 + 85e2d3a commit 69c0be3

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed

Sources/SwiftSyntax/SyntaxCollection.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extension SyntaxCollection {
3333
self.init(Syntax(data))!
3434
}
3535

36-
public init(_ children: [Element]) {
36+
public init<Children: Sequence>(_ children: Children) where Children.Element == Element {
3737
let arena = SyntaxArena()
3838
// Extend the lifetime of children so their arenas don't get destroyed
3939
// before they can be added as children of the new arena.
@@ -70,6 +70,7 @@ extension SyntaxCollection {
7070
///
7171
/// - Parameter syntax: The element to append.
7272
/// - Returns: A new collection with that element appended to the end.
73+
@available(*, deprecated, message: "Create a new array of elements and construct a new collection type from those elements")
7374
public func appending(_ syntax: Element) -> Self {
7475
var newLayout = layoutView.formLayoutArray()
7576
newLayout.append(syntax.raw)
@@ -82,6 +83,7 @@ extension SyntaxCollection {
8283
/// - Parameter syntax: The element to prepend.
8384
/// - Returns: A new collection with that element prepended to the
8485
/// beginning.
86+
@available(*, deprecated, message: "Create a new array of elements and construct a new collection type from those elements")
8587
public func prepending(_ syntax: Element) -> Self {
8688
return inserting(syntax, at: 0)
8789
}
@@ -94,6 +96,7 @@ extension SyntaxCollection {
9496
/// - index: The index at which to insert the element in the collection.
9597
///
9698
/// - Returns: A new collection with that element appended to the end.
99+
@available(*, deprecated, message: "Create a new array of elements and construct a new collection type from those elements")
97100
public func inserting(_ syntax: Element, at index: Int) -> Self {
98101
var newLayout = layoutView.formLayoutArray()
99102
/// Make sure the index is a valid insertion index (0 to 1 past the end)
@@ -130,6 +133,7 @@ extension SyntaxCollection {
130133
/// - Parameter index: The index of the element to remove from the collection.
131134
/// - Returns: A new collection with the element at the provided index
132135
/// removed.
136+
@available(*, deprecated, message: "Use filter to remove unwanted elements and construct a new collection type from the filtered elements")
133137
public func removing(childAt index: Int) -> Self {
134138
var newLayout = layoutView.formLayoutArray()
135139
newLayout.remove(at: index)
@@ -139,6 +143,7 @@ extension SyntaxCollection {
139143
/// Creates a new collection by removing the first element.
140144
///
141145
/// - Returns: A new collection with the first element removed.
146+
@available(*, deprecated, message: "Use CollectionType(node.dropFirst())")
142147
public func removingFirst() -> Self {
143148
var newLayout = layoutView.formLayoutArray()
144149
newLayout.removeFirst()
@@ -148,6 +153,7 @@ extension SyntaxCollection {
148153
/// Creates a new collection by removing the last element.
149154
///
150155
/// - Returns: A new collection with the last element removed.
156+
@available(*, deprecated, message: "Use CollectionType(node.dropLast())")
151157
public func removingLast() -> Self {
152158
var newLayout = layoutView.formLayoutArray()
153159
newLayout.removeLast()

Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ extension MacroApplication {
497497
}
498498

499499
let newAttributes = attributes.reduce(attributedDecl.attributes ?? .init([])) {
500-
$0.appending(AttributeListSyntax.Element($1))
500+
AttributeListSyntax($0 + [AttributeListSyntax.Element($1)])
501501
}
502502

503503
let newDecl = attributedDecl.with(\.attributes, newAttributes).as(DeclSyntax.self)!

Tests/SwiftOperatorsTest/OperatorTableTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ class ExplicitParenFolder: SyntaxRewriter {
5252
let sequenceExpr = firstNode.expression.as(SequenceExprSyntax.self),
5353
sequenceExpr.elements.count == 3,
5454
let leftOperand = sequenceExpr.elements.first,
55-
let middleExpr = sequenceExpr.elements.removingFirst().first,
55+
let middleExpr = sequenceExpr.elements.dropFirst().first,
5656
let rightOperand =
57-
sequenceExpr.elements.removingFirst().removingFirst().first
57+
sequenceExpr.elements.dropFirst(2).first
5858
else {
5959
return ExprSyntax(node)
6060
}

Tests/SwiftSyntaxMacroExpansionTest/MacroSystemTests.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,20 @@ public struct AddCompletionHandler: PeerMacro {
351351
let newParameterList: FunctionParameterListSyntax
352352
if let lastParam = parameterList.last {
353353
// We need to add a trailing comma to the preceding list.
354-
newParameterList = parameterList.removingLast()
355-
.appending(
354+
let newParameterListElements =
355+
parameterList.dropLast()
356+
+ [
356357
lastParam.with(
357358
\.trailingComma,
358359
.commaToken(trailingTrivia: .space)
359-
)
360-
)
361-
.appending(completionHandlerParam)
360+
),
361+
completionHandlerParam,
362+
]
363+
newParameterList = FunctionParameterListSyntax(newParameterListElements)
362364
} else {
363-
newParameterList = parameterList.appending(completionHandlerParam)
365+
newParameterList = FunctionParameterListSyntax(
366+
parameterList + [completionHandlerParam]
367+
)
364368
}
365369

366370
let callArguments: [String] = parameterList.map { param in

Tests/SwiftSyntaxTest/SyntaxCollectionsTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class SyntaxCollectionsTests: XCTestCase {
2828
integerLiteralElement(0)
2929
])
3030

31-
let newArrayElementList = arrayElementList.appending(integerLiteralElement(1))
31+
let newArrayElementList = ArrayElementListSyntax(arrayElementList + [integerLiteralElement(1)])
3232
XCTAssert(newArrayElementList.kind.isSyntaxCollection)
3333
XCTAssertEqual(newArrayElementList.count, 2)
3434
XCTAssertNotNil(newArrayElementList.child(at: 1))
@@ -41,13 +41,13 @@ public class SyntaxCollectionsTests: XCTestCase {
4141
integerLiteralElement(1)
4242
])
4343

44-
var newArrayElementList = arrayElementList.inserting(integerLiteralElement(0), at: 0)
44+
var newArrayElementList = ArrayElementListSyntax([integerLiteralElement(0)] + arrayElementList)
4545

4646
XCTAssertEqual(newArrayElementList.count, 2)
4747
XCTAssertNotNil(newArrayElementList.child(at: 0))
4848
XCTAssertEqual("\(newArrayElementList.child(at: 0)!)", "0")
4949

50-
newArrayElementList = newArrayElementList.inserting(integerLiteralElement(2), at: 2)
50+
newArrayElementList = ArrayElementListSyntax(newArrayElementList + [integerLiteralElement(2)])
5151

5252
XCTAssertEqual(newArrayElementList.count, 3)
5353
XCTAssertNotNil(newArrayElementList.child(at: 2))
@@ -59,7 +59,7 @@ public class SyntaxCollectionsTests: XCTestCase {
5959
integerLiteralElement(1)
6060
])
6161

62-
let newArrayElementList = arrayElementList.prepending(integerLiteralElement(0))
62+
let newArrayElementList = ArrayElementListSyntax([integerLiteralElement(0)] + arrayElementList)
6363

6464
XCTAssertEqual(newArrayElementList.count, 2)
6565
XCTAssertNotNil(newArrayElementList.child(at: 0))
@@ -72,7 +72,7 @@ public class SyntaxCollectionsTests: XCTestCase {
7272
integerLiteralElement(1),
7373
])
7474

75-
let newArrayElementList = arrayElementList.removingFirst()
75+
let newArrayElementList = ArrayElementListSyntax(arrayElementList.dropFirst())
7676

7777
XCTAssertEqual(newArrayElementList.count, 1)
7878
XCTAssertNotNil(newArrayElementList.child(at: 0))
@@ -85,7 +85,7 @@ public class SyntaxCollectionsTests: XCTestCase {
8585
integerLiteralElement(1),
8686
])
8787

88-
let newArrayElementList = arrayElementList.removingLast()
88+
let newArrayElementList = ArrayElementListSyntax(arrayElementList.dropLast())
8989

9090
XCTAssertEqual(newArrayElementList.count, 1)
9191
XCTAssertNotNil(newArrayElementList.child(at: 0))
@@ -97,7 +97,7 @@ public class SyntaxCollectionsTests: XCTestCase {
9797
integerLiteralElement(0)
9898
])
9999

100-
let newArrayElementList = arrayElementList.removing(childAt: 0)
100+
let newArrayElementList = ArrayElementListSyntax(arrayElementList.dropFirst())
101101

102102
XCTAssertEqual(newArrayElementList.count, 0)
103103
XCTAssertNil(newArrayElementList.child(at: 0))

0 commit comments

Comments
 (0)