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

Avoid triggering redundant_type_annotation with @IBInspectable #2843

Merged
merged 1 commit into from
Aug 25, 2019
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
[Timofey Solonin](https://github.com/biboran)
[#2737](https://github.com/realm/SwiftLint/issues/2737)

* Avoid triggering `redundant_type_annotation` rule when declaring
`IBInspectable` properties.
[Marcelo Fabri](https://github.com/marcelofabri)
[#2842](https://github.com/realm/SwiftLint/issues/2842)

## 0.34.0: Anti-Static Wool Dryer Balls

#### Breaking
Expand Down
4 changes: 4 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -17123,6 +17123,10 @@ var url = URL()
var url: CustomStringConvertible = URL()
```

```swift
@IBInspectable var color: UIColor = UIColor.white
```

</details>
<details>
<summary>Triggering Examples</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public struct RedundantTypeAnnotationRule: OptInRule, SubstitutionCorrectableRul
kind: .idiomatic,
nonTriggeringExamples: [
"var url = URL()",
"var url: CustomStringConvertible = URL()"
"var url: CustomStringConvertible = URL()",
"@IBInspectable var color: UIColor = UIColor.white"
],
triggeringExamples: [
"var url↓:URL=URL()",
Expand Down Expand Up @@ -72,7 +73,7 @@ public struct RedundantTypeAnnotationRule: OptInRule, SubstitutionCorrectableRul
let pattern = "(var|let)\\s?\\w+\(typeAnnotationPattern)\\s?=\\s?\\w+(\\(|.)"
let foundRanges = file.match(pattern: pattern, with: [.keyword, .identifier, .typeidentifier, .identifier])
return foundRanges
.filter { !isFalsePositive(in: file, range: $0) }
.filter { !isFalsePositive(in: file, range: $0) && !isIBInspectable(range: $0, file: file) }
.compactMap {
file.match(pattern: typeAnnotationPattern,
excludingSyntaxKinds: SyntaxKind.commentAndStringKinds, range: $0).first
Expand All @@ -95,4 +96,15 @@ public struct RedundantTypeAnnotationRule: OptInRule, SubstitutionCorrectableRul
let rhsTypeName = components[1].trimmingCharacters(in: charactersToTrimFromRhs)
return lhsTypeName != rhsTypeName
}

private func isIBInspectable(range: NSRange, file: File) -> Bool {
guard let byteRange = file.contents.bridge().NSRangeToByteRange(start: range.location, length: range.length),
let dict = file.structure.structures(forByteOffset: byteRange.location).last,
let kind = dict.kind.flatMap(SwiftDeclarationKind.init(rawValue:)),
SwiftDeclarationKind.variableKinds.contains(kind) else {
return false
}

return dict.enclosedSwiftAttributes.contains(.ibinspectable)
}
}