js-cpa

Code pattern analysis for JS

  • js-cpa

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
js-cpa
124230.2.16 years ago6 years agoMinified + gzip package size for js-cpa in KB

Readme

js-cpa
Identify structurally similar functions that are duplicated across a JavaScript bundle/file seamlessly.
Running it on Inferno
CPA on Inferno

Features

  • Works only across function boundaries
  • Matches the longest common subsequence and ignores the children
  • Ignores comments on the output
  • HTML report generation

Related

Installation

npm install -g js-cpa

CLI

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

API

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

Options
  • 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

findDuplicates(code, opts)
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

findAllDuplicates(code, opts)
Finds all duplicate functions that are structurally similar.

stringify(duplicates, options)
Gets the output in a more presentable way
Options
  • colors - enable colors on the stdout
  • newline - prints newline after each duplicates