Skip to content

Commit 88f19c7

Browse files
committed
Adjustment for removed with<childName> functions
1 parent 7272f19 commit 88f19c7

26 files changed

+103
-102
lines changed

Sources/SwiftFormatCore/LegacyTriviaBehavior.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ private final class LegacyTriviaBehaviorRewriter: SyntaxRewriter {
1717
override func visit(_ token: TokenSyntax) -> TokenSyntax {
1818
var token = token
1919
if let pendingLeadingTrivia = pendingLeadingTrivia {
20-
token = token.withLeadingTrivia(pendingLeadingTrivia + token.leadingTrivia)
20+
token = token.with(\.leadingTrivia, pendingLeadingTrivia + token.leadingTrivia)
2121
self.pendingLeadingTrivia = nil
2222
}
2323
if token.nextToken != nil,
2424
let firstIndexToMove = token.trailingTrivia.firstIndex(where: shouldTriviaPieceBeMoved)
2525
{
2626
pendingLeadingTrivia = Trivia(pieces: Array(token.trailingTrivia[firstIndexToMove...]))
2727
token =
28-
token.withTrailingTrivia(Trivia(pieces: Array(token.trailingTrivia[..<firstIndexToMove])))
28+
token.with(\.trailingTrivia, Trivia(pieces: Array(token.trailingTrivia[..<firstIndexToMove])))
2929
}
3030
return token
3131
}

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3476,7 +3476,7 @@ class CommentMovingRewriter: SyntaxRewriter {
34763476

34773477
override func visit(_ token: TokenSyntax) -> TokenSyntax {
34783478
if let rewrittenTrivia = rewriteTokenTriviaMap[token] {
3479-
return token.withLeadingTrivia(rewrittenTrivia)
3479+
return token.with(\.leadingTrivia, rewrittenTrivia)
34803480
}
34813481
return token
34823482
}

Sources/SwiftFormatRules/AddModifierRewriter.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fileprivate final class AddModifierRewriter: SyntaxRewriter {
3333

3434
// Put accessor keyword before the first modifier keyword in the declaration
3535
let newModifiers = modifiers.prepend(modifier: modifierKeyword)
36-
return DeclSyntax(node.withModifiers(newModifiers))
36+
return DeclSyntax(node.with(\.modifiers, newModifiers))
3737
}
3838

3939
override func visit(_ node: FunctionDeclSyntax) -> DeclSyntax {
@@ -46,7 +46,7 @@ fileprivate final class AddModifierRewriter: SyntaxRewriter {
4646
}
4747
guard modifiers.accessLevelModifier == nil else { return DeclSyntax(node) }
4848
let newModifiers = modifiers.prepend(modifier: modifierKeyword)
49-
return DeclSyntax(node.withModifiers(newModifiers))
49+
return DeclSyntax(node.with(\.modifiers, newModifiers))
5050
}
5151

5252
override func visit(_ node: AssociatedtypeDeclSyntax) -> DeclSyntax {
@@ -59,7 +59,7 @@ fileprivate final class AddModifierRewriter: SyntaxRewriter {
5959
}
6060
guard modifiers.accessLevelModifier == nil else { return DeclSyntax(node) }
6161
let newModifiers = modifiers.prepend(modifier: modifierKeyword)
62-
return DeclSyntax(node.withModifiers(newModifiers))
62+
return DeclSyntax(node.with(\.modifiers, newModifiers))
6363
}
6464

6565
override func visit(_ node: ClassDeclSyntax) -> DeclSyntax {
@@ -72,7 +72,7 @@ fileprivate final class AddModifierRewriter: SyntaxRewriter {
7272
}
7373
guard modifiers.accessLevelModifier == nil else { return DeclSyntax(node) }
7474
let newModifiers = modifiers.prepend(modifier: modifierKeyword)
75-
return DeclSyntax(node.withModifiers(newModifiers))
75+
return DeclSyntax(node.with(\.modifiers, newModifiers))
7676
}
7777

7878
override func visit(_ node: EnumDeclSyntax) -> DeclSyntax {
@@ -85,7 +85,7 @@ fileprivate final class AddModifierRewriter: SyntaxRewriter {
8585
}
8686
guard modifiers.accessLevelModifier == nil else { return DeclSyntax(node) }
8787
let newModifiers = modifiers.prepend(modifier: modifierKeyword)
88-
return DeclSyntax(node.withModifiers(newModifiers))
88+
return DeclSyntax(node.with(\.modifiers, newModifiers))
8989
}
9090

9191
override func visit(_ node: ProtocolDeclSyntax) -> DeclSyntax {
@@ -98,7 +98,7 @@ fileprivate final class AddModifierRewriter: SyntaxRewriter {
9898
}
9999
guard modifiers.accessLevelModifier == nil else { return DeclSyntax(node) }
100100
let newModifiers = modifiers.prepend(modifier: modifierKeyword)
101-
return DeclSyntax(node.withModifiers(newModifiers))
101+
return DeclSyntax(node.with(\.modifiers, newModifiers))
102102
}
103103

104104
override func visit(_ node: StructDeclSyntax) -> DeclSyntax {
@@ -111,7 +111,7 @@ fileprivate final class AddModifierRewriter: SyntaxRewriter {
111111
}
112112
guard modifiers.accessLevelModifier == nil else { return DeclSyntax(node) }
113113
let newModifiers = modifiers.prepend(modifier: modifierKeyword)
114-
return DeclSyntax(node.withModifiers(newModifiers))
114+
return DeclSyntax(node.with(\.modifiers, newModifiers))
115115
}
116116

117117
override func visit(_ node: TypealiasDeclSyntax) -> DeclSyntax {
@@ -124,7 +124,7 @@ fileprivate final class AddModifierRewriter: SyntaxRewriter {
124124
}
125125
guard modifiers.accessLevelModifier == nil else { return DeclSyntax(node) }
126126
let newModifiers = modifiers.prepend(modifier: modifierKeyword)
127-
return DeclSyntax(node.withModifiers(newModifiers))
127+
return DeclSyntax(node.with(\.modifiers, newModifiers))
128128
}
129129

130130
override func visit(_ node: InitializerDeclSyntax) -> DeclSyntax {
@@ -137,7 +137,7 @@ fileprivate final class AddModifierRewriter: SyntaxRewriter {
137137
}
138138
guard modifiers.accessLevelModifier == nil else { return DeclSyntax(node) }
139139
let newModifiers = modifiers.prepend(modifier: modifierKeyword)
140-
return DeclSyntax(node.withModifiers(newModifiers))
140+
return DeclSyntax(node.with(\.modifiers, newModifiers))
141141
}
142142

143143
override func visit(_ node: SubscriptDeclSyntax) -> DeclSyntax {
@@ -150,7 +150,7 @@ fileprivate final class AddModifierRewriter: SyntaxRewriter {
150150
}
151151
guard modifiers.accessLevelModifier == nil else { return DeclSyntax(node) }
152152
let newModifiers = modifiers.prepend(modifier: modifierKeyword)
153-
return DeclSyntax(node.withModifiers(newModifiers))
153+
return DeclSyntax(node.with(\.modifiers, newModifiers))
154154
}
155155

156156
/// Moves trivia in the given node to correct the placement of potentially displaced trivia in the

Sources/SwiftFormatRules/DoNotUseSemicolons.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public final class DoNotUseSemicolons: SyntaxFormatRule {
8787

8888
// This discards any trailingTrivia from the semicolon. That trivia is at most some spaces,
8989
// and the pretty printer adds any necessary spaces so it's safe to discard.
90-
newItem = newItem.withSemicolon(nil)
90+
newItem = newItem.with(\.semicolon, nil)
9191
if idx < node.count - 1 {
9292
diagnose(.removeSemicolonAndMove, on: semicolon)
9393
} else {

Sources/SwiftFormatRules/FileScopedDeclarationPrivacy.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import SwiftSyntax
2424
public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {
2525
public override func visit(_ node: SourceFileSyntax) -> SourceFileSyntax {
2626
let newStatements = rewrittenCodeBlockItems(node.statements)
27-
return node.withStatements(newStatements)
27+
return node.with(\.statements, newStatements)
2828
}
2929

3030
/// Returns a list of code block items equivalent to the given list, but where any file-scoped
@@ -40,7 +40,7 @@ public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {
4040
let newCodeBlockItems = codeBlockItems.map { codeBlockItem -> CodeBlockItemSyntax in
4141
switch codeBlockItem.item {
4242
case .decl(let decl):
43-
return codeBlockItem.withItem(.decl(rewrittenDecl(decl)))
43+
return codeBlockItem.with(\.item, .decl(rewrittenDecl(decl)))
4444
default:
4545
return codeBlockItem
4646
}
@@ -59,43 +59,43 @@ public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {
5959
return DeclSyntax(rewrittenDecl(
6060
functionDecl,
6161
modifiers: functionDecl.modifiers,
62-
factory: functionDecl.withModifiers))
62+
factory: { functionDecl.with(\.modifiers, $0) }))
6363

6464
case .variableDecl(let variableDecl):
6565
return DeclSyntax(rewrittenDecl(
6666
variableDecl,
6767
modifiers: variableDecl.modifiers,
68-
factory: variableDecl.withModifiers))
68+
factory: { variableDecl.with(\.modifiers, $0) }))
6969

7070
case .classDecl(let classDecl):
7171
return DeclSyntax(rewrittenDecl(
7272
classDecl,
7373
modifiers: classDecl.modifiers,
74-
factory: classDecl.withModifiers))
74+
factory: { classDecl.with(\.modifiers, $0) }))
7575

7676
case .structDecl(let structDecl):
7777
return DeclSyntax(rewrittenDecl(
7878
structDecl,
7979
modifiers: structDecl.modifiers,
80-
factory: structDecl.withModifiers))
80+
factory: { structDecl.with(\.modifiers, $0) }))
8181

