smelly-detector
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -27,5 +27,3 @@ "use strict"; | ||
exports.SmellDetector = void 0; | ||
const esprima_1 = require("esprima"); | ||
const ts = __importStar(require("typescript")); | ||
const JavascriptSmells_1 = require("./languages/JavascriptSmells"); | ||
const TypescriptSmells_1 = require("./languages/TypescriptSmells"); | ||
@@ -36,20 +34,7 @@ const types_1 = require("./types"); | ||
code; | ||
language; | ||
constructor(fileName, code, language) { | ||
constructor(fileName, code) { | ||
this.fileName = fileName; | ||
this.code = code; | ||
this.language = language; | ||
} | ||
findAll() { | ||
if (this.language === types_1.SupportedLanguages.javascript) { | ||
const ast = (0, esprima_1.parseScript)(this.code, { loc: true }); | ||
const finder = new JavascriptSmells_1.JavascriptSmells(ast); | ||
const smellsList = { | ||
fileName: this.fileName, | ||
fileContent: this.code, | ||
smells: finder.searchSmells(), | ||
language: this.language | ||
}; | ||
return { smellsList, testCases: [] }; | ||
} | ||
// wondering why createSource? https://stackoverflow.com/a/60462133/2258921 | ||
@@ -63,2 +48,3 @@ const ast = ts.createSourceFile('temp.ts', this.code, ts.ScriptTarget.ES2020, true); | ||
})); | ||
const language = this.isJavascriptFile() ? types_1.SupportedLanguages.javascript : types_1.SupportedLanguages.typescript; | ||
const foundItEachCalls = this.findItEachCalls(ast); | ||
@@ -70,6 +56,9 @@ testCases.push(...foundItEachCalls); | ||
fileContent: this.code, | ||
smells: new TypescriptSmells_1.TypescriptSmells(ast).searchSmells(), language: types_1.SupportedLanguages.typescript | ||
smells: new TypescriptSmells_1.TypescriptSmells(ast).searchSmells(), language | ||
}; | ||
return { smellsList, testCases }; | ||
} | ||
isJavascriptFile() { | ||
return this.fileName.endsWith('.js') || this.fileName.endsWith('.jsx'); | ||
} | ||
findItCalls(sourceFile) { | ||
@@ -76,0 +65,0 @@ const itCalls = []; |
{ | ||
"name": "smelly-detector", | ||
"private": false, | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"main": "build/src/index.js", | ||
@@ -6,0 +6,0 @@ "description": "Find out the smells in your tests, suggestions for correction and the theory behind them", |
@@ -23,3 +23,3 @@ # @smelly/detector | ||
const detector = new SmellDetector("my source code", "javascript"); | ||
const detector = new SmellDetector("my-file.js", "my source code"); | ||
@@ -26,0 +26,0 @@ console.log(detector.findAll()); |
@@ -1,4 +0,2 @@ | ||
import { parseScript } from 'esprima'; | ||
import * as ts from 'typescript'; | ||
import { JavascriptSmells } from './languages/JavascriptSmells'; | ||
import { TypescriptSmells } from './languages/TypescriptSmells'; | ||
@@ -11,20 +9,6 @@ import { SmellDetectorRunnerResult, SupportedLanguages, TestCase } from './types'; | ||
private readonly fileName: string, | ||
private readonly code: string, | ||
private readonly language: string | ||
private readonly code: string | ||
) { } | ||
findAll(): SmellDetectorRunnerResult { | ||
if (this.language === SupportedLanguages.javascript) { | ||
const ast = parseScript(this.code, { loc: true }); | ||
const finder = new JavascriptSmells(ast); | ||
const smellsList = { | ||
fileName: this.fileName, | ||
fileContent: this.code, | ||
smells: finder.searchSmells(), | ||
language: this.language | ||
}; | ||
return { smellsList, testCases: [] }; | ||
} | ||
// wondering why createSource? https://stackoverflow.com/a/60462133/2258921 | ||
@@ -40,2 +24,4 @@ const ast = ts.createSourceFile('temp.ts', this.code, ts.ScriptTarget.ES2020, true); | ||
const language = this.isJavascriptFile() ? SupportedLanguages.javascript : SupportedLanguages.typescript; | ||
const foundItEachCalls = this.findItEachCalls(ast); | ||
@@ -48,3 +34,3 @@ testCases.push(...foundItEachCalls); | ||
fileContent: this.code, | ||
smells: new TypescriptSmells(ast).searchSmells(), language: SupportedLanguages.typescript | ||
smells: new TypescriptSmells(ast).searchSmells(), language | ||
}; | ||
@@ -54,2 +40,6 @@ return { smellsList, testCases }; | ||
private isJavascriptFile() { | ||
return this.fileName.endsWith('.js') || this.fileName.endsWith('.jsx'); | ||
} | ||
private findItCalls(sourceFile: ts.SourceFile): { lineStart: number, startAt: number, lineEnd: number, endsAt: number }[] { | ||
@@ -56,0 +46,0 @@ const itCalls: { lineStart: number, startAt: number, lineEnd: number, endsAt: number }[] = []; |
@@ -119,2 +119,18 @@ import { test, describe, expect, afterEach, beforeEach } from 'vitest'; | ||
test('renders yellow line when smells exists', async () => { | ||
const generatedHtml = await buildHtmlReportForTestSmellsFor(exportsOptions, filePath, oneFileWithOneConsoleSmells()); | ||
const root = parse(generatedHtml); | ||
expect(root.querySelector('[data-testid="file-list"] tbody tr')?.classList.value).toContain('odd:bg-yellow-400'); | ||
expect(root.querySelector('[data-testid="file-list"] tbody tr')?.classList.value).toContain('even:bg-yellow-400'); | ||
}); | ||
test('should not render yellow for file without smells', async () => { | ||
const generatedHtml = await buildHtmlReportForTestSmellsFor(exportsOptions, filePath, emptySmellsListForSingleFile()); | ||
const root = parse(generatedHtml); | ||
expect(root.querySelector('[data-testid="file-list"] tbody tr')?.classList.value).not.toContain('odd:bg-yellow-400'); | ||
expect(root.querySelector('[data-testid="file-list"] tbody tr')?.classList.value).not.toContain('even:bg-yellow-400'); | ||
}); | ||
describe('smells table with smells', () => { | ||
@@ -121,0 +137,0 @@ test('renders info about no smells', async () => { |
@@ -12,7 +12,7 @@ import { SmellDetector } from "../src"; | ||
export const JAVASCRIPT = 'javascript'; | ||
export const TYPESCRIPT = 'typescript'; | ||
export const JAVASCRIPT_FILE = 'javascript.js'; | ||
export const TYPESCRIPT_FILE = 'javascript.ts'; | ||
export function smellDetectorInstance(code: string, language: string): Smell[] { | ||
const smellDetector = new SmellDetector('my-file', code, language); | ||
const smellDetector = new SmellDetector(language, code); | ||
return smellDetector.findAll().smellsList.smells; | ||
@@ -22,4 +22,4 @@ } | ||
export function totalTestCaseDetectorInstance(code: string, language: string): TestCase[] { | ||
const smellDetector = new SmellDetector('my-file', code, language); | ||
const smellDetector = new SmellDetector(language, code); | ||
return smellDetector.findAll().testCases; | ||
} |
import { describe, expect, test } from 'vitest'; | ||
import { CONSOLE, FOR, FOR_IN, FOR_OF, IF_STATEMENT, JAVASCRIPT, MOCKERY, smellDetectorInstance, TIMEOUT, totalTestCaseDetectorInstance, TYPESCRIPT } from './smells-detector-builder'; | ||
import { CONSOLE, FOR, FOR_IN, FOR_OF, IF_STATEMENT, MOCKERY, smellDetectorInstance, TIMEOUT, totalTestCaseDetectorInstance, JAVASCRIPT_FILE, TYPESCRIPT_FILE } from './smells-detector-builder'; | ||
@@ -8,3 +8,3 @@ describe('Smelly Test Smell Detection Suite', () => { | ||
if (a === 1) {}`, | ||
language: JAVASCRIPT, | ||
fileName: JAVASCRIPT_FILE, | ||
index: 0, | ||
@@ -26,3 +26,3 @@ type: IF_STATEMENT, | ||
}`, | ||
language: JAVASCRIPT, | ||
fileName: JAVASCRIPT_FILE, | ||
type: FOR_OF, | ||
@@ -44,3 +44,3 @@ index: 0, | ||
}`, | ||
language: JAVASCRIPT, | ||
fileName: JAVASCRIPT_FILE, | ||
type: FOR_IN, | ||
@@ -62,3 +62,3 @@ index: 0, | ||
}`, | ||
language: JAVASCRIPT, | ||
fileName: JAVASCRIPT_FILE, | ||
type: FOR, | ||
@@ -78,3 +78,3 @@ index: 0, | ||
});`, | ||
language: JAVASCRIPT, | ||
fileName: JAVASCRIPT_FILE, | ||
index: 0, | ||
@@ -95,3 +95,3 @@ type: TIMEOUT, | ||
});`, | ||
language: JAVASCRIPT, | ||
fileName: JAVASCRIPT_FILE, | ||
index: 0, | ||
@@ -110,3 +110,3 @@ type: TIMEOUT, | ||
if (a === 1) { }`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
index: 0, | ||
@@ -129,3 +129,3 @@ type: IF_STATEMENT, | ||
}`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
index: 1, | ||
@@ -147,3 +147,3 @@ type: IF_STATEMENT, | ||
}`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
index: 0, | ||
@@ -165,3 +165,3 @@ type: FOR_OF, | ||
}`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
index: 0, | ||
@@ -183,3 +183,3 @@ type: FOR_IN, | ||
}`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
index: 0, | ||
@@ -199,3 +199,3 @@ type: FOR, | ||
});`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
index: 0, | ||
@@ -216,3 +216,3 @@ type: TIMEOUT, | ||
});`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
index: 0, | ||
@@ -234,3 +234,3 @@ type: TIMEOUT, | ||
})`, | ||
language: JAVASCRIPT, | ||
fileName: JAVASCRIPT_FILE, | ||
index: 0, | ||
@@ -252,3 +252,3 @@ type: CONSOLE, | ||
})`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
index: 0, | ||
@@ -270,3 +270,3 @@ type: CONSOLE, | ||
})`, | ||
language: JAVASCRIPT, | ||
fileName: JAVASCRIPT_FILE, | ||
index: 0, | ||
@@ -288,3 +288,3 @@ type: CONSOLE, | ||
})`, | ||
language: JAVASCRIPT, | ||
fileName: JAVASCRIPT_FILE, | ||
index: 0, | ||
@@ -306,3 +306,3 @@ type: CONSOLE, | ||
})`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
index: 0, | ||
@@ -324,3 +324,3 @@ type: CONSOLE, | ||
})`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
index: 0, | ||
@@ -347,3 +347,3 @@ type: CONSOLE, | ||
jest.mock("../");`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
index: 0, | ||
@@ -371,3 +371,3 @@ type: MOCKERY, | ||
jest.mock("../");`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
index: 0, | ||
@@ -383,6 +383,6 @@ type: MOCKERY, | ||
}] | ||
])(`detect test smell for %s %s: type %s %s at index %s`, ({ code, language, index, type, lineStart, lineEnd, startAt, endsAt, total, description, diagnostic }) => { | ||
])(`detect test smell for %s %s: type %s %s at index %s`, ({ code, fileName, index, type, lineStart, lineEnd, startAt, endsAt, total, description, diagnostic }) => { | ||
test(`should find ${total} test smells`, () => { | ||
const result = smellDetectorInstance(code, language); | ||
const result = smellDetectorInstance(code, fileName); | ||
@@ -393,3 +393,3 @@ expect(result.length).toEqual(total); | ||
test(`at ${index} should match ${type}`, () => { | ||
const result = smellDetectorInstance(code, language); | ||
const result = smellDetectorInstance(code, fileName); | ||
@@ -400,3 +400,3 @@ expect(result[index].type).toEqual(type); | ||
test(`at ${index} should find line start`, () => { | ||
const result = smellDetectorInstance(code, language); | ||
const result = smellDetectorInstance(code, fileName); | ||
@@ -407,3 +407,3 @@ expect(result[index].lineStart).toEqual(lineStart); | ||
test(`at ${index} should find line end`, () => { | ||
const result = smellDetectorInstance(code, language); | ||
const result = smellDetectorInstance(code, fileName); | ||
@@ -414,3 +414,3 @@ expect(result[index].lineEnd).toEqual(lineEnd); | ||
test(`at ${index} should find column start at`, () => { | ||
const result = smellDetectorInstance(code, language); | ||
const result = smellDetectorInstance(code, fileName); | ||
@@ -421,3 +421,3 @@ expect(result[index].startAt).toEqual(startAt); | ||
test(`at ${index} should find column ends at`, () => { | ||
const result = smellDetectorInstance(code, language); | ||
const result = smellDetectorInstance(code, fileName); | ||
@@ -428,3 +428,3 @@ expect(result[index].endsAt).toEqual(endsAt); | ||
test(`at ${index} should find description`, () => { | ||
const result = smellDetectorInstance(code, language); | ||
const result = smellDetectorInstance(code, fileName); | ||
@@ -435,3 +435,3 @@ expect(result[index].description).toEqual(description); | ||
test(`at ${index} should find diagnostic`, () => { | ||
const result = smellDetectorInstance(code, language); | ||
const result = smellDetectorInstance(code, fileName); | ||
@@ -464,3 +464,3 @@ expect(result[index].diagnostic).toEqual(diagnostic); | ||
])('should identify the test cases that exists in the file with .each and with .skip', ({ code, expected }) => { | ||
const result = totalTestCaseDetectorInstance(code, TYPESCRIPT); | ||
const result = totalTestCaseDetectorInstance(code, TYPESCRIPT_FILE); | ||
@@ -475,3 +475,3 @@ expect(result).toHaveLength(expected); | ||
});`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
testCases: [{ | ||
@@ -488,3 +488,3 @@ lineStart: 1, | ||
});`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
testCases: [{ | ||
@@ -502,3 +502,3 @@ lineStart: 1, | ||
test("b", () => {});`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
testCases: [{ | ||
@@ -516,5 +516,5 @@ lineStart: 1, | ||
}], | ||
])('testCases', ({ index, code, language, testCases }) => { | ||
])('testCases', ({ index, code, fileName, testCases }) => { | ||
test(`should line start for test case at index ${index}`, () => { | ||
const result = totalTestCaseDetectorInstance(code, language); | ||
const result = totalTestCaseDetectorInstance(code, fileName); | ||
@@ -525,3 +525,3 @@ expect(result[index].lineStart).toEqual(testCases[index].lineStart); | ||
test(`should column start for test case at index ${index}`, () => { | ||
const result = totalTestCaseDetectorInstance(code, language); | ||
const result = totalTestCaseDetectorInstance(code, fileName); | ||
@@ -532,3 +532,3 @@ expect(result[index].startAt).toEqual(testCases[index].startAt); | ||
test(`should line end for test case at index ${index}`, () => { | ||
const result = totalTestCaseDetectorInstance(code, language); | ||
const result = totalTestCaseDetectorInstance(code, fileName); | ||
@@ -539,3 +539,3 @@ expect(result[index].lineEnd).toEqual(testCases[index].lineEnd); | ||
test(`should column end for test case at index ${index}`, () => { | ||
const result = totalTestCaseDetectorInstance(code, language); | ||
const result = totalTestCaseDetectorInstance(code, fileName); | ||
@@ -542,0 +542,0 @@ expect(result[index].endsAt).toEqual(testCases[index].endsAt); |
import { describe, expect, test } from 'vitest'; | ||
import { smellDetectorInstance, TYPESCRIPT } from './smells-detector-builder'; | ||
import { smellDetectorInstance, TYPESCRIPT_FILE } from './smells-detector-builder'; | ||
@@ -8,8 +8,8 @@ describe('Smelly Test Smell Detection Suite', () => { | ||
jest.mock("../");`, | ||
language: TYPESCRIPT, | ||
fileName: TYPESCRIPT_FILE, | ||
} | ||
])(`detect code without smells`, ({ code, language }) => { | ||
const result = smellDetectorInstance(code, language); | ||
])(`detect code without smells`, ({ code, fileName }) => { | ||
const result = smellDetectorInstance(code, fileName); | ||
expect(result.length).toEqual(0); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
163951
3091