Skip to content

Commit

Permalink
Check disabled regions for all syntax types in parent rewriter class (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Oct 23, 2023
1 parent ee02f19 commit 4c0f337
Show file tree
Hide file tree
Showing 39 changed files with 68 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,20 @@ 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)
.with(\.trailingTrivia, node.trailingTrivia)
}

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
12 changes: 2 additions & 10 deletions Source/SwiftLintBuiltInRules/Rules/Lint/PrivateUnitTestRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Loading

0 comments on commit 4c0f337

Please sign in to comment.