8282
case .enumDecl(let enumDecl):
8383
return DeclSyntax(rewrittenDecl(
8484
enumDecl,
8585
modifiers: enumDecl.modifiers,
86-
factory: enumDecl.withModifiers))
86+
factory: { enumDecl.with(\.modifiers, $0) }))
8787

8888
case .protocolDecl(let protocolDecl):
8989
return DeclSyntax(rewrittenDecl(
9090
protocolDecl,
9191
modifiers: protocolDecl.modifiers,
92-
factory: protocolDecl.withModifiers))
92+
factory: { protocolDecl.with(\.modifiers, $0) }))
9393

9494
case .typealiasDecl(let typealiasDecl):
9595
return DeclSyntax(rewrittenDecl(
9696
typealiasDecl,
9797
modifiers: typealiasDecl.modifiers,
98-
factory: typealiasDecl.withModifiers))
98+
factory: { typealiasDecl.with(\.modifiers, $0) }))
9999

100100
default:
101101
return decl
@@ -113,12 +113,12 @@ public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {
113113
let newClauses = ifConfigDecl.clauses.map { clause -> IfConfigClauseSyntax in
114114
switch clause.elements {
115115
case .statements(let codeBlockItemList)?:
116-
return clause.withElements(.statements(rewrittenCodeBlockItems(codeBlockItemList)))
116+
return clause.with(\.elements, .statements(rewrittenCodeBlockItems(codeBlockItemList)))
117117
default:
118118
return clause
119119
}
120120
}
121-
return ifConfigDecl.withClauses(IfConfigClauseListSyntax(newClauses))
121+
return ifConfigDecl.with(\.clauses, IfConfigClauseListSyntax(newClauses))
122122
}
123123

124124
/// Returns a rewritten version of the given declaration if its modifier list contains `private`
@@ -161,7 +161,7 @@ public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {
161161
let name = modifier.name
162162
if name.tokenKind == invalidAccess {
163163
diagnose(diagnostic, on: name)
164-
return modifier.withName(name.withKind(validAccess))
164+
return modifier.with(\.name, name.withKind(validAccess))
165165
}
166166
return modifier
167167
}

Sources/SwiftFormatRules/FullyIndirectEnum.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public final class FullyIndirectEnum: SyntaxFormatRule {
4444
return member
4545
}
4646

47-
let newCase = caseMember.withModifiers(modifiers.remove(name: "indirect"))
47+
let newCase = caseMember.with(\.modifiers, modifiers.remove(name: "indirect"))
4848
let formattedCase = formatCase(
4949
unformattedCase: newCase, leadingTrivia: firstModifier.leadingTrivia)
50-
return member.withDecl(DeclSyntax(formattedCase))
50+
return member.with(\.decl, DeclSyntax(formattedCase))
5151
}
5252

5353
// If the `indirect` keyword being added would be the first token in the decl, we need to move
@@ -70,8 +70,8 @@ public final class FullyIndirectEnum: SyntaxFormatRule {
7070
name: TokenSyntax.identifier(
7171
"indirect", leadingTrivia: leadingTrivia, trailingTrivia: .spaces(1)), detail: nil)
7272

73-
let newMemberBlock = node.members.withMembers(MemberDeclListSyntax(newMembers))
74-
return DeclSyntax(newEnumDecl.addModifier(newModifier).withMembers(newMemberBlock))
73+
let newMemberBlock = node.members.with(\.members, MemberDeclListSyntax(newMembers))
74+
return DeclSyntax(newEnumDecl.addModifier(newModifier).with(\.members, newMemberBlock))
7575
}
7676

7777
/// Returns a value indicating whether all enum cases in the given list are indirect.

