Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/vigneshshanmugam/js-cpa
- Owner: vigneshshanmugam
- License: mit
- Created: 2017-07-03T22:31:42.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-06T20:22:17.000Z (about 2 years ago)
- Last Synced: 2024-04-14T01:00:16.084Z (9 months ago)
- Topics: bundle-analyzer, code-duplication, code-quality, code-repetition
- Language: JavaScript
- Size: 327 KB
- Stars: 126
- Watchers: 4
- Forks: 11
- Open Issues: 23
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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)
### 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 wayOptions
+ colors - enable colors on the stdout
+ newline - prints newline after each duplicates