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

Validate implicit getters #57

Closed
jpsim opened this issue May 28, 2015 · 1 comment · Fixed by #862
Closed

Validate implicit getters #57

jpsim opened this issue May 28, 2015 · 1 comment · Fixed by #862
Labels
rule-request Requests for a new rules.

Comments

@jpsim
Copy link
Collaborator

jpsim commented May 28, 2015

Read-only properties and subscripts should avoid using the get keyword.

@jpsim jpsim added the rule-request Requests for a new rules. label May 28, 2015
@delebedev
Copy link
Contributor

delebedev commented Oct 15, 2016

cat file.swift

struct Thing {

    var foo: Int {
      get { return 3 }
      set { _abc = newValue }
    }

    var bar: Int { return 42 }

    var baz: Int {
      get { return 42 }
    }
}

sourcekitten structure --file file.swift:

{
  "key.substructure" : [
    {
      "key.kind" : "source.lang.swift.decl.struct",
      "key.offset" : 1,
      "key.nameoffset" : 8,
      "key.namelength" : 5,
      "key.bodyoffset" : 15,
      "key.bodylength" : 162,
      "key.accessibility" : "source.lang.swift.accessibility.internal",
      "key.substructure" : [
        {
          "key.kind" : "source.lang.swift.decl.var.instance",
          "key.offset" : 21,
          "key.nameoffset" : 25,
          "key.namelength" : 3,
          "key.bodyoffset" : 35,
          "key.bodylength" : 58,
          "key.accessibility" : "source.lang.swift.accessibility.internal",
          "key.setter_accessibility" : "source.lang.swift.accessibility.internal",
          "key.typename" : "Int",
          "key.name" : "foo",
          "key.length" : 12
        },
        {
          "key.kind" : "source.lang.swift.decl.var.instance",
          "key.offset" : 100,
          "key.nameoffset" : 104,
          "key.namelength" : 3,
          "key.bodyoffset" : 114,
          "key.bodylength" : 11,
          "key.accessibility" : "source.lang.swift.accessibility.internal",
          "key.length" : 12,
          "key.typename" : "Int",
          "key.name" : "bar"
        },
        {
          "key.kind" : "source.lang.swift.decl.var.instance",
          "key.offset" : 132,
          "key.nameoffset" : 136,
          "key.namelength" : 3,
          "key.bodyoffset" : 146,
          "key.bodylength" : 29,
          "key.accessibility" : "source.lang.swift.accessibility.internal",
          "key.length" : 12,
          "key.typename" : "Int",
          "key.name" : "baz"
        }
      ],
      "key.name" : "Thing",
      "key.length" : 177
    }
  ],
  "key.offset" : 0,
  "key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
  "key.length" : 179
}

As you can notice there is no structural difference between variable, variable with implicit and explicit getter.

I can start with ASTRule and check every source.lang.swift.decl.var.instance, but what is reliable way to check for get and set keywords? Matching string directly does not look like a good idea (it can be getVar inside a scope or one can omit space and have valid get{ piece.

Is there any way to check for syntax tokens in range? I've found SyntaxMap.tokensIn but I am not sure how to use it outside of the File

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

Successfully merging a pull request may close this issue.

2 participants