Sources/SwiftFormatRules/GroupNumericLiterals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public final class GroupNumericLiterals: SyntaxFormatRule {
5959
}
6060

6161
newDigits = isNegative ? "-" + newDigits : newDigits
62-
let result = node.withDigits(
62+
let result = node.with(\.digits,
6363
TokenSyntax.integerLiteral(
6464
newDigits,
6565
leadingTrivia: node.digits.leadingTrivia,

Sources/SwiftFormatRules/NeverForceUnwrap.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public final class NeverForceUnwrap: SyntaxLintRule {
3131

3232
public override func visit(_ node: ForcedValueExprSyntax) -> SyntaxVisitorContinueKind {
3333
guard context.importsXCTest == .doesNotImportXCTest else { return .skipChildren }
34-
diagnose(.doNotForceUnwrap(name: node.expression.withoutTrivia().description), on: node)
34+
diagnose(.doNotForceUnwrap(name: node.expression.trimmedDescription), on: node)
3535
return .skipChildren
3636
}
3737

@@ -41,7 +41,7 @@ public final class NeverForceUnwrap: SyntaxLintRule {
4141
guard context.importsXCTest == .doesNotImportXCTest else { return .skipChildren }
4242
guard let questionOrExclamation = node.questionOrExclamationMark else { return .skipChildren }
4343
guard questionOrExclamation.tokenKind == .exclamationMark else { return .skipChildren }
44-
diagnose(.doNotForceCast(name: node.typeName.withoutTrivia().description), on: node)
44+
diagnose(.doNotForceCast(name: node.typeName.trimmedDescription), on: node)
4545
return .skipChildren
4646
}
4747
}

Sources/SwiftFormatRules/NeverUseImplicitlyUnwrappedOptionals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public final class NeverUseImplicitlyUnwrappedOptionals: SyntaxLintRule {
5959
guard let violation = type.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) else { return }
6060
diagnose(
6161
.doNotUseImplicitUnwrapping(
62-
identifier: violation.wrappedType.withoutTrivia().description), on: type)
62+
identifier: violation.wrappedType.trimmedDescription), on: type)
6363
}
6464
}
6565

Sources/SwiftFormatRules/NoAccessLevelOnExtensionDeclaration.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public final class NoAccessLevelOnExtensionDeclaration: SyntaxFormatRule {
3939
let accessKeywordToAdd: DeclModifierSyntax
4040
if keywordKind == .keyword(.private) {
4141
accessKeywordToAdd
42-
= accessKeyword.withName(accessKeyword.name.withKind(.keyword(.fileprivate)))
42+
= accessKeyword.with(\.name, accessKeyword.name.withKind(.keyword(.fileprivate)))
4343
} else {
4444
accessKeywordToAdd = accessKeyword
4545
}
@@ -52,9 +52,9 @@ public final class NoAccessLevelOnExtensionDeclaration: SyntaxFormatRule {
5252
on: node.extensionKeyword,
5353
token: node.extensionKeyword,
5454
leadingTrivia: accessKeyword.leadingTrivia)
55-
let result = node.withMembers(newMembers)
56-
.withModifiers(modifiers.remove(name: accessKeyword.name.text))
57-
.withExtensionKeyword(newKeyword)
55+
let result = node.with(\.members, newMembers)
56+
.with(\.modifiers, modifiers.remove(name: accessKeyword.name.text))
57+
.with(\.extensionKeyword, newKeyword)
5858
return DeclSyntax(result)
5959

6060
// Internal keyword redundant, delete
@@ -66,8 +66,8 @@ public final class NoAccessLevelOnExtensionDeclaration: SyntaxFormatRule {
6666
on: node.extensionKeyword,
6767
token: node.extensionKeyword,
6868
leadingTrivia: accessKeyword.leadingTrivia)
69-
let result = node.withModifiers(modifiers.remove(name: accessKeyword.name.text))
70-
.withExtensionKeyword(newKeyword)
69+
let result = node.with(\.modifiers, modifiers.remove(name: accessKeyword.name.text))
70+
.with(\.extensionKeyword, newKeyword)
7171
return DeclSyntax(result)
7272

7373
default:
@@ -94,7 +94,7 @@ public final class NoAccessLevelOnExtensionDeclaration: SyntaxFormatRule {
9494
let newDecl = addModifier(declaration: member, modifierKeyword: formattedKeyword)
9595
.as(DeclSyntax.self)
9696
else { continue }
97-
newMembers.append(memberItem.withDecl(newDecl))
97+
newMembers.append(memberItem.with(\.decl, newDecl))
9898
}
9999
return MemberDeclListSyntax(newMembers)
100100
}

