Skip to content

Adjustment for removed with<childName> functions #477

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 26, 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
4 changes: 2 additions & 2 deletions Sources/SwiftFormatCore/LegacyTriviaBehavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ private final class LegacyTriviaBehaviorRewriter: SyntaxRewriter {
override func visit(_ token: TokenSyntax) -> TokenSyntax {
var token = token
if let pendingLeadingTrivia = pendingLeadingTrivia {
token = token.withLeadingTrivia(pendingLeadingTrivia + token.leadingTrivia)
token = token.with(\.leadingTrivia, pendingLeadingTrivia + token.leadingTrivia)
self.pendingLeadingTrivia = nil
}
if token.nextToken != nil,
let firstIndexToMove = token.trailingTrivia.firstIndex(where: shouldTriviaPieceBeMoved)
{
pendingLeadingTrivia = Trivia(pieces: Array(token.trailingTrivia[firstIndexToMove...]))
token =
token.withTrailingTrivia(Trivia(pieces: Array(token.trailingTrivia[..<firstIndexToMove])))
token.with(\.trailingTrivia, Trivia(pieces: Array(token.trailingTrivia[..<firstIndexToMove])))
}
return token
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3483,7 +3483,7 @@ class CommentMovingRewriter: SyntaxRewriter {

override func visit(_ token: TokenSyntax) -> TokenSyntax {
if let rewrittenTrivia = rewriteTokenTriviaMap[token] {
return token.withLeadingTrivia(rewrittenTrivia)
return token.with(\.leadingTrivia, rewrittenTrivia)
}
return token
}
Expand Down
20 changes: 10 additions & 10 deletions Sources/SwiftFormatRules/AddModifierRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fileprivate final class AddModifierRewriter: SyntaxRewriter {

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

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

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

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

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

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

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

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

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

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

/// Moves trivia in the given node to correct the placement of potentially displaced trivia in the
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatRules/DoNotUseSemicolons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public final class DoNotUseSemicolons: SyntaxFormatRule {

// This discards any trailingTrivia from the semicolon. That trivia is at most some spaces,
// and the pretty printer adds any necessary spaces so it's safe to discard.
newItem = newItem.withSemicolon(nil)
newItem = newItem.with(\.semicolon, nil)
if idx < node.count - 1 {
diagnose(.removeSemicolonAndMove, on: semicolon)
} else {
Expand Down
24 changes: 12 additions & 12 deletions Sources/SwiftFormatRules/FileScopedDeclarationPrivacy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import SwiftSyntax
public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {
public override func visit(_ node: SourceFileSyntax) -> SourceFileSyntax {
let newStatements = rewrittenCodeBlockItems(node.statements)
return node.withStatements(newStatements)
return node.with(\.statements, newStatements)
}

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

case .variableDecl(let variableDecl):
return DeclSyntax(rewrittenDecl(
variableDecl,
modifiers: variableDecl.modifiers,
factory: variableDecl.withModifiers))
factory: { variableDecl.with(\.modifiers, $0) }))

case .classDecl(let classDecl):
return DeclSyntax(rewrittenDecl(
classDecl,
modifiers: classDecl.modifiers,
factory: classDecl.withModifiers))
factory: { classDecl.with(\.modifiers, $0) }))

case .structDecl(let structDecl):
return DeclSyntax(rewrittenDecl(
structDecl,
modifiers: structDecl.modifiers,
factory: structDecl.withModifiers))
factory: { structDecl.with(\.modifiers, $0) }))

case .enumDecl(let enumDecl):
return DeclSyntax(rewrittenDecl(
enumDecl,
modifiers: enumDecl.modifiers,
factory: enumDecl.withModifiers))
factory: { enumDecl.with(\.modifiers, $0) }))

case .protocolDecl(let protocolDecl):
return DeclSyntax(rewrittenDecl(
protocolDecl,
modifiers: protocolDecl.modifiers,
factory: protocolDecl.withModifiers))
factory: { protocolDecl.with(\.modifiers, $0) }))

case .typealiasDecl(let typealiasDecl):
return DeclSyntax(rewrittenDecl(
typealiasDecl,
modifiers: typealiasDecl.modifiers,
factory: typealiasDecl.withModifiers))
factory: { typealiasDecl.with(\.modifiers, $0) }))

