Skip to content

Commit

Permalink
Introduce parent class for rewriters (#5282)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Oct 17, 2023
1 parent 759f1aa commit 5763201
Show file tree
Hide file tree
Showing 39 changed files with 61 additions and 362 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ enum LegacyFunctionRuleHelper {
}
}

final class Rewriter: SyntaxRewriter, ViolationsSyntaxRewriter {
private(set) var correctionPositions: [AbsolutePosition] = []
private let locationConverter: SourceLocationConverter
private let disabledRegions: [SourceRange]
final class Rewriter: ViolationsSyntaxRewriter {
private let legacyFunctions: [String: RewriteStrategy]

init(
Expand All @@ -47,8 +44,7 @@ enum LegacyFunctionRuleHelper {
disabledRegions: [SourceRange]
) {
self.legacyFunctions = legacyFunctions
self.locationConverter = locationConverter
self.disabledRegions = disabledRegions
super.init(locationConverter: locationConverter, disabledRegions: disabledRegions)
}

override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,7 @@ private extension ExplicitInitRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax {
guard
let calledExpression = node.calledExpression.as(MemberAccessExprSyntax.self),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,7 @@ private extension JoinedDefaultParameterRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax {
guard let violationPosition = node.violationPosition,
!node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,7 @@ private extension LegacyConstantRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: DeclReferenceExprSyntax) -> ExprSyntax {
guard
let correction = LegacyConstantRuleExamples.patterns[node.baseName.text],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,7 @@ private extension LegacyConstructorRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax {
guard let identifierExpr = node.calledExpression.as(DeclReferenceExprSyntax.self),
case let identifier = identifierExpr.baseName.text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,7 @@ private extension PreferZeroOverExplicitInitRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax {
guard node.hasViolation,
let name = node.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,12 @@ private extension PrivateOverFilePrivateRule {
}
}

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

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

// don't call super in any of the `visit` methods to avoid digging into the children
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,7 @@ private extension RedundantNilCoalescingRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: ExprListSyntax) -> ExprListSyntax {
guard
node.count > 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,7 @@ private extension RedundantOptionalInitializationRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: VariableDeclSyntax) -> DeclSyntax {
guard node.bindingSpecifier.tokenKind == .keyword(.var),
!node.modifiers.contains(keyword: .lazy) else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,7 @@ private extension ShorthandOptionalBindingRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: OptionalBindingConditionSyntax) -> OptionalBindingConditionSyntax {
guard
node.isShadowingOptionalBinding,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,7 @@ private extension ToggleBoolRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: ExprListSyntax) -> ExprListSyntax {
guard node.hasToggleBoolViolation,
!node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,7 @@ private extension TrailingSemicolonRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: TokenSyntax) -> TokenSyntax {
guard
node.isTrailingSemicolon,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,7 @@ private extension UnneededBreakInSwitchRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: SwitchCaseSyntax) -> SwitchCaseSyntax {
let stmts = CodeBlockItemListSyntax(node.statements.dropLast())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,9 @@ private extension UnneededSynthesizedInitializerRule {
}
}

final class Rewriter: SyntaxRewriter, ViolationsSyntaxRewriter {
var correctionPositions: [AbsolutePosition] = []
final class Rewriter: ViolationsSyntaxRewriter {
private var unneededInitializers: [InitializerDeclSyntax] = []

let locationConverter: SourceLocationConverter
let disabledRegions: [SourceRange]

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

override func visit(_ node: StructDeclSyntax) -> DeclSyntax {
unneededInitializers = node.unneededInitializers.filter {
!$0.isContainedIn(regions: disabledRegions, locationConverter: locationConverter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,7 @@ private extension UntypedErrorInCatchRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: CatchClauseSyntax) -> CatchClauseSyntax {
guard
let item = node.catchItems.onlyElement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,7 @@ private extension AnyObjectProtocolRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: InheritedTypeSyntax) -> InheritedTypeSyntax {
let typeName = node.type
guard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,7 @@ private extension LowerACLThanParentRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: DeclModifierSyntax) -> DeclModifierSyntax {
guard
node.isHigherACLThanParent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,14 @@ private extension PrivateUnitTestRule {
}
}

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

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

override func visit(_ node: ClassDeclSyntax) -> DeclSyntax {
Expand Down
11 changes: 1 addition & 10 deletions Source/SwiftLintBuiltInRules/Rules/Lint/StrongIBOutletRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,7 @@ private extension StrongIBOutletRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: VariableDeclSyntax) -> DeclSyntax {
guard let violationPosition = node.violationPosition,
let weakOrUnownedModifier = node.weakOrUnownedModifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,7 @@ private extension UnneededOverrideRule {
}
}

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
}

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: FunctionDeclSyntax) -> DeclSyntax {
if isUnneededOverride(node) &&
!node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,7 @@ private extension UnusedClosureParameterRule {
}
}

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

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

final class Rewriter: ViolationsSyntaxRewriter {
override func visit(_ node: ClosureExprSyntax) -> ExprSyntax {
guard
node.namedParameters.isNotEmpty,
Expand Down
Loading

0 comments on commit 5763201

Please sign in to comment.