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

Rule Request: Prefer Array(foo) over foo.map({ $0 }) #1271

Closed
jpsim opened this issue Feb 1, 2017 · 4 comments
Closed

Rule Request: Prefer Array(foo) over foo.map({ $0 }) #1271

jpsim opened this issue Feb 1, 2017 · 4 comments
Labels
rule-request Requests for a new rules.

Comments

@jpsim
Copy link
Collaborator

jpsim commented Feb 1, 2017

Clearer and lots of optimizations

@ornithocoder
Copy link
Contributor

Hi there!

Any suggestions on how to detect that $0 there?

Thanks!

$ cat file.swift 
let foo = [1, 2, 3].map { $0 }
$ sourcekitten structure --file file.swift 
{
  "key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
  "key.substructure" : [
    {
      "key.nameoffset" : 4,
      "key.accessibility" : "source.lang.swift.accessibility.internal",
      "key.length" : 30,
      "key.name" : "foo",
      "key.kind" : "source.lang.swift.decl.var.global",
      "key.namelength" : 3,
      "key.offset" : 0
    },
    {
      "key.namelength" : 13,
      "key.nameoffset" : 10,
      "key.length" : 20,
      "key.substructure" : [
        {
          "key.namelength" : 0,
          "key.nameoffset" : 0,
          "key.elements" : [
            {
              "key.kind" : "source.lang.swift.structure.elem.expr",
              "key.offset" : 11,
              "key.length" : 1
            },
            {
              "key.kind" : "source.lang.swift.structure.elem.expr",
              "key.offset" : 14,
              "key.length" : 1
            },
            {
              "key.kind" : "source.lang.swift.structure.elem.expr",
              "key.offset" : 17,
              "key.length" : 1
            }
          ],
          "key.length" : 9,
          "key.kind" : "source.lang.swift.expr.array",
          "key.bodylength" : 7,
          "key.bodyoffset" : 11,
          "key.offset" : 10
        }
      ],
      "key.name" : "[1, 2, 3].map",
      "key.bodyoffset" : 25,
      "key.bodylength" : 4,
      "key.kind" : "source.lang.swift.expr.call",
      "key.offset" : 10
    }
  ],
  "key.offset" : 0,
  "key.length" : 31
}

@marcelofabri
Copy link
Collaborator

@ornithocoder You could use "key.bodyoffset" and "key.bodylength" from the call and put together with the information of the syntaxMap. $0 will be a source.lang.swift.syntaxtype.identifier.

It'd be interesting if this rule supported all these variations:

  • foo.map({ $0 })
  • foo.map { $0 }
  • foo.map({ return $0 })
  • foo.map({ bar in\n bar })
  • foo.map({ bar in\n return bar })

@ornithocoder
Copy link
Contributor

ornithocoder commented Aug 1, 2017

Cool, thanks @marcelofabri.

@ornithocoder
Copy link
Contributor

ornithocoder commented Aug 2, 2017

Other cases to think about when implementing the rule:

var a: Int?
let foo = a.map { $0 }
let foo = zip(["a", "b", "c"], [1, 2, 3]).map { $0 }    // Should trigger the rule
let foo = zip(["a", "b", "c"], [1, 2, 3]).map { $0.0 }
let foo = zip(["a", "b", "c"], [1, 2, 3]).map { $0.1 }
let foo = zip(["a", "b", "c"], [1, 2, 3]).map { $1 }

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