default:
return decl
Expand All @@ -113,12 +113,12 @@ public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {
let newClauses = ifConfigDecl.clauses.map { clause -> IfConfigClauseSyntax in
switch clause.elements {
case .statements(let codeBlockItemList)?:
return clause.withElements(.statements(rewrittenCodeBlockItems(codeBlockItemList)))
return clause.with(\.elements, .statements(rewrittenCodeBlockItems(codeBlockItemList)))
default:
return clause
}
}
return ifConfigDecl.withClauses(IfConfigClauseListSyntax(newClauses))
return ifConfigDecl.with(\.clauses, IfConfigClauseListSyntax(newClauses))
}

/// Returns a rewritten version of the given declaration if its modifier list contains `private`
Expand Down Expand Up @@ -161,7 +161,7 @@ public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {
let name = modifier.name
if name.tokenKind == invalidAccess {
diagnose(diagnostic, on: name)
return modifier.withName(name.withKind(validAccess))
return modifier.with(\.name, name.withKind(validAccess))
}
return modifier
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftFormatRules/FullyIndirectEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public final class FullyIndirectEnum: SyntaxFormatRule {
return member
}

let newCase = caseMember.withModifiers(modifiers.remove(name: "indirect"))
let newCase = caseMember.with(\.modifiers, modifiers.remove(name: "indirect"))
let formattedCase = formatCase(
unformattedCase: newCase, leadingTrivia: firstModifier.leadingTrivia)
return member.withDecl(DeclSyntax(formattedCase))
return member.with(\.decl, DeclSyntax(formattedCase))
}

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

let newMemberBlock = node.members.withMembers(MemberDeclListSyntax(newMembers))
return DeclSyntax(newEnumDecl.addModifier(newModifier).withMembers(newMemberBlock))
let newMemberBlock = node.members.with(\.members, MemberDeclListSyntax(newMembers))
return DeclSyntax(newEnumDecl.addModifier(newModifier).with(\.members, newMemberBlock))
}

/// Returns a value indicating whether all enum cases in the given list are indirect.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatRules/GroupNumericLiterals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public final class GroupNumericLiterals: SyntaxFormatRule {
}

