SwiftStepdown never builds your code. It reads each declaration's uses of names with swift-syntax and matches them to the declarations beside it the way Swift looks a name up inside a type, which finds the type's own members before anything outside it:
- What a use is written on. A bare name,
self.,Self., or the type's own name can mean a member beside it;other.,super., a leading dot, and a key path never do. - Labels. A call matches the overloads whose parameters its labels fit, leaving out defaulted ones
and filling the rest with trailing closures.
#selector(load(id:))and other compound names match their labels exactly. Overloads only types tell apart all match. - Static or not. A use in an instance member matches instance members, one in a static member matches static ones, and nested types match from either.
- Local names. A parameter, a
let, a closure's argument, or a loop variable with the same name hides the member, within Swift's scopes:let items = items.sorted()still reads the member on the right, and aguard letbinds to the end of its block. - Values and functions. A bare name used as a value, with a member, a subscript, or in a
forloop, matches only properties; one passed along, as inmap(transform), can match a function. - Nested types and extensions use the declarations around them only by names they do not declare themselves, and only the static ones of an enclosing type.
Accuracy measures how often this agrees with the compiler.
SwiftStepdown reads each file on its own, once, so a run costs about as much as the files it is given. On an Apple M5, with a release build on one core:
| Run | Time |
|---|---|
| Starting up | 3 ms |
| One file | 5 ms typical, 17 ms for a 1,600-line file |
| Checking the five files a commit touches | 10 to 30 ms |
| Checking a whole 67,000-line package | 0.5 s |
| Reordering that whole package | 0.55 s |
A commit hook that checks only what a commit touches adds no wait anyone notices; a whole codebase is
the only run long enough to see. What can cost more than SwiftStepdown itself is how it is started:
swift run spends a second or two making sure its build is current, every time. A hook that runs on
every commit should call the built binary instead (Installation).
SwiftStepdown's two layers are libraries a tool can build on:
| Library | Holds |
|---|---|
StepdownRead |
one pass over a file: its bodies, their declarations, and each declaration's uses, matched by name |
StepdownOrder |
the ordering: the style, where each declaration belongs, and the rewrite or the findings |
.package(url: "https://github.com/acfriedman/SwiftStepdown", from: "0.1.1")
// in a target's dependencies:
.product(name: "StepdownOrder", package: "SwiftStepdown")StepdownOrder depends only on StepdownRead, and StepdownRead only on swift-syntax.
Stepdown.order(_:edges:style:) takes links from anywhere, so a tool with the compiler's index at hand
can order by those instead. Each module's README.md under Sources lists its types.
The repository holds itself to its own standards. SwiftFormat and SwiftLint keep the code in one style,
with their defaults and the few settings in .swiftformat and .swiftlint.yml; the doc linter pinned in
BuildTools checks every doc comment and module page, tests included; and SwiftStepdown keeps its own
declarations in stepdown order:
brew install swiftformat swiftlint
swiftformat .
swiftlint lint
swift run --quiet -c release --package-path BuildTools swiftlucid --include-tests .
swift run --quiet -c release swiftstepdown --lint Sources Tests Measure/Sources
swift testThe commit hook in .githooks/pre-commit runs all four on what each commit touches: it formats and
restages the files, then stops the commit on a SwiftLint error, a doc finding, or a declaration out of
order. SwiftLint's warnings are printed but never block. Turn the hook on once per clone with
git config core.hooksPath .githooks.
The Measure folder is a package of its own, so IndexStoreDB and the C++ it builds stay out of the
formatter. Its stepdown-measure command compares name matching with the compiler's index on any Swift
package:
# Build the package you want to measure, writing its index somewhere you choose.
swift build --package-path ~/Code/MyApp --scratch-path /tmp/myapp-build --enable-index-store
# Build the study, then point it at the sources and the index.
cd Measure && swift build -c release
.build/release/stepdown-measure --samples /tmp/samples.tsv \
MyApp=$HOME/Code/MyApp/Sources=/tmp/myapp-build/arm64-apple-macosx/debug/index/storeName several packages to get a total. It prints each package's verdicts, link precision and recall, how
the two orders compare under the default style and with everything moving, and the cause of each
disagreement. --samples writes every disagreement, with its line of source, to a tab-separated file.
It reads index stores with the toolchain's libIndexStore.dylib from Xcode, and its IndexStoreDB is the
one tagged for Swift 6.3.1; a much newer or older toolchain may need the tag in Measure/Package.swift
moved to match.