attractor-swift
Swift plugin for attractor, the churn vs. complexity tool. Complexity is the per-function cyclomatic complexity reported by lizard; churn comes from attractor's git history.
Installation
gem install attractor attractor-swiftlizard is a Python tool. attractor runs it through uv when
lizard itself is not on your PATH:
# one of
brew install uv # then `uvx lizard` fetches lizard on first use
pip install lizardattractor finds the plugin by its gem name; nothing to configure.
Usage
cd my-swift-project
attractor calc -c 0 --format table # every file, table
attractor calc --format json # per-function details with line ranges
attractor diff --base main --head HEAD -c 0 # complexity delta of a branch
attractor report # the interactive chartMethod-level details carry lizard's CCN and location:
"details": {
"total": {"score": 3, "line": 6, "end_line": 15},
"pay": {"score": 3, "line": 24, "end_line": 30}
}Two things to know about the numbers:
- CCN is McCabe cyclomatic complexity, a different scale from flog's Ruby score. attractor reports per language, so they are never compared.
- lizard names functions without their type (
total, notCart.total). Two functions with the same name in one file are kept apart by their start line (card@32,card@50).
Force a specific runner with ATTRACTOR_LIZARD="python -m lizard".
Using this repository as a template for another language
Everything language-specific is two strings and a detector. To make attractor-kotlin:
- Copy this repository. Rename
swift→kotlinin file names, module names, the gemspec and the workflow. -
lib/attractor/calculators/kotlin_calculator.rb:language: "kotlin"(a name fromlizard --help, under-l),file_extension: "kt"(a regex-ish string is fine:"(kt|kts)"),@type = "Kotlin". -
lib/attractor/detectors/kotlin_detector.rb: what marks a project (build.gradle.kts, any*.kt). - Replace the fixture in
spec/fixtures/and the expected numbers in the calculator spec; runuvx lizard -l kotlin --csv spec/fixtures/...once to get them. - Push, tag,
rake release.
Languages lizard scores today include C/C++, C#, Go, Java, JavaScript, Kotlin, Lua, Objective-C, PHP, Python, Rust, Scala, Swift, TypeScript and more. For languages with a better native metric tool (Ruby has flog, JavaScript has escomplex with class and import context), write the calculator against that tool instead, as attractor-ruby and attractor-javascript do.
Development
bin/setup
bundle exec rspec # the lizard integration example skips without lizard or uv
bundle exec standardrbAgainst an unreleased attractor: ATTRACTOR_PATH=../attractor bundle install.
License
MIT.