Copy/paste detector for source code that supports 150+ programming languages and provides both CLI and programmatic APIs
npx @tessl/cli install tessl/npm-jscpd@4.0.0JSCPD 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.
npm install -g jscpd (global) or npm install jscpd (local)import { detectClones, jscpd } from "jscpd";For CommonJS:
const { detectClones, jscpd } = require("jscpd");# 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 ./reportsimport { 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"
]);JSCPD is built as a modular system with several key components:
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;
}Command-line interface with full argument processing and configuration file support.
function jscpd(
argv: string[],
exitCallback?: (code: number) => {}
): Promise<IClone[]>;Advanced configuration system supporting multiple sources and validation.
function initOptionsFromCli(cli: Command): IOptions;
function prepareOptions(cli: Command): IOptions;// 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;
}