Genese complexity report

<- ast-file-generation.service.ts
Methods : 7
Complexity index : 79.8
Cyclomatic complexity : 17
Cognitive complexity
42.9 % Correct 3/7
42.9 % Warning 3/7 (threshold : 10)
14.3 % Error 1/7 (threshold : 20)
Cyclomatic complexity
100 % Correct 7/7
0 % Warning 0/7 (threshold : 5)
0 % Error 0/7 (threshold : 10)
Methods of ast-file-generation.service.ts
generateFromString Complexity Index 6.4 Cyclomatic complexity 1
                            
                                
            
            
                /**
                 * Generate the AstFile corresponding to a given source code
                 * @param sourceCode
                 * @returns {{astNode: AstNodeInterface, name: string, text: string}}
                 */
                generateFromString(sourceCode: string): AstFileInterface { // ------------------------------- +0.4 Complexity index (+0.4 atomic)
                    const randomName = randomString(10); // ------------------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
                    const sourceFile = project.createSourceFile(`./${randomName}.ts`, sourceCode); // ------- +1.6 Complexity index (+0.6 atomic, +1 structural)
                    return { // ----------------------------------------------------------------------------- +0.1 Complexity index (+0.1 atomic)
                        name: `${randomName}.ts`, // -------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                        text: sourceFile.getFullText(), // -------------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                        astNode: this.createAstNodeChildren(sourceFile) // ---------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
                    };
                }
            
                            
                        
