Provides JavaScript/TypeScript helper functions and utilities for SonarJS rule implementation. Use when implementing rule fixes, searching for existing utilities, or needing to check available helper functions.
Helper functions are located in packages/analysis/src/jsts/rules/helpers/. These provide common utilities for rule implementations.
isCallingMethod() - Check if a call expression is calling a specific methodgetUniqueWriteUsageOrNode() - Track variable assignmentsisRequiredParserServices() - Check if TypeScript type information is availableisArray(), , etc. - Type checking helpersisString()getImportDeclarations() - Get import declarations from the contextBefore writing any new function, search packages/analysis/src/jsts/rules/helpers/ for an existing equivalent. Key files to check:
ast.ts — generic AST utilities (child enumeration, node classification, ancestor walking)vue.ts — Vue-specific helpersreact.ts — React/JSX-specific helpersReviewers will reject re-implementations of existing helpers.
Anti-pattern: Iterating Object.keys(node) to enumerate child nodes. Use the existing childrenOf helper instead:
// ❌ Do not do this
for (const key of Object.keys(node)) { ... }
// ✅ Use this
for (const child of childrenOf(node)) { ... }
If you write a new utility function that only operates on estree/TSESTree nodes with no rule-specific domain logic, place it in helpers/ast.ts (or helpers/vue.ts, helpers/react.ts for domain-specific utilities) — not inside the individual rule file. Functions buried in a rule file are invisible to future implementers.
To explore available helpers:
ls packages/analysis/src/jsts/rules/helpers/
Read helper implementations to understand their usage and parameters.
childrenOf(node) always returns estree.Node[]. Do not write custom type guard predicates or Array.isArray() checks on its return values — they are dead code:
// ❌ Redundant — childrenOf already guarantees estree.Node[]
for (const child of childrenOf(node)) {
if (typeof child === 'object' && 'type' in child) { ... }
}
// ✅ Iterate directly
for (const child of childrenOf(node)) {
// child is already estree.Node
}