newDigits = isNegative ? "-" + newDigits : newDigits
let result = node.withDigits(
let result = node.with(\.digits,
TokenSyntax.integerLiteral(
newDigits,
leadingTrivia: node.digits.leadingTrivia,
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftFormatRules/NeverForceUnwrap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class NeverForceUnwrap: SyntaxLintRule {

public override func visit(_ node: ForcedValueExprSyntax) -> SyntaxVisitorContinueKind {
guard context.importsXCTest == .doesNotImportXCTest else { return .skipChildren }
diagnose(.doNotForceUnwrap(name: node.expression.withoutTrivia().description), on: node)
diagnose(.doNotForceUnwrap(name: node.expression.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description), on: node)
return .skipChildren
}

Expand All @@ -41,7 +41,7 @@ public final class NeverForceUnwrap: SyntaxLintRule {
guard context.importsXCTest == .doesNotImportXCTest else { return .skipChildren }
guard let questionOrExclamation = node.questionOrExclamationMark else { return .skipChildren }
guard questionOrExclamation.tokenKind == .exclamationMark else { return .skipChildren }
diagnose(.doNotForceCast(name: node.typeName.withoutTrivia().description), on: node)
diagnose(.doNotForceCast(name: node.typeName.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description), on: node)
return .skipChildren
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public final class NeverUseImplicitlyUnwrappedOptionals: SyntaxLintRule {
guard let violation = type.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) else { return }
diagnose(
.doNotUseImplicitUnwrapping(
identifier: violation.wrappedType.withoutTrivia().description), on: type)
identifier: violation.wrappedType.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description), on: type)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class NoAccessLevelOnExtensionDeclaration: SyntaxFormatRule {
let accessKeywordToAdd: DeclModifierSyntax
if keywordKind == .keyword(.private) {
accessKeywordToAdd
= accessKeyword.withName(accessKeyword.name.withKind(.keyword(.fileprivate)))
= accessKeyword.with(\.name, accessKeyword.name.withKind(.keyword(.fileprivate)))
} else {
accessKeywordToAdd = accessKeyword
}
Expand All @@ -52,9 +52,9 @@ public final class NoAccessLevelOnExtensionDeclaration: SyntaxFormatRule {
on: node.extensionKeyword,
token: node.extensionKeyword,
leadingTrivia: accessKeyword.leadingTrivia)
let result = node.withMembers(newMembers)
.withModifiers(modifiers.remove(name: accessKeyword.name.text))
.withExtensionKeyword(newKeyword)
let result = node.with(\.members, newMembers)
.with(\.modifiers, modifiers.remove(name: accessKeyword.name.text))
.with(\.extensionKeyword, newKeyword)
return DeclSyntax(result)

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

default:
Expand All @@ -94,7 +94,7 @@ public final class NoAccessLevelOnExtensionDeclaration: SyntaxFormatRule {
let newDecl = addModifier(declaration: member, modifierKeyword: formattedKeyword)
.as(DeclSyntax.self)
else { continue }
newMembers.append(memberItem.withDecl(newDecl))
newMembers.append(memberItem.with(\.decl, newDecl))
}
return MemberDeclListSyntax(newMembers)
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftFormatRules/NoAssignmentInExpressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ public final class NoAssignmentInExpressions: SyntaxFormatRule {
item: .expr(ExprSyntax(assignmentExpr)),
semicolon: nil
)
.withLeadingTrivia(
.with(\.leadingTrivia,
(returnStmt.leadingTrivia ?? []) + (assignmentExpr.leadingTrivia ?? []))
.withTrailingTrivia([]))
.with(\.trailingTrivia, []))
newItems.append(
CodeBlockItemSyntax(
item: .stmt(StmtSyntax(returnStmt.withExpression(nil))),
item: .stmt(StmtSyntax(returnStmt.with(\.expression, nil))),
semicolon: nil
)
.withLeadingTrivia([.newlines(1)])
.withTrailingTrivia(returnStmt.trailingTrivia?.withoutLeadingSpaces() ?? []))
.with(\.leadingTrivia, [.newlines(1)])
.with(\.trailingTrivia, returnStmt.trailingTrivia?.withoutLeadingSpaces() ?? []))

default:
newItems.append(newItem)
Expand Down
11 changes: 6 additions & 5 deletions Sources/SwiftFormatRules/NoCasesWithOnlyFallthrough.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,25 +178,26 @@ public final class NoCasesWithOnlyFallthrough: SyntaxFormatRule {
// more items.
newCaseItems.append(contentsOf: label.caseItems.dropLast())
newCaseItems.append(
label.caseItems.last!.withTrailingComma(
label.caseItems.last!.with(
\.trailingComma,
TokenSyntax.commaToken(trailingTrivia: .spaces(1))))

// Diagnose the cases being collapsed. We do this for all but the last one in the array; the
// last one isn't diagnosed because it will contain the body that applies to all the previous
// cases.
diagnose(.collapseCase(name: label.caseItems.withoutTrivia().description), on: label)
diagnose(.collapseCase(name: label.caseItems.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description), on: label)
}
newCaseItems.append(contentsOf: labels.last!.caseItems)

let newCase = cases.last!.withLabel(.case(
labels.last!.withCaseItems(CaseItemListSyntax(newCaseItems))))
let newCase = cases.last!.with(\.label, .case(
labels.last!.with(\.caseItems, CaseItemListSyntax(newCaseItems))))

// Only the first violation case can have displaced trivia, because any non-whitespace
// trivia in the other violation cases would've prevented collapsing.
if let displacedLeadingTrivia = cases.first!.leadingTrivia?.withoutLastLine() {
let existingLeadingTrivia = newCase.leadingTrivia ?? []
let mergedLeadingTrivia = displacedLeadingTrivia + existingLeadingTrivia
return newCase.withLeadingTrivia(mergedLeadingTrivia)
return newCase.with(\.leadingTrivia, mergedLeadingTrivia)
} else {
return newCase
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class NoEmptyTrailingClosureParentheses: SyntaxFormatRule {
{
return super.visit(node)
}
guard let name = node.calledExpression.lastToken?.withoutTrivia() else {
guard let name = node.calledExpression.lastToken?.with(\.leadingTrivia, []).with(\.trailingTrivia, []) else {
return super.visit(node)
}

Expand All @@ -45,8 +45,8 @@ public final class NoEmptyTrailingClosureParentheses: SyntaxFormatRule {
token: rewrittenCalledExpr.lastToken,
trailingTrivia: .spaces(1))
let formattedClosure = visit(trailingClosure).as(ClosureExprSyntax.self)
let result = node.withLeftParen(nil).withRightParen(nil).withCalledExpression(formattedExp)
.withTrailingClosure(formattedClosure)
let result = node.with(\.leftParen, nil).with(\.rightParen, nil).with(\.calledExpression, formattedExp)
.with(\.trailingClosure, formattedClosure)
return ExprSyntax(result)
}
}
Expand Down
Loading