Skip to content

Commit 149b6a0

Browse files
committed
refactor: changed signature result from smell detector
BREAKING CHANGE: changed result signature from smells detector
1 parent 7c36643 commit 149b6a0

File tree

8 files changed

+29
-31
lines changed

8 files changed

+29
-31
lines changed

cli/src/find-smells.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async function execute() {
4141
for (const file of pathWithAllFilesFound) {
4242
const fileContent = await fs.readFile(file, { encoding: 'utf8' });
4343
const smellDetector = new SmellDetector(file, fileContent, language);
44-
const smells = smellDetector.findAll().smells;
44+
const smells = smellDetector.findAll().smellsList.smells;
4545
aggregator.push({ fileName: file, smells, language, fileContent });
4646
}
4747

@@ -57,7 +57,7 @@ async function execute() {
5757
for (const file of pathWithAllFilesFound) {
5858
const fileContents = await fs.readFile(file, { encoding: 'utf8' });
5959
const smellDetector = new SmellDetector(file, fileContents, language);
60-
const result = smellDetector.findAll().smells;
60+
const result = smellDetector.findAll().smellsList.smells;
6161

6262
if (result.length) {
6363
result.forEach((e: Smell) => {

detector/src/reporters/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ export interface AggregatedData {
1414
export interface ExportOptions {
1515
to: string
1616
}
17-

detector/src/smells-detector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class SmellDetector {
1717
const ast = parseScript(this.code, { loc: true });
1818

1919
const finder = new JavascriptSmells(ast);
20-
return { smells: finder.searchSmells(), testCases: [] };
20+
return { smellsList: { fileName: this.fileName, fileContent: this.code, smells: finder.searchSmells(), language: this.language }, testCases: [] };
2121
}
2222

2323
// wondering why createSource? https://stackoverflow.com/a/60462133/2258921
@@ -30,7 +30,7 @@ export class SmellDetector {
3030
endsAt: columnEnd,
3131
}));
3232

33-
return { smells: new TypescriptSmells(ast).searchSmells(), testCases };
33+
return { smellsList: { fileName: this.fileName, fileContent: this.code, smells: new TypescriptSmells(ast).searchSmells(), language: SupportedLanguages.typescript }, testCases };
3434
}
3535
}
3636

detector/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface SmellsFinder {
2727
}
2828

2929
export type SmellDetectorRunnerResult = {
30-
smells: Smell[];
30+
smellsList: SmellsList;
3131
testCases: TestCase[];
3232
};
3333

detector/test/smells-detector-builder.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { SmellDetector } from "../src";
2+
import { Smell } from "../src/types";
3+
14
export const IF_STATEMENT = 'if-statement';
25
export const FOR_OF = 'for-of-statement';
36
export const FOR_IN = 'for-in-statement';
@@ -7,4 +10,11 @@ export const CONSOLE = 'console-statement';
710
export const MOCKERY = 'excessive-jest-mock';
811

912
export const JAVASCRIPT = 'javascript';
10-
export const TYPESCRIPT = 'typescript';
13+
export const TYPESCRIPT = 'typescript';
14+
15+
16+
export function smellDetectorInstance(code: string, language: string): Smell[] {
17+
const smellDetector = new SmellDetector('my-file', code, language);
18+
const result = smellDetector.findAll().smellsList.smells;
19+
return result;
20+
}

detector/test/smells-detector-with-smells.test.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from 'vitest';
22
import { SmellDetector } from '../src/index';
3-
import { CONSOLE, FOR, FOR_IN, FOR_OF, IF_STATEMENT, JAVASCRIPT, MOCKERY, TIMEOUT, TYPESCRIPT } from './smells-detector-builder';
3+
import { CONSOLE, FOR, FOR_IN, FOR_OF, IF_STATEMENT, JAVASCRIPT, MOCKERY, smellDetectorInstance, TIMEOUT, TYPESCRIPT } from './smells-detector-builder';
44

55

66
describe('Smelly Test Smell Detection Suite', () => {
@@ -364,57 +364,49 @@ jest.mock("../");`,
364364
])(`detect test smell for %s %s: type %s %s at index %s`, ({ code, language, index, type, lineStart, lineEnd, startAt, endsAt, total, description, diagnostic }) => {
365365

366366
test(`should find ${total} test smells`, () => {
367-
const smellDetector = new SmellDetector('my-file', code, language);
368-
const result = smellDetector.findAll().smells;
367+
const result = smellDetectorInstance(code, language);
369368

370369
expect(result.length).toEqual(total);
371370
});
372371

373372
test(`at ${index} should match ${type}`, () => {
374-
const smellDetector = new SmellDetector('my-file', code, language);
375-
const result = smellDetector.findAll().smells;
373+
const result = smellDetectorInstance(code, language);
376374

377375
expect(result[index].type).toEqual(type);
378376
});
379377

380378
test(`at ${index} should find line start`, () => {
381-
const smellDetector = new SmellDetector('my-file', code, language);
382-
const result = smellDetector.findAll().smells;
379+
const result = smellDetectorInstance(code, language);
383380

384381
expect(result[index].lineStart).toEqual(lineStart);
385382
});
386383

387384
test(`at ${index} should find line end`, () => {
388-
const smellDetector = new SmellDetector('my-file', code, language);
389-
const result = smellDetector.findAll().smells;
385+
const result = smellDetectorInstance(code, language);
390386

391387
expect(result[index].lineEnd).toEqual(lineEnd);
392388
});
393389

394390
test(`at ${index} should find column start at`, () => {
395-
const smellDetector = new SmellDetector('my-file', code, language);
396-
const result = smellDetector.findAll().smells;
391+
const result = smellDetectorInstance(code, language);
397392

398393
expect(result[index].startAt).toEqual(startAt);
399394
});
400395

401396
test(`at ${index} should find column ends at`, () => {
402-
const smellDetector = new SmellDetector('my-file', code, language);
403-
const result = smellDetector.findAll().smells;
397+
const result = smellDetectorInstance(code, language);
404398

405399
expect(result[index].endsAt).toEqual(endsAt);
406400
});
407401

408402
test(`at ${index} should find description`, () => {
409-
const smellDetector = new SmellDetector('my-file', code, language);
410-
const result = smellDetector.findAll().smells;
403+
const result = smellDetectorInstance(code, language);
411404

412405
expect(result[index].description).toEqual(description);
413406
});
414407

415408
test(`at ${index} should find diagnostic`, () => {
416-
const smellDetector = new SmellDetector('my-file', code, language);
417-
const result = smellDetector.findAll().smells;
409+
const result = smellDetectorInstance(code, language);
418410

419411
expect(result[index].diagnostic).toEqual(diagnostic);
420412
});
@@ -500,4 +492,4 @@ test("b", () => {});`,
500492
expect(result[index].endsAt).toEqual(testCases[index].endsAt);
501493
});
502494
});
503-
});
495+
});
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, expect, test } from 'vitest';
2-
import { SmellDetector } from '../src/index';
3-
import { TYPESCRIPT } from './smells-detector-builder';
2+
import { smellDetectorInstance, TYPESCRIPT } from './smells-detector-builder';
43

54
describe('Smelly Test Smell Detection Suite', () => {
65
test.each([{
@@ -9,9 +8,7 @@ jest.mock("../");`,
98
language: TYPESCRIPT,
109
}
1110
])(`detect code without smells`, ({ code, language }) => {
12-
const smellDetector = new SmellDetector(code, language);
13-
const result = smellDetector.findAll().smells;
14-
11+
const result = smellDetectorInstance(code, language);
1512
expect(result.length).toEqual(0);
1613
});
1714
});

vscode/src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ function generateHighlighting(context: vscode.ExtensionContext) {
192192

193193
const findSmells = (fileName: string, text: string, language: string): Smell[] => {
194194
const detect = new SmellDetector(fileName, text, language);
195-
return detect.findAll().smells;
195+
return detect.findAll().smellsList.smells;
196196
};
197197

198198
export function findMatch(fileName: string, text: string, language: string): void {

0 commit comments

Comments
 (0)