Genese complexity report

<- ast-node.service.ts
Methods : 9
Complexity index : 74.3
Cyclomatic complexity : 48
Cognitive complexity
55.6 % Correct 5/9
44.4 % Warning 4/9 (threshold : 10)
0 % Error 0/9 (threshold : 20)
Cyclomatic complexity
44.4 % Correct 4/9
55.6 % Warning 5/9 (threshold : 5)
0 % Error 0/9 (threshold : 10)
Methods of ast-node.service.ts
getCode Complexity Index 11 Cyclomatic complexity 8
                            
                                
            
            
            
                /**
                 * Returns the source code of a given AstNode
                 * @param astNode       // The AstNode to analyse
                 */
                getCode(astNode: AstNode): string { // ------------------------------------------------------------------ +0.4 Complexity index (+0.4 atomic)
                    if (!astNode?.astFile?.text || astNode?.pos === undefined || astNode?.end === undefined) { // ------- +4.6 Complexity index (+1.6 atomic, +3 structural)
                        return ''; // ----------------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                    }
                    const text = astNode.astFile.text.replace(/\\/g, '\\'); // ------------------------------------------ +3.1 Complexity index (+0.8 atomic, +0.3 aggregation, +2 structural)
                    return text.slice(astNode.pos, astNode.end); // ----------------------------------------------------- +2.7 Complexity index (+0.7 atomic, +1 structural, +1 use)
                }
            
                            
                        
getContext Complexity Index 9.9 Cyclomatic complexity 7
                            
                                
                
            
                /**
                 * Gets the javascript context of the AST node of a AstNode
                 * @param astNode      // The AstNode for which we search the context
                 */
                getContext(astNode: AstNode): AstNode { // ------------------------------------- +0.4 Complexity index (+0.4 atomic)
                    if (!astNode) { // --------------------------------------------------------- +1.2 Complexity index (+0.2 atomic, +1 structural)
                        return undefined; // --------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                    }
                    switch (astNode.kind) { // ------------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                        case SyntaxKind.SourceFile: // ----------------------------------------- +0.3 Complexity index (+0.3 atomic)
                            return astNode; // ------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                        case SyntaxKind.Identifier: // ----------------------------------------- +0.3 Complexity index (+0.3 atomic)
                            return this.getIdentifierContext(astNode); // ---------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
                        case SyntaxKind.ThisKeyword: // ---------------------------------------- +0.3 Complexity index (+0.3 atomic)
                            return astNode.parent?.context?.context; // ------------------------ +0.5 Complexity index (+0.5 atomic)
                        default: // ------------------------------------------------------------ +0.1 Complexity index (+0.1 atomic)
                            if (astNode.parent?.mayDefineContext) { // ------------------------- +1.9 Complexity index (+0.4 atomic, +0.5 nesting, +1 structural)
                                return astNode.parent; // -------------------------------------- +0.3 Complexity index (+0.3 atomic)
                            } else { // -------------------------------------------------------- +1.1 Complexity index (+0.1 atomic, +1 structural)
                                return astNode.parent?.context; // ----------------------------- +0.4 Complexity index (+0.4 atomic)
                            }
                    }
                }
            
                            
                        
getIdentifierContext Complexity Index 7.1 Cyclomatic complexity 7
                            
                                
                
            
            
                /**
                 * Gets the javascript context of an Identifier AST node of a given AstNode
                 * @param astNode      // The concerned AstNode
                 */
                private getIdentifierContext(astNode: AstNode): AstNode { // ---------------------------------------------------------------------- +0.4 Complexity index (+0.4 atomic)
                    if (this.isSecondSonOfPropertyAccessExpression(astNode)) { // ----------------------------------------------------------------- +2.4 Complexity index (+0.4 atomic, +2 structural)
                        return astNode.parent?.firstSon?.mayDefineContext ? astNode.parent?.firstSon : astNode.parent?.firstSon.context; // ------- +2.8 Complexity index (+1.3 atomic, +0.5 nesting, +1 structural)
                    } else { // ------------------------------------------------------------------------------------------------------------------- +1.1 Complexity index (+0.1 atomic, +1 structural)
                        return astNode.parent?.context; // ---------------------------------------------------------------------------------------- +0.4 Complexity index (+0.4 atomic)
                    }
                }
            
                            
                        
isSecondSonOfPropertyAccessExpression Complexity Index 3.5 Cyclomatic complexity 4
                            
                                
                
            
            
                /**
                 * Checks if a AstNode is the second son of an AST node "PropertyAccessExpression"
                 * (the first son is the object and the second is its property)
                 * @param astNode
                 */
                private isSecondSonOfPropertyAccessExpression(astNode: AstNode): boolean { // --------------------------------- +0.3 Complexity index (+0.3 atomic)
                    return Ast.isPropertyAccessExpression(astNode?.parent) && astNode === astNode?.parent.secondSon; // ------- +3.2 Complexity index (+1.2 atomic, +2 structural)
                }
            
                            
                        
isCallback Complexity Index 3.4 Cyclomatic complexity 2
                            
                                
                
            
            
                /**
                 * Checks if a AstNode is a Callback (ie a parameter which is used later in a CallExpression)
                 * @param astNode      // The AstNode to check
                 */
                isCallback(astNode: AstNode): boolean { // ------------------------------------------------------------- +0.3 Complexity index (+0.3 atomic)
                    if (!astNode.isParam) { // ------------------------------------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                        return false; // ------------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                    }
                    return this.hasCallBack(astNode, astNode.parent); // ----------------------------------------------- +1.6 Complexity index (+0.6 atomic, +1 structural)
                }
            
                            
                        
hasCallBack Complexity Index 12.7 Cyclomatic complexity 7
                            
                                
                
            
            
                /**
                 * Checks if a Parameter AstNode is used in a CallExpression of its method
                 * @param astNodeParam     // The Parameter AstNode
                 * @param astNode          // Parameter used for recursion
                 */
                private hasCallBack(astNodeParam: AstNode, astNode?: AstNode): boolean { // ----------------------------------------------------------------------- +2.5 Complexity index (+0.5 atomic, +2 recursivity)
                    for (const childAstNode of astNode?.children) { // -------------------------------------------------------------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
                        if (childAstNode.name === astNodeParam.name && childAstNode.context === astNodeParam.context && childAstNode.isCallIdentifier) { // ------- +5.2 Complexity index (+1.7 atomic, +0.5 nesting, +3 structural)
                            return true; // ----------------------------------------------------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                        }
                        if (this.hasCallBack(astNodeParam, childAstNode)) { // ------------------------------------------------------------------------------------ +3.0 Complexity index (+0.5 atomic, +0.5 nesting, +2 structural)
                            return true; // ----------------------------------------------------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                        }
                    }
                    return false; // ------------------------------------------------------------------------------------------------------------------------------ +0.2 Complexity index (+0.2 atomic)
                }
            
                            
                        
isRecursiveMethod Complexity Index 3.4 Cyclomatic complexity 2
                            
                                
                
            
            
                /**
                 * Checks if a AstNode is a recursive method (ie a MethodDeclaration or a FunctionDeclaration which is called from inside)
                 * @param astNode      // The AstNode to check
                 */
                isRecursiveMethod(astNode: AstNode): boolean { // ----------------------------------------------------------------------------------- +0.3 Complexity index (+0.3 atomic)
                    if (!astNode.isFunctionOrMethodDeclaration) { // -------------------------------------------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                        return false; // ------------------------------------------------------------------------------------------------------------ +0.2 Complexity index (+0.2 atomic)
                    }
                    return this.hasRecursiveNode(astNode.astMethod, astNode); // -------------------------------------------------------------------- +1.6 Complexity index (+0.6 atomic, +1 structural)
                }
            
                            
                        
hasRecursiveNode Complexity Index 12.8 Cyclomatic complexity 7
                            
                                
                
            
            
                /**
                 * Checks if a MethodDeclaration or a FunctionDeclaration AstNode is called by one of its children
                 * @param astNodeMethod     // The MethodDeclaration or FunctionDeclaration AstNode
                 * @param astNode          // Parameter used for recursion
                 */
                private hasRecursiveNode(astNodeMethod: AstMethod, astNode?: AstNode): boolean { // ---------------------------------------------------------------------------------- +2.5 Complexity index (+0.5 atomic, +2 recursivity)
                    for (const childAstNode of astNode?.children) { // --------------------------------------------------------------------------------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
                        if (childAstNode.name === astNodeMethod.name && childAstNode.context === astNodeMethod.astNode.context && !astNode.isFunctionOrMethodDeclaration) { // ------- +5.3 Complexity index (+1.8 atomic, +0.5 nesting, +3 structural)
                            return true; // ------------------------------------------------------------------------------------------------------------------------------------------ +0.2 Complexity index (+0.2 atomic)
                        }
                        if (this.hasRecursiveNode(astNodeMethod, childAstNode)) { // ------------------------------------------------------------------------------------------------- +3.0 Complexity index (+0.5 atomic, +0.5 nesting, +2 structural)
                            return true; // ------------------------------------------------------------------------------------------------------------------------------------------ +0.2 Complexity index (+0.2 atomic)
                        }
                    }
                    return false; // ------------------------------------------------------------------------------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                }
            
                            
                        
flatMapAstNodes Complexity Index 10.5 Cyclomatic complexity 4
                            
                                
                
            
            
                /**
                 * Returns an array of AstNodes with all the AstNode children of the first param concatenated with the second param
                 * @param astNode      // The "parent" node to parse
                 * @param astNodes     // The "accumulator"
                 */
                flatMapAstNodes(astNode: AstNode, astNodes: AstNode[]): AstNode[] { // ------------------------------------------------------- +2.8 Complexity index (+0.8 atomic, +2 recursivity)
                    for (const childAstNode of astNode?.children) { // ----------------------------------------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
                        astNodes.push(childAstNode); // -------------------------------------------------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                        if (childAstNode.children.length > 0) { // --------------------------------------------------------------------------- +2.1 Complexity index (+0.6 atomic, +0.5 nesting, +1 structural)
                            astNodes = astNodes.concat(this.flatMapAstNodes(childAstNode, [])); // ------------------------------------------- +2.7 Complexity index (+0.7 atomic, +2 structural)
                        }
                    }
                    return astNodes; // ------------------------------------------------------------------------------------------------------ +0.2 Complexity index (+0.2 atomic)
                }