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

Fix non accessible declarations under @objcMembers #2501

Merged
merged 3 commits into from
Dec 5, 2018
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
13 changes: 8 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

#### Enhancements

* Add new opt-in rule `vertical_whitespace_opening_braces` to warn against empty lines
after opening braces.
* Add new opt-in rule `vertical_whitespace_opening_braces` to warn against empty
lines after opening braces.
[Cihat Gündüz](https://github.com/Dschee)
[#1518](https://github.com/realm/SwiftLint/issues/1518)

* Add new opt-in rule `vertical_whitespace_closing_braces` to warn against empty lines
before closing braces.
* Add new opt-in rule `vertical_whitespace_closing_braces` to warn against empty
lines before closing braces.
[Cihat Gündüz](https://github.com/Dschee)
[#1518](https://github.com/realm/SwiftLint/issues/1518)

Expand All @@ -26,7 +26,10 @@

#### Bug Fixes

* None.
* Fix false positives in `redundant_objc_attribute` for private declarations
under `@objcMembers`.
[Daniel Metzing](https://github.com/dirtydanee)
[#2270](https://github.com/realm/SwiftLint/pull/2270)

## 0.29.1: There’s Always More Laundry

Expand Down
22 changes: 21 additions & 1 deletion Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -15072,7 +15072,7 @@ var myVar: Int? = nil; myVar↓??nil

Identifier | Enabled by default | Supports autocorrection | Kind | Analyzer | Minimum Swift Compiler Version
--- | --- | --- | --- | --- | ---
`redundant_objc_attribute` | Enabled | No | idiomatic | No | 3.0.0
`redundant_objc_attribute` | Enabled | No | idiomatic | No | 4.1.0

Objective-C attribute (@objc) is redundant in declaration.

Expand Down Expand Up @@ -15157,6 +15157,16 @@ extension Foo {
}
```

```swift
@objcMembers
class Foo: NSObject {
@objc
private var bar: Int {
return 0
}
}
```

</details>
<details>
<summary>Triggering Examples</summary>
Expand Down Expand Up @@ -15237,6 +15247,16 @@ extension Foo {
}
```

```swift
@objc
extension Foo {
@objc
private ↓var bar: Int {
return 0
}
}
```

</details>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import SourceKittenFramework
private let kindsImplyingObjc: Set<SwiftDeclarationAttributeKind> =
[.ibaction, .iboutlet, .ibinspectable, .gkinspectable, .ibdesignable, .nsManaged]

private let privateACL: Set<SwiftDeclarationAttributeKind> = [.private, .fileprivate]

public struct RedundantObjcAttributeRule: ASTRule, ConfigurationProviderRule, AutomaticTestableRule {
public var configuration = SeverityConfiguration(.warning)

Expand All @@ -14,6 +16,7 @@ public struct RedundantObjcAttributeRule: ASTRule, ConfigurationProviderRule, Au
name: "Redundant @objc Attribute",
description: "Objective-C attribute (@objc) is redundant in declaration.",
kind: .idiomatic,
minSwiftVersion: .fourDotOne,
nonTriggeringExamples: [
"@objc private var foo: String? {}",
"@IBInspectable private var foo: String? {}",
Expand Down Expand Up @@ -61,6 +64,15 @@ public struct RedundantObjcAttributeRule: ASTRule, ConfigurationProviderRule, Au
var bar: Int { return 0 }
var fooBar: Int { return 1 }
}
""",
"""
@objcMembers
class Foo: NSObject {
@objc
private var bar: Int {
return 0
}
}
"""
],
triggeringExamples: [
Expand Down Expand Up @@ -108,9 +120,23 @@ public struct RedundantObjcAttributeRule: ASTRule, ConfigurationProviderRule, Au
return 0
}
}
""",
"""
@objc
extension Foo {
@objc
private ↓var bar: Int {
return 0
}
}
"""
])

fileprivate struct ObjcAttributeLocations {
var inObjcMembers = [NSRange]()
var inObjcExtension = [NSRange]()
}

public func validate(file: File,
kind: SwiftDeclarationKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
Expand All @@ -121,12 +147,16 @@ public struct RedundantObjcAttributeRule: ASTRule, ConfigurationProviderRule, Au
return []
}

let isInObjcVisibleScope = {
file.structure.dictionary.objcVisibleRanges.contains(where: { $0.contains(offset) })
let isInObjcVisibleScope = { () -> Bool in
let ranges = file.structure.dictionary.objcAttributeLocationRanges
if ranges.inObjcMembers.contains(where: { $0.contains(offset) })
&& !enclosedSwiftAttributes.isDisjoint(with: privateACL) {
return false
}
return (ranges.inObjcMembers + ranges.inObjcExtension).contains(where: { $0.contains(offset) })
}

let isUsedWithObjcAttribute = { !enclosedSwiftAttributes.isDisjoint(with: kindsImplyingObjc) }

if isInObjcVisibleScope() || isUsedWithObjcAttribute() {
return [StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
Expand All @@ -138,27 +168,37 @@ public struct RedundantObjcAttributeRule: ASTRule, ConfigurationProviderRule, Au
}

private extension Dictionary where Key == String, Value == SourceKitRepresentable {
var objcVisibleRanges: [NSRange] {
var ranges = [NSRange]()
func search(in dictionary: [String: SourceKitRepresentable]) {
let enclosedRanges = [dictionary.enclosedObjcMembersRange, dictionary.enclosedObjcExtensionRange]
ranges.append(contentsOf: enclosedRanges.compactMap({ $0 }))
var objcAttributeLocationRanges: RedundantObjcAttributeRule.ObjcAttributeLocations {
var objcImpliedAttributeLocatioRanges = RedundantObjcAttributeRule.ObjcAttributeLocations()
func search(in dictionary: [Key: Value]) {
if let enclosedObjcMembersRange = dictionary.enclosedObjcMembersRange {
objcImpliedAttributeLocatioRanges.inObjcMembers.append(enclosedObjcMembersRange)
}

if let enclosedNonObjcMembersClassRange = dictionary.enclosedNonObjcMembersClassRange {
let intersectingRanges = ranges.filter { $0.intersects(enclosedNonObjcMembersClassRange) }
intersectingRanges.compactMap(ranges.index(of:))
.forEach { ranges.remove(at: $0) }
if let enclosedObjcExtensionRange = dictionary.enclosedObjcExtensionRange {
objcImpliedAttributeLocatioRanges.inObjcExtension.append(enclosedObjcExtensionRange)
}

intersectingRanges.forEach {
let (lhs, rhs) = $0.split(by: enclosedNonObjcMembersClassRange)
ranges += [lhs, rhs]
if let enclosedNonObjcMembersClassRange = dictionary.enclosedNonObjcMembersClassRange {
func split(ranges: inout [NSRange]) {
let intersectingRanges = ranges.filter { $0.intersects(enclosedNonObjcMembersClassRange) }
intersectingRanges.compactMap(ranges.index(of:))
.forEach { ranges.remove(at: $0) }

intersectingRanges.forEach {
let (lhs, rhs) = $0.split(by: enclosedNonObjcMembersClassRange)
ranges += [lhs, rhs]
}
}

split(ranges: &objcImpliedAttributeLocatioRanges.inObjcMembers)
split(ranges: &objcImpliedAttributeLocatioRanges.inObjcExtension)
}

dictionary.substructure.forEach(search)
}
search(in: self)
return ranges
return objcImpliedAttributeLocatioRanges
}

var bodyRange: NSRange? {
Expand Down