CRAP Complexity Coverage Location
1.01 1 24/31 (77.42%) crap/complexity.service.ts (L40 - L99)
    public async getComplexity({ sourcePath }: { sourcePath: string }): Promise<Array<FunctionComplexity | null>> {
        const source = await this.fileSystemService.loadSourceFile(sourcePath);
        const lines = source.split("\n");

        const lintResult = await this.lint({ source });

        return lintResult.messages.map((messageData) => {
            let complexity: string | undefined;
            let functionName: string | undefined;
            if (messageData.messageId === "enum") {
                const matches = messageData.message.match(this.enumRegex);
                complexity = "1";
                functionName = matches?.groups?.name;
            } else if (messageData.messageId === "class") {
                const matches = messageData.message.match(this.classRegex);
                complexity = "1";
                functionName = matches?.groups?.name;
            } else if (messageData.messageId === "export") {
                complexity = "1";
                functionName = "export";
            } else if (messageData.messageId === "function") {
                const matches = messageData.message.match(this.functionRegex);
                complexity = matches?.groups?.complexity;
                functionName = matches?.groups?.name;
            } else {
                this.logger.error("Unknown message ID.", { messageData, path: sourcePath });
                return null;
            }

            if (!messageData.endLine || !messageData.endColumn) {
                this.logger.error("No end location.", { messageData, path: sourcePath });
                return null;
            }

            if (!complexity || !functionName) {
                this.logger.error("Could not compute complexity.", { messageData, path: sourcePath });
                return null;
            }

            const result: FunctionComplexity = {
                start: {
                    line: messageData.line,
                    column: messageData.column,
                },
                end: {
                    line: messageData.endLine,
                    column: messageData.endColumn,
                },
                complexity: parseInt(complexity, 10),
                functionName,
                type: messageData.messageId,
            };

            if (this.configService.getHtmlReportDir()) {
                result.sourceCode = lines.slice(messageData.line - 1, messageData.endLine).join("\n");
            }

            return result;
        });
    }