Skip to content

lawlesx/PastaPolice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PastaPolice

A CLI tool to detect copy-paste code (duplicate pasta) in your TypeScript/JavaScript projects.

Features

  • Semantic Detection (default): Finds semantically similar functions using AST-based normalization (different variable names, same logic)
  • Syntactic Detection: Finds exact duplicate code blocks using line-based hashing

Installation

Using Bun (recommended)

bun install -g pastapolice

Using npm

npm install -g pastapolice

Usage

pastapolice <path> [options]

Options

Option Alias Description Default
--min-lines -m Minimum lines for a code block 5
--syntactic -s Use syntactic (line-based) detection false

Examples

# Scan current directory (semantic mode)
pastapolice .

# Scan with custom minimum lines
pastapolice . -m 10

# Syntactic mode (exact duplicates)
pastapolice . --syntactic

# Scan specific directory
pastapolice ./src -m 3

Modes

Semantic Detection (Default)

Uses AST-based normalization to find functions that do the same thing but have different variable names, formatting, or minor syntax differences.

Syntactic Detection

Finds exact duplicate code blocks by comparing normalized lines. Good for catching literal copy-paste.

How It Works

Semantic Mode

  1. Parses TypeScript files using TypeScript compiler API
  2. Extracts function declarations, methods, arrow functions, and function expressions
  3. Normalizes by removing comments and replacing:
    • Identifiers → VAR1, VAR2, VAR3...
    • String/numeric literals → LIT
  4. Hashes normalized representation
  5. Groups functions with identical hashes as semantic duplicates

Syntactic Mode

  1. Sliding window scans files for consecutive lines
  2. Normalizes lines (removes whitespace)
  3. Hashes using xxhash64
  4. Groups identical hashes as duplicates

Development

# Install dependencies
bun install

# Run directly
bun run src/index.ts .

# Build
bun run build

License

MIT

About

Duplicate code detector for typescript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors