Skip to content

Commit

Permalink
Extract method for common code and make signatures swiftier (#5554)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny committed May 1, 2024
1 parent 16c0213 commit 1658f1d
Showing 1 changed file with 20 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,39 +182,31 @@ struct RedundantTypeAnnotationRule: OptInRule, SwiftSyntaxCorrectableRule {
private extension RedundantTypeAnnotationRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override func visitPost(_ node: PatternBindingSyntax) {
guard let varDecl = node.parent?.parent?.as(VariableDeclSyntax.self),
configuration.ignoreAttributes.allSatisfy({ !varDecl.attributes.contains(attributeNamed: $0) }),
let typeAnnotation = node.typeAnnotation,
let initializer = node.initializer?.value else {
return
}
let validateLiterals = configuration.considerDefaultLiteralTypesRedundant
let isLiteralRedundant = validateLiterals && initializer.hasRedundantLiteralType(typeAnnotation.type)
guard isLiteralRedundant || initializer.hasRedundantType(typeAnnotation.type) else {
return
if let varDecl = node.parent?.parent?.as(VariableDeclSyntax.self),
configuration.ignoreAttributes.allSatisfy({ !varDecl.attributes.contains(attributeNamed: $0) }),
let typeAnnotation = node.typeAnnotation,
let initializer = node.initializer?.value {
collectViolation(forType: typeAnnotation, withInitializer: initializer)
}
violations.append(typeAnnotation.positionAfterSkippingLeadingTrivia)
violationCorrections.append(ViolationCorrection(
start: typeAnnotation.position,
end: typeAnnotation.endPositionBeforeTrailingTrivia,
replacement: ""
))
}

override func visitPost(_ node: OptionalBindingConditionSyntax) {
guard let typeAnnotation = node.typeAnnotation,
let initializer = node.initializer?.value else {
return
if let typeAnnotation = node.typeAnnotation,
let initializer = node.initializer?.value {
collectViolation(forType: typeAnnotation, withInitializer: initializer)
}
}

private func collectViolation(forType type: TypeAnnotationSyntax, withInitializer initializer: ExprSyntax) {
let validateLiterals = configuration.considerDefaultLiteralTypesRedundant
let isLiteralRedundant = validateLiterals && initializer.hasRedundantLiteralType(typeAnnotation.type)
guard isLiteralRedundant || initializer.hasRedundantType(typeAnnotation.type) else {
let isLiteralRedundant = validateLiterals && initializer.hasRedundant(literalType: type.type)
guard isLiteralRedundant || initializer.hasRedundant(type: type.type) else {
return
}
violations.append(typeAnnotation.positionAfterSkippingLeadingTrivia)
violations.append(type.positionAfterSkippingLeadingTrivia)
violationCorrections.append(ViolationCorrection(
start: typeAnnotation.position,
end: typeAnnotation.endPositionBeforeTrailingTrivia,
start: type.position,
end: type.endPositionBeforeTrailingTrivia,
replacement: ""
))
}
Expand All @@ -241,16 +233,13 @@ private extension ExprSyntax {
}
}

func hasRedundantLiteralType(_ type: TypeSyntax) -> Bool {
func hasRedundant(literalType type: TypeSyntax) -> Bool {
type.trimmedDescription == kind.compilerInferredLiteralType
}

func hasRedundantType(_ type: TypeSyntax) -> Bool {
var expr = self
if let forceUnwrap = expr.as(ForceUnwrapExprSyntax.self) {
expr = forceUnwrap.expression
}
return expr.accessedNames.contains(type.trimmedDescription)
func hasRedundant(type: TypeSyntax) -> Bool {
`as`(ForceUnwrapExprSyntax.self)?.expression.hasRedundant(type: type)
?? accessedNames.contains(type.trimmedDescription)
}
}

Expand Down

0 comments on commit 1658f1d

Please sign in to comment.