Skip to content

Refactor: improve quality, fewer calls to fs.statSync #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions generated/avg-maintainability.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions generated/worst-maintainability.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 7 additions & 73 deletions src/codehawk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,20 @@ import { analyzeFile, calculateComplexity } from './analyze'
import { getFileContents, walkSync } from './traverseProject'
import { getTimesDependedOn, getProjectDeps } from './dependencies'
import type {
ParsedEntity,
ParsedFile,
AnalyzedFile,
AnalyzedDirectory,
AnalyzedEntity,
AnalyzedFile,
AssembledOptions,
FullyAnalyzedDirectory,
FullyAnalyzedEntity,
FullyAnalyzedFile,
FullyAnalyzedDirectory,
AssembledOptions,
ParsedEntity,
ParsedFile,
ResultsSummary,
} from './types'
import { buildOptions, getConfiguration } from './options'
import { flattenEntireTree } from './util'
import { generateBadge } from './badge'

export interface ResultsSummary {
average: number
median: number
worst: number
}
import { getResultsAsList, getResultsSummary } from './utils'

export interface Results {
options: AssembledOptions
Expand Down Expand Up @@ -153,66 +148,5 @@ const analyzeProject = (rawPath: string, isCliContext?: boolean): Results => {
}
}

const getResultsAsList = (
analyzedEntities: FullyAnalyzedEntity[],
limit?: number
): FullyAnalyzedFile[] => {
const flatFileResults: FullyAnalyzedFile[] = flattenEntireTree<
FullyAnalyzedFile
>(analyzedEntities)
.filter((entity) => {
return entity.type === 'file' && !!entity.complexityReport
})
// Sort by codehawk score, ascending (most complex files are first in the list)
.sort((entityA, entityB) => {
return (
entityA.complexityReport.codehawkScore -
entityB.complexityReport.codehawkScore
)
})

return limit ? flatFileResults.slice(0, limit) : flatFileResults
}

const getMedian = (numbers: number[]): number => {
const sorted = numbers.slice().sort((a, b) => a - b)
const middle = Math.floor(sorted.length / 2)

if (sorted.length % 2 === 0) {
return (sorted[middle - 1] + sorted[middle]) / 2
}

return sorted[middle]
}

const getResultsSummary = (
resultsAsList: FullyAnalyzedFile[]
): ResultsSummary => {
const allScores: number[] = resultsAsList.reduce((arr: number[], current) => {
if (current.complexityReport) {
arr.push(current.complexityReport.codehawkScore)
}
return arr
}, [])
const total = allScores.reduce((total: number, score) => {
return total + score
}, 0)
const worst = allScores.reduce((worst: number, score) => {
if (score < worst) {
return score
}
return worst
}, 100) // Start with max maintainability, work downwards

const average = total / allScores.length
const median = getMedian(allScores)

return {
average,
median,
worst,
}
}

// Public APIs
export { calculateComplexity, analyzeProject, generateBadge }
2 changes: 1 addition & 1 deletion src/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import slash from 'slash'
import { flattenEntireTree } from './util'
import { flattenEntireTree } from './utils'
import type { AnalyzedEntity, AnalyzedFile } from './types'

// Gathers all the dependencies as a flat array of strings across all analyzed files
Expand Down
34 changes: 27 additions & 7 deletions src/traverseProject.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs'
import * as path from 'path'
import slash from 'slash'
import { getFsEntity, shouldSeeEntity, shouldAnalyzeEntity } from './util'
import { getFsEntity, shouldSeeEntity, shouldAnalyzeEntity } from './utils'
import type {
AssembledOptions,
ParsedEntity,
Expand All @@ -17,15 +17,35 @@ export const walkSync = (
): ParsedEntity[] => {
const fileList: ParsedEntity[] = []
const items = fs.readdirSync(dir)
const visibleEntities = items.filter((i) => shouldSeeEntity(dir, i, options))
const parsedEntities = items.map((filename) => ({
filename,
fullPath: path.join(dir, filename),
entity: getFsEntity(path.join(dir, filename)),
relativeDir: slash(dir).replace(slash(process.cwd()), ''),
}))
const visibleEntities = parsedEntities.filter((item) =>
shouldSeeEntity({
filename: item.filename,
dir,
fullPath: item.fullPath,
entity: item.entity,
options,
relativeDir: item.relativeDir,
})
)

visibleEntities.forEach((item) => {
const fullPath = path.join(dir, item)
const entity = getFsEntity(fullPath)
const { fullPath, filename, entity, relativeDir } = item
const baseParsedEntity = {
fullPath: slash(fullPath),
filename: item,
shouldAnalyze: shouldAnalyzeEntity(dir, item, options),
fullPath: slash(item.fullPath),
filename,
shouldAnalyze: shouldAnalyzeEntity({
entity,
filename,
fullPath,
options,
relativeDir,
}),
}

if (entity.isDirectory()) {
Expand Down
6 changes: 6 additions & 0 deletions src/types/codehawk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,10 @@ export interface CodehawkComplexityResult extends ComplexityResult {
// After coverage mapping
export interface CompleteCodehawkComplexityResult extends CodehawkComplexityResult {
coverage: string
}

export interface ResultsSummary {
average: number
median: number
worst: number
}
2 changes: 1 addition & 1 deletion src/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isBlocklisted } from './util'
import { isBlocklisted } from './utils'
import { buildOptions } from './options'

const options = buildOptions({
Expand Down
169 changes: 0 additions & 169 deletions src/util.ts

This file was deleted.

Loading