From 4c0f33782c145f172cc3bba0bac418b0f5a585d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danny=20M=C3=B6sch?= Date: Mon, 23 Oct 2023 21:34:51 +0200 Subject: [PATCH] Check disabled regions for all syntax types in parent rewriter class (#5292) --- .../Helpers/LegacyFunctionRuleHelper.swift | 7 ++---- .../Rules/Idiomatic/ExplicitInitRule.swift | 10 +++----- .../JoinedDefaultParameterRule.swift | 4 +--- .../Rules/Idiomatic/LegacyConstantRule.swift | 14 +++-------- .../Idiomatic/LegacyConstructorRule.swift | 3 +-- .../PreferZeroOverExplicitInitRule.swift | 4 +--- .../PrivateOverFilePrivateRule.swift | 24 +++++++------------ .../RedundantNilCoalescingRule.swift | 3 +-- .../RedundantOptionalInitializationRule.swift | 2 ++ .../ShorthandOptionalBindingRule.swift | 5 +--- .../Rules/Idiomatic/ToggleBoolRule.swift | 4 +--- .../Idiomatic/TrailingSemicolonRule.swift | 5 +--- .../Idiomatic/UnneededBreakInSwitchRule.swift | 3 +-- .../UnneededSynthesizedInitializerRule.swift | 2 ++ .../Idiomatic/UntypedErrorInCatchRule.swift | 6 +---- .../Rules/Lint/AnyObjectProtocolRule.swift | 6 +---- .../Rules/Lint/LowerACLThanParentRule.swift | 5 +--- .../Rules/Lint/PrivateUnitTestRule.swift | 12 ++-------- .../Rules/Lint/StrongIBOutletRule.swift | 4 +--- .../Rules/Lint/UnneededOverrideRule.swift | 3 +-- .../Lint/UnusedClosureParameterRule.swift | 9 +++---- .../Lint/UnusedControlFlowLabelRule.swift | 3 +-- .../Rules/Style/ClosingBraceRule.swift | 6 +---- .../Rules/Style/ClosureSpacingRule.swift | 5 +--- .../Rules/Style/ControlStatementRule.swift | 15 ++++-------- .../Rules/Style/DirectReturnRule.swift | 1 - .../Rules/Style/EmptyEnumArgumentsRule.swift | 12 ++-------- .../Rules/Style/EmptyParametersRule.swift | 6 +---- ...tyParenthesesWithTrailingClosureRule.swift | 5 +--- .../Rules/Style/NoSpaceInMethodCallRule.swift | 5 +--- .../Rules/Style/NumberSeparatorRule.swift | 11 ++------- .../Style/OptionalEnumCaseMatchingRule.swift | 3 +-- .../PreferSelfTypeOverTypeOfSelfRule.swift | 4 +--- .../ProtocolPropertyAccessorsOrderRule.swift | 5 +--- .../Style/RedundantDiscardableLetRule.swift | 5 +--- .../Rules/Style/SelfBindingRule.swift | 7 ++---- .../Rules/Style/TrailingCommaRule.swift | 6 ++--- ...ededParenthesesInClosureArgumentRule.swift | 9 +++---- .../SwiftSyntaxCorrectableRule.swift | 4 ++++ 39 files changed, 68 insertions(+), 179 deletions(-) diff --git a/Source/SwiftLintBuiltInRules/Helpers/LegacyFunctionRuleHelper.swift b/Source/SwiftLintBuiltInRules/Helpers/LegacyFunctionRuleHelper.swift index 009a7a747d..774a7d99a1 100644 --- a/Source/SwiftLintBuiltInRules/Helpers/LegacyFunctionRuleHelper.swift +++ b/Source/SwiftLintBuiltInRules/Helpers/LegacyFunctionRuleHelper.swift @@ -48,11 +48,8 @@ enum LegacyFunctionRuleHelper { } override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax { - guard - node.isLegacyFunctionExpression(legacyFunctions: legacyFunctions), - let funcName = node.calledExpression.as(DeclReferenceExprSyntax.self)?.baseName.text, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard node.isLegacyFunctionExpression(legacyFunctions: legacyFunctions), + let funcName = node.calledExpression.as(DeclReferenceExprSyntax.self)?.baseName.text else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExplicitInitRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExplicitInitRule.swift index 8285b54bc0..64fe2eccb8 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExplicitInitRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExplicitInitRule.swift @@ -191,15 +191,11 @@ private extension ExplicitInitRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax { - guard - let calledExpression = node.calledExpression.as(MemberAccessExprSyntax.self), - let violationPosition = calledExpression.explicitInitPosition, - let calledBase = calledExpression.base, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard let calledExpression = node.calledExpression.as(MemberAccessExprSyntax.self), + let violationPosition = calledExpression.explicitInitPosition, + let calledBase = calledExpression.base else { return super.visit(node) } - correctionPositions.append(violationPosition) let newNode = node.with(\.calledExpression, calledBase.trimmed) return super.visit(newNode) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/JoinedDefaultParameterRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/JoinedDefaultParameterRule.swift index 91d177a176..abeecf98f5 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/JoinedDefaultParameterRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/JoinedDefaultParameterRule.swift @@ -49,11 +49,9 @@ private extension JoinedDefaultParameterRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax { - guard let violationPosition = node.violationPosition, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + guard let violationPosition = node.violationPosition else { return super.visit(node) } - correctionPositions.append(violationPosition) let newNode = node.with(\.arguments, []) return super.visit(newNode) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/LegacyConstantRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/LegacyConstantRule.swift index e48acc0280..9902cb1de3 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/LegacyConstantRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/LegacyConstantRule.swift @@ -33,13 +33,9 @@ private extension LegacyConstantRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: DeclReferenceExprSyntax) -> ExprSyntax { - guard - let correction = LegacyConstantRuleExamples.patterns[node.baseName.text], - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard let correction = LegacyConstantRuleExamples.patterns[node.baseName.text] else { return super.visit(node) } - correctionPositions.append(node.positionAfterSkippingLeadingTrivia) return ("\(raw: correction)" as ExprSyntax) .with(\.leadingTrivia, node.leadingTrivia) @@ -47,14 +43,10 @@ private extension LegacyConstantRule { } override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax { - guard - node.isLegacyPiExpression, - let calledExpression = node.calledExpression.as(DeclReferenceExprSyntax.self), - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard node.isLegacyPiExpression, + let calledExpression = node.calledExpression.as(DeclReferenceExprSyntax.self) else { return super.visit(node) } - correctionPositions.append(node.positionAfterSkippingLeadingTrivia) return ("\(raw: calledExpression.baseName.text).pi" as ExprSyntax) .with(\.leadingTrivia, node.leadingTrivia) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/LegacyConstructorRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/LegacyConstructorRule.swift index b7de39b097..bac00ac6ad 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/LegacyConstructorRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/LegacyConstructorRule.swift @@ -139,8 +139,7 @@ private extension LegacyConstructorRule { guard let identifierExpr = node.calledExpression.as(DeclReferenceExprSyntax.self), case let identifier = identifierExpr.baseName.text, let correctedName = constructorsToCorrectedNames[identifier], - let args = constructorsToArguments[identifier], - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + let args = constructorsToArguments[identifier] else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferZeroOverExplicitInitRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferZeroOverExplicitInitRule.swift index ccb3112f82..973f3f5338 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferZeroOverExplicitInitRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferZeroOverExplicitInitRule.swift @@ -47,9 +47,7 @@ private extension PreferZeroOverExplicitInitRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax { - guard node.hasViolation, - let name = node.name, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + guard node.hasViolation, let name = node.name else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PrivateOverFilePrivateRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PrivateOverFilePrivateRule.swift index 033cdc501b..8ad50f51eb 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PrivateOverFilePrivateRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PrivateOverFilePrivateRule.swift @@ -134,8 +134,7 @@ private extension PrivateOverFilePrivateRule { // 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, - let modifierIndex = node.modifiers.fileprivateModifierIndex, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + let modifierIndex = node.modifiers.fileprivateModifierIndex else { return DeclSyntax(node) } @@ -146,8 +145,7 @@ private extension PrivateOverFilePrivateRule { override func visit(_ node: ClassDeclSyntax) -> DeclSyntax { guard let modifier = node.modifiers.fileprivateModifier, - let modifierIndex = node.modifiers.fileprivateModifierIndex, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + let modifierIndex = node.modifiers.fileprivateModifierIndex else { return DeclSyntax(node) } @@ -158,8 +156,7 @@ private extension PrivateOverFilePrivateRule { override func visit(_ node: StructDeclSyntax) -> DeclSyntax { guard let modifier = node.modifiers.fileprivateModifier, - let modifierIndex = node.modifiers.fileprivateModifierIndex, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + let modifierIndex = node.modifiers.fileprivateModifierIndex else { return DeclSyntax(node) } @@ -170,8 +167,7 @@ private extension PrivateOverFilePrivateRule { override func visit(_ node: EnumDeclSyntax) -> DeclSyntax { guard let modifier = node.modifiers.fileprivateModifier, - let modifierIndex = node.modifiers.fileprivateModifierIndex, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + let modifierIndex = node.modifiers.fileprivateModifierIndex else { return DeclSyntax(node) } @@ -182,8 +178,7 @@ private extension PrivateOverFilePrivateRule { override func visit(_ node: ProtocolDeclSyntax) -> DeclSyntax { guard let modifier = node.modifiers.fileprivateModifier, - let modifierIndex = node.modifiers.fileprivateModifierIndex, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + let modifierIndex = node.modifiers.fileprivateModifierIndex else { return DeclSyntax(node) } @@ -194,8 +189,7 @@ private extension PrivateOverFilePrivateRule { override func visit(_ node: FunctionDeclSyntax) -> DeclSyntax { guard let modifier = node.modifiers.fileprivateModifier, - let modifierIndex = node.modifiers.fileprivateModifierIndex, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + let modifierIndex = node.modifiers.fileprivateModifierIndex else { return DeclSyntax(node) } @@ -206,8 +200,7 @@ private extension PrivateOverFilePrivateRule { override func visit(_ node: VariableDeclSyntax) -> DeclSyntax { guard let modifier = node.modifiers.fileprivateModifier, - let modifierIndex = node.modifiers.fileprivateModifierIndex, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + let modifierIndex = node.modifiers.fileprivateModifierIndex else { return DeclSyntax(node) } @@ -218,8 +211,7 @@ private extension PrivateOverFilePrivateRule { override func visit(_ node: TypeAliasDeclSyntax) -> DeclSyntax { guard let modifier = node.modifiers.fileprivateModifier, - let modifierIndex = node.modifiers.fileprivateModifierIndex, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + let modifierIndex = node.modifiers.fileprivateModifierIndex else { return DeclSyntax(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantNilCoalescingRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantNilCoalescingRule.swift index 2024084744..61bd0040d8 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantNilCoalescingRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantNilCoalescingRule.swift @@ -40,8 +40,7 @@ private extension RedundantNilCoalescingRule { let lastExpression = node.last, lastExpression.is(NilLiteralExprSyntax.self), let secondToLastExpression = node.dropLast().last?.as(BinaryOperatorExprSyntax.self), - secondToLastExpression.operator.tokenKind.isNilCoalescingOperator, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) + secondToLastExpression.operator.tokenKind.isNilCoalescingOperator else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantOptionalInitializationRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantOptionalInitializationRule.swift index 202da63d25..e14d730646 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantOptionalInitializationRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantOptionalInitializationRule.swift @@ -119,6 +119,8 @@ private extension RedundantOptionalInitializationRule { } final class Rewriter: ViolationsSyntaxRewriter { + override func visitAny(_ node: Syntax) -> Syntax? { nil } + override func visit(_ node: VariableDeclSyntax) -> DeclSyntax { guard node.bindingSpecifier.tokenKind == .keyword(.var), !node.modifiers.contains(keyword: .lazy) else { diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ShorthandOptionalBindingRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ShorthandOptionalBindingRule.swift index 8a48a73812..203a993c67 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ShorthandOptionalBindingRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ShorthandOptionalBindingRule.swift @@ -92,10 +92,7 @@ private extension ShorthandOptionalBindingRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: OptionalBindingConditionSyntax) -> OptionalBindingConditionSyntax { - guard - node.isShadowingOptionalBinding, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard node.isShadowingOptionalBinding else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ToggleBoolRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ToggleBoolRule.swift index b72bd533b3..cc1435a1a3 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ToggleBoolRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ToggleBoolRule.swift @@ -42,9 +42,7 @@ private extension ToggleBoolRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: ExprListSyntax) -> ExprListSyntax { - guard node.hasToggleBoolViolation, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter), - let firstExpr = node.first, let index = node.index(of: firstExpr) else { + guard node.hasToggleBoolViolation, let firstExpr = node.first, let index = node.index(of: firstExpr) else { return super.visit(node) } correctionPositions.append(node.positionAfterSkippingLeadingTrivia) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/TrailingSemicolonRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/TrailingSemicolonRule.swift index 27b486d971..13459b37fc 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/TrailingSemicolonRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/TrailingSemicolonRule.swift @@ -37,10 +37,7 @@ private extension TrailingSemicolonRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: TokenSyntax) -> TokenSyntax { - guard - node.isTrailingSemicolon, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard node.isTrailingSemicolon else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededBreakInSwitchRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededBreakInSwitchRule.swift index 7bba1f9432..035f338419 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededBreakInSwitchRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededBreakInSwitchRule.swift @@ -103,8 +103,7 @@ private extension UnneededBreakInSwitchRule { override func visit(_ node: SwitchCaseSyntax) -> SwitchCaseSyntax { let stmts = CodeBlockItemListSyntax(node.statements.dropLast()) - guard let breakStatement = node.unneededBreak, let secondLast = stmts.last, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + guard let breakStatement = node.unneededBreak, let secondLast = stmts.last else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededSynthesizedInitializerRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededSynthesizedInitializerRule.swift index 12aa684318..bf999e09a4 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededSynthesizedInitializerRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededSynthesizedInitializerRule.swift @@ -49,6 +49,8 @@ private extension UnneededSynthesizedInitializerRule { final class Rewriter: ViolationsSyntaxRewriter { private var unneededInitializers: [InitializerDeclSyntax] = [] + override func visitAny(_ node: Syntax) -> Syntax? { nil } + override func visit(_ node: StructDeclSyntax) -> DeclSyntax { unneededInitializers = node.unneededInitializers.filter { !$0.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UntypedErrorInCatchRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UntypedErrorInCatchRule.swift index 67307f8ef8..b6e9636bbc 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UntypedErrorInCatchRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UntypedErrorInCatchRule.swift @@ -121,11 +121,7 @@ private extension UntypedErrorInCatchRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: CatchClauseSyntax) -> CatchClauseSyntax { - guard - let item = node.catchItems.onlyElement, - item.isIdentifierPattern, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard let item = node.catchItems.onlyElement, item.isIdentifierPattern else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/AnyObjectProtocolRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/AnyObjectProtocolRule.swift index 14c5ba8f78..d71c3d275f 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/AnyObjectProtocolRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/AnyObjectProtocolRule.swift @@ -61,13 +61,9 @@ private extension AnyObjectProtocolRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: InheritedTypeSyntax) -> InheritedTypeSyntax { let typeName = node.type - guard - typeName.is(ClassRestrictionTypeSyntax.self), - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard typeName.is(ClassRestrictionTypeSyntax.self) else { return super.visit(node) } - correctionPositions.append(node.positionAfterSkippingLeadingTrivia) return super.visit( node.with( diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/LowerACLThanParentRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/LowerACLThanParentRule.swift index 76a4115f60..a9043a0945 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/LowerACLThanParentRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/LowerACLThanParentRule.swift @@ -93,10 +93,7 @@ private extension LowerACLThanParentRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: DeclModifierSyntax) -> DeclModifierSyntax { - guard - node.isHigherACLThanParent, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard node.isHigherACLThanParent else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/PrivateUnitTestRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/PrivateUnitTestRule.swift index 8d427bd7d1..18464f1f3c 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/PrivateUnitTestRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/PrivateUnitTestRule.swift @@ -167,11 +167,7 @@ private extension PrivateUnitTestRule { } override func visit(_ node: ClassDeclSyntax) -> DeclSyntax { - guard - node.isPrivate, - node.hasParent(configuredIn: configuration), - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard node.isPrivate, node.hasParent(configuredIn: configuration) else { return super.visit(node) } @@ -181,11 +177,7 @@ private extension PrivateUnitTestRule { } override func visit(_ node: FunctionDeclSyntax) -> DeclSyntax { - guard - node.isTestMethod, - node.isPrivate, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard node.isTestMethod, node.isPrivate else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/StrongIBOutletRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/StrongIBOutletRule.swift index 829d9d52d7..44f0f0b1ff 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/StrongIBOutletRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/StrongIBOutletRule.swift @@ -42,9 +42,7 @@ private extension StrongIBOutletRule { override func visit(_ node: VariableDeclSyntax) -> DeclSyntax { guard let violationPosition = node.violationPosition, let weakOrUnownedModifier = node.weakOrUnownedModifier, - case let modifiers = node.modifiers, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + case let modifiers = node.modifiers else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/UnneededOverrideRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/UnneededOverrideRule.swift index 096873cd47..18e17df06f 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/UnneededOverrideRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/UnneededOverrideRule.swift @@ -61,8 +61,7 @@ private extension UnneededOverrideRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: FunctionDeclSyntax) -> DeclSyntax { - if isUnneededOverride(node) && - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) { + if isUnneededOverride(node) { correctionPositions.append(node.positionAfterSkippingLeadingTrivia) let expr: DeclSyntax = "" return expr diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedClosureParameterRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedClosureParameterRule.swift index b02b5ecab2..97a8145eec 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedClosureParameterRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedClosureParameterRule.swift @@ -35,12 +35,9 @@ private extension UnusedClosureParameterRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: ClosureExprSyntax) -> ExprSyntax { - guard - node.namedParameters.isNotEmpty, - let signature = node.signature, - let input = signature.parameterClause, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard node.namedParameters.isNotEmpty, + let signature = node.signature, + let input = signature.parameterClause else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedControlFlowLabelRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedControlFlowLabelRule.swift index 8cb9711849..72e341abab 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedControlFlowLabelRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedControlFlowLabelRule.swift @@ -96,8 +96,7 @@ private extension UnusedControlFlowLabelRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: LabeledStmtSyntax) -> StmtSyntax { - guard let violationPosition = node.violationPosition, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + guard let violationPosition = node.violationPosition else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/ClosingBraceRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/ClosingBraceRule.swift index a3f3d5b374..3468b32726 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/ClosingBraceRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/ClosingBraceRule.swift @@ -34,13 +34,9 @@ private extension ClosingBraceRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: TokenSyntax) -> TokenSyntax { - guard - node.hasClosingBraceViolation, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard node.hasClosingBraceViolation else { return super.visit(node) } - correctionPositions.append(node.positionAfterSkippingLeadingTrivia) return super.visit(node.with(\.trailingTrivia, Trivia())) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/ClosureSpacingRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/ClosureSpacingRule.swift index df49ec95cb..2fc2cdbf25 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/ClosureSpacingRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/ClosureSpacingRule.swift @@ -61,10 +61,7 @@ private extension ClosureSpacingRule { var node = node node.statements = visit(node.statements) - guard - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter), - node.shouldCheckForClosureSpacingRule(locationConverter: locationConverter) - else { + guard node.shouldCheckForClosureSpacingRule(locationConverter: locationConverter) else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/ControlStatementRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/ControlStatementRule.swift index 0ccd2cf1fb..833c8343de 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/ControlStatementRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/ControlStatementRule.swift @@ -110,8 +110,7 @@ private extension ControlStatementRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: CatchClauseSyntax) -> CatchClauseSyntax { - guard !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter), - case let items = node.catchItems, items.containSuperfluousParens == true else { + guard case let items = node.catchItems, items.containSuperfluousParens == true else { return super.visit(node) } correctionPositions.append(node.positionAfterSkippingLeadingTrivia) @@ -122,8 +121,7 @@ private extension ControlStatementRule { } override func visit(_ node: GuardStmtSyntax) -> StmtSyntax { - guard !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter), - node.conditions.containSuperfluousParens else { + guard node.conditions.containSuperfluousParens else { return super.visit(node) } correctionPositions.append(node.positionAfterSkippingLeadingTrivia) @@ -134,8 +132,7 @@ private extension ControlStatementRule { } override func visit(_ node: IfExprSyntax) -> ExprSyntax { - guard !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter), - node.conditions.containSuperfluousParens else { + guard node.conditions.containSuperfluousParens else { return super.visit(node) } correctionPositions.append(node.positionAfterSkippingLeadingTrivia) @@ -146,8 +143,7 @@ private extension ControlStatementRule { } override func visit(_ node: SwitchExprSyntax) -> ExprSyntax { - guard !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter), - let tupleElement = node.subject.unwrapped else { + guard let tupleElement = node.subject.unwrapped else { return super.visit(node) } correctionPositions.append(node.positionAfterSkippingLeadingTrivia) @@ -158,8 +154,7 @@ private extension ControlStatementRule { } override func visit(_ node: WhileStmtSyntax) -> StmtSyntax { - guard !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter), - node.conditions.containSuperfluousParens else { + guard node.conditions.containSuperfluousParens else { return super.visit(node) } correctionPositions.append(node.positionAfterSkippingLeadingTrivia) diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/DirectReturnRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/DirectReturnRule.swift index 873c8834c7..c486deb4ec 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/DirectReturnRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/DirectReturnRule.swift @@ -194,7 +194,6 @@ private extension DirectReturnRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ statements: CodeBlockItemListSyntax) -> CodeBlockItemListSyntax { guard let (binding, returnStmt) = statements.violation, - !binding.isContainedIn(regions: disabledRegions, locationConverter: locationConverter), let bindingList = binding.parent?.as(PatternBindingListSyntax.self), let varDecl = bindingList.parent?.as(VariableDeclSyntax.self), var initExpression = binding.initializer?.value else { diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/EmptyEnumArgumentsRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/EmptyEnumArgumentsRule.swift index df8b764369..1daae7ad0e 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/EmptyEnumArgumentsRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/EmptyEnumArgumentsRule.swift @@ -130,25 +130,17 @@ private extension EmptyEnumArgumentsRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: SwitchCaseItemSyntax) -> SwitchCaseItemSyntax { - guard - let (violationPosition, newPattern) = node.pattern.emptyEnumArgumentsViolation(rewrite: true), - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard let (violationPosition, newPattern) = node.pattern.emptyEnumArgumentsViolation(rewrite: true) else { return super.visit(node) } - correctionPositions.append(violationPosition) return super.visit(node.with(\.pattern, newPattern)) } override func visit(_ node: MatchingPatternConditionSyntax) -> MatchingPatternConditionSyntax { - guard - let (violationPosition, newPattern) = node.pattern.emptyEnumArgumentsViolation(rewrite: true), - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard let (violationPosition, newPattern) = node.pattern.emptyEnumArgumentsViolation(rewrite: true) else { return super.visit(node) } - correctionPositions.append(violationPosition) return super.visit(node.with(\.pattern, newPattern)) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/EmptyParametersRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/EmptyParametersRule.swift index 4eae974129..bbf6dc319a 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/EmptyParametersRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/EmptyParametersRule.swift @@ -46,13 +46,9 @@ private extension EmptyParametersRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: FunctionTypeSyntax) -> TypeSyntax { - guard - let violationPosition = node.emptyParametersViolationPosition, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard let violationPosition = node.emptyParametersViolationPosition else { return super.visit(node) } - correctionPositions.append(violationPosition) return super.visit(node.with(\.parameters, TupleTypeElementListSyntax([]))) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/EmptyParenthesesWithTrailingClosureRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/EmptyParenthesesWithTrailingClosureRule.swift index 6732e28b1d..f35ec73afc 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/EmptyParenthesesWithTrailingClosureRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/EmptyParenthesesWithTrailingClosureRule.swift @@ -59,10 +59,7 @@ private extension EmptyParenthesesWithTrailingClosureRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax { - guard - let violationPosition = node.violationPosition, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard let violationPosition = node.violationPosition else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/NoSpaceInMethodCallRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/NoSpaceInMethodCallRule.swift index 93fd362fd5..ef78736564 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/NoSpaceInMethodCallRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/NoSpaceInMethodCallRule.swift @@ -58,10 +58,7 @@ private extension NoSpaceInMethodCallRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax { - guard - node.hasNoSpaceInMethodCallViolation, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard node.hasNoSpaceInMethodCallViolation else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/NumberSeparatorRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/NumberSeparatorRule.swift index a046953a27..6d6b759e12 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/NumberSeparatorRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/NumberSeparatorRule.swift @@ -61,10 +61,7 @@ private extension NumberSeparatorRule { } override func visit(_ node: FloatLiteralExprSyntax) -> ExprSyntax { - guard - let violation = violation(token: node.literal), - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard let violation = violation(token: node.literal) else { return super.visit(node) } @@ -80,13 +77,9 @@ private extension NumberSeparatorRule { } override func visit(_ node: IntegerLiteralExprSyntax) -> ExprSyntax { - guard - let violation = violation(token: node.literal), - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard let violation = violation(token: node.literal) else { return super.visit(node) } - let newNode = node.with(\.literal, node.literal.with(\.tokenKind, .integerLiteral(violation.correction))) correctionPositions.append(violation.position) return super.visit(newNode) diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/OptionalEnumCaseMatchingRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/OptionalEnumCaseMatchingRule.swift index 1ac3d7d84c..377bceeaa8 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/OptionalEnumCaseMatchingRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/OptionalEnumCaseMatchingRule.swift @@ -178,8 +178,7 @@ private extension OptionalEnumCaseMatchingRule { override func visit(_ node: SwitchCaseItemSyntax) -> SwitchCaseItemSyntax { guard let pattern = node.pattern.as(ExpressionPatternSyntax.self), - pattern.expression.is(OptionalChainingExprSyntax.self) || pattern.expression.is(TupleExprSyntax.self), - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) + pattern.expression.is(OptionalChainingExprSyntax.self) || pattern.expression.is(TupleExprSyntax.self) else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfTypeOverTypeOfSelfRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfTypeOverTypeOfSelfRule.swift index 3d4914cf34..ebf1714d5c 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfTypeOverTypeOfSelfRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfTypeOverTypeOfSelfRule.swift @@ -118,9 +118,7 @@ private extension PreferSelfTypeOverTypeOfSelfRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: MemberAccessExprSyntax) -> ExprSyntax { - guard let function = node.base?.as(FunctionCallExprSyntax.self), - function.hasViolation, - !function.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + guard let function = node.base?.as(FunctionCallExprSyntax.self), function.hasViolation else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/ProtocolPropertyAccessorsOrderRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/ProtocolPropertyAccessorsOrderRule.swift index 3cc2714183..391676386b 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/ProtocolPropertyAccessorsOrderRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/ProtocolPropertyAccessorsOrderRule.swift @@ -41,10 +41,7 @@ private extension ProtocolPropertyAccessorsOrderRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: AccessorBlockSyntax) -> AccessorBlockSyntax { - guard - node.hasViolation, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard node.hasViolation else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/RedundantDiscardableLetRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/RedundantDiscardableLetRule.swift index 8987579d3a..cdf1c95f48 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/RedundantDiscardableLetRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/RedundantDiscardableLetRule.swift @@ -39,10 +39,7 @@ private extension RedundantDiscardableLetRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: VariableDeclSyntax) -> DeclSyntax { - guard - node.hasRedundantDiscardableLetViolation, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard node.hasRedundantDiscardableLetViolation else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/SelfBindingRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/SelfBindingRule.swift index 5d37797ac2..da4783a283 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/SelfBindingRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/SelfBindingRule.swift @@ -89,11 +89,8 @@ private extension SelfBindingRule { } override func visit(_ node: OptionalBindingConditionSyntax) -> OptionalBindingConditionSyntax { - guard - let identifierPattern = node.pattern.as(IdentifierPatternSyntax.self), - identifierPattern.identifier.text != bindIdentifier, - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard let identifierPattern = node.pattern.as(IdentifierPatternSyntax.self), + identifierPattern.identifier.text != bindIdentifier else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/TrailingCommaRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/TrailingCommaRule.swift index c974080ddd..0861c34afd 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/TrailingCommaRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/TrailingCommaRule.swift @@ -106,8 +106,7 @@ private extension TrailingCommaRule { } override func visit(_ node: DictionaryElementListSyntax) -> DictionaryElementListSyntax { - guard let lastElement = node.last, let index = node.index(of: lastElement), - !lastElement.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + guard let lastElement = node.last, let index = node.index(of: lastElement) else { return super.visit(node) } @@ -142,8 +141,7 @@ private extension TrailingCommaRule { } override func visit(_ node: ArrayElementListSyntax) -> ArrayElementListSyntax { - guard let lastElement = node.last, let index = node.index(of: lastElement), - !lastElement.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) else { + guard let lastElement = node.last, let index = node.index(of: lastElement) else { return super.visit(node) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/UnneededParenthesesInClosureArgumentRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/UnneededParenthesesInClosureArgumentRule.swift index ac559df254..be736b1726 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/UnneededParenthesesInClosureArgumentRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/UnneededParenthesesInClosureArgumentRule.swift @@ -89,12 +89,9 @@ private extension UnneededParenthesesInClosureArgumentRule { final class Rewriter: ViolationsSyntaxRewriter { override func visit(_ node: ClosureSignatureSyntax) -> ClosureSignatureSyntax { - guard - let clause = node.parameterClause?.as(ClosureParameterClauseSyntax.self), - clause.parameters.isNotEmpty, - clause.parameters.allSatisfy({ $0.type == nil }), - !node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) - else { + guard let clause = node.parameterClause?.as(ClosureParameterClauseSyntax.self), + clause.parameters.isNotEmpty, + clause.parameters.allSatisfy({ $0.type == nil }) else { return super.visit(node) } diff --git a/Source/SwiftLintCore/Protocols/SwiftSyntaxCorrectableRule.swift b/Source/SwiftLintCore/Protocols/SwiftSyntaxCorrectableRule.swift index bb109f1d6c..132c1cf3a6 100644 --- a/Source/SwiftLintCore/Protocols/SwiftSyntaxCorrectableRule.swift +++ b/Source/SwiftLintCore/Protocols/SwiftSyntaxCorrectableRule.swift @@ -84,4 +84,8 @@ open class ViolationsSyntaxRewriter: SyntaxRewriter { self.locationConverter = locationConverter self.disabledRegions = disabledRegions } + + override open func visitAny(_ node: Syntax) -> Syntax? { + node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) ? node : nil + } }