or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli-integration.mdclone-detection.mdconfiguration.mdindex.md
tile.json

tessl/npm-jscpd

Copy/paste detector for source code that supports 150+ programming languages and provides both CLI and programmatic APIs

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/jscpd@4.0.x

To install, run

npx @tessl/cli install tessl/npm-jscpd@4.0.0

index.mddocs/

JSCPD

JSCPD is a comprehensive copy/paste detector for programming source code that supports over 150 programming languages and document formats. It implements the Rabin-Karp algorithm for efficient duplication detection and provides both CLI and programmatic APIs for integration into development workflows.

Package Information

  • Package Name: jscpd
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install -g jscpd (global) or npm install jscpd (local)

Core Imports

import { detectClones, jscpd } from "jscpd";

For CommonJS:

const { detectClones, jscpd } = require("jscpd");

Basic Usage

CLI Usage

# Global installation
npm install -g jscpd
jscpd /path/to/source

# Or using npx
npx jscpd /path/to/source

# With pattern matching
jscpd --pattern "src/**/*.js"

# With configuration options
jscpd --min-lines 5 --min-tokens 50 --reporters console,html --output ./reports

Programmatic Usage

import { detectClones, jscpd } from "jscpd";

// Direct clone detection
const clones = await detectClones({
  path: ["./src"],
  minLines: 5,
  minTokens: 50,
  format: ["javascript", "typescript"],
  reporters: ["console"]
});

console.log(`Found ${clones.length} code duplications`);
clones.forEach(clone => {
  console.log(`Format: ${clone.format}`);
  console.log(`Duplication A: ${clone.duplicationA.sourceId}:${clone.duplicationA.start.line}-${clone.duplicationA.end.line}`);
  console.log(`Duplication B: ${clone.duplicationB.sourceId}:${clone.duplicationB.start.line}-${clone.duplicationB.end.line}`);
});

// CLI-style programmatic usage
const clones2 = await jscpd([
  "node", "jscpd", "./src", 
  "--min-lines", "5", 
  "--format", "javascript,typescript"
]);

Architecture

JSCPD is built as a modular system with several key components:

  • Core Detection Engine: Implements Rabin-Karp algorithm for efficient pattern matching
  • Tokenizer: Language-specific tokenization supporting 150+ formats
  • File Finder: Glob-based file discovery with ignore patterns
  • Reporter System: Multiple output formats (console, HTML, JSON, SARIF, XML)
  • Configuration System: CLI args, config files, and package.json integration
  • Store System: Pluggable storage backends for large codebases

Capabilities

Programmatic Clone Detection

Core API for detecting code duplications programmatically. Ideal for CI/CD integration and custom tooling.

function detectClones(
  opts: IOptions, 
  store?: IStore<IMapFrame>
): Promise<IClone[]>;

interface IOptions {
  path?: string[];
  minLines?: number;
  minTokens?: number;
  maxLines?: number;
  maxSize?: string;
  threshold?: number;
  format?: string[];
  pattern?: string;
  ignore?: string[];
  ignorePattern?: string[];
  reporters?: string[];
  output?: string;
  mode?: string;
  silent?: boolean;
  debug?: boolean;
  verbose?: boolean;
  // ... additional options
}

interface IClone {
  format: string;
  isNew?: boolean;
  foundDate?: number;
  duplicationA: IDuplication;
  duplicationB: IDuplication;
}

Clone Detection API

CLI Integration

Command-line interface with full argument processing and configuration file support.

function jscpd(
  argv: string[], 
  exitCallback?: (code: number) => {}
): Promise<IClone[]>;

CLI Integration

Configuration Management

Advanced configuration system supporting multiple sources and validation.

function initOptionsFromCli(cli: Command): IOptions;
function prepareOptions(cli: Command): IOptions;

Configuration

Types

// External types from dependencies
interface Command {
  // Commander.js Command interface from 'commander' package
  // Full Command interface available at: https://github.com/tj/commander.js/
}

interface IBlamedLines {
  // Git blame information for duplicated lines
  // From @jscpd/core package
}

interface IDuplication {
  sourceId: string;
  start: ITokenLocation;
  end: ITokenLocation;
  range: [number, number];
  fragment?: string;
  blame?: IBlamedLines;
}

interface ITokenLocation {
  line: number;
  column?: number;
  position?: number;
}

interface IMapFrame {
  // Internal type for duplicate detection
}

interface IStore<T> {
  namespace(name: string): void;
  get(key: string): Promise<T>;
  set(key: string, value: T): Promise<T>;
  close(): void;
}