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

Comments

Projects
None yet
3 participants
@jpsim
Collaborator

jpsim commented Feb 1, 2017

Clearer and lots of optimizations

@ornithocoder

This comment has been minimized.

Show comment
Hide comment
@ornithocoder

ornithocoder Aug 1, 2017

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
}
Contributor

ornithocoder commented Aug 1, 2017

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

This comment has been minimized.

Show comment
Hide comment
@marcelofabri

marcelofabri Aug 1, 2017

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 })
Collaborator

marcelofabri commented Aug 1, 2017

@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

This comment has been minimized.

Show comment
Hide comment
@ornithocoder

ornithocoder Aug 1, 2017

Contributor

Cool, thanks @marcelofabri.

Contributor

ornithocoder commented Aug 1, 2017

Cool, thanks @marcelofabri.

@ornithocoder

This comment has been minimized.

Show comment
Hide comment
@ornithocoder

ornithocoder Aug 2, 2017

Contributor

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 }
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 }

marcelofabri added a commit to marcelofabri/SwiftLint that referenced this issue Sep 16, 2017

marcelofabri added a commit to marcelofabri/SwiftLint that referenced this issue Sep 16, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment