Skip to content

Commit

Permalink
Merge pull request #2392 from daltonclaybrook/collection-alignment
Browse files Browse the repository at this point in the history
New Rule - Collection Element Alignment
  • Loading branch information
marcelofabri committed Sep 11, 2018
2 parents 00b559b + c0a52bb commit 8610f83
Show file tree
Hide file tree
Showing 8 changed files with 492 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#1371](https://github.com/realm/SwiftLint/issues/1371)

* Add `collection_alignment` opt-in rule to validate that all elements in a
collection literal are aligned vertically.
[Dalton Claybrook](https://github.com/daltonclaybrook)
[#2326](https://github.com/realm/SwiftLint/issues/2326)

#### Bug Fixes

* Fix `comma` rule false positives on object literals (for example, images).
Expand Down
124 changes: 124 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [Closure End Indentation](#closure-end-indentation)
* [Closure Parameter Position](#closure-parameter-position)
* [Closure Spacing](#closure-spacing)
* [Collection Element Alignment](#collection-element-alignment)
* [Colon](#colon)
* [Comma Spacing](#comma-spacing)
* [Compiler Protocol Init](#compiler-protocol-init)
Expand Down Expand Up @@ -1705,6 +1706,129 @@ filter ↓{ sorted ↓{ $0 < $1}}



## Collection Element Alignment

Identifier | Enabled by default | Supports autocorrection | Kind | Analyzer | Minimum Swift Compiler Version
--- | --- | --- | --- | --- | ---
`collection_alignment` | Disabled | No | style | No | 3.0.0

All elements in a collection literal should be vertically aligned

### Examples

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

```swift
doThings(arg: [
"foo": 1,
"bar": 2,
"fizz": 2,
"buzz": 2
])
```

```swift
let abc = [
"alpha": "a",
"beta": "b",
"gamma": "g",
"delta": "d",
"epsilon": "e"
]
```

```swift
let meals = [
"breakfast": "oatmeal",
"lunch": "sandwich",
"dinner": "burger"
]
```

```swift
let coordinates = [
CLLocationCoordinate2D(latitude: 0, longitude: 33),
CLLocationCoordinate2D(latitude: 0, longitude: 66),
CLLocationCoordinate2D(latitude: 0, longitude: 99)
]
```

```swift
var evenNumbers: Set<Int> = [
2,
4,
6
]
```

```swift
let abc = [1, 2, 3, 4]
```

```swift
let abc = [
1, 2, 3, 4
]
```

```swift
let abc = [
"foo": "bar", "fizz": "buzz"
]
```

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

```swift
doThings(arg: [
"foo": 1,
"bar": 2,
"fizz": 2,
"buzz": 2
])
```

```swift
let abc = [
"alpha": "a",
"beta": "b",
"gamma": "g",
"delta": "d",
"epsilon": "e"
]
```

```swift
let meals = [
"breakfast": "oatmeal",
"lunch": "sandwich",
"dinner": "burger"
]
```

```swift
let coordinates = [
CLLocationCoordinate2D(latitude: 0, longitude: 33),
CLLocationCoordinate2D(latitude: 0, longitude: 66),
CLLocationCoordinate2D(latitude: 0, longitude: 99)
]
```

```swift
var evenNumbers: Set<Int> = [
2,
4,
6
]
```

</details>



## Colon

Identifier | Enabled by default | Supports autocorrection | Kind | Analyzer | Minimum Swift Compiler Version
Expand Down
1 change: 1 addition & 0 deletions Source/SwiftLintFramework/Models/MasterRuleList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public let masterRuleList = RuleList(rules: [
ClosureEndIndentationRule.self,
ClosureParameterPositionRule.self,
ClosureSpacingRule.self,
CollectionAlignmentRule.self,
ColonRule.self,
CommaRule.self,
CompilerProtocolInitRule.self,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
public struct CollectionAlignmentConfiguration: RuleConfiguration, Equatable {
private(set) var severityConfiguration = SeverityConfiguration(.warning)
private(set) var alignColons = false

init() {}

public var consoleDescription: String {
return severityConfiguration.consoleDescription + ", align_colons: \(alignColons)"
}

public mutating func apply(configuration: Any) throws {
guard let configuration = configuration as? [String: Any] else {
throw ConfigurationError.unknownConfiguration
}

alignColons = configuration["align_colons"] as? Bool ?? false

if let severityString = configuration["severity"] as? String {
try severityConfiguration.apply(configuration: severityString)
}
}

public static func == (lhs: CollectionAlignmentConfiguration,
rhs: CollectionAlignmentConfiguration) -> Bool {
return lhs.alignColons == rhs.alignColons &&
lhs.severityConfiguration == rhs.severityConfiguration
}
}

0 comments on commit 8610f83

Please sign in to comment.