Skip to content

Commit

Permalink
Merge pull request #2164 from realm/ks/acl-rule
Browse files Browse the repository at this point in the history
Remove extensions from LowerACLThanBodyRule
  • Loading branch information
jpsim authored May 6, 2018
2 parents 13d9e81 + b881334 commit cd7f0c1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@

#### Bug Fixes

* None.
* Update `LowerACLThanParent` rule to not lint extensions.
[Keith Smiley](https://github.com/keith)
[#2164](https://github.com/realm/SwiftLint/pull/2164)

## 0.25.1: Lid Locked

Expand Down
16 changes: 8 additions & 8 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -8486,6 +8486,14 @@ fileprivate struct Foo { private func bar() {} }
private struct Foo { private func bar(id: String) }
```

```swift
extension Foo { public func bar() {} }
```

```swift
private struct Foo { fileprivate func bar() {} }
```

```swift
private func foo(id: String) {}
```
Expand All @@ -8498,10 +8506,6 @@ private func foo(id: String) {}
struct Foo { public func bar() {} }
```

```swift
extension Foo { public func bar() {} }
```

```swift
enum Foo { public func bar() {} }
```
Expand All @@ -8510,10 +8514,6 @@ enum Foo { public func bar() {} }
public class Foo { open func bar() }
```

```swift
private struct Foo { fileprivate func bar() {} }
```

```swift
class Foo { public private(set) var bar: String? }
```
Expand Down
37 changes: 24 additions & 13 deletions Source/SwiftLintFramework/Rules/LowerACLThanBodyRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public struct LowerACLThanParentRule: OptInRule, ConfigurationProviderRule {
"open class Foo { open func bar() {} }",
"fileprivate struct Foo { private func bar() {} }",
"private struct Foo { private func bar(id: String) }",
"extension Foo { public func bar() {} }",
"private struct Foo { fileprivate func bar() {} }",
"private func foo(id: String) {}"
],
triggeringExamples: [
"struct Foo { public func bar() {} }",
"extension Foo { public func bar() {} }",
"enum Foo { public func bar() {} }",
"public class Foo { open func bar() }",
"private struct Foo { fileprivate func bar() {} }",
"class Foo { public private(set) var bar: String? }"
]
)
Expand All @@ -50,7 +50,7 @@ public struct LowerACLThanParentRule: OptInRule, ConfigurationProviderRule {
var violationOffset: Int?
let accessibility = element.accessibility.flatMap(AccessControlLevel.init(identifier:))
?? .`internal`
if accessibility > parentAccessibility {
if accessibility.priority > parentAccessibility.priority {
violationOffset = element.offset
}

Expand All @@ -62,18 +62,29 @@ public struct LowerACLThanParentRule: OptInRule, ConfigurationProviderRule {
private extension SwiftDeclarationKind {
var isRelevantDeclaration: Bool {
switch self {
case .`associatedtype`, .enumcase, .enumelement, .functionAccessorAddress,
.functionAccessorDidset, .functionAccessorGetter, .functionAccessorMutableaddress,
.functionAccessorSetter, .functionAccessorWillset, .functionDestructor, .genericTypeParam, .module,
.precedenceGroup, .varLocal, .varParameter:
case .`associatedtype`, .enumcase, .enumelement, .`extension`, .`extensionClass`, .`extensionEnum`,
.extensionProtocol, .extensionStruct, .functionAccessorAddress, .functionAccessorDidset,
.functionAccessorGetter, .functionAccessorMutableaddress, .functionAccessorSetter,
.functionAccessorWillset, .functionDestructor, .genericTypeParam, .module, .precedenceGroup, .varLocal,
.varParameter:
return false
case .`class`, .`enum`, .`extension`, .`extensionClass`, .`extensionEnum`,
.extensionProtocol, .extensionStruct, .functionConstructor,
.functionFree, .functionMethodClass, .functionMethodInstance, .functionMethodStatic,
.functionOperator, .functionOperatorInfix, .functionOperatorPostfix, .functionOperatorPrefix,
.functionSubscript, .`protocol`, .`struct`, .`typealias`, .varClass,
.varGlobal, .varInstance, .varStatic:
case .`class`, .`enum`, .functionConstructor, .functionFree, .functionMethodClass, .functionMethodInstance,
.functionMethodStatic, .functionOperator, .functionOperatorInfix, .functionOperatorPostfix,
.functionOperatorPrefix, .functionSubscript, .`protocol`, .`struct`, .`typealias`, .varClass, .varGlobal,
.varInstance, .varStatic:
return true
}
}
}

private extension AccessControlLevel {
var priority: Int {
switch self {
case .private: return 1
case .fileprivate: return 1
case .internal: return 2
case .public: return 3
case .open: return 4
}
}
}

0 comments on commit cd7f0c1

Please sign in to comment.