Skip to content

Commit 26fa23f

Browse files
authored
Refactor: improve quality, fewer calls to fs.statSync (#88)
1 parent 632ff25 commit 26fa23f

File tree

13 files changed

+297
-260
lines changed

13 files changed

+297
-260
lines changed

generated/avg-maintainability.svg

Lines changed: 5 additions & 5 deletions
Loading

generated/worst-maintainability.svg

Lines changed: 4 additions & 4 deletions
Loading

src/codehawk.ts

Lines changed: 7 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,20 @@ import { analyzeFile, calculateComplexity } from './analyze'
55
import { getFileContents, walkSync } from './traverseProject'
66
import { getTimesDependedOn, getProjectDeps } from './dependencies'
77
import type {
8-
ParsedEntity,
9-
ParsedFile,
10-
AnalyzedFile,
118
AnalyzedDirectory,
129
AnalyzedEntity,
10+
AnalyzedFile,
11+
AssembledOptions,
12+
FullyAnalyzedDirectory,
1313
FullyAnalyzedEntity,
1414
FullyAnalyzedFile,
15-
FullyAnalyzedDirectory,
16-
AssembledOptions,
15+
ParsedEntity,
16+
ParsedFile,
17+
ResultsSummary,
1718
} from './types'
1819
import { buildOptions, getConfiguration } from './options'
19-
import { flattenEntireTree } from './util'
2020
import { generateBadge } from './badge'
21-
22-
export interface ResultsSummary {
23-
average: number
24-
median: number
25-
worst: number
26-
}
21+
import { getResultsAsList, getResultsSummary } from './utils'
2722

2823
export interface Results {
2924
options: AssembledOptions
@@ -153,66 +148,5 @@ const analyzeProject = (rawPath: string, isCliContext?: boolean): Results => {
153148
}
154149
}
155150

156-
const getResultsAsList = (
157-
analyzedEntities: FullyAnalyzedEntity[],
158-
limit?: number
159-
): FullyAnalyzedFile[] => {
160-
const flatFileResults: FullyAnalyzedFile[] = flattenEntireTree<
161-
FullyAnalyzedFile
162-
>(analyzedEntities)
163-
.filter((entity) => {
164-
return entity.type === 'file' && !!entity.complexityReport
165-
})
166-
// Sort by codehawk score, ascending (most complex files are first in the list)
167-
.sort((entityA, entityB) => {
168-
return (
169-
entityA.complexityReport.codehawkScore -
170-
entityB.complexityReport.codehawkScore
171-
)
172-
})
173-
174-
return limit ? flatFileResults.slice(0, limit) : flatFileResults
175-
}
176-
177-
const getMedian = (numbers: number[]): number => {
178-
const sorted = numbers.slice().sort((a, b) => a - b)
179-
const middle = Math.floor(sorted.length / 2)
180-
181-
if (sorted.length % 2 === 0) {
182-
return (sorted[middle - 1] + sorted[middle]) / 2
183-
}
184-
185-
return sorted[middle]
186-
}
187-
188-
const getResultsSummary = (
189-
resultsAsList: FullyAnalyzedFile[]
190-
): ResultsSummary => {
191-
const allScores: number[] = resultsAsList.reduce((arr: number[], current) => {
192-
if (current.complexityReport) {
193-
arr.push(current.complexityReport.codehawkScore)
194-
}
195-
return arr
196-
}, [])
197-
const total = allScores.reduce((total: number, score) => {
198-
return total + score
199-
}, 0)
200-
const worst = allScores.reduce((worst: number, score) => {
201-
if (score < worst) {
202-
return score
203-
}
204-
return worst
205-
}, 100) // Start with max maintainability, work downwards
206-
207-
const average = total / allScores.length
208-
const median = getMedian(allScores)
209-
210-
return {
211-
average,
212-
median,
213-
worst,
214-
}
215-
}
216-
217151
// Public APIs
218152
export { calculateComplexity, analyzeProject, generateBadge }

src/dependencies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path'
22
import slash from 'slash'
3-
import { flattenEntireTree } from './util'
3+
import { flattenEntireTree } from './utils'
44
import type { AnalyzedEntity, AnalyzedFile } from './types'
55

66
// Gathers all the dependencies as a flat array of strings across all analyzed files

src/traverseProject.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from 'fs'
22
import * as path from 'path'
33
import slash from 'slash'
4-
import { getFsEntity, shouldSeeEntity, shouldAnalyzeEntity } from './util'
4+
import { getFsEntity, shouldSeeEntity, shouldAnalyzeEntity } from './utils'
55
import type {
66
AssembledOptions,
77
ParsedEntity,
@@ -17,15 +17,35 @@ export const walkSync = (
1717
): ParsedEntity[] => {
1818
const fileList: ParsedEntity[] = []
1919
const items = fs.readdirSync(dir)
20-
const visibleEntities = items.filter((i) => shouldSeeEntity(dir, i, options))
20+
const parsedEntities = items.map((filename) => ({
21+
filename,
22+
fullPath: path.join(dir, filename),
23+
entity: getFsEntity(path.join(dir, filename)),
24+
relativeDir: slash(dir).replace(slash(process.cwd()), ''),
25+
}))
26+
const visibleEntities = parsedEntities.filter((item) =>
27+
shouldSeeEntity({
28+
filename: item.filename,
29+
dir,
30+
fullPath: item.fullPath,
31+
entity: item.entity,
32+
options,
33+
relativeDir: item.relativeDir,
34+
})
35+
)
2136

2237
visibleEntities.forEach((item) => {
23-
const fullPath = path.join(dir, item)
24-
const entity = getFsEntity(fullPath)
38+
const { fullPath, filename, entity, relativeDir } = item
2539
const baseParsedEntity = {
26-
fullPath: slash(fullPath),
27-
filename: item,
28-
shouldAnalyze: shouldAnalyzeEntity(dir, item, options),
40+
fullPath: slash(item.fullPath),
41+
filename,
42+
shouldAnalyze: shouldAnalyzeEntity({
43+
entity,
44+
filename,
45+
fullPath,
46+
options,
47+
relativeDir,
48+
}),
2949
}
3050

3151
if (entity.isDirectory()) {

src/types/codehawk.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,10 @@ export interface CodehawkComplexityResult extends ComplexityResult {
139139
// After coverage mapping
140140
export interface CompleteCodehawkComplexityResult extends CodehawkComplexityResult {
141141
coverage: string
142+
}
143+
144+
export interface ResultsSummary {
145+
average: number
146+
median: number
147+
worst: number
142148
}

src/util.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isBlocklisted } from './util'
1+
import { isBlocklisted } from './utils'
22
import { buildOptions } from './options'
33

44
const options = buildOptions({

src/util.ts

Lines changed: 0 additions & 169 deletions
This file was deleted.

0 commit comments

Comments
 (0)