Skip to content

Commit 5f9e125

Browse files
committed
fix(blamer): Fix usage fix of blamer
fix #238
1 parent 5605530 commit 5f9e125

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

src/hooks/post/blamer.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1+
import Blamer from 'blamer';
12
import { yellow } from 'colors/safe';
23
import { IClone } from '../..';
34
import { IBlamedLines } from '../../interfaces/blame.interface';
4-
import { IHook } from '../../interfaces/hook.interface';
5-
import { JSCPD } from '../../jscpd';
5+
import { IPostHook } from '../../interfaces/post-hook.interface';
66

7-
const Blamer = require('blamer');
8-
9-
export class BlamerPostHook implements IHook {
10-
public async use(jscpd: JSCPD): Promise<any> {
11-
jscpd.clones = await Promise.all(
12-
jscpd.clones.map((clone: IClone) => {
7+
export class BlamerPostHook implements IPostHook {
8+
public async use(clones: IClone[]): Promise<any> {
9+
return await Promise.all(
10+
clones.map((clone: IClone) => {
1311
return this.matchClone(clone);
1412
})
1513
);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { IClone } from './clone.interface';
2+
3+
export interface IPostHook {
4+
use(closes: IClone[]): Promise<any>;
5+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { JSCPD } from '../jscpd';
22

3-
export interface IHook {
3+
export interface IPreHook {
44
use(jscpd: JSCPD): Promise<any>;
55
}

src/jscpd.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import { getSourceFragmentLength } from './clone';
88
import { Detector } from './detector';
99
import { END_EVENT, JscpdEventEmitter, MATCH_SOURCE_EVENT, SOURCE_SKIPPED_EVENT } from './events';
1010
import { IClone } from './interfaces/clone.interface';
11-
import { IHook } from './interfaces/hook.interface';
1211
import { IListener } from './interfaces/listener.interface';
1312
import { IOptions } from './interfaces/options.interface';
13+
import { IPostHook } from './interfaces/post-hook.interface';
14+
import { IPreHook } from './interfaces/pre-hook.interface';
1415
import { IReporter } from './interfaces/reporter.interface';
1516
import { ISourceOptions } from './interfaces/source-options.interface';
1617
import { IStatistic } from './interfaces/statistic.interface';
@@ -72,8 +73,8 @@ export class JSCPD {
7273
private _options: IOptions;
7374
private _files: EntryItem[] = [];
7475
private _clones: IClone[] = [];
75-
private _preHooks: IHook[] = [];
76-
private _postHooks: IHook[] = [];
76+
private _preHooks: IPreHook[] = [];
77+
private _postHooks: IPostHook[] = [];
7778

7879
constructor(options: IOptions = {} as IOptions, eventEmitter?: EventEmitter) {
7980
this.eventEmitter = eventEmitter || new JscpdEventEmitter();
@@ -84,16 +85,16 @@ export class JSCPD {
8485
StoresManager.initialize(this._options.storeOptions);
8586
}
8687

87-
public attachPreHook(hook: IHook) {
88+
public attachPreHook(hook: IPreHook) {
8889
this._preHooks.push(hook);
8990
}
9091

91-
public attachPostHook(hook: IHook) {
92+
public attachPostHook(hook: IPostHook) {
9293
this._postHooks.push(hook);
9394
}
9495

9596
public async detect(code: string, options: ISourceOptions): Promise<IClone[]> {
96-
await Promise.all(this._preHooks.map((hook: IHook) => hook.use(this)));
97+
await Promise.all(this._preHooks.map((hook: IPreHook) => hook.use(this)));
9798
this._clones = await this._detect(code, { ...options, source: code });
9899
await this._detectionFinished(this._clones, true);
99100
return this._clones;
@@ -138,7 +139,7 @@ export class JSCPD {
138139
console.log(bold(`Found ${this._files.length} files to detect.`));
139140
}
140141

141-
await Promise.all(this._preHooks.map((hook: IHook) => hook.use(this)));
142+
await Promise.all(this._preHooks.map((hook: IPreHook) => hook.use(this)));
142143

143144
const sources: Array<{ source: string; sourceOptions: ISourceOptions }> = this._files
144145
.filter((stats: any) => {
@@ -239,7 +240,10 @@ export class JSCPD {
239240
}
240241

241242
private async _detectionFinished(clones: IClone[], pesists: boolean = false) {
242-
await Promise.all(this._postHooks.map((hook: IHook) => hook.use(this)));
243+
await Promise.all(this._postHooks.map(async (hook: IPostHook) => {
244+
clones = await hook.use(clones);
245+
return clones;
246+
}));
243247
await this.generateReports(clones);
244248
this.eventEmitter.emit(END_EVENT, clones);
245249
if (!pesists) {

0 commit comments

Comments
 (0)