Skip to content
New issue

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

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

Already on GitHub? # to your account

Add private_over_fileprivate and strict_fileprivate rules #1656

Merged
merged 5 commits into from
Jul 4, 2017
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#1078](https://github.com/realm/SwiftLint/issues/1078)

* Add `private_over_fileprivate` correctable rule to check for top-level usages
of `fileprivate` and recommend `private` instead. This is inline with
SE-0169's goal "for `fileprivate` to be used rarely". There is a also a new
`strict_fileprivate` opt-in rule that will mark every `fileprivate`
as a violation (specially useful with Swift 4).
[Jose Cheyo Jimenez](https://github.com/masters3d)
[Marcelo Fabri](https://github.com/marcelofabri)
[#1469](https://github.com/realm/SwiftLint/issues/1469)
[#1058](https://github.com/realm/SwiftLint/issues/1058)

##### Bug Fixes

* None.
Expand Down
188 changes: 183 additions & 5 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
* [Operator Function Whitespace](#operator-function-whitespace)
* [Overridden methods call super](#overridden-methods-call-super)
* [Private Outlets](#private-outlets)
* [Private over fileprivate](#private-over-fileprivate)
* [Private Unit Test](#private-unit-test)
* [Prohibited calls to super](#prohibited-calls-to-super)
* [Protocol Property Accessors Order](#protocol-property-accessors-order)
Expand All @@ -71,6 +72,7 @@
* [Shorthand Operator](#shorthand-operator)
* [Sorted Imports](#sorted-imports)
* [Statement Position](#statement-position)
* [Strict fileprivate](#strict-fileprivate)
* [Switch Case on Newline](#switch-case-on-newline)
* [Syntactic Sugar](#syntactic-sugar)
* [Todo](#todo)
Expand Down Expand Up @@ -5875,24 +5877,24 @@ extension String {}
<summary>Triggering Examples</summary>

```swift
private extension String {}
private extension String {}
```

```swift
public
public
extension String {}
```

```swift
open extension String {}
open extension String {}
```

```swift
internal extension String {}
internal extension String {}
```

```swift
fileprivate extension String {}
fileprivate extension String {}
```

</details>
Expand Down Expand Up @@ -6852,6 +6854,99 @@ class Foo {



## Private over fileprivate

Identifier | Enabled by default | Supports autocorrection | Kind
--- | --- | --- | ---
`private_over_fileprivate` | Enabled | Yes | idiomatic

Prefer `private` over `fileprivate` declarations.

### Examples

<details>
<summary>Non Triggering Examples</summary>

```swift
extension String {}
```

```swift
private extension String {}
```

```swift
public
enum MyEnum {}
```

```swift
open extension
String {}
```

```swift
internal extension String {}
```

```swift
extension String {
fileprivate func Something(){}
}
```

```swift
class MyClass {
fileprivate let myInt = 4
}
```

```swift
class MyClass {
fileprivate(set) var myInt = 4
}
```

```swift
struct Outter {
struct Inter {
fileprivate struct Inner {}
}
}
```

</details>
<details>
<summary>Triggering Examples</summary>

```swift
↓fileprivate enum MyEnum {}
```

```swift
↓fileprivate extension String {}
```

```swift
↓fileprivate
extension String {}
```

```swift
↓fileprivate extension
String {}
```

```swift
↓fileprivate class MyClass {
fileprivate(set) var myInt = 4
}
```

</details>



## Private Unit Test

Identifier | Enabled by default | Supports autocorrection | Kind
Expand Down Expand Up @@ -7889,6 +7984,89 @@ catch {



## Strict fileprivate

Identifier | Enabled by default | Supports autocorrection | Kind
--- | --- | --- | ---
`strict_fileprivate` | Disabled | No | idiomatic

`fileprivate` should be avoided.

### Examples

<details>
<summary>Non Triggering Examples</summary>

```swift
extension String {}
```

```swift
private extension String {}
```

```swift
public
extension String {}
```

```swift
open extension
String {}
```

```swift
internal extension String {}
```

</details>
<details>
<summary>Triggering Examples</summary>

```swift
↓fileprivate extension String {}
```

```swift
↓fileprivate
extension String {}
```

```swift
↓fileprivate extension
String {}
```

```swift
extension String {
↓fileprivate func Something(){}
}
```

```swift
class MyClass {
↓fileprivate let myInt = 4
}
```

```swift
class MyClass {
↓fileprivate(set) var myInt = 4
}
```

```swift
struct Outter {
struct Inter {
↓fileprivate struct Inner {}
}
}
```

</details>



## Switch Case on Newline

Identifier | Enabled by default | Supports autocorrection | Kind
Expand Down
2 changes: 2 additions & 0 deletions Source/SwiftLintFramework/Models/MasterRuleList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public let masterRuleList = RuleList(rules: [
OperatorUsageWhitespaceRule.self,
OverriddenSuperCallRule.self,
PrivateOutletRule.self,
PrivateOverFilePrivateRule.self,
PrivateUnitTestRule.self,
ProhibitedSuperRule.self,
ProtocolPropertyAccessorsOrderRule.self,
Expand All @@ -79,6 +80,7 @@ public let masterRuleList = RuleList(rules: [
ShorthandOperatorRule.self,
SortedImportsRule.self,
StatementPositionRule.self,
StrictFilePrivateRule.self,
SwitchCaseOnNewlineRule.self,
SyntacticSugarRule.self,
TodoRule.self,
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/IdentifierNameRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public struct IdentifierNameRule: ASTRule, ConfigurationProviderRule {
}
}

fileprivate extension String {
private extension String {
var isViolatingCase: Bool {
let secondIndex = characters.index(after: startIndex)
let firstCharacter = substring(to: secondIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public struct NoExtensionAccessModifierRule: ASTRule, OptInRule, ConfigurationPr
"\n\n extension String {}"
],
triggeringExamples: [
"private extension String {}",
"public \n extension String {}",
"open extension String {}",
"internal extension String {}",
"fileprivate extension String {}"
"private extension String {}",
"public \n extension String {}",
"open extension String {}",
"internal extension String {}",
"fileprivate extension String {}"
]
)

Expand All @@ -47,7 +47,7 @@ public struct NoExtensionAccessModifierRule: ASTRule, OptInRule, ConfigurationPr
return [
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset))
location: Location(file: file, byteOffset: aclToken.offset))
]
}
}
Loading