Skip to content

Commit

Permalink
Implement fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
keith committed Jul 26, 2023
1 parent 2e9eede commit fde0577
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
43 changes: 34 additions & 9 deletions Source/SwiftLintBuiltInRules/Rules/Lint/UnneededOverrideRule.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwiftSyntax
import SwiftSyntaxBuilder

struct UnneededOverrideRule: ConfigurationProviderRule, SwiftSyntaxRule {
struct UnneededOverrideRule: ConfigurationProviderRule, SwiftSyntaxCorrectableRule {
var configuration = SeverityConfiguration<Self>(.warning)

static let description = RuleDescription(
Expand All @@ -10,20 +10,20 @@ struct UnneededOverrideRule: ConfigurationProviderRule, SwiftSyntaxRule {
description: "Remove overridden functions that don't do anything except call their super",
kind: .lint,
nonTriggeringExamples: UnneededOverrideRuleExamples.nonTriggeringExamples,
triggeringExamples: UnneededOverrideRuleExamples.triggeringExamples
// corrections: UnneededOverrideRuleExamples.corrections
triggeringExamples: UnneededOverrideRuleExamples.triggeringExamples,
corrections: UnneededOverrideRuleExamples.corrections
)

func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
Visitor(viewMode: .sourceAccurate)
}

// func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
// Rewriter(
// locationConverter: file.locationConverter,
// disabledRegions: disabledRegions(file: file)
// )
// }
func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
Rewriter(
locationConverter: file.locationConverter,
disabledRegions: disabledRegions(file: file)
)
}
}

private func simplify(_ node: SyntaxProtocol) -> ExprSyntaxProtocol? {
Expand Down Expand Up @@ -68,6 +68,31 @@ private final class Visitor: ViolationsSyntaxVisitor {
}
}

private final class Rewriter: SyntaxRewriter, ViolationsSyntaxRewriter {
var correctionPositions: [AbsolutePosition] = []
let locationConverter: SourceLocationConverter
let disabledRegions: [SourceRange]

init(locationConverter: SourceLocationConverter, disabledRegions: [SourceRange]) {
self.locationConverter = locationConverter
self.disabledRegions = disabledRegions
}

override func visit(_ node: FunctionDeclSyntax) -> DeclSyntax {
if isUnneededOverride(node) &&
!node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter)
{
correctionPositions.append(node.positionAfterSkippingLeadingTrivia)
let expr: DeclSyntax = ""
return expr
.with(\.leadingTrivia, node.leadingTrivia)
.with(\.trailingTrivia, node.trailingTrivia)
}

return super.visit(node)
}
}

private func isUnneededOverride(_ node: FunctionDeclSyntax) -> Bool {
guard node.modifiers.containsOverride, let statement = node.body?.statements.onlyElement else {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,43 @@ struct UnneededOverrideRuleExamples {
}
""")
]

static let corrections = [
Example("""
class Foo {
↓override func bar(animated: Bool, completion: () -> Void) {
super.bar(animated: animated, completion: completion)
}
}
"""): Example("""
class Foo {
}
"""),
Example("""
class Foo {
↓override func bar() {
super.bar()
}
}
"""): Example("""
class Foo {
}
"""),
Example("""
class Foo {
↓override func bar() {
super.bar()
}
// This is another function
func baz() {}
}
"""): Example("""
class Foo {
// This is another function
func baz() {}
}
"""),
]
}

0 comments on commit fde0577

Please sign in to comment.