Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: benny! on January 03, 2012

Title: Javascript Copy&Paste Detection
Post by: benny! on January 03, 2012
Hi,

does anyone of you know a Copy&Paste detection tool (preferably free) that works for JavaScript. What I am looking for is something like PMD (http://pmd.sourceforge.net/) for Javascript.

Any help is appreciated.

Best,
benny!
Title: Re: Javascript Copy&Paste Detection
Post by: combatking0 on January 03, 2012
Do you mean copied & pasted code or the use of the paste feature at runtime?
Title: Re: Javascript Copy&Paste Detection
Post by: benny! on January 04, 2012
I mean Copy&Pasted code - basically a programm which parses the complete javascript sourcecodes and checks for redundant code bits.
Title: Re: Javascript Copy&Paste Detection
Post by: Raizor on January 04, 2012
I've got some code that reads the contents of the clipboard that I use at work for pasting a license key using javascript:

var PastedText = document.getElementById("textfield").createTextRange();
PastedText.execCommand("Paste");

I've only tested that in IE, so not sure if it works in other browsers. It's attached to a button click event, you might need to do the same as I doubt arbitrary clipboard access is allowed.
Title: Re: Javascript Copy&Paste Detection
Post by: benny! on January 04, 2012
Thanks Raizor.

But what I am looking for is a programm that scans the files/sources on the harddisk and parses the javascript and compares across all files if there are redundant code bits. I want to add this into our build process, so all programmers get notified when checking in redundant code into our SVN.
Title: Re: Javascript Copy&Paste Detection
Post by: Raizor on January 04, 2012
Hmm, so a file compare that compares functions within different js files to determine if they are the same?
Title: Re: Javascript Copy&Paste Detection
Post by: benny! on January 04, 2012
Hmm, so a file compare that compares functions within different js files to determine if they are the same?

Kind of. Those programms do not simply compare files line by line. They really parses the code and see if they are redundant parts of a certain size (on a compiler-like basis). In addition those programms checks for unreachable code, unused variables and stuff like that. There are some cool tools available for PHP, Java and I guess C++, too - but I cannot find a working solution for javascript code.
Title: Re: Javascript Copy&Paste Detection
Post by: benny! on January 04, 2012
To find duplicate code those programms use the so called Rabin Karp Algorithm (http://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_string_search_algorithm) and basically I have such kind of programs in mind:

PMD Software (http://en.wikipedia.org/wiki/PMD_(software))
Title: Re: Javascript Copy&Paste Detection
Post by: Raizor on January 04, 2012
Ah, I see. That could be very useful, yeah :)

Apparently this handles javascript:

http://www.semdesigns.com/Products/Clone/ECMAScriptCloneDR.html (http://www.semdesigns.com/Products/Clone/ECMAScriptCloneDR.html)

Not sure how much it costs, but there's an eval version available.

Title: Re: Javascript Copy&Paste Detection
Post by: hellfire on January 04, 2012
To find duplicate code those programms use the so called Rabin Karp Algorithm (http://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_string_search_algorithm)
Quote from: wikipedia
A practical application of Rabin–Karp is detecting plagiarism.

Do you really want to find redundancy or do you just want to know where your colleagues ripped their code? ;)

In my opinion it's the wrong approach:
This way some automatism will tell me that the code I've just written already exists, so my work was useless.
Instead just do a good documentation of existing modules and the redundant code won't be written in the first place.
Title: Re: Javascript Copy&Paste Detection
Post by: benny! on January 04, 2012
@HellFire:

No, I really just need to find redundant code this time. Totally, agree with you in general, with clean software design and good documentation you can avoid most of the redundancy beforehand. However, current project is a bit different. Projectmangement is not in our hands and it is extremely agile - that's why we agreed of having a certain level of redundancy. Of course having in mind that each sprint we have a look at what certain mess detection software says and then discuss it together.

Well downloading evaluation suite of the software raizor mentioned to see how it works ...

Title: Re: Javascript Copy&Paste Detection
Post by: benny! on January 05, 2012
Just want to provide a small update - I downloaded the ECMAScriptCloneDR tool (evaluation copy) - and did a test run with the IDE on my project. It crashes with unhandled java exceptions. Really frustrating ...