# SwiftSyntax

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](https://github.com/apple/swift/tree/main/tools/SourceKit) as a framework for supporting IDE features like
indexing, syntax-coloring, code-completion, etc. That's how Xcode gets its intelligence.  [Here](https://www.jpsim.com/uncovering-sourcekit/) is a really good article if you wanna know more about SourceKit.

The gold standard is (was?) [SourceKitten](https://github.com/jpsim/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](https://github.com/realm/SwiftLint) : A tool to enforce Swift style and conventions.
-  [Jazzy](https://github.com/realm/Jazzy) : Soulful docs for Swift & Objective-C.
-  [Sourcery](https://github.com/krzysztofzablocki/Sourcery): Meta-programming for Swift, stop writing boilerplate code.

But wait! Sourcery [recently](https://github.com/krzysztofzablocki/Sourcery/releases/tag/1.3.0) 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.

%[https://github.com/apple/swift-syntax]

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](https://swift-ast-explorer.kishikawakatsumi.com/)  

![Swift AST Explorer](https://cdn.hashnode.com/res/hashnode/image/upload/v1615348676589/-4YoG782C.png)

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.

%[https://github.com/MarcoEidinger/SwiftPlantUML]

Then I'll provide feedback about my experience with SwiftSyntax.

This is merely a teaser :)


