Image you want to write a developer tool for Swift. You probably want to parse, inspect, generate and transform Swift source code.
What are your best options?
Old School: SourceKit / SourceKitten
Apple provides SourceKit as a framework for supporting IDE features like indexing, syntax-coloring, code-completion, etc. That's how Xcode gets its intelligence. Here is a really good article if you wanna know more about SourceKit.
The gold standard is (was?) SourceKitten , a open source and community-maintained library which significantly reduces the complexity to communicate with SourceKit directly.
It is very popular (~ 1.9k stars on Github) and is used by well-known tools for Swift developers like
- SwiftLint : A tool to enforce Swift style and conventions.
- Jazzy : Soulful docs for Swift & Objective-C.
- Sourcery: Meta-programming for Swift, stop writing boilerplate code.
But wait! Sourcery recently switched from SourceKit/SourceKitten to SwiftSyntax and announced significant performance improvements.
The new kid: SwiftSyntax
SwiftSyntax is a set of Swift bindings for the libSyntax library. It allows for Swift tools to parse, inspect, generate, and transform Swift source code.
According to Apple:
Its API is designed for performance critical applications. It uses value types almost exclusively and aims to avoid existential conversions where possible.
There is a really nice Swift Abstract Syntax Tree (AST) visualizer
It looks for me that SwiftSyntax can provide information, which were previously very expensive to obtain in SourceKit/SourceKitten, much faster. For example, SourceKit/SourceKitten has to ask the Swift compiler to determine the runtime name of an inferred type (
var hasBasket = false // type is Bool) or to determine the used types within a typealias.
I am planning to evaluate and probably switch from SourceKitten to SwiftSyntax for my open source project SwiftPlantUML.
Then I'll provide feedback about my experience with SwiftSyntax.
This is merely a teaser :)