Skip to content

Commit

Permalink
Let all rewriters have a configuration by default (#5467)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Feb 11, 2024
1 parent c874c4e commit 4078e41
Show file tree
Hide file tree
Showing 54 changed files with 128 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ enum LegacyFunctionRuleHelper {
}
}

final class Rewriter: ViolationsSyntaxRewriter {
final class Rewriter<Configuration: RuleConfiguration>: ViolationsSyntaxRewriter<Configuration> {
private let legacyFunctions: [String: RewriteStrategy]

init(
legacyFunctions: [String: RewriteStrategy],
locationConverter: SourceLocationConverter,
disabledRegions: [SourceRange]
configuration: Configuration,
file: SwiftLintFile
) {
self.legacyFunctions = legacyFunctions
super.init(locationConverter: locationConverter, disabledRegions: disabledRegions)
super.init(configuration: configuration, file: file)
}

override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ struct DuplicateImportsRule: SwiftSyntaxCorrectableRule {
queuedFatalError("Unreachable: `validate(file:)` will be used instead")
}

func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
Rewriter(
importPositionsToRemove: file.duplicateImportsViolationPositions(),
locationConverter: file.locationConverter,
disabledRegions: disabledRegions(file: file)
)
func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter<ConfigurationType>? {
Rewriter(configuration: configuration, file: file)
}
}

Expand Down Expand Up @@ -152,17 +148,8 @@ private extension SwiftLintFile {
}

private extension DuplicateImportsRule {
final class Rewriter: ViolationsSyntaxRewriter {
let importPositionsToRemove: [AbsolutePosition]

init(
importPositionsToRemove: [AbsolutePosition],
locationConverter: SourceLocationConverter,
disabledRegions: [SourceRange]
) {
self.importPositionsToRemove = importPositionsToRemove
super.init(locationConverter: locationConverter, disabledRegions: disabledRegions)
}
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
private lazy var importPositionsToRemove = file.duplicateImportsViolationPositions()

override func visit(_ node: CodeBlockItemListSyntax) -> CodeBlockItemListSyntax {
let itemsToRemove = node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private extension ExplicitInitRule {
}
}

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

final class Rewriter: ViolationsSyntaxRewriter {
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax {
guard let violationPosition = node.violationPosition else {
return super.visit(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ struct LegacyCGGeometryFunctionsRule: SwiftSyntaxCorrectableRule {
)
}

func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter<ConfigurationType>? {
LegacyFunctionRuleHelper.Rewriter(
legacyFunctions: Self.legacyFunctions,
locationConverter: file.locationConverter,
disabledRegions: disabledRegions(file: file)
configuration: configuration,
file: file
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private extension LegacyConstantRule {
}
}

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

final class Rewriter: ViolationsSyntaxRewriter {
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
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 @@ -108,11 +108,11 @@ struct LegacyNSGeometryFunctionsRule: SwiftSyntaxCorrectableRule {
)
}

func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter<ConfigurationType>? {
LegacyFunctionRuleHelper.Rewriter(
legacyFunctions: Self.legacyFunctions,
locationConverter: file.locationConverter,
disabledRegions: disabledRegions(file: file)
configuration: configuration,
file: file
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private extension NimbleOperatorRule {
}
}

final class Rewriter: ViolationsSyntaxRewriter {
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax {
guard let expectation = node.expectation(),
let predicate = predicatesMapping[expectation.operatorExpr.baseName.text],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private extension PreferZeroOverExplicitInitRule {
}
}

final class Rewriter: ViolationsSyntaxRewriter {
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax {
guard node.hasViolation, let name = node.name else {
return super.visit(node)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwiftSyntax

@SwiftSyntaxRule
struct PrivateOverFilePrivateRule: SwiftSyntaxCorrectableRule {
@SwiftSyntaxRule(explicitRewriter: true)
struct PrivateOverFilePrivateRule: Rule {
var configuration = PrivateOverFilePrivateConfiguration()

static let description = RuleDescription(
Expand Down Expand Up @@ -54,14 +54,6 @@ struct PrivateOverFilePrivateRule: SwiftSyntaxCorrectableRule {
Example("private class MyClass {\nfileprivate(set) var myInt = 4\n}")
]
)

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

private extension PrivateOverFilePrivateRule {
Expand Down Expand Up @@ -123,17 +115,10 @@ private extension PrivateOverFilePrivateRule {
}
}

final class Rewriter: ViolationsSyntaxRewriter {
private let validateExtensions: Bool

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

final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
// don't call super in any of the `visit` methods to avoid digging into the children
override func visit(_ node: ExtensionDeclSyntax) -> DeclSyntax {
guard validateExtensions, let modifier = node.modifiers.fileprivateModifier,
guard configuration.validateExtensions, let modifier = node.modifiers.fileprivateModifier,
let modifierIndex = node.modifiers.fileprivateModifierIndex else {
return DeclSyntax(node)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private extension RedundantNilCoalescingRule {
}
}

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

final class Rewriter: ViolationsSyntaxRewriter {
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
override func visitAny(_ node: Syntax) -> Syntax? { nil }

override func visit(_ node: VariableDeclSyntax) -> DeclSyntax {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwiftSyntax

@SwiftSyntaxRule
struct RedundantVoidReturnRule: SwiftSyntaxCorrectableRule {
@SwiftSyntaxRule(explicitRewriter: true)
struct RedundantVoidReturnRule: Rule {
var configuration = RedundantVoidReturnConfiguration()

static let description = RuleDescription(
Expand Down Expand Up @@ -68,14 +68,6 @@ struct RedundantVoidReturnRule: SwiftSyntaxCorrectableRule {
Example("protocol Foo {\n #if true\n func foo()\n #endif\n}")
]
)

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

private extension RedundantVoidReturnRule {
Expand All @@ -92,18 +84,7 @@ private extension RedundantVoidReturnRule {
}
}

final class Rewriter: ViolationsSyntaxRewriter {
let configuration: ConfigurationType

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

final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
override func visit(_ node: ClosureSignatureSyntax) -> ClosureSignatureSyntax {
guard configuration.includeClosures,
let output = node.returnClause,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private extension ReturnValueFromVoidFunctionRule {
}
}

final class Rewriter: ViolationsSyntaxRewriter {
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
override func visit(_ statements: CodeBlockItemListSyntax) -> CodeBlockItemListSyntax {
guard let returnStmt = statements.last?.item.as(ReturnStmtSyntax.self),
let expr = returnStmt.expression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private extension ShorthandOptionalBindingRule {
}
}

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

final class Rewriter: ViolationsSyntaxRewriter {
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
override func visit(_ node: ExprListSyntax) -> ExprListSyntax {
guard node.hasToggleBoolViolation, let firstExpr = node.first, let index = node.index(of: firstExpr) else {
return super.visit(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private extension TrailingSemicolonRule {
}
}

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

final class Rewriter: ViolationsSyntaxRewriter {
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
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 @@ -46,7 +46,7 @@ private extension UnneededSynthesizedInitializerRule {
}
}

final class Rewriter: ViolationsSyntaxRewriter {
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
private var unneededInitializers: [InitializerDeclSyntax] = []

override func visitAny(_ node: Syntax) -> Syntax? { nil }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private extension UntypedErrorInCatchRule {
}
}

final class Rewriter: ViolationsSyntaxRewriter {
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
override func visit(_ node: CatchClauseSyntax) -> CatchClauseSyntax {
guard let item = node.catchItems.onlyElement, item.isIdentifierPattern else {
return super.visit(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ struct AnyObjectProtocolRule: SwiftSyntaxCorrectableRule, OptInRule {
return Visitor(configuration: configuration, file: file)
}

func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
Rewriter(
locationConverter: file.locationConverter,
disabledRegions: disabledRegions(file: file)
)
func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter<ConfigurationType>? {
Rewriter(configuration: configuration, file: file)
}
}

Expand All @@ -58,7 +55,7 @@ private extension AnyObjectProtocolRule {
}
}

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

final class Rewriter: ViolationsSyntaxRewriter {
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
override func visit(_ node: DeclModifierSyntax) -> DeclModifierSyntax {
guard node.isHigherACLThanParent else {
return super.visit(node)
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintBuiltInRules/Rules/Lint/MarkRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private extension MarkRule {
}
}

final class Rewriter: ViolationsSyntaxRewriter {
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
override func visit(_ token: TokenSyntax) -> TokenSyntax {
var pieces = token.leadingTrivia.pieces
for result in token.violationResults() {
Expand Down
23 changes: 3 additions & 20 deletions Source/SwiftLintBuiltInRules/Rules/Lint/PrivateUnitTestRule.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Foundation
import SwiftSyntax

@SwiftSyntaxRule
struct PrivateUnitTestRule: SwiftSyntaxCorrectableRule {
@SwiftSyntaxRule(explicitRewriter: true)
struct PrivateUnitTestRule: Rule {
var configuration = PrivateUnitTestConfiguration()

static let description = RuleDescription(
Expand Down Expand Up @@ -125,14 +125,6 @@ struct PrivateUnitTestRule: SwiftSyntaxCorrectableRule {
""")
]
)

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

private extension PrivateUnitTestRule {
Expand All @@ -156,16 +148,7 @@ private extension PrivateUnitTestRule {
}
}

final class Rewriter: ViolationsSyntaxRewriter {
private let configuration: PrivateUnitTestConfiguration

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

final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
override func visit(_ node: ClassDeclSyntax) -> DeclSyntax {
guard node.isPrivate, node.hasParent(configuredIn: configuration) else {
return super.visit(node)
Expand Down
Loading

0 comments on commit 4078e41

Please sign in to comment.