Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vigneshshanmugam/js-cpa

Code pattern analysis for JS
https://github.com/vigneshshanmugam/js-cpa

bundle-analyzer code-duplication code-quality code-repetition

Last synced: 5 days ago
JSON representation

Code pattern analysis for JS

Awesome Lists containing this project

README

        

# js-cpa

Identify structurally similar functions that are duplicated across a JavaScript bundle/file seamlessly.

Running it on [Inferno](https://github.com/infernojs/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

+ [bundle-duplicates-plugin](https://github.com/vigneshshanmugam/bundle-duplicates-plugin) - Identify duplicate functions across all webpack JS bundles.

### Installation

```sh
npm install -g js-cpa
```

### CLI
```sh
Usage: js-cpa [options]

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 (js|ts|flow)
-t, --threshold threshold (in bytes)
-C, --no-colors disable colors in output
-R, --report generate reports (html|term)
-o, --report-file path for report generation
-h, --help output usage information
```

### API

```js
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