Metadata-Version: 2.4
Name: code-prune
Version: 1.3.0
Summary: Find unreachable, unused, and obsolete code before it becomes technical debt.
License: MIT License
         
         Copyright (c) 2026 Ron Webb
         
         Permission is hereby granted, free of charge, to any person obtaining a copy
         of this software and associated documentation files (the "Software"), to deal
         in the Software without restriction, including without limitation the rights
         to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
         copies of the Software, and to permit persons to whom the Software is
         furnished to do so, subject to the following conditions:
         
         The above copyright notice and this permission notice shall be included in all
         copies or substantial portions of the Software.
         
         THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
         IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
         FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
         AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
         LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
         OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
         SOFTWARE.
License-File: LICENSE
Keywords: cli,static-analysis,dead-code,unused-code,tree-sitter,code-quality
Author: Ron Webb
Requires-Python: >=3.14
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Dist: braincraft (>=1.3.1,<2.0.0)
Requires-Dist: click (>=8.0.0,<9.0.0)
Requires-Dist: env-dir-bootstrap (>=1.0.0,<2.0.0)
Requires-Dist: logenrich (>=1.0.1,<2.0.0)
Requires-Dist: rich (>=15.0.0,<16.0.0)
Requires-Dist: tree-sitter (>=0.26.0,<0.27.0)
Requires-Dist: tree-sitter-gosu (>=0.3.0,<0.4.0)
Requires-Dist: tree-sitter-java (>=0.23.5,<0.24.0)
Requires-Dist: tree-sitter-javascript (>=0.25.0,<0.26.0)
Requires-Dist: tree-sitter-python (>=0.25.0,<0.26.0)
Requires-Dist: tree-sitter-typescript (>=0.23.2,<0.24.0)
Project-URL: Changelog, https://github.com/rcw3bb/code-prune/blob/main/CHANGELOG.md
Project-URL: Homepage, https://github.com/rcw3bb/code-prune
Project-URL: Issues, https://github.com/rcw3bb/code-prune/issues
Project-URL: Repository, https://github.com/rcw3bb/code-prune
Description-Content-Type: text/markdown