Sources/SwiftFormatRules/NoAssignmentInExpressions.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ public final class NoAssignmentInExpressions: SyntaxFormatRule {
5959
item: .expr(ExprSyntax(assignmentExpr)),
6060
semicolon: nil
6161
)
62-
.withLeadingTrivia(
62+
.with(\.leadingTrivia,
6363
(returnStmt.leadingTrivia ?? []) + (assignmentExpr.leadingTrivia ?? []))
64-
.withTrailingTrivia([]))
64+
.with(\.trailingTrivia, []))
6565
newItems.append(
6666
CodeBlockItemSyntax(
67-
item: .stmt(StmtSyntax(returnStmt.withExpression(nil))),
67+
item: .stmt(StmtSyntax(returnStmt.with(\.expression, nil))),
6868
semicolon: nil
6969
)
70-
.withLeadingTrivia([.newlines(1)])
71-
.withTrailingTrivia(returnStmt.trailingTrivia?.withoutLeadingSpaces() ?? []))
70+
.with(\.leadingTrivia, [.newlines(1)])
71+
.with(\.trailingTrivia, returnStmt.trailingTrivia?.withoutLeadingSpaces() ?? []))
7272

7373
default:
7474
newItems.append(newItem)

Sources/SwiftFormatRules/NoCasesWithOnlyFallthrough.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,25 +178,26 @@ public final class NoCasesWithOnlyFallthrough: SyntaxFormatRule {
178178
// more items.
179179
newCaseItems.append(contentsOf: label.caseItems.dropLast())
180180
newCaseItems.append(
181-
label.caseItems.last!.withTrailingComma(
181+
label.caseItems.last!.with(
182+
\.trailingComma,
182183
TokenSyntax.commaToken(trailingTrivia: .spaces(1))))
183184

184185
// Diagnose the cases being collapsed. We do this for all but the last one in the array; the
185186
// last one isn't diagnosed because it will contain the body that applies to all the previous
186187
// cases.
187-
diagnose(.collapseCase(name: label.caseItems.withoutTrivia().description), on: label)
188+
diagnose(.collapseCase(name: label.caseItems.trimmedDescription), on: label)
188189
}
189190
newCaseItems.append(contentsOf: labels.last!.caseItems)
190191

191-
let newCase = cases.last!.withLabel(.case(
192-
labels.last!.withCaseItems(CaseItemListSyntax(newCaseItems))))
192+
let newCase = cases.last!.with(\.label, .case(
193+
labels.last!.with(\.caseItems, CaseItemListSyntax(newCaseItems))))
193194

194195
// Only the first violation case can have displaced trivia, because any non-whitespace
195196
// trivia in the other violation cases would've prevented collapsing.
196197
if let displacedLeadingTrivia = cases.first!.leadingTrivia?.withoutLastLine() {
197198
let existingLeadingTrivia = newCase.leadingTrivia ?? []
198199
let mergedLeadingTrivia = displacedLeadingTrivia + existingLeadingTrivia
199-
return newCase.withLeadingTrivia(mergedLeadingTrivia)
200+
return newCase.with(\.leadingTrivia, mergedLeadingTrivia)
200201
} else {
201202
return newCase
202203
}

Sources/SwiftFormatRules/NoEmptyTrailingClosureParentheses.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public final class NoEmptyTrailingClosureParentheses: SyntaxFormatRule {
2929
{
3030
return super.visit(node)
3131
}
32-
guard let name = node.calledExpression.lastToken?.withoutTrivia() else {
32+
guard let name = node.calledExpression.lastToken?.with(\.leadingTrivia, []).with(\.trailingTrivia, []) else {
3333
return super.visit(node)
3434
}
3535

@@ -45,8 +45,8 @@ public final class NoEmptyTrailingClosureParentheses: SyntaxFormatRule {
4545
token: rewrittenCalledExpr.lastToken,
4646
trailingTrivia: .spaces(1))
4747
let formattedClosure = visit(trailingClosure).as(ClosureExprSyntax.self)
48-
let result = node.withLeftParen(nil).withRightParen(nil).withCalledExpression(formattedExp)
49-
.withTrailingClosure(formattedClosure)
48+
let result = node.with(\.leftParen, nil).with(\.rightParen, nil).with(\.calledExpression, formattedExp)
49+
.with(\.trailingClosure, formattedClosure)
5050
return ExprSyntax(result)
5151
}
5252
}

0 commit comments

Comments
 (0)