-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add CyclomaticComplexity Analyzer.
- Loading branch information
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
src/Analyzer/CodeMetricsCalculator/CyclomaticComplexity/Adapter/ComplexityCountableNode.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface ComplexityCountableNode { | ||
isIncrement(): boolean; | ||
getChildren(): ComplexityCountableNode[]; | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Analyzer/CodeMetricsCalculator/CyclomaticComplexity/Calculator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ComplexityCountableNode } from './Adapter/ComplexityCountableNode'; | ||
import { ComplexityIncrement } from './ComplexityIncrement'; | ||
import { CyclomaticComplexity } from './CyclomaticComplexity'; | ||
import { CalculatorForAST } from '../../FromASTNode/CalculatorForAST'; | ||
import { MethodAnalyzer } from '../../FromASTNode/MethodAnalyzer'; | ||
import { Metrics } from '../../Metrics/Metrics'; | ||
import { ASTNodeSource } from '../../FromASTNode/ASTNodeSource'; | ||
import { inject, injectable } from 'inversify'; | ||
import { Types } from '../../../types/Types'; | ||
import { Converter } from '../../Adapter/Converter'; | ||
|
||
@injectable() | ||
export class Calculator implements CalculatorForAST { | ||
constructor( | ||
@inject(MethodAnalyzer) private readonly analyzer: MethodAnalyzer, | ||
@inject(Types.cognitiveComplexityConverter) | ||
private readonly converter: Converter<ComplexityCountableNode> | ||
) {} | ||
|
||
analyze(astNodes: ASTNodeSource[]) { | ||
return this.analyzer | ||
.analyze(astNodes) | ||
.map(({ astNode, ...other }) => ({ | ||
...other, | ||
countableNode: this.converter.convert(astNode), | ||
})) | ||
.map((row) => new Metrics(row.file, row.codePoints, this.calculate(row.countableNode))); | ||
} | ||
|
||
calculate(node: ComplexityCountableNode): CyclomaticComplexity[] { | ||
const complexities = this.extractComplexity(node); | ||
|
||
return [new CyclomaticComplexity(complexities)]; | ||
} | ||
|
||
private extractComplexity(node: ComplexityCountableNode): ComplexityIncrement[] { | ||
const result: ComplexityIncrement[] = []; | ||
|
||
if (node.isIncrement()) { | ||
result.push(new ComplexityIncrement(node)); | ||
} | ||
|
||
return result.concat(...node.getChildren().map((row) => this.extractComplexity(row))); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Analyzer/CodeMetricsCalculator/CyclomaticComplexity/ComplexityIncrement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ComplexityCountableNode } from './Adapter/ComplexityCountableNode'; | ||
|
||
export class ComplexityIncrement { | ||
public readonly complexity: number; | ||
|
||
constructor(complexityCountableNode: ComplexityCountableNode) { | ||
this.complexity = complexityCountableNode.isIncrement() ? 1 : 0; | ||
} | ||
|
||
valueOf() { | ||
return this.complexity; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Analyzer/CodeMetricsCalculator/CyclomaticComplexity/CyclomaticComplexity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { MetricsType } from '../../Metrics/MetricsType'; | ||
import { MetricsValue } from '../../Metrics/MetricsValue'; | ||
import { ComplexityIncrement } from './ComplexityIncrement'; | ||
|
||
export class CyclomaticComplexity implements MetricsValue { | ||
public readonly type = MetricsType.CognitiveComplexity; | ||
|
||
constructor(private readonly complexities: ComplexityIncrement[]) {} | ||
|
||
valueOf(): number { | ||
return this.complexities.reduce((prev, next) => Number(prev) + Number(next), 0); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/Analyzer/CodeMetricsCalculator/CyclomaticComplexity/__tests__/Calculator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { Calculator } from '../Calculator'; | ||
import { ComplexityCountableNode } from '../../../../TestHelpers/ComplexityCountableNode'; | ||
import { MethodAnalyzer } from '../../../FromASTNode/MethodAnalyzer'; | ||
import { ASTNodeExtractor } from '../../../ASTNodeExtractor'; | ||
import { ASTNode } from '../../../../TestHelpers/ASTNode'; | ||
import { CyclomaticComplexity } from '../CyclomaticComplexity'; | ||
|
||
describe('Cyclomatic Complexity Calculator', () => { | ||
describe('should increment when incrementable node.', () => { | ||
const createCountableNodeSeed: (ast: ASTNode) => any = (ast: ASTNode) => ({ | ||
DSL: ast.getName(), | ||
children: ast.getChildren().map((ast) => createCountableNodeSeed(ast)), | ||
}); | ||
const calculator = new Calculator(new MethodAnalyzer(new ASTNodeExtractor()), { | ||
convert: (ast: ASTNode) => new ComplexityCountableNode(createCountableNodeSeed(ast)), | ||
}); | ||
|
||
it('should returns 1 when incrementable node.', () => { | ||
const actual = calculator.analyze([ | ||
{ | ||
astNode: new ASTNode(':root:0:0', { | ||
'C:DummyClass:0:20': { | ||
'M:I:1:9': {}, | ||
}, | ||
}), | ||
file: { | ||
fullPath: '/tmp/dummy.ts', | ||
relativePath: 'dummy.ts', | ||
extension: 'ts', | ||
}, | ||
}, | ||
]); | ||
|
||
expect(Number(actual[0].getMetricsByMetricsValue(CyclomaticComplexity))).toBe(1); | ||
}); | ||
|
||
it('should returns 1 when childNode is incrementable node.', () => { | ||
const actual = calculator.analyze([ | ||
{ | ||
astNode: new ASTNode(':root:0:0', { | ||
'C:DummyClass:0:20': { | ||
'M::1:9': { | ||
':I:2:3': {}, | ||
}, | ||
}, | ||
}), | ||
file: { | ||
fullPath: '/tmp/dummy.ts', | ||
relativePath: 'dummy.ts', | ||
extension: 'ts', | ||
}, | ||
}, | ||
]); | ||
|
||
expect(Number(actual[0].getMetricsByMetricsValue(CyclomaticComplexity))).toBe(1); | ||
}); | ||
|
||
it('should return 2 when has tow incrementable node.', () => { | ||
const actual = calculator.analyze([ | ||
{ | ||
astNode: new ASTNode(':root:0:0', { | ||
'C:DummyClass:0:20': { | ||
'M:N:1:9': { | ||
':I:2:3': {}, | ||
':I:4:5': {}, | ||
}, | ||
}, | ||
}), | ||
file: { | ||
fullPath: '/tmp/dummy.ts', | ||
relativePath: 'dummy.ts', | ||
extension: 'ts', | ||
}, | ||
}, | ||
]); | ||
|
||
expect(Number(actual[0].getMetricsByMetricsValue(CyclomaticComplexity))).toBe(2); | ||
}); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
...nalyzer/CodeMetricsCalculator/CyclomaticComplexity/__tests__/CyclomaticComplexity.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { CyclomaticComplexity } from '../CyclomaticComplexity'; | ||
import { ComplexityIncrement } from '../ComplexityIncrement'; | ||
|
||
describe('Complexity Store Class', () => { | ||
describe('.valueOf()', () => { | ||
it('return sum complexities', () => { | ||
const complexityStore = new CyclomaticComplexity([ | ||
new ComplexityIncrement(createDummyNode('I')), | ||
new ComplexityIncrement(createDummyNode('I')), | ||
]); | ||
|
||
expect(complexityStore.valueOf()).toStrictEqual(2); | ||
}); | ||
}); | ||
}); | ||
|
||
const createDummyNode = (DSL: string) => ({ | ||
isIncrement: () => DSL.includes('I'), | ||
getChildren: () => [], | ||
}); |