|
| 1 | +import { JAVA_COMMENT_QUERY, JAVA_COMPLEXITY_QUERY } from "./queries.ts"; |
| 2 | +import type { |
| 3 | + CodeCounts, |
| 4 | + CommentSpan, |
| 5 | + JavaComplexityMetrics, |
| 6 | +} from "./types.ts"; |
| 7 | +import type Parser from "tree-sitter"; |
| 8 | + |
| 9 | +// This class has no tests because it is copy-pasted from C. |
| 10 | +// If the C metrics work, then the Java ones do. |
| 11 | + |
| 12 | +export class JavaMetricsAnalyzer { |
| 13 | + /** |
| 14 | + * Calculates metrics for a Java symbol. |
| 15 | + * @param node - The syntax node to analyze. |
| 16 | + * @returns An object containing the complexity metrics. |
| 17 | + */ |
| 18 | + public analyzeNode(node: Parser.SyntaxNode): JavaComplexityMetrics { |
| 19 | + if (node.type === "preproc_function_def") { |
| 20 | + const value = node.childForFieldName("value"); |
| 21 | + if (value) { |
| 22 | + node = value; |
| 23 | + } |
| 24 | + } |
| 25 | + const complexityCount = this.getComplexityCount(node); |
| 26 | + const linesCount = node.endPosition.row - node.startPosition.row + 1; |
| 27 | + const codeCounts = this.getCodeCounts(node); |
| 28 | + const codeLinesCount = codeCounts.lines; |
| 29 | + const characterCount = node.endIndex - node.startIndex; |
| 30 | + const codeCharacterCount = codeCounts.characters; |
| 31 | + |
| 32 | + return { |
| 33 | + cyclomaticComplexity: complexityCount, |
| 34 | + linesCount, |
| 35 | + codeLinesCount, |
| 36 | + characterCount, |
| 37 | + codeCharacterCount, |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + private getComplexityCount(node: Parser.SyntaxNode): number { |
| 42 | + const complexityMatches = JAVA_COMPLEXITY_QUERY.captures(node); |
| 43 | + return complexityMatches.length; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Finds comments in the given node and returns their spans. |
| 48 | + * @param node - The AST node to analyze |
| 49 | + * @returns An object containing pure comment lines and comment spans |
| 50 | + */ |
| 51 | + private findComments( |
| 52 | + node: Parser.SyntaxNode, |
| 53 | + lines: string[], |
| 54 | + ): { |
| 55 | + pureCommentLines: Set<number>; |
| 56 | + commentSpans: CommentSpan[]; |
| 57 | + } { |
| 58 | + const pureCommentLines = new Set<number>(); |
| 59 | + const commentSpans: CommentSpan[] = []; |
| 60 | + |
| 61 | + const commentCaptures = JAVA_COMMENT_QUERY.captures(node); |
| 62 | + |
| 63 | + for (const capture of commentCaptures) { |
| 64 | + const commentNode = capture.node; |
| 65 | + |
| 66 | + // Record the comment span for character counting |
| 67 | + commentSpans.push({ |
| 68 | + start: { |
| 69 | + row: commentNode.startPosition.row, |
| 70 | + column: commentNode.startPosition.column, |
| 71 | + }, |
| 72 | + end: { |
| 73 | + row: commentNode.endPosition.row, |
| 74 | + column: commentNode.endPosition.column, |
| 75 | + }, |
| 76 | + }); |
| 77 | + |
| 78 | + // Check if the comment starts at the beginning of the line (ignoring whitespace) |
| 79 | + const lineIdx = commentNode.startPosition.row - node.startPosition.row; |
| 80 | + if (lineIdx >= 0 && lineIdx < lines.length) { |
| 81 | + const lineText = lines[lineIdx]; |
| 82 | + const textBeforeComment = lineText.substring( |
| 83 | + 0, |
| 84 | + commentNode.startPosition.column, |
| 85 | + ); |
| 86 | + |
| 87 | + // If there's only whitespace before the comment, it's a pure comment line |
| 88 | + if (textBeforeComment.trim().length === 0) { |
| 89 | + for ( |
| 90 | + let line = commentNode.startPosition.row; |
| 91 | + line <= commentNode.endPosition.row; |
| 92 | + line++ |
| 93 | + ) { |
| 94 | + pureCommentLines.add(line); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return { pureCommentLines, commentSpans }; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Finds all empty lines in a node |
| 105 | + * |
| 106 | + * @param node The syntax node to analyze |
| 107 | + * @param lines The lines of text in the node |
| 108 | + * @returns Set of line numbers that are empty |
| 109 | + */ |
| 110 | + private findEmptyLines( |
| 111 | + node: Parser.SyntaxNode, |
| 112 | + lines: string[], |
| 113 | + ): Set<number> { |
| 114 | + const emptyLines = new Set<number>(); |
| 115 | + for (let i = 0; i < lines.length; i++) { |
| 116 | + const lineIndex = node.startPosition.row + i; |
| 117 | + if (lines[i].trim().length === 0) { |
| 118 | + emptyLines.add(lineIndex); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return emptyLines; |
| 123 | + } |
| 124 | + |
| 125 | + private getCodeCounts(node: Parser.SyntaxNode): CodeCounts { |
| 126 | + const lines = node.text.split(/\r?\n/); |
| 127 | + const linesCount = lines.length; |
| 128 | + // Find comments and their spans |
| 129 | + const { pureCommentLines, commentSpans } = this.findComments(node, lines); |
| 130 | + |
| 131 | + // Find empty lines |
| 132 | + const emptyLines = this.findEmptyLines(node, lines); |
| 133 | + |
| 134 | + // Calculate code lines |
| 135 | + const nonCodeLines = new Set([...pureCommentLines, ...emptyLines]); |
| 136 | + const codeLinesCount = linesCount - nonCodeLines.size; |
| 137 | + |
| 138 | + let codeCharCount = 0; |
| 139 | + |
| 140 | + // Process each line individually |
| 141 | + for (let i = 0; i < lines.length; i++) { |
| 142 | + const lineIndex = node.startPosition.row + i; |
| 143 | + const line = lines[i]; |
| 144 | + |
| 145 | + // Skip empty lines and pure comment lines |
| 146 | + if (emptyLines.has(lineIndex) || pureCommentLines.has(lineIndex)) { |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + // Process line for code characters |
| 151 | + let lineText = line; |
| 152 | + |
| 153 | + // Remove comment content from the line if present |
| 154 | + for (const span of commentSpans) { |
| 155 | + if (span.start.row === lineIndex) { |
| 156 | + // Comment starts on this line |
| 157 | + lineText = lineText.substring(0, span.start.column); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + // Count normalized code characters (trim excessive whitespace) |
| 162 | + const normalizedText = lineText.trim().replace(/\s+/g, " "); |
| 163 | + codeCharCount += normalizedText.length; |
| 164 | + } |
| 165 | + |
| 166 | + return { |
| 167 | + lines: codeLinesCount, |
| 168 | + characters: codeCharCount, |
| 169 | + }; |
| 170 | + } |
| 171 | +} |
0 commit comments