-
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 CyclomaticComplexityCountableNode for TypeScript.
- Loading branch information
Showing
8 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
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
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
4 changes: 2 additions & 2 deletions
4
...nguage/TypeScript/Converter/Complexity.ts → ...peScript/Converter/CognitiveComplexity.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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { injectable } from 'inversify'; | ||
import { ASTNode } from '../ASTNode'; | ||
import { ComplexityCountableNode } from '../ComplexityCountableNode'; | ||
import { CognitiveComplexityCountableNode } from '../CognitiveComplexityCountableNode'; | ||
|
||
@injectable() | ||
export class Complexity { | ||
convert(astNode: ASTNode) { | ||
return new ComplexityCountableNode(astNode); | ||
return new CognitiveComplexityCountableNode(astNode); | ||
} | ||
} |
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,10 @@ | ||
import { injectable } from 'inversify'; | ||
import { ASTNode } from '../ASTNode'; | ||
import { CyclomaticComplexityCountableNode } from '../CyclomaticComplexityCountableNode'; | ||
|
||
@injectable() | ||
export class Complexity { | ||
convert(astNode: ASTNode) { | ||
return new CyclomaticComplexityCountableNode(astNode); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Language/TypeScript/CyclomaticComplexityCountableNode.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,42 @@ | ||
import { injectable } from 'inversify'; | ||
import * as ts from 'typescript'; | ||
import { ComplexityCountableNode as ComplexityCountableNodeInterface } from '../../Analyzer/CodeMetricsCalculator/CyclomaticComplexity/Adapter/ComplexityCountableNode'; | ||
import { ASTNode } from './ASTNode'; | ||
|
||
@injectable() | ||
export class CyclomaticComplexityCountableNode implements ComplexityCountableNodeInterface { | ||
private static readonly incrementSyntaxKinds = [ | ||
ts.SyntaxKind.IfStatement, | ||
ts.SyntaxKind.SwitchStatement, | ||
ts.SyntaxKind.ForInStatement, | ||
ts.SyntaxKind.ForOfStatement, | ||
ts.SyntaxKind.ForStatement, | ||
ts.SyntaxKind.WhileStatement, | ||
ts.SyntaxKind.CatchClause, | ||
ts.SyntaxKind.ConditionalExpression, | ||
ts.SyntaxKind.AmpersandAmpersandToken, | ||
ts.SyntaxKind.BarBarToken, | ||
ts.SyntaxKind.LabeledStatement, | ||
ts.SyntaxKind.QuestionQuestionToken, | ||
ts.SyntaxKind.QuestionDotToken, | ||
]; | ||
|
||
private readonly node: ASTNode; | ||
|
||
private readonly pureNode: ts.Node; | ||
|
||
constructor(node: ASTNode) { | ||
this.node = node; | ||
this.pureNode = node.node; | ||
} | ||
|
||
isIncrement() { | ||
const allIncrementSyntaxKinds = CyclomaticComplexityCountableNode.incrementSyntaxKinds; | ||
|
||
return allIncrementSyntaxKinds.includes(this.pureNode.kind); | ||
} | ||
|
||
getChildren() { | ||
return this.node.getChildren().map((node) => new CyclomaticComplexityCountableNode(node)); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
src/Language/TypeScript/__tests__/CyclomaticComplexityCountableNode.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,51 @@ | ||
import { readFileSync } from 'fs'; | ||
import ts from 'typescript'; | ||
import { ASTNode } from '../ASTNode'; | ||
import { CyclomaticComplexityCountableNode } from '../CyclomaticComplexityCountableNode'; | ||
|
||
describe('CyclomaticComplexityCountableNode', () => { | ||
const parent = ts.createSourceFile( | ||
`${__dirname}/fixtures/example.ts`, | ||
readFileSync(`${__dirname}/fixtures/example.ts`).toString(), | ||
ts.ScriptTarget.ES2016, | ||
true | ||
); | ||
const members = <ts.NodeArray<ts.MethodDeclaration>>(<ts.ClassDeclaration>parent.statements[0]).members; | ||
const map = [...members].reduce((map, node) => map.set(node.name.getText(), node.body!), new Map<string, ts.Block>()); | ||
|
||
describe('.isIncrement()', () => { | ||
it.each([ | ||
['if', map.get('if')!.statements[0]], | ||
['switch', map.get('switch')!.statements[0]], | ||
['for', map.get('for')!.statements[0]], | ||
['forIn', map.get('forIn')!.statements[0]], | ||
['forOf', map.get('forOf')!.statements[0]], | ||
['while', map.get('while')!.statements[0]], | ||
['catch', (<ts.TryStatement>map.get('try')!.statements[0]).catchClause!], | ||
['conditional', (<ts.ReturnStatement>map.get('conditional')!.statements[0]).expression!], | ||
[ | ||
'ampersand', | ||
(<ts.BinaryExpression>(<ts.ReturnStatement>map.get('ampersand')!.statements[0]).expression!).operatorToken, | ||
], | ||
[ | ||
'barbar', | ||
(<ts.BinaryExpression>(<ts.ReturnStatement>map.get('barbar')!.statements[0]).expression!).operatorToken, | ||
], | ||
[ | ||
'nullishCoalescingOperator', | ||
(<ts.BinaryExpression>(<ts.ReturnStatement>map.get('nullishCoalescingOperator')!.statements[0]).expression!) | ||
.operatorToken, | ||
], | ||
[ | ||
'optionalChaining', | ||
(<ts.CallExpression>(<ts.ReturnStatement>map.get('optionalChaining')!.statements[0]).expression!) | ||
.questionDotToken, | ||
], | ||
])('should %s is increment.', (_, statement) => { | ||
const astNode = new ASTNode(statement, parent); | ||
const actual = new CyclomaticComplexityCountableNode(astNode); | ||
|
||
expect(actual.isIncrement()).toBe(true); | ||
}); | ||
}); | ||
}); |
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