mirror of
https://github.com/samiyev/puaros.git
synced 2025-12-28 07:16:53 +05:00
refactor: migrate hardcode detector from regex to AST-based analysis
- Replace regex-based matchers with tree-sitter AST traversal - Add duplicate value tracking across files - Implement boolean literal detection - Add value type classification (email, url, ip, api_key, etc.) - Improve context awareness with AST node analysis - Reduce false positives with better constant detection Breaking changes removed: - BraceTracker.ts - ExportConstantAnalyzer.ts - MagicNumberMatcher.ts - MagicStringMatcher.ts New components added: - AstTreeTraverser for AST walking - DuplicateValueTracker for cross-file tracking - AstContextChecker for node context analysis - AstNumberAnalyzer, AstStringAnalyzer, AstBooleanAnalyzer - ValuePatternMatcher for type detection Test coverage: 87.97% statements, 96.75% functions
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import Parser from "tree-sitter"
|
||||
import { HardcodedValue, HardcodeType } from "../../domain/value-objects/HardcodedValue"
|
||||
import { DETECTION_VALUES } from "../../shared/constants/rules"
|
||||
import { AstContextChecker } from "./AstContextChecker"
|
||||
|
||||
/**
|
||||
* AST-based analyzer for detecting magic booleans
|
||||
*
|
||||
* Detects boolean literals used as arguments without clear meaning.
|
||||
* Example: doSomething(true, false, true) - hard to understand
|
||||
* Better: doSomething({ sync: true, validate: false, cache: true })
|
||||
*/
|
||||
export class AstBooleanAnalyzer {
|
||||
constructor(private readonly contextChecker: AstContextChecker) {}
|
||||
|
||||
/**
|
||||
* Analyzes a boolean node and returns a violation if it's a magic boolean
|
||||
*/
|
||||
public analyze(node: Parser.SyntaxNode, lines: string[]): HardcodedValue | null {
|
||||
if (!this.shouldDetect(node)) {
|
||||
return null
|
||||
}
|
||||
|
||||
const value = node.text === DETECTION_VALUES.BOOLEAN_TRUE
|
||||
|
||||
return this.createViolation(node, value, lines)
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if boolean should be detected
|
||||
*/
|
||||
private shouldDetect(node: Parser.SyntaxNode): boolean {
|
||||
if (this.contextChecker.isInExportedConstant(node)) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (this.contextChecker.isInTypeContext(node)) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (this.contextChecker.isInTestDescription(node)) {
|
||||
return false
|
||||
}
|
||||
|
||||
const parent = node.parent
|
||||
if (!parent) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (parent.type === "arguments") {
|
||||
return this.isInFunctionCallWithMultipleBooleans(parent)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if function call has multiple boolean arguments
|
||||
*/
|
||||
private isInFunctionCallWithMultipleBooleans(argsNode: Parser.SyntaxNode): boolean {
|
||||
let booleanCount = 0
|
||||
|
||||
for (const child of argsNode.children) {
|
||||
if (child.type === "true" || child.type === "false") {
|
||||
booleanCount++
|
||||
}
|
||||
}
|
||||
|
||||
return booleanCount >= 2
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a HardcodedValue violation from a boolean node
|
||||
*/
|
||||
private createViolation(
|
||||
node: Parser.SyntaxNode,
|
||||
value: boolean,
|
||||
lines: string[],
|
||||
): HardcodedValue {
|
||||
const lineNumber = node.startPosition.row + 1
|
||||
const column = node.startPosition.column
|
||||
const context = lines[node.startPosition.row]?.trim() ?? ""
|
||||
|
||||
return HardcodedValue.create(
|
||||
value,
|
||||
"MAGIC_BOOLEAN" as HardcodeType,
|
||||
lineNumber,
|
||||
column,
|
||||
context,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user