Giter Site home page Giter Site logo

pecker's Introduction

Pecker

Note: Current same users may encounter some problems,such as "dyld: Library not loaded: @rpath/lib_InternalSwiftSyntaxParser.dylib" or not show warning. This is SwiftSyntax issue, SwiftSyntax with Swift 5.1, pls check it.

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

If you have questions, feel free to connect me, my Twitter Roy, Email: [email protected].

Language Switch: 中文.

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

Using Homebrew

$ brew install woshiccm/homebrew-tap/pecker

Using CocoaPods:

pod 'Pecker'

This will download the Pecker binaries and dependencies in Pods/ during your next pod install execution and will allow you to invoke it via ${PODS_ROOT}/Pecker/bin/pecker in your Script Build Phases.

This is the recommended way to install a specific version of Pecker since it supports installing a pinned version rather than simply the latest.

Using Mint:

mint install woshiccm/Pecker

Manually

$ 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

Integrate Pecker into an Xcode scheme to get warnings and errors displayed in the IDE. Just add a new "Run Script Phase" with:

if which pecker >/dev/null; then
  pecker
else
  echo "warning: Pecker not installed, download from https://github.com/woshiccm/Pecker"
fi

Alternatively, if you've installed Pecker via CocoaPods the script should look like this:

${PODS_ROOT}/Pecker/bin/pecker

Terminal

Note:

  1. In terminal, can't get project index path automatically, so you need to set index path through -i/--index-store-path
  2. Need to set set report as json and set output_file, the path should be absolute path, if not specified output_file, the default is your project directory path.

For example:

.pecker.yml configuration:

reporter: "json"

output_file: "/Users/ming/Desktop/PeckerResultDirectory"

terminal input

pecker /Users/ming/Desktop/Testttt -i /Users/ming/Library/Developer/Xcode/DerivedData/Testttt-aohluxvofrwtfagozexmpeifvryf/Index/DataStore

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/{your project}/Index/DataStore.

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

Rules

Current only 5 rules are included in Pecker, They are skip_public, xctest, attributes, xml, comment. You can add them to disabled_rules if you don't need it. 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.

class ExampleUITests: XCTestCase {

    func testExample() { //used
    }

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

attributes

If a Declaration contains the attribute in BlackListAttribute, skip. Such as IBAction, we are continuously collecting, if you find new cases, please let us know.

@IBAction func buttonTap(_ sender: Any) { // used
        
}

xml

If code is used in xib or storyboard, it also means used.

comment

Code can be ignore with a comment inside a source file with the following format:

  • Ignore specified code
// pecker:ignore 

For example:

// pecker:ignore
class TestCommentObject { // skip
    
    // pecker:ignore
    func test1() { // skip
    }
    
    func test2() { // unused
    }
}
  • Ignore all symbols under scope
// pecker:ignore all

For example:

// pecker:ignore all
class TestCommentObject { // skip
    
    func test1() { // skip
    }
    
    struct SubClass { // skip
        
        func test2() { // skip
        }
    }
}

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 Dog: Animal {
    override func run() { // used
    }
}

extensions

Referenced elsewhere means used, except for extensions.

class UnusedExample { // unused
    
}

extension UnusedExample {
    
}

Configuration

This is optinal, will use default, if unspecified. 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 json file named pecker.result.json, you can set path by output_file, and the path should be absolute path, if unspecified, the default is current project directory path.

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

blacklist_superclass: # all the class inherit from class specified in the list will ignore
    - UITableViewCell

output_file: "/Users/ming/Desktop/PeckerResultDirectory"

Contributions and support

pecker is developed completely in the open.

Any contributing and pull requests are warmly welcome. If you are interested in developing pecker, submit ideas and submit pull requests!

Contributors

Licence

pecker is released under the MIT License.

pecker's People

Contributors

woshiccm avatar rastersize avatar sinchang avatar hodovani avatar noahsark769 avatar heyvito avatar yume190 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.