Pecker

pecker is a tool to automatically detect unused code. It based on IndexStoreDB and SwiftSyntax.

Why use this?

During the project development process, you may write a lot of code. Over time, a lot of code is no longer used, but it is difficult to find. pecker can help you locate these unused code conveniently and accurately.

Features

pecker can detect the following kinds of unused Swift code.

  1. class
  2. struct
  3. enum
  4. protocol
  5. function
  6. typealias
  7. operator

Installation

$ git clone https://github.com/woshiccm/Pecker.git
$ cd Pecker
$ make install

With that installed and on our bin folder, now we can use it.

Usage

Xcode

  1. Click on your project in the file list, choose your target under TARGETS, click the Build Phases tab and add a New Run Script Phase by clicking the little plus icon in the top left.
  2. Paste the following script:

/usr/local/bin/pecker

Command Line Usage

pecker [OPTIONS]

  • -v/--version: Prints the pecker version and exits.
  • -i/--index-store-path: The Index path of your project, if unspecified, the default is ~Library/Developer/Xcode/DerivedData/<target>/Index/DataStore.

Run pecker in the project target to detect. Project will be searched Swift files recursively.

Rules

Current only 2 rules are included in Pecker, They are skip_public and xctest, You can also check Source/PeckerKit/Rules directory to see their implementation.

skip_public

This rule means skip detect public class, struct, function, etc. Usually the public code is provided for other users, so it is difficult to determine whether it is used. So we don't detect it by default. But in some cases, such as using submodule to organize code, you need to detect public code, you can add it to disabled_rules.

xctest

XCTest is special, we stipulate that ignore classes inherited from XCTestCase and functions of this class that hasPrefix "test" and do not contain parameters. You can add it to disabled_rules if you don't need it.

class ExampleUITests: XCTestCase {

    func testExample() { //used
    }

    func test(name: String) { // unused
    }
    
    func get() { // unsed
    }
}

Other rules

These rules are used by default, you cannot configure them.

override

Skip declarations that override another. This works for both subclass overrides & protocol extension overrides.


protocol ExampleProtocol {
	func test() // used
}

class Example: ExampleProtocol {
    func test() { // used
    }
}

class Animal {
    func run() {  // used
    }
}

class Dod: Animal {
    override func run() { // used
    }
}

extensions

Referenced elsewhere means used, except for extensions.

class UnusedExample { // unused
    
}

extension UnusedExample {
    
}

Configuration

Configure pecker by adding a .pecker.yml file from the directory you'll
run pecker from. The following parameters can be configured:

Rule inclusion:

  • disabled_rules: Disable rules from the default enabled set.

Reporter inclusion:

  • xcode: Warnings displayed in the IDE.
  • json: Generate a warnings json file.
reporter: "xcode"

disabled_rules:
  - skip_public

included: # paths to include during detecting. `--path` is ignored if present.
  - ./
  
excluded: # paths to ignore during detecting. Takes precedence over `included`.
  - Carthage
  - Pods

blacklist_files: # files to ignore during detecting, only need to add file name, the file extension default is swift.
  - HomeViewController

blacklist_symbols: # symbols to ignore during detecting, contains class, struct, enum, etc.
  - AppDelegate
  - viewDidLoad

GitHub