Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RedundantTypeAnnotation: add 'ignore_type_interfaces' option #5839

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
argument `--use-script-input-file-lists`.
[BlueVirusX](https://github.com/BlueVirusX)

* The `redundant_type_annotation` rule gains a new option,
`ignore_type_interfaces`, that skips enforcement on members in a
type declaration (like a `struct`). This helps the rule coexist with
the `explicit_type_interface` rule that requires such redundancy.
[jaredgrubb](https://github.com/jaredgrubb)
[#3750](https://github.com/realm/SwiftLint/issues/3750)

#### Bug Fixes

* Run command plugin in whole package if no targets are defined in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ struct RedundantTypeAnnotationRule: OptInRule, SwiftSyntaxCorrectableRule {
Example("var dbl: Double = 0.0"),
Example("var int: Int = 0"),
Example("var str: String = \"str\""),
Example("""
struct Foo {
var url: URL = URL()
let myVar: Int? = 0, s: String = ""
}
""", configuration: ["ignore_type_interfaces": true]),
],
triggeringExamples: [
Example("var url↓:URL=URL()"),
Expand Down Expand Up @@ -88,6 +94,13 @@ struct RedundantTypeAnnotationRule: OptInRule, SwiftSyntaxCorrectableRule {
}
}
"""),
Example("""
class ViewController: UIViewController {
func someMethod() {
let myVar↓: Int = Int(5)
}
}
""", configuration: ["ignore_type_interfaces": true]),
Example("let a↓: [Int] = [Int]()"),
Example("let a↓: A.B = A.B()"),
Example("""
Expand Down Expand Up @@ -183,7 +196,7 @@ private extension RedundantTypeAnnotationRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override func visitPost(_ node: PatternBindingSyntax) {
if let varDecl = node.parent?.parent?.as(VariableDeclSyntax.self),
configuration.ignoreAttributes.allSatisfy({ !varDecl.attributes.contains(attributeNamed: $0) }),
!configuration.shouldSkipRuleCheck(for: varDecl),
let typeAnnotation = node.typeAnnotation,
let initializer = node.initializer?.value {
collectViolation(forType: typeAnnotation, withInitializer: initializer)
Expand Down Expand Up @@ -261,3 +274,17 @@ private extension SyntaxKind {
}
}
}

extension RedundantTypeAnnotationConfiguration {
func shouldSkipRuleCheck(for varDecl: VariableDeclSyntax) -> Bool {
if ignoreAttributes.contains(where: { varDecl.attributes.contains(attributeNamed: $0) }) {
return true
}
if ignoreTypeInterfaces,
let parentNode = varDecl.parent,
parentNode.as(CodeBlockItemSyntax.self) == nil {
return true
}
return false
Comment on lines +283 to +288
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ignoreTypeInterfaces,
let parentNode = varDecl.parent,
parentNode.as(CodeBlockItemSyntax.self) == nil {
return true
}
return false
return ignoreTypeInterfaces && varDecl.parent?.is(MemberBlockItemSyntax.self) == true

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ struct RedundantTypeAnnotationConfiguration: SeverityBasedRuleConfiguration {
var severityConfiguration = SeverityConfiguration<Parent>(.warning)
@ConfigurationElement(key: "ignore_attributes")
var ignoreAttributes = Set<String>(["IBInspectable"])
@ConfigurationElement(key: "ignore_type_interfaces")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which other elements apart from properties does this include?

If it's only about properties, we could be more specific with the name by calling it ignore_properties.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think it is limited only to that. I was trying to echo the other rule (explicit_type_interfaces) by the name, but your suggestion is probably more accurate. I can change it!

private(set) var ignoreTypeInterfaces = false
@ConfigurationElement(key: "consider_default_literal_types_redundant")
private(set) var considerDefaultLiteralTypesRedundant = false
}
1 change: 1 addition & 0 deletions Tests/IntegrationTests/default_rule_configurations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ redundant_string_enum_value:
redundant_type_annotation:
severity: warning
ignore_attributes: ["IBInspectable"]
ignore_type_interfaces: false
consider_default_literal_types_redundant: false
redundant_void_return:
severity: warning
Expand Down