Skip to content

Commit

Permalink
Avoid triggering redundant_type_annotation with @IBInspectable
Browse files Browse the repository at this point in the history
Fixes #2842
  • Loading branch information
marcelofabri committed Aug 25, 2019
1 parent 7800220 commit f6b7343
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
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)
}
}

0 comments on commit f6b7343

Please sign in to comment.