Skip to content

Commit 2e6963f

Browse files
committed
feat(detector): detect test cases
1 parent f0d6ad3 commit 2e6963f

File tree

2 files changed

+119
-3
lines changed

2 files changed

+119
-3
lines changed

detector/src/smells-detector.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parseScript } from 'esprima';
2-
import { createSourceFile, ScriptTarget } from 'typescript';
2+
import * as ts from 'typescript';
33
import { JavascriptSmells } from './languages/JavascriptSmells';
44
import { TypescriptSmells } from './languages/TypescriptSmells';
55
import { SmellDetectorRunnerResult, SupportedLanguages } from './types';
@@ -17,7 +17,42 @@ export class SmellDetector {
1717
}
1818

1919
// wondering why createSource? https://stackoverflow.com/a/60462133/2258921
20-
const ast = createSourceFile('temp.ts', this.code, ScriptTarget.ES2020, true);
21-
return { smells: new TypescriptSmells(ast).searchSmells(), testCases: [] };
20+
const ast = ts.createSourceFile('temp.ts', this.code, ts.ScriptTarget.ES2020, true);
21+
22+
const testCases = findItCalls(ast).map(({ lineStart, columnStart, lineEnd, columnEnd }) => ({
23+
lineStart,
24+
startAt: columnStart,
25+
lineEnd,
26+
endsAt: columnEnd,
27+
}));
28+
29+
return { smells: new TypescriptSmells(ast).searchSmells(), testCases };
30+
}
31+
}
32+
33+
function findItCalls(sourceFile: ts.SourceFile): { lineStart: number, columnStart: number, lineEnd: number, columnEnd: number }[] {
34+
const itCalls: { lineStart: number, columnStart: number, lineEnd: number, columnEnd: number }[] = [];
35+
36+
function traverse(node: ts.Node) {
37+
if (ts.isCallExpression(node)) {
38+
const expression = node.expression;
39+
const isItCall = ts.isIdentifier(expression) && expression.text === 'it';
40+
const isTestCall = ts.isIdentifier(expression) && expression.text === 'test';
41+
if (isItCall || isTestCall) {
42+
const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart());
43+
const { line: lineEnd, character: columnEnd } = sourceFile.getLineAndCharacterOfPosition(node.getEnd());
44+
itCalls.push({
45+
lineStart: line + 1,
46+
columnStart: character,
47+
lineEnd: lineEnd + 1,
48+
columnEnd,
49+
});
50+
}
51+
}
52+
53+
ts.forEachChild(node, traverse);
2254
}
55+
56+
traverse(sourceFile);
57+
return itCalls;
2358
}

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,4 +419,85 @@ jest.mock("../");`,
419419
expect(result[index].diagnostic).toEqual(diagnostic);
420420
});
421421
});
422+
423+
test('should identify the test cases that exists in the file', () => {
424+
const code = 'it("a", () => {});';
425+
const smellDetector = new SmellDetector(code, TYPESCRIPT);
426+
const result = smellDetector.findAll().testCases;
427+
428+
expect(result).toHaveLength(1);
429+
});
430+
431+
describe.each([
432+
[{
433+
index: 0,
434+
code: `it("a", () => {
435+
});`,
436+
language: TYPESCRIPT,
437+
testCases: [{
438+
lineStart: 1,
439+
lineEnd: 2,
440+
startAt: 0,
441+
endsAt: 2,
442+
}]
443+
}],
444+
[{
445+
index: 0,
446+
code: `test("a", () => {
447+
});`,
448+
language: TYPESCRIPT,
449+
testCases: [{
450+
lineStart: 1,
451+
lineEnd: 2,
452+
startAt: 0,
453+
endsAt: 2,
454+
}]
455+
}],
456+
[{
457+
index: 1,
458+
code: `test("a", () => {
459+
});
460+
test("b", () => {});`,
461+
language: TYPESCRIPT,
462+
testCases: [{
463+
lineStart: 1,
464+
lineEnd: 2,
465+
startAt: 0,
466+
endsAt: 2,
467+
}, {
468+
lineStart: 3,
469+
lineEnd: 3,
470+
startAt: 0,
471+
endsAt: 19,
472+
}]
473+
}],
474+
])('testCases', ({ index, code, language, testCases }) => {
475+
test(`should line start for test case at index ${index}`, () => {
476+
const smellDetector = new SmellDetector(code, language);
477+
const result = smellDetector.findAll().testCases;
478+
479+
expect(result[index].lineStart).toEqual(testCases[index].lineStart);
480+
});
481+
482+
test(`should column start for test case at index ${index}`, () => {
483+
const smellDetector = new SmellDetector(code, language);
484+
const result = smellDetector.findAll().testCases;
485+
486+
expect(result[index].startAt).toEqual(testCases[index].startAt);
487+
});
488+
489+
test(`should line end for test case at index ${index}`, () => {
490+
const smellDetector = new SmellDetector(code, language);
491+
const result = smellDetector.findAll().testCases;
492+
493+
expect(result[index].lineEnd).toEqual(testCases[index].lineEnd);
494+
});
495+
496+
test(`should column end for test case at index ${index}`, () => {
497+
const smellDetector = new SmellDetector(code, language);
498+
const result = smellDetector.findAll().testCases;
499+
500+
expect(result[index].endsAt).toEqual(testCases[index].endsAt);
501+
});
502+
});
422503
});

0 commit comments

Comments
 (0)