Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | 8x 8x 77x 83x 77x 66x 60x 60x 83x 138x 6x 55x 55x 55x 45x 105x 115x 60x 3852x 52x 52x 52x 105x 105x 3845x 3845x 45x 85x 28811x 105x 105x 25071x 339x 339x 339x 4199x 399x 3815x 40x 28532x 4466x 27866x 3875x 27791x 3806x 101x 25011x 540x 578x 522x 582x 522x 18x 26x 25011x 8745x 8223x 8115x 101x 25067x 101x 45x 25011x 25011x 25011x 56x 25011x 1026x 45x 45x 942x 942x 942x 942x 942x 942x 45x 942x 45x 45x 942x 45x 105x 942x 45x 837x 33x 33x 45x 33x 804x 45x 45x 24x 24x 45x 45x 759x 759x 759x 45x 45x 25011x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 21x 21x 105x 60x 26x 15x 48x 27x 21x 3x 3x 18x 18x 18x 3x 6x 6x | import { TSESTree } from '@typescript-eslint/typescript-estree';
import { Severity } from '@aiready/core';
import type { NamingIssue } from '../types';
import {
parseFile,
traverseAST,
getLineNumber,
isLoopStatement,
} from '../utils/ast-parser';
import {
buildCodeContext,
isAcceptableInContext,
adjustSeverity,
} from '../utils/context-detector';
/**
* Advanced naming analyzer using TypeScript AST
*/
export async function analyzeNamingAST(
filePaths: string[]
): Promise<NamingIssue[]> {
const allIssues: NamingIssue[] = [];
for (const filePath of filePaths) {
try {
const ast = parseFile(filePath);
if (!ast) continue;
const context = buildCodeContext(filePath, ast);
const issues = analyzeIdentifiers(ast, filePath, context);
allIssues.push(...issues);
} catch (err) {
void err;
}
}
return allIssues;
}
/**
* Traverse AST and find naming issues in identifiers
*/
function analyzeIdentifiers(
ast: TSESTree.Program,
filePath: string,
context: any
): NamingIssue[] {
const issues: NamingIssue[] = [];
const scopeTracker = new ScopeTracker();
traverseAST(ast, {
enter: (node) => {
// 1. Variable Declarations
if (node.type === 'VariableDeclarator' && node.id.type === 'Identifier') {
const isParameter = false;
const isLoopVariable = isLoopStatement(node.parent?.parent);
scopeTracker.declareVariable(
node.id.name,
node.id,
getLineNumber(node.id),
{ isParameter, isLoopVariable }
);
}
// 2. Function Parameters
if (
node.type === 'FunctionDeclaration' ||
node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression'
) {
const isArrowParameter = node.type === 'ArrowFunctionExpression';
node.params.forEach((param) => {
if (param.type === 'Identifier') {
scopeTracker.declareVariable(
param.name,
param,
getLineNumber(param),
{ isParameter: true, isArrowParameter }
E );
EE} else if (param.type === 'ObjectPattern') {
// Handle destructured parameters: { id, name }
extractDestructuredIdentifiers(param, scopeTracker, {
isParameter: true,
isArrowParameter,
});
}
});
}
// 3. Class/Interface/Type names
if (
(node.type === 'ClassDeclaration' ||
node.type === 'TSInterfaceDeclaration' ||
node.type === 'TSTypeAliasDeclaration') &&
node.id
) {
checkNamingConvention(
node.id.name,
'PascalCase',
node.id,
filePath,
issues,
context
);
}
},
});
// Check all collected variables
for (const varInfo of scopeTracker.getVariables()) {
checkVariableNaming(varInfo, filePath, issues, context);
}
E
return issues;
}
/**
* Check if a name follows a specific convention
*/
function checkNamingConvention(
name: string,
convention: 'camelCase' | 'PascalCase' | 'UPPER_CASE',
node: TSESTree.Node,
file: string,
issues: NamingIssue[],
context: any
) {
let isValid = true;
if (convention === 'PascalCase') {
isValid = /^[A-Z][a-zA-Z0-9]*$/.test(name);
E} else if (convention === 'camelCase') {
isValid = /^[a-z][a-zA-Z0-9]*$/.test(name);
} else if (convention === 'UPPER_CASE') {
isValid = /^[A-Z][A-Z0-9_]*$/.test(name);
}
Iif (!isValid) {
const severity = adjustSeverity(Severity.Info, context, 'convention-mix');
issues.push({
file,
line: getLineNumber(node),
type: 'convention-mix',
identifier: name,
severity,
suggestion: `Follow ${convention} for this identifier`,
});
}
}
/**
* Advanced variable naming checks
*/
funcItion checkVariableNaming(
vaIrInfo: any,
file: string,
issues: NamingIssue[],
context: any
) {I
const { name, line, options } = varInfo;
// Skip very common small names if they are in acceptable context
if (isAcceptableInContext(name, context, options)) {
return;
}
// 1. Single letter names
if (
name.length === 1 &&
!options.isLoopVariable &&
!options.isArrowParameter
) {
const severity = adjustSeverity(Severity.Minor, context, 'poor-naming');
issues.push({
file,
line,
type: 'poor-naming',
identifier: name,
Iseverity,
suggestion: 'Use a more descriptive name than a single letter',
});
}
// 2. Vague names
const vagueNames = [
'data',
'info',
'item',
'obj',
'val',
'tmp',
'temp',
'thing',
'stuff',
];
if (vagueNames.includes(name.toLowerCase())) {
const severity = adjustSeverity(Severity.Minor, context, 'poor-naming');
issues.push({
file,
line,
type: 'poor-naming',
identifier: name,
severity,
suggestion: `Avoid vague names like '${name}'. What does this data represent?`,
});
}
// 3. Abbreviations
if (
name.length > 1 &&
name.length <= 3 &&
E!options.isLoopVariable &&
!iIsCommonAbbreviation(name)
) {
const severity = adjustSeverity(Severity.Info, context, 'abbreviation');
issues.push({
file,
line,
type: 'abbreviation',
identifier: name,
severity,
suggestion: 'Avoid non-standard abbreviations',
});
}
}
function isCommonAbbreviation(name: string): boolean {
Econst common = [
'id',
'db',
'fs',
'os',
'ip',I
'ui',
'ux',
'api',
'env',
'url',I
'req',
'res',
'err',
'ctx',
'cb',
'idx',
'src',
'dir',
'app',
'dev',
'qa',
'dto',
'dao',
'ref',
'ast',
'dom',
'log',
'msg',
'pkg',
'req',
'err',
'res',
'css',
'html',
'xml',
'jsx',
'tsx',
'ts',
'js',
];
return common.includes(name.toLowerCase());
}
/**
* Simple scope-aware variable tracker
*/
class ScopeTracker {
private variables: any[] = [];
declareVariable(
name: string,
node: TSESTree.Node,
line: number,
options = {}
) {
this.variables.push({ name, node, line, options });
}
getVariables() {
return this.variables;
}
}
/**
* Extracts identifiers from destructured patterns (object or array destructuring)
* and registers them in the scope tracker.
* @param node - The AST node representing the destructured pattern
* @param isParameter - When true, indicates the destructured variable is a function parameter; when false, it's a local variable
* @param scopeTracker - The scope tracker to register variables with
*/
function extractDestructuredIdentifiers(
node: TSESTree.ObjectPattern | TSESTree.ArrayPattern,
scopeTracker: ScopeTracker,
options: {
isParameter?: boolean;
isArrowParameter?: boolean;
} = {}
) {
const { isParameter = false, isArrowParameter = false } = options;
if (node.type === 'ObjectPattern') {
node.properties.forEach((prop) => {
if (prop.type === 'Property' && prop.value.type === 'Identifier') {
sEcopeTracker.declareVariable(
prop.value.name,
E prop.value,
getLineNumber(prop.value),
{
isParameter,
isDestructured: true,
isArrowParameter,
}
);
}
});
} else if (node.type === 'ArrayPattern') {
for (const element of node.elements) {
if (element?.type === 'Identifier') {
scopeTracker.declareVariable(
element.name,
element,
getLineNumber(element),
{
isParameter,
isDestructured: true,
isArrowParameter,
}
);
}
}
}
}
|