Identify structurally similar functions that are duplicated across a JavaScript bundle/file seamlessly.
Running it on Inferno
- Works only across function boundaries
- Matches the longest common subsequence and ignores the children
- Ignores comments on the output
- HTML report generation
- bundle-duplicates-plugin - Identify duplicate functions across all webpack JS bundles.
npm install -g js-cpa
Usage: js-cpa [options] <file ...>
Options:
-V, --version output the version number
-f, --filelist read filelist from STDIN stream - if/when you cross ARG_MAX. eg: ls *.js | js-cpa -f
-m, --module parse files with sourceType=module
-l, --language <language> language (js|ts|flow)
-t, --threshold <n> threshold (in bytes)
-C, --no-colors disable colors in output
-R, --report <type> generate reports (html|term)
-o, --report-file <path> path for report generation
-h, --help output usage information
const { findDuplicates, stringify }= require('js-cpa');
const fs = require("fs");
const code = fs.readFileSync(filePath, "utf-8");
const duplicates = findDuplicates(code, {
filename: "test"
});
process.stdout.write(stringify(duplicates)); // prints to stdout
- filename - name of the file used in the output
- sourceType - denotes the mode the code should be parsed in. eg - script|module
- language - denotes the language. eg - (js|ts|flow)
- threshold - threshold in bytes
Finds the optimal duplicate functions that are structurally similar. It tries to find the longest matching subexpression across the AST which ignores the children(inner function) if the parent(outer function) is already mached
Finds all duplicate functions that are structurally similar.
Gets the output in a more presentable way
Options
- colors - enable colors on the stdout
- newline - prints newline after each duplicates