jscpd
Version:
Copy/paste detector for programming code, support JavaScript, CoffeeScript, PHP, Ruby, Python, Less, Go, Java, Yaml, C#, C++, C languages
181 lines (131 loc) • 6.89 kB
Markdown
# Copy/paste detector for programming source code.
`jscpd` is a tool for detect copy/paste "design pattern" in programming source code.
| _Supported languages_ | | |
|-----------------------|--------------|---------------|
| JavaScript | Java | YAML |
| CoffeeScript | C++ | Haxe |
| PHP | C# | TypeScript |
| Go | Python | Mixed HTML |
| Ruby | C | SCSS |
| Less | CSS | |
If you need support language not from list feel free to create [request](https://github.com/kucherenko/jscpd/issues/new).
## Status
[](https://gemnasium.com/kucherenko/jscpd)
[](https://travis-ci.org/kucherenko/jscpd)
[](https://coveralls.io/r/kucherenko/jscpd?branch=master)
[](https://waffle.io/kucherenko/jscpd)
[](https://gitter.im/kucherenko/jscpd?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[](https://nodei.co/npm/jscpd/)
## Installation
npm install jscpd -g
## Usage
jscpd --path my_project/ --languages javascript,coffee
jscpd -f **/*.js -e **/node_modules/**
jscpd --files **/*.js --exclude **/*.min.js --output report.xml
jscpd --files **/*.js --exclude **/*.min.js --reporter json --output report.json
or
If you have file `.cpd.yaml` in your directory
```yaml
#.cpd.yaml
languages:
- javascript
- coffeescript
- typescript
- php
- python
- jsx
- haxe
- yaml
- css
- ruby
- go
- java
- clike # c++ or c source
- csharp # c# source
- htmlmixed # html mixed source like knockout.js templates
files:
- "test/**/*"
exclude:
- "**/*.min.js"
- "**/*.mm.js"
reporter: json
```
and run `jscpd` command, you will check code for duplicates according config from .cpd.yaml
or
```coffeescript
# coffeescript
jscpd = require('jscpd')
result = jscpd::run
path: 'my/project/folder'
files: '**/*.js'
exclude: ['**/*.min.js', '**/node_modules/**']
reporter: json
```
Please see the [minimatch documentation](https://github.com/isaacs/minimatch) for more details.
## Options:
Option | Type | Default | Description
--------------------|-----------|---------------|-------------------------------------------------------------
- -l, --min-lines | [NUMBER] | 5 | min size of duplication in code lines
- -t, --min-tokens | [NUMBER] | 70 | min size of duplication in code tokens
- -f, --files | [STRING] | * | glob pattern for find code
- -r, --reporter | [STRING] | xml | reporter name or path
- -x, --xsl-href | [STRING] | - | path to xsl file for include to xml report
- -e, --exclude | [STRING] | - | directory to ignore
- -g, --languages | [STRING] | All supported | list of languages which scan for duplicates, separated with coma
- -o, --output | [PATH] | - | path to report file
- --verbose | | - | show full info about copies
- --skip-comments| false | - | skip comments in code when duplications finding
- -b --blame | false | - | blame authors of duplications (get information about authors from git)
- -p, --path | [PATH] | Current dir | path to code
- --limit | [NUMBER] | 50 | limit of allowed duplications, if real duplications percent more then limit jscpd exit with error
- -d, --debug | | - | show debug information (options list and selected files)
- -v, --version | | - | Display the current version
- -h, --help | | - | Display help and usage details
Verbose output:

Blame mode use information from git blame and concat it with duplications report:

## Reporters
`jscpd` shipped with two standard reporters `xml` and [`json`](test/reporters/json-report.schema.json). It is possible to write custom reporter script too. For hooking reporter up wrap it into node module and provide path to it as `reporter` parameter e.g. `./scripts/jscpd-custom-reporter.coffee` (works with javascript too).
Custom reporter is a function which is executed into context of `Report` (`report.coffee`) class and thus has access to the report object and options. Expected output is array with following elements:
`[raw, dump, log]`
- `raw` is raw report object which will be passed through.
- `dump` is report which will be written into output file if any provided.
- `log` custom log output for cli.
At least one of `raw` or `dump` needs to be provided, `log` is fully optional.
## XSLT reports support
You can point xsl file for add it to xml report
```
jscpd -x reporters-xslt/simple.xsl -p test/fixtures/ -r xml -o report.xml
```
In this case report.xml will include following lines:
```
<?xml version='1.0' encoding='UTF-8' ?>
<?xml-stylesheet type="text/xsl" href="reporters-xslt/simple.xsl"?>
<pmd-cpd>
<!-- ... -->
</pmd-cpd>
```
If you open xml file in browser template from `reporters-xslt/simple.xsl` will apply to your xml and show pretty html report.
You can find example of xsl template in reporters-xslt folder.
## Run tests
```
npm test
```
```
npm run coverage
```
## Changelog
[Project changelog](https://github.com/kucherenko/jscpd/blob/master/changelog.md)
## TODO
[Project plans](https://github.com/kucherenko/jscpd/blob/master/todo.md)
## License
[The MIT License](https://github.com/kucherenko/jscpd/blob/master/LICENSE)
## Thanks
Thanks to [Mathieu Desvé](https://github.com/mazerte) for [grunt-jscpd](https://github.com/mazerte/grunt-jscpd).
Thanks to [Yannick Croissant](https://yannick.cr/) for [gulp-jscpd](https://github.com/yannickcr/gulp-jscpd).
Thanks to [linslin](https://github.com/linslin) for [grunt-jscpd-reporter](https://github.com/linslin/grunt-jscpd-reporter).
Project developed with [PyCharm](http://www.jetbrains.com/pycharm/)

Thanks to [JetBrains](http://www.jetbrains.com/) company for license key.
Feel free to contribute this project and you will have chance to get license key too.