Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

"Attributes should be on their own line" rule #846

Closed
marcelofabri opened this issue Oct 15, 2016 · 6 comments
Closed

"Attributes should be on their own line" rule #846

marcelofabri opened this issue Oct 15, 2016 · 6 comments
Labels
rule-request Requests for a new rules.

Comments

@marcelofabri
Copy link
Collaborator

marcelofabri commented Oct 15, 2016

I think we should have a rule that some (that's the tricky) part attributes should be on their own line.

The attributes that I'd like to see in that list are:

  • @discardableResult
  • @available
  • @NSApplicationMain
  • @UIApplicationMain
  • @IBDesignable

The attributes that I particularly use on the same line:

  • @GKInspectable
  • @objc
  • @nonobjc
  • @NSCopying
  • @NSManaged
  • @testable
  • @IBAction
  • @IBOutlet
  • @IBInspectable

It seems to me that all the attributes that I use on the same line are in these categories:

  1. Applied to a var
  2. Applied to import
  3. @objc or @nonobj

I'd be OK with @objc and @nonobj being on their own line for functions/types but I don't think we can just use the "where is this attribute" rule because of @available.

@keith
Copy link
Collaborator

keith commented Oct 15, 2016

👍🏻 this seems useful. For 3 we also put those on their own line. But that's just us!

@marcelofabri
Copy link
Collaborator Author

I've started looking into this, but I'm having trouble because the AST doesn't expose the range for the attribute:

$ sourcekitten structure --text "@IBOutlet private var label: UILabel"
{
  "key.substructure" : [
    {
      "key.kind" : "source.lang.swift.decl.var.global",
      "key.offset" : 18,
      "key.attributes" : [
        {
          "key.attribute" : "source.decl.attribute.iboutlet"
        }
      ],
      "key.nameoffset" : 22,
      "key.namelength" : 5,
      "key.length" : 18,
      "key.accessibility" : "source.lang.swift.accessibility.private",
      "key.setter_accessibility" : "source.lang.swift.accessibility.private",
      "key.typename" : "UILabel",
      "key.name" : "label"
    }
  ],
  "key.offset" : 0,
  "key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
  "key.length" : 36
}

I can get it from sourcekitten syntax, but the type is source.lang.swift.syntaxtype.attribute.builtin, which is used for other things (such as private).

$ sourcekitten syntax --text "@IBOutlet private var label: UILabel"
[
  {
    "offset" : 0,
    "length" : 9,
    "type" : "source.lang.swift.syntaxtype.attribute.builtin"
  },
  {
    "offset" : 10,
    "length" : 7,
    "type" : "source.lang.swift.syntaxtype.attribute.builtin"
  },
  {
    "offset" : 18,
    "length" : 3,
    "type" : "source.lang.swift.syntaxtype.keyword"
  },
  {
    "offset" : 22,
    "length" : 5,
    "type" : "source.lang.swift.syntaxtype.identifier"
  },
  {
    "offset" : 29,
    "length" : 7,
    "type" : "source.lang.swift.syntaxtype.typeidentifier"
  }
]

How bad would be checking if it starts with @? 😅

@keith
Copy link
Collaborator

keith commented Oct 15, 2016

It might be worth looking into how the valid documentation comment stuff works. As we don't get the offset for the comment either, but still access it.

FWIW we have a rule for @objc on a newline implemented and we just used regex.

@marcelofabri marcelofabri mentioned this issue Oct 16, 2016
3 tasks
@marcelofabri
Copy link
Collaborator Author

From what I've seen, the documentation rules use regex. I've opened #847 with my approach.

@ghost
Copy link

ghost commented Oct 18, 2016

FWIW, Erica Sadun did a quick poll around styling of attributes: http://ericasadun.com/2016/10/02/quick-style-survey/

@marcelofabri
Copy link
Collaborator Author

Thanks for sharing! I've based my opinions on what I've seen in then Apple docs, although I forgot that you can use @objc to specify a name.

@jpsim jpsim added the rule-request Requests for a new rules. label Nov 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule-request Requests for a new rules.
Projects
None yet
Development

No branches or pull requests

3 participants