Swiftlint: Rule request: Vertical alignment of collection types

Created on 1 Aug 2018  ·  4Comments  ·  Source: realm/SwiftLint

New Issue Checklist

Rule Request

This issue is to propose the addition of a new style rule that would check for vertical alignment of collection types (such as with arrays, dictionaries, and sets).

  1. Why should this rule be added?

The proposed rule would ensure that collections that span multiple lines would be vertically aligned on the first character, or on colons for dictionaries.

  1. Provide several examples of what _would_ and _wouldn't_ trigger violations.

Triggering examples

// ❌ Coordinates not aligned on first character
let coordinates = [
    CLLocationCoordinate2D(latitude: 0, longitude: 33),
        CLLocationCoordinate2D(latitude: 0, longitude: 66),
    CLLocationCoordinate2D(latitude: 0, longitude: 99)
]
// ❌ Strings not aligned on first character or on colons
let meals = [
            "breakfast": "oatmeal",
            "lunch": "sandwich",
"dinner": "burger"
]
// ❌ Integers not aligned on first character
var evenNumbers: Set<Int> = [
    2,
  4,
    6
]

Non-triggering examples

// ✅ Dictionary keys aligned on first character
let colorDictionary: [NSNumber: UIColor] = [
    0: .clear,
    5: .white,
    10: .green,
    15: .yellow,
    20: .red
]
// ✅ Dictionary keys aligned on colon
let colorDictionary: [String: UIColor] = [
          "mild": .green,
      "moderate": .yellow,
        "severe": .red
]
✅ Array of strings aligned on first character
let pets = [
    "cat",
    "dog",
    "fish"
]
  1. Should the rule be configurable, if so what parameters should be configurable?

No, I don't believe the rule should be configurable.

  1. Should the rule be opt-in or enabled by default? Why?

The proposed rule should be enabled by default to promote consistency in alignment, which is enforced through other rules. I'm open to this being different though.

rule-request

Most helpful comment

I think this should be configurable on how to validate dictionaries: aligning colons x the first character.

All 4 comments

I think this should be configurable on how to validate dictionaries: aligning colons x the first character.

I'd like to give this one a try if no one else is working on it.

@daltonclaybrook as far as I know no one is working on this. Feel free to grab it 🎉

Let me now if you have any issues! Looking how the other vertical alignment rules are implemented is probably a good start.

Hey - looks like you forgot to add a T:* label - could you please add one?

Was this page helpful?
0 / 5 - 0 ratings