SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Create a rule enforcing colon position for dictionaries

Open Noobish1 opened this issue 9 years ago • 10 comments
trafficstars

Not sure if this is possible, but i'd love a rule that enforces colon position for dictionaries, I like dictionaries to be defined as [Key : Value] with a space on either side of the colon, this rule could be for dictionary instances and also for dictionary type definitions (e.g [String : AnyObject]).

Noobish1 avatar Apr 04 '16 16:04 Noobish1

Would love this too.

Personally, I see String: Int as 'string is of type Int' rather than String maps to Int. A separation between the two would be great.

ceeK avatar Apr 15 '16 14:04 ceeK

I totally agree we should have a rule for the colon position on dictionaries, but I'm not sure if it should be [Key : Value]. I personally prefer [Key: Value] which is what the GitHub code style suggests and how Apple itself is writing on their documentation.

phelgo avatar May 05 '16 16:05 phelgo

Perhaps then it should be configurable?

ceeK avatar May 06 '16 08:05 ceeK

@ceeK: That would be ideal! Though it would be cool to agree on a default as well

phelgo avatar May 06 '16 08:05 phelgo

Yep, and on that point, I think if Apple + Github style guide has it Key: Value, then that should be the default.

ceeK avatar May 06 '16 09:05 ceeK

I agree, configurable with key: value as the default.

Noobish1 avatar May 06 '16 13:05 Noobish1

related: https://github.com/realm/SwiftLint/issues/637

masters3d avatar Aug 26 '16 06:08 masters3d

Sorry, but I don't understand, why it was closed.

The apply_to_dictionaries parameter of the colon rule affects only dictionary contents, but not the dictionary type declaration (as is described in the feature proposal).

To elaborate, assume we have this code:

let dict: [String : String] = ["string" : "string"]

colon with apply_to_dictionaries: true produces this formatting:

let dict: [String: String] = ["string": "string"]

colon with apply_to_dictionaries: false produces this:

let dict: [String: String] = ["string" : "string"]

Whereas the feature proposal requires an opportunity to produce this:

let dict: [String : String] = ["string" : "string"]

Or did I miss something?

If there's another way to achieve that, please let me know!

lazarevzubov avatar Apr 08 '22 07:04 lazarevzubov

+1 Would be nice to have such rule

xSiavAx avatar May 31 '22 16:05 xSiavAx

The format requested by this issue is indeed not covered by the existing colon rule.

SimplyDanny avatar Sep 02 '22 20:09 SimplyDanny