# code-prune

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/rcw3bb/code-prune/blob/main/LICENSE) [![Version](https://img.shields.io/badge/Version-1.3.0-green.svg)](https://github.com/rcw3bb/code-prune/blob/main/CHANGELOG.md) [![Python](https://img.shields.io/badge/Python-3.14%2B-blue)](https://www.python.org/) [![PyPI](https://img.shields.io/badge/PyPI-code--prune-orange)](https://pypi.org/project/code-prune/)


A CLI tool that scans a codebase for unreachable, unused, and obsolete code,
helping you find dead code before it becomes technical debt.

## Features

`code-prune` parses source files with [tree-sitter](https://tree-sitter.github.io/tree-sitter/)
and reports six categories of findings across **Python, JavaScript,
TypeScript, Java, and Gosu**:

- **Unreachable code** — statements after `return`/`raise`/`throw`/`break`/
  `continue`, and `if` branches whose condition is a constant falsy literal
  (`False`/`false`/`0`). Comments are never reported as unreachable.
- **Unused imports & local variables** — per-file, syntactic detection of
  bindings that are never referenced again in the same file.
- **Unused functions/classes/methods/fields** — cross-file detection of
  definitions that are never referenced anywhere in the scanned project. Fields
  cover class-level fields in every language, Python `self.x` attributes,
  JavaScript `#private` fields, TypeScript interface properties and constructor
  parameter properties, and Java enum constants.
- **Obsolete markers** — `@deprecated`/`@Deprecated`-style decorators and
  annotations, plus comments mentioning `deprecated`.
- **TODO markers** — `TODO`/`FIXME`/`XXX` comment tags (not necessarily
  obsolete, so reported separately).
- **Commented-out code** — comments whose content heuristically resembles
  source code. Documentation comments (Javadoc/JSDoc `/** ... */`) and Python
  docstrings are not treated as commented-out code.

Only supported files (`.py`, `.pyi`, `.js`, `.mjs`, `.cjs`, `.ts`, `.tsx`,
`.java`, `.gs`, `.gsx`) are read; every other file is skipped.

> Detection is heuristic and syntactic rather than a full scope- or
> call-graph-aware analysis — treat findings as a starting point for review,
> not a guarantee.

## Requirements

- Python `>=3.14`

## Installation

```powershell
pip install code-prune
```

## Usage

```powershell
code-prune <path> [options]
```

`<path>` may be a file, a directory, or several of them separated by `;`.

Common options:

| Option            | Description                                                                 |
|-------------------|------------------------------------------------------------------------------|
| `--ext`           | Comma-separated extensions to include (e.g. `py,js,ts,java,gs,gsx`). Omit to scan every supported file. |
| `--categories`    | Comma-separated categories to report: `unreachable`, `unused-import`, `unused-local`, `unused-symbol`, `obsolete`, `todo-marker`, `commented-out`. `unused` is shorthand for all three `unused-*` categories (default: all). |
| `--format`        | Output format: `json`, `html`, or `all` (default: `json`).                  |
| `--output`        | Base output filename without extension (default: `code-prune-output`).     |
| `--output-dir`    | Directory for the report file(s) (default: `reports`).                     |
| `--workers`       | Number of parallel worker threads used to scan files.                      |
| `--db`            | Path to the SQLite scratch database used during a scan run.                |
| `--target-list`   | Treat `path` as a file listing target paths, one per line.                 |
| `--search-path`   | Files and/or directories (separated by `;`) searched textually for each `unused-symbol` candidate; a symbol found there is not reported. Classes, functions/methods and fields are all matched by a whole-word regex; a method or field additionally requires its enclosing class's name to appear in the same file. The symbol's own file is skipped and `.codepruneignore` rules apply. |
| `--search-ext`    | Comma-separated extensions of the files searched in `--search-path` (e.g. `xml,json`). Omit to search every text file. Requires `--search-path`. |

Example:

```powershell
code-prune src --categories unreachable,unused-import,todo-marker --format all
code-prune src --categories unused-symbol --search-path "templates;../other-repo" --search-ext xml,json,py
```

### Reports

Both report formats include the number of findings per category: the JSON
report has a `category-counts` object, and the HTML report shows the counts in
a category dropdown filter listing the categories present in the report. The
HTML summary line shows only the total number of findings.

## Configuration

Logging is bootstrapped via `env-dir-bootstrap`. Set the `CODE_PRUNE_CONFIG_DIR`
environment variable to override the default configuration directory used to
resolve `logging.ini`, `config.ini`, `.codepruneignore`, and `symbol_ignore`.

### Ignoring files and directories

`.codepruneignore` holds gitignore-style patterns for files/directories to
exclude from scanning (mirroring `.gitignore` syntax). Override the filename
used in the configuration directory via `config.ini`:

```ini
[override]
ignore-file = .codepruneignore
```

In addition, a `.codepruneignore` placed in the target path (the scanned
directory, or the directory containing a scanned file) extends those patterns.
It always uses that fixed name and is not affected by `config.ini`.

### Ignoring symbol names

`symbol_ignore` holds one regex pattern per line, matched against symbol
names to suppress from "unused function/class/method/field" findings — useful for
dunder methods, `main`, test hooks (`test_*`, `setUp`), `serialVersionUID`, and similar
intentionally-unreferenced symbols.

## Development

Development uses [Poetry](https://python-poetry.org/) `2.2`.

Install the project and its dev dependencies:

```powershell
poetry install
```

Run the CLI from the working tree:

```powershell
poetry run code-prune <path> [options]
```

Run tests with coverage and generate an HTML report:

```powershell
poetry run pytest --cov=code_prune tests --cov-report html
```

Format and lint the code:

```powershell
poetry run black code_prune; poetry run pylint code_prune
```

## License

This project is licensed under the [MIT License](LICENSE).

