Skip to content

Commit 90bd987

Browse files
authored
Merge pull request #433 from ahoppen/ahoppen/update-for-restricted-rewriter
Update for the more restrictive return types in SyntaxRewriter
2 parents 63f4001 + 0880f19 commit 90bd987

13 files changed

+50
-53
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3583,23 +3583,23 @@ class CommentMovingRewriter: SyntaxRewriter {
35833583
/// Map of tokens to alternate trivia to use as the token's leading trivia.
35843584
var rewriteTokenTriviaMap: [TokenSyntax: Trivia] = [:]
35853585

3586-
override func visit(_ node: SourceFileSyntax) -> Syntax {
3586+
override func visit(_ node: SourceFileSyntax) -> SourceFileSyntax {
35873587
if shouldFormatterIgnore(file: node) {
3588-
return Syntax(node)
3588+
return node
35893589
}
35903590
return super.visit(node)
35913591
}
35923592

3593-
override func visit(_ node: CodeBlockItemSyntax) -> Syntax {
3593+
override func visit(_ node: CodeBlockItemSyntax) -> CodeBlockItemSyntax {
35943594
if shouldFormatterIgnore(node: Syntax(node)) {
3595-
return Syntax(node)
3595+
return node
35963596
}
35973597
return super.visit(node)
35983598
}
35993599

3600-
override func visit(_ node: MemberDeclListItemSyntax) -> Syntax {
3600+
override func visit(_ node: MemberDeclListItemSyntax) -> MemberDeclListItemSyntax {
36013601
if shouldFormatterIgnore(node: Syntax(node)) {
3602-
return Syntax(node)
3602+
return node
36033603
}
36043604
return super.visit(node)
36053605
}

Sources/SwiftFormatRules/DoNotUseSemicolons.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ public final class DoNotUseSemicolons: SyntaxFormatRule {
9898
return nodeCreator(newItems)
9999
}
100100

101-
public override func visit(_ node: CodeBlockItemListSyntax) -> Syntax {
102-
return Syntax(nodeByRemovingSemicolons(from: node, nodeCreator: CodeBlockItemListSyntax.init))
101+
public override func visit(_ node: CodeBlockItemListSyntax) -> CodeBlockItemListSyntax {
102+
return nodeByRemovingSemicolons(from: node, nodeCreator: CodeBlockItemListSyntax.init)
103103
}
104104

105-
public override func visit(_ node: MemberDeclListSyntax) -> Syntax {
106-
return Syntax(nodeByRemovingSemicolons(from: node, nodeCreator: MemberDeclListSyntax.init))
105+
public override func visit(_ node: MemberDeclListSyntax) -> MemberDeclListSyntax {
106+
return nodeByRemovingSemicolons(from: node, nodeCreator: MemberDeclListSyntax.init)
107107
}
108108
}
109109

Sources/SwiftFormatRules/FileScopedDeclarationPrivacy.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import SwiftSyntax
2222
/// Format: File-scoped declarations that have formal access opposite to the desired access level in
2323
/// the formatter's configuration will have their access level changed.
2424
public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {
25-
public override func visit(_ node: SourceFileSyntax) -> Syntax {
25+
public override func visit(_ node: SourceFileSyntax) -> SourceFileSyntax {
2626
let newStatements = rewrittenCodeBlockItems(node.statements)
27-
return Syntax(node.withStatements(newStatements))
27+
return node.withStatements(newStatements)
2828
}
2929

3030
/// Returns a list of code block items equivalent to the given list, but where any file-scoped

Sources/SwiftFormatRules/NoCasesWithOnlyFallthrough.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import SwiftSyntax
2121
/// `default`; in that case, the fallthrough `case` is deleted.
2222
public final class NoCasesWithOnlyFallthrough: SyntaxFormatRule {
2323

24-
public override func visit(_ node: SwitchCaseListSyntax) -> Syntax {
24+
public override func visit(_ node: SwitchCaseListSyntax) -> SwitchCaseListSyntax {
2525
var newChildren: [Syntax] = []
2626
var fallthroughOnlyCases: [SwitchCaseSyntax] = []
2727

2828
/// Flushes any un-collapsed violations to the new cases list.
2929
func flushViolations() {
3030
fallthroughOnlyCases.forEach {
31-
newChildren.append(super.visit($0))
31+
newChildren.append(Syntax(super.visit($0)))
3232
}
3333
fallthroughOnlyCases.removeAll()
3434
}
@@ -50,14 +50,14 @@ public final class NoCasesWithOnlyFallthrough: SyntaxFormatRule {
5050
guard !fallthroughOnlyCases.isEmpty else {
5151
// If there are no violations recorded, just append the case. There's nothing we can try
5252
// to merge into it.
53-
newChildren.append(visit(switchCase))
53+
newChildren.append(Syntax(visit(switchCase)))
5454
continue
5555
}
5656

5757
if canMergeWithPreviousCases(switchCase) {
5858
// If the current case can be merged with the ones before it, merge them all, leaving no
5959
// `fallthrough`-only cases behind.
60-
newChildren.append(visit(mergedCases(fallthroughOnlyCases + [switchCase])))
60+
newChildren.append(Syntax(visit(mergedCases(fallthroughOnlyCases + [switchCase]))))
6161
} else {
6262
// If the current case can't be merged with the ones before it, merge the previous ones
6363
// into a single `fallthrough`-only case and then append the current one. This could
@@ -71,8 +71,8 @@ public final class NoCasesWithOnlyFallthrough: SyntaxFormatRule {
7171
// the program's behavior.
7272
// 3. The current case is `@unknown default`, which can't be merged notwithstanding the
7373
// side-effect issues discussed above.
74-
newChildren.append(visit(mergedCases(fallthroughOnlyCases)))
75-
newChildren.append(visit(switchCase))
74+
newChildren.append(Syntax(visit(mergedCases(fallthroughOnlyCases))))
75+
newChildren.append(Syntax(visit(switchCase)))
7676
}
7777

7878
fallthroughOnlyCases.removeAll()
@@ -83,7 +83,7 @@ public final class NoCasesWithOnlyFallthrough: SyntaxFormatRule {
8383
// anything.
8484
flushViolations()
8585

86-
return Syntax(SwitchCaseListSyntax(newChildren))
86+
return SwitchCaseListSyntax(newChildren)
8787
}
8888

8989
/// Returns true if this case can definitely be merged with any that come before it.

Sources/SwiftFormatRules/NoLabelsInCasePatterns.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import SwiftSyntax
2323
///
2424
/// Format: Redundant labels in case patterns are removed.
2525
public final class NoLabelsInCasePatterns: SyntaxFormatRule {
26-
public override func visit(_ node: SwitchCaseLabelSyntax) -> Syntax {
26+
public override func visit(_ node: SwitchCaseLabelSyntax) -> SwitchCaseLabelSyntax {
2727
var newCaseItems: [CaseItemSyntax] = []
2828
for item in node.caseItems {
2929
guard let expPat = item.pattern.as(ExpressionPatternSyntax.self) else {
@@ -66,7 +66,7 @@ public final class NoLabelsInCasePatterns: SyntaxFormatRule {
6666
newCaseItems.append(newItem)
6767
}
6868
let newCaseItemList = CaseItemListSyntax(newCaseItems)
69-
return Syntax(node.withCaseItems(newCaseItemList))
69+
return node.withCaseItems(newCaseItemList)
7070
}
7171
}
7272

Sources/SwiftFormatRules/NoParensAroundConditions.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,23 @@ public final class NoParensAroundConditions: SyntaxFormatRule {
5353
}
5454

5555
public override func visit(_ node: IfStmtSyntax) -> StmtSyntax {
56-
let conditions = visit(node.conditions).as(ConditionElementListSyntax.self)!
56+
let conditions = visit(node.conditions)
5757
var result = node.withIfKeyword(node.ifKeyword.withOneTrailingSpace())
5858
.withConditions(conditions)
59-
.withBody(CodeBlockSyntax(visit(node.body)))
59+
.withBody(visit(node.body))
6060
if let elseBody = node.elseBody {
6161
result = result.withElseBody(visit(elseBody))
6262
}
6363
return StmtSyntax(result)
6464
}
6565

66-
public override func visit(_ node: ConditionElementSyntax) -> Syntax {
66+
public override func visit(_ node: ConditionElementSyntax) -> ConditionElementSyntax {
6767
guard let tup = node.condition.as(TupleExprSyntax.self),
6868
tup.elementList.firstAndOnly != nil
6969
else {
7070
return super.visit(node)
7171
}
72-
return Syntax(node.withCondition(Syntax(extractExpr(tup))))
72+
return node.withCondition(Syntax(extractExpr(tup)))
7373
}
7474

7575
/// FIXME(hbh): Parsing for SwitchStmtSyntax is not implemented.
@@ -80,7 +80,7 @@ public final class NoParensAroundConditions: SyntaxFormatRule {
8080
return super.visit(node)
8181
}
8282
return StmtSyntax(
83-
node.withExpression(extractExpr(tup)).withCases(SwitchCaseListSyntax(visit(node.cases))))
83+
node.withExpression(extractExpr(tup)).withCases(visit(node.cases)))
8484
}
8585

8686
public override func visit(_ node: RepeatWhileStmtSyntax) -> StmtSyntax {
@@ -91,7 +91,7 @@ public final class NoParensAroundConditions: SyntaxFormatRule {
9191
}
9292
let newNode = node.withCondition(extractExpr(tup))
9393
.withWhileKeyword(node.whileKeyword.withOneTrailingSpace())
94-
.withBody(CodeBlockSyntax(visit(node.body)))
94+
.withBody(visit(node.body))
9595
return StmtSyntax(newNode)
9696
}
9797
}

Sources/SwiftFormatRules/NoVoidReturnOnFunctionSignature.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ public final class NoVoidReturnOnFunctionSignature: SyntaxFormatRule {
2323
/// Remove the `-> Void` return type for function signatures. Do not remove
2424
/// it for closure signatures, because that may introduce an ambiguity when closure signatures
2525
/// are inferred.
26-
public override func visit(_ node: FunctionSignatureSyntax) -> Syntax {
26+
public override func visit(_ node: FunctionSignatureSyntax) -> FunctionSignatureSyntax {
2727
if let ret = node.output?.returnType.as(SimpleTypeIdentifierSyntax.self), ret.name.text == "Void" {
2828
diagnose(.removeRedundantReturn("Void"), on: ret)
29-
return Syntax(node.withOutput(nil))
29+
return node.withOutput(nil)
3030
}
3131
if let tup = node.output?.returnType.as(TupleTypeSyntax.self), tup.elements.isEmpty {
3232
diagnose(.removeRedundantReturn("()"), on: tup)
33-
return Syntax(node.withOutput(nil))
33+
return node.withOutput(nil)
3434
}
35-
return Syntax(node)
35+
return node
3636
}
3737
}
3838

Sources/SwiftFormatRules/OneVariableDeclarationPerLine.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import SwiftSyntax
2323
/// split into multiple declarations, each declaring one of the variables, as
2424
/// long as the result would still be syntactically valid.
2525
public final class OneVariableDeclarationPerLine: SyntaxFormatRule {
26-
public override func visit(_ node: CodeBlockItemListSyntax) -> Syntax {
26+
public override func visit(_ node: CodeBlockItemListSyntax) -> CodeBlockItemListSyntax {
2727
guard node.contains(where: codeBlockItemHasMultipleVariableBindings) else {
2828
return super.visit(node)
2929
}
@@ -36,7 +36,7 @@ public final class OneVariableDeclarationPerLine: SyntaxFormatRule {
3636
// It's not a variable declaration with multiple bindings, so visit it
3737
// recursively (in case it's something that contains bindings that need
3838
// to be split) but otherwise do nothing.
39-
let newItem = super.visit(codeBlockItem).as(CodeBlockItemSyntax.self)!
39+
let newItem = super.visit(codeBlockItem)
4040
newItems.append(newItem)
4141
continue
4242
}
@@ -57,7 +57,7 @@ public final class OneVariableDeclarationPerLine: SyntaxFormatRule {
5757
newItems.append(contentsOf: splitter.nodes(bySplitting: visitedDecl))
5858
}
5959

60-
return Syntax(CodeBlockItemListSyntax(newItems))
60+
return CodeBlockItemListSyntax(newItems)
6161
}
6262

6363
/// Returns true if the given `CodeBlockItemSyntax` contains a `let` or `var`

Sources/SwiftFormatRules/OrderedImports.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import SwiftSyntax
2525
/// Format: Imports will be reordered and grouped at the top of the file.
2626
public final class OrderedImports: SyntaxFormatRule {
2727

28-
public override func visit(_ node: SourceFileSyntax) -> Syntax {
28+
public override func visit(_ node: SourceFileSyntax) -> SourceFileSyntax {
2929
let lines = generateLines(codeBlockItemList: node.statements, context: context)
3030

3131
// Stores the formatted and sorted lines that will be used to reconstruct the list of code block
@@ -134,7 +134,7 @@ public final class OrderedImports: SyntaxFormatRule {
134134
let newNode = node.withStatements(
135135
CodeBlockItemListSyntax(convertToCodeBlockItems(lines: formattedLines))
136136
)
137-
return Syntax(newNode)
137+
return newNode
138138
}
139139

140140
/// Raise lint errors if the different import types appear in the wrong order, and if import

Sources/SwiftFormatRules/ReturnVoidInsteadOfEmptyTuple.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
4141

4242
// Make sure that function types nested in the argument list are also rewritten (for example,
4343
// `(Int -> ()) -> ()` should become `(Int -> Void) -> Void`).
44-
let arguments = visit(node.arguments).as(TupleTypeElementListSyntax.self)!
44+
let arguments = visit(node.arguments)
4545
let voidKeyword = makeVoidIdentifierType(toReplace: returnType)
4646
return TypeSyntax(node.withArguments(arguments).withReturnType(TypeSyntax(voidKeyword)))
4747
}
4848

49-
public override func visit(_ node: ClosureSignatureSyntax) -> Syntax {
49+
public override func visit(_ node: ClosureSignatureSyntax) -> ClosureSignatureSyntax {
5050
guard let output = node.output,
5151
let returnType = output.returnType.as(TupleTypeSyntax.self),
5252
returnType.elements.count == 0
@@ -69,14 +69,14 @@ public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
6969
// If the closure input is a complete parameter clause (variables and types), make sure that
7070
// nested function types are also rewritten (for example, `label: (Int -> ()) -> ()` should
7171
// become `label: (Int -> Void) -> Void`).
72-
input = visit(parameterClause)
72+
input = Syntax(visit(parameterClause))
7373
} else {
7474
// Otherwise, it's a simple signature (just variable names, no types), so there is nothing to
7575
// rewrite.
7676
input = node.input
7777
}
7878
let voidKeyword = makeVoidIdentifierType(toReplace: returnType)
79-
return Syntax(node.withInput(input).withOutput(output.withReturnType(TypeSyntax(voidKeyword))))
79+
return node.withInput(input).withOutput(output.withReturnType(TypeSyntax(voidKeyword)))
8080
}
8181

8282
/// Returns a value indicating whether the leading trivia of the given token contained any

Sources/SwiftFormatRules/UseEarlyExits.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,10 @@ public final class UseEarlyExits: SyntaxFormatRule {
4848
/// be enabled by default.
4949
public override class var isOptIn: Bool { return true }
5050

51-
public override func visit(_ node: CodeBlockItemListSyntax) -> Syntax {
51+
public override func visit(_ node: CodeBlockItemListSyntax) -> CodeBlockItemListSyntax {
5252
// Continue recursing down the tree first, so that any nested/child nodes get transformed first.
53-
let nodeAfterTransformingChildren = super.visit(node)
54-
guard let codeBlockItems = nodeAfterTransformingChildren.as(CodeBlockItemListSyntax.self) else {
55-
return nodeAfterTransformingChildren
56-
}
57-
53+
let codeBlockItems = super.visit(node)
54+
5855
let result = CodeBlockItemListSyntax(
5956
codeBlockItems.flatMap { (codeBlockItem: CodeBlockItemSyntax) -> [CodeBlockItemSyntax] in
6057
// The `elseBody` of an `IfStmtSyntax` will be a `CodeBlockSyntax` if it's an `else` block,
@@ -84,7 +81,7 @@ public final class UseEarlyExits: SyntaxFormatRule {
8481
CodeBlockItemSyntax(item: Syntax(trueBlock), semicolon: nil, errorTokens: nil),
8582
]
8683
})
87-
return Syntax(result)
84+
return result
8885
}
8986

9087
/// Returns true if the last statement in the given code block is one that will cause an early

Sources/SwiftFormatRules/UseShorthandTypeNames.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
4040

4141
// Ensure that all arguments in the clause are shortened and in the expected format by visiting
4242
// the argument list, first.
43-
let genericArgumentList = visit(genericArgumentClause.arguments).as(GenericArgumentListSyntax.self)!
43+
let genericArgumentList = visit(genericArgumentClause.arguments)
4444

4545
let (leadingTrivia, trailingTrivia) = boundaryTrivia(around: Syntax(node))
4646
let newNode: TypeSyntax?
@@ -125,7 +125,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
125125
// Ensure that all arguments in the clause are shortened and in the expected format by visiting
126126
// the argument list, first.
127127
let genericArgumentList =
128-
visit(node.genericArgumentClause.arguments).as(GenericArgumentListSyntax.self)!
128+
visit(node.genericArgumentClause.arguments)
129129

130130
let (leadingTrivia, trailingTrivia) = boundaryTrivia(around: Syntax(node))
131131
let newNode: ExprSyntax?
@@ -371,7 +371,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
371371
// the identifier and the generic arguments. Otherwise, we can return just the
372372
// `IdentifierExpr` itself.
373373
if let genericArgumentClause = simpleTypeIdentifier.genericArgumentClause {
374-
let newGenericArgumentClause = visit(genericArgumentClause).as(GenericArgumentClauseSyntax.self)!
374+
let newGenericArgumentClause = visit(genericArgumentClause)
375375
let result = SpecializeExprSyntax(
376376
expression: ExprSyntax(identifierExpr),
377377
genericArgumentClause: newGenericArgumentClause)

Sources/SwiftFormatRules/UseSingleLinePropertyGetter.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import SwiftSyntax
2020
/// Format: Explicit `get` blocks are rendered implicit by removing the `get`.
2121
public final class UseSingleLinePropertyGetter: SyntaxFormatRule {
2222

23-
public override func visit(_ node: PatternBindingSyntax) -> Syntax {
23+
public override func visit(_ node: PatternBindingSyntax) -> PatternBindingSyntax {
2424
guard
2525
let accessorBlock = node.accessor?.as(AccessorBlockSyntax.self),
2626
let acc = accessorBlock.accessors.first,
@@ -31,14 +31,14 @@ public final class UseSingleLinePropertyGetter: SyntaxFormatRule {
3131
acc.modifier == nil,
3232
acc.asyncKeyword == nil,
3333
acc.throwsKeyword == nil
34-
else { return Syntax(node) }
34+
else { return node }
3535

3636
diagnose(.removeExtraneousGetBlock, on: acc)
3737

3838
let newBlock = CodeBlockSyntax(
3939
leftBrace: accessorBlock.leftBrace, statements: body.statements,
4040
rightBrace: accessorBlock.rightBrace)
41-
return Syntax(node.withAccessor(Syntax(newBlock)))
41+
return node.withAccessor(Syntax(newBlock))
4242
}
4343
}
4444

0 commit comments

Comments
 (0)