generate Complexity Index 10.3 Cyclomatic complexity 3
                            
                                
                
            
            
                /**
                 * Generates the AstFile corresponding to a given path and a given AstFolder
                 * @param path          // The path of the file
                 * @param astFolder     // The AstFolder containing the AstFile
                 */
                generate(path: string, astFolder: AstFolderInterface): AstFileInterface { // ---------- +0.6 Complexity index (+0.6 atomic)
                    if (!path || !astFolder) { // ----------------------------------------------------- +2.5 Complexity index (+0.5 atomic, +2 structural)
                        console.warn('No path or AstFolder : impossible to create AstFile'); // ------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                        return undefined; // ---------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                    }
                    const sourceFile: SourceFile = project.getSourceFileOrThrow(path); // ------------- +1.6 Complexity index (+0.6 atomic, +1 structural)
                    return { // ----------------------------------------------------------------------- +0.1 Complexity index (+0.1 atomic)
                        name: getFilename(path), // --------------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                        text: sourceFile.getFullText(), // -------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                        astNode: this.createAstNodeChildren(sourceFile) // ---------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
                    };
                }
            
                            
                        
createAstNodeChildren Complexity Index 22.5 Cyclomatic complexity 3
                            
                                
                
            
            
                /**
                 * Returns the Node children of a given Node
                 * @param node      // The Node to analyse
                 */
                createAstNodeChildren(node: Node): AstNodeInterface { // --------------------------- +2.4 Complexity index (+0.4 atomic, +2 recursivity)
                    let astNode: AstNodeInterface = { // ------------------------------------------- +0.3 Complexity index (+0.3 atomic)
                        end: node.getEnd(), // ----------------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                        kind: Ts.getKindAlias(node), // -------------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
                        name: Ts.getName(node), // ------------------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
                        pos: node.getPos(), // ----------------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                        start: node.getStart() // -------------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                    };
                    astNode = this.addTypeAndCpxFactors(node, astNode); // ------------------------- +1.6 Complexity index (+0.6 atomic, +1 structural)
                    if (node.getKindName() !== SyntaxKind.JsxElement) { // ------------------------- +2.6 Complexity index (+0.6 atomic, +2 structural)
                        node.forEachChild((childNode: Node) => { // -------------------------------- +3.0 Complexity index (+0.5 atomic, +0.5 nesting, +2 structural)
                            if (!astNode.children) { // -------------------------------------------- +2.8 Complexity index (+0.3 atomic, +1.5 nesting, +1 structural)
                                astNode.children = []; // ------------------------------------------ +0.3 Complexity index (+0.3 atomic)
                            }
                            astNode.children.push(this.createAstNodeChildren(childNode)); // ------- +2.6 Complexity index (+0.6 atomic, +2 structural)
                        });
                    }
                    return astNode; // ------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                }
            
                            
                        
addTypeAndCpxFactors Complexity Index 10.8 Cyclomatic complexity 4
                            
                                
                
            
            
                /**
                 * Adds the type to identifiers or parameters and calculates the CpxFactors of identifiers
                 * @param node          // The Node to analyse
                 * @param astNode       // The AstNode which will be updated with its type and CpxFactors
                 */
                private addTypeAndCpxFactors(node: Node, astNode: AstNodeInterface): AstNodeInterface { // ---------- +0.6 Complexity index (+0.6 atomic)
                    if (Ts.isFunctionCall(node)) { // --------------------------------------------------------------- +2.4 Complexity index (+0.4 atomic, +2 structural)
                        astNode.type = 'function'; // --------------------------------------------------------------- +0.4 Complexity index (+0.4 atomic)
                        if (WEIGHTED_METHODS.includes(astNode.name)) { // ------------------------------------------- +3.0 Complexity index (+0.5 atomic, +0.5 nesting, +2 structural)
                            const cpxFactors: CpxFactorsInterface = this.getCpxFactors(node); // -------------------- +1.6 Complexity index (+0.6 atomic, +1 structural)
                            if (cpxFactors) { // -------------------------------------------------------------------- +2.2 Complexity index (+0.2 atomic, +1 nesting, +1 structural)
                                astNode.cpxFactors = cpxFactors; // ------------------------------------------------- +0.4 Complexity index (+0.4 atomic)
                            }
                        }
                    }
                    return astNode; // ------------------------------------------------------------------------------ +0.2 Complexity index (+0.2 atomic)
                }
            
                            
                        
getCpxFactors Complexity Index 7.8 Cyclomatic complexity 3
                            
                                
                
            
            
                /**
                 * Returns the CpxFactors of a given Node (Identifier)
                 * @param node      // The Node to analyse
                 */
                private getCpxFactors(node: Node): CpxFactorsInterface { // ------- +0.4 Complexity index (+0.4 atomic)
                    if (node.getKindName() !== SyntaxKind.Identifier) { // -------- +2.6 Complexity index (+0.6 atomic, +2 structural)
                        return undefined; // -------------------------------------- +0.2 Complexity index (+0.2 atomic)
                    }
                    const identifier = node as Identifier; // --------------------- +0.4 Complexity index (+0.4 atomic)
                    const definition = identifier.getDefinitions()?.[0]; // ------- +1.5 Complexity index (+0.5 atomic, +1 structural)
                    return this.useWeight(definition, Ts.getName(node)); // ------- +2.7 Complexity index (+0.7 atomic, +2 structural)
                }
            
                            
                        
useWeight Complexity Index 13.9 Cyclomatic complexity 2
                            
                                
                
            
            
                /**
                 * Returns the cpxFActors relative to method usage.
                 * @param definition        // The DefinitionInfo of the Node corresponding to a method
                 * @param nodeName          // The name of the Node (redundant, but avoids new calculation of this value)
                 */
                useWeight(definition: DefinitionInfo, nodeName: string): CpxFactorsInterface { // ---------------------------------- +0.6 Complexity index (+0.6 atomic)
                    if (!definition) { // ------------------------------------------------------------------------------------------ +1.2 Complexity index (+0.2 atomic, +1 structural)
                        return undefined; // --------------------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                    }
                    const lib = this.library(definition); // ----------------------------------------------------------------------- +1.5 Complexity index (+0.5 atomic, +1 structural)
                    const method = lib ? Object.keys(WEIGHTS[lib]).find(e => e === nodeName) : undefined; // ----------------------- +7.5 Complexity index (+1.5 atomic, +2 nesting, +4 structural)
                    return method ? // --------------------------------------------------------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                        {
                            use: { // ---------------------------------------------------------------------------------------------- +0.1 Complexity index (+0.1 atomic)
                                method: WEIGHTS[lib][method] // -------------------------------------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 aggregation)
                            }
                        }
                        : undefined; // -------------------------------------------------------------------------------------------- +0.1 Complexity index (+0.1 atomic)
                }
            
                            
                        
library Complexity Index 8.1 Cyclomatic complexity 1
                            
                                
                
            
            
                // TODO: implement this method for libraries different than TypeScript itself
                /**
                 * Returns the library corresponding to the DefinitionInfo of a method's Node.
                 * @param definition
                 */
                library(definition: DefinitionInfo): string { // ---------------------------------------- +0.4 Complexity index (+0.4 atomic)
                    const path = definition.getSourceFile().getFilePath(); // --------------------------- +2.5 Complexity index (+0.5 atomic, +2 structural)
                    return path.match(/typescript\/lib/) ? 'typescript' : undefined; // ----------------- +5.2 Complexity index (+0.7 atomic, +1.5 aggregation, +3 structural)
                }