Cannot run `detectInFiles()` several times in a row
See original GitHub issueDescribe the bug
When running detectInFiles()
several times in a row, the second run re-uses state from the previous run. This leads to files reporting duplication βon themselvesβ (i.e. duplicationA
and duplicationB
are the same line in the same file, for all lines in all files).
To Reproduce
example.js
:
module.exports = {}
bug.js
:
'use strict'
const { JSCPD } = require('jscpd')
const instance = new JSCPD({ minTokens: 1, minLines: 1 })
const detect = async function() {
const clones = await instance.detectInFiles(['example.js'])
console.log(clones)
}
detect().then(detect)
Output:
ββββββββββββββ¬βββββββββββββββββ¬ββββββββββββββ¬βββββββββββββββ¬βββββββββββββββββββ¬βββββ
β Format β Files analyzed β Total lines β Clones found β Duplicated lines β % β
ββββββββββββββΌβββββββββββββββββΌββββββββββββββΌβββββββββββββββΌβββββββββββββββββββΌβββββ€
β javascript β 1 β 1 β 0 β 0 β 0% β
ββββββββββββββΌβββββββββββββββββΌββββββββββββββΌβββββββββββββββΌβββββββββββββββββββΌβββββ€
β Total: β 1 β 1 β 0 β 0 β 0% β
ββββββββββββββ΄βββββββββββββββββ΄ββββββββββββββ΄βββββββββββββββ΄βββββββββββββββββββ΄βββββ
Execution Time: 91.967ms
[]
ββββββββββββββ¬βββββββββββββββββ¬ββββββββββββββ¬βββββββββββββββ¬βββββββββββββββββββ¬βββββ
β Format β Files analyzed β Total lines β Clones found β Duplicated lines β % β
ββββββββββββββΌβββββββββββββββββΌββββββββββββββΌβββββββββββββββΌβββββββββββββββββββΌβββββ€
β javascript β 2 β 2 β 0 β 0 β 0% β
ββββββββββββββΌβββββββββββββββββΌββββββββββββββΌβββββββββββββββΌβββββββββββββββββββΌβββββ€
β Total: β 2 β 2 β 0 β 0 β 0% β
ββββββββββββββ΄βββββββββββββββββ΄ββββββββββββββ΄βββββββββββββββ΄βββββββββββββββββββ΄βββββ
[ { format: 'javascript',
foundDate: 1550844212331,
duplicationA:
{ sourceId: '/home/user/Desktop/example.js',
start: [Object],
end: [Object],
range: [Array],
fragment: 'module.exports = {}' },
duplicationB:
{ sourceId: '/home/user/Desktop/example.js',
start: [Object],
end: [Object],
range: [Array],
fragment: 'module.exports = {}' } } ]
(node:30646) Warning: No such label 'Execution Time' for console.timeEnd()
Expected behavior No clones should be returned.
Possible cause It seems like the store state is not cleaned between run. Iβm not sure if this is intentional.
In jscpd.js
line 244 there are two possible problems:
EventEmitter.emit()
is synchronous with both core Node.js andeventemitter3
. So it should be afterEventEmitter.on()
. This causesStoresManager.close()
to never be closed.EventEmitter.once()
should probably be used instead. Otherwise re-running the main path will setup an additional end listener.
Desktop (please complete the following information):
- OS: Ubuntu
- OS Version:
18.10
- NodeJS Version:
11.10.0
- jscpd version:
2.0.9
Additional context
Note the (node:30646) Warning: No such label 'Execution Time' for console.timeEnd()
at the end, which is probably a separate bug.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Invoking functions multiple times in a row - Stack Overflow
The function returns a string. After it is executed, the second function call would be 'one'() which is not the same as one()...
Read more >erikstricker/PDE source: R/PDE.R - Rdrr.io
#'@examples #' #'## Running a simple analysis with filter and search words to extract sentences and tables #'if(PDE_check_Xpdf_install() == TRUE){Β ...
Read more >Wayner P. Disappearing cryptography.. Information hiding ...
Hiding the information so it can't be found is a similar but often distinct process often called steganography. There are many historical examples...
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 Free
Top 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
Another way to reproduce the issue with
2.0.10
and a single filebug.js
:Output:
fixed in jscpd@3.3.0-rc.9