Swiftlint: Use SwiftSyntax from Swift 4.1?

Created on 12 Dec 2017  Â·  8Comments  Â·  Source: realm/SwiftLint

New Issue Checklist

Rule Request

Did someone take a look at https://github.com/omochi/SwiftSyntax/tree/master/Sources/SwiftSyntax ? As I understand it is a new API for parsing Swift code. I think, it will give ability to SwiftLint to "understand" Swift code structure(not only classes, functions and methods) and do linting in more accurate way.

discussion

Most helpful comment

@SDGGiesbrecht Just to complement: nothing everything is exposed by SourceKit to us. I've been doing some PRs to it to add more information that could be helpful to us, but SwiftSyntax would be much more powerful and provide more details.

One downside though is that (from my understanding) we would need to link against a specific version of it, instead of "finding it", as we do with SourceKit. This means that a certain version of SwiftLint would only reliably work with the Swift version from which SwiftSyntax was used.

IMO SwiftSyntax would be the way to go in the future, but it looks like too immature right now. I'd love to see some proofs of concept though on how would it be to use it in a SwiftLint context.

All 8 comments

Very interesting.

For now, however, the library itself recommends against using it yet. From apple/swift/lib/Syntax,

This library is a work in progress and should be expected to be in a molten state for some time. Don[’]t integrate this into other areas of the compiler or use it for anything serious just now.

It also says,

Eventually, the goal of this library is to represent Swift syntax in all of the compiler.

It sounds like when (Swift‐)Syntax is ready, SourceKit (apple/swift/tools/SourceKit) itself will be powered by it. SwiftSyntax may enable more fine‐grained queries, but whatever “understanding” it has gets inherited by the higher‐level SourceKit. Since it is on SourceKit that SwiftLint is built even now, resulting improvements will be inherited for free.

Still very much worth watching to see how SwiftSyntax evolves. Thanks for the tip @SiarheiFedartsou!

@SDGGiesbrecht Just to complement: nothing everything is exposed by SourceKit to us. I've been doing some PRs to it to add more information that could be helpful to us, but SwiftSyntax would be much more powerful and provide more details.

One downside though is that (from my understanding) we would need to link against a specific version of it, instead of "finding it", as we do with SourceKit. This means that a certain version of SwiftLint would only reliably work with the Swift version from which SwiftSyntax was used.

IMO SwiftSyntax would be the way to go in the future, but it looks like too immature right now. I'd love to see some proofs of concept though on how would it be to use it in a SwiftLint context.

SwiftSyntax will probably be locatable alongside SourceKit.

(As opposed to using the extracted package linked from the first comment, which would involve the drawbacks you mentioned, @marcelofabri.)

There is just nothing to locate without Swift 4.1.

Worth noting too that SourceKit provides the structure from a later point after the parser, which means that it can sometimes disambiguate what the parser thought it saw. So moving entirely to SwiftSyntax, even when it's been completed, might still not be sufficient for all our existing rules.

SwiftSyntax was not released with Swift 4.1. Next release maybe?

Any updates on this? Seems like blocking performance issues in SwiftSyntax (mentioned in #2476) were fixed with the release of Swift 5.1.

@MaxDesiatov would you be interested in refreshing https://github.com/realm/SwiftLint/pull/2476 to see how it performs with Swift 5.1.3? Some updated performance numbers could help assess the viability of using SwiftSyntax.

Using SwiftSyntax is now possible after https://github.com/realm/SwiftLint/pull/3054. I recommend using it only for opt-in rules for now, at least until Swift 5.2 is released, which will contain a few speed optimizations (https://github.com/apple/swift-syntax/pull/155 and https://github.com/apple/swift-syntax/pull/158)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rajohns08 picture rajohns08  Â·  3Comments

larslockefeer picture larslockefeer  Â·  3Comments

jcarroll-mediafly picture jcarroll-mediafly  Â·  3Comments

msanders picture msanders  Â·  4Comments

BalestraPatrick picture BalestraPatrick  Â·  3Comments