Rule idea: detect code duplication
See original GitHub issueI’d like eslint to produce a warning when I have pieces of duplicated code.
Precisely, I’d like no n- (default 3) line chunk to be repeated more then k (default 2) times. If it happens, the k+1-th occurrence should trigger a warning: “These n lines are duplicated for k+1-th time”.
Quite possibly we will also need to only take into account lines containing alpha-numeric characters, because we don’t want to detect chunks like:
return this;
},
<empty line>
This rule does not prevent errors, but helps maintain overall code quality. I think that (in its simplest version) it’s generic enough to be useful for any code, regardless of JS dialect or framework.
Do you think it’s a good idea to have this rule in core ESLint, or shall it be a plugin?
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Analyze duplicates | IntelliJ IDEA Documentation - JetBrains
In the search field of the Inspections dialog, type duplicated code fragment to locate the inspection in the list. Once you click the...
Read more >Chapter 4. Working with duplicate code - SonarQube in Action
When you start a new project, you have a clean code base with no duplications, unless you copied another project to start with....
Read more >The key to eliminating duplicated code patterns - Medium
However, there is one key concept that will solve all semantic code duplication (as well as structural duplication), namely “abstraction”.
Read more >How to detect code duplication during development? [closed]
Simian detects duplicate code in C++ projects. Update: Also works with Java, C#, C, COBOL, Ruby, JSP, ASP, HTML, XML, Visual Basic, Groovy...
Read more >Duplicate code - Wikipedia
In computer programming, duplicate code is a sequence of source code that occurs more than once, either within a program or across different...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
ESLint rules only operate on the context of a single file. So you won’t be able to detect duplicated code across multiple files.
I suggest to use tools like
jscpd
orjsinspect
.As @lo1tuma suggested, this isn’t an area where ESLint is the best tool, as each file is looked at separately.