@sun-asterisk/sunlint 1.2.1 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config/rule-analysis-strategies.js +18 -2
- package/engines/eslint-engine.js +9 -11
- package/engines/heuristic-engine.js +55 -31
- package/package.json +2 -1
- package/rules/README.md +252 -0
- package/rules/common/C002_no_duplicate_code/analyzer.js +65 -0
- package/rules/common/C002_no_duplicate_code/config.json +23 -0
- package/rules/common/C003_no_vague_abbreviations/analyzer.js +418 -0
- package/rules/common/C003_no_vague_abbreviations/config.json +35 -0
- package/rules/common/C006_function_naming/analyzer.js +504 -0
- package/rules/common/C006_function_naming/config.json +86 -0
- package/rules/common/C006_function_naming/smart-analyzer.js +503 -0
- package/rules/common/C010_limit_block_nesting/analyzer.js +389 -0
- package/rules/common/C012_command_query_separation/analyzer.js +481 -0
- package/rules/common/C012_command_query_separation/ast-analyzer.js +495 -0
- package/rules/common/C013_no_dead_code/analyzer.js +206 -0
- package/rules/common/C014_dependency_injection/analyzer.js +338 -0
- package/rules/common/C017_constructor_logic/analyzer.js +314 -0
- package/rules/common/C019_log_level_usage/analyzer.js +362 -0
- package/rules/common/C019_log_level_usage/config.json +121 -0
- package/rules/common/C029_catch_block_logging/analyzer-backup.js +426 -0
- package/rules/common/C029_catch_block_logging/analyzer-fixed.js +130 -0
- package/rules/common/C029_catch_block_logging/analyzer-multi-tech.js +487 -0
- package/rules/common/C029_catch_block_logging/analyzer-simple.js +110 -0
- package/rules/common/C029_catch_block_logging/analyzer-smart-pipeline.js +755 -0
- package/rules/common/C029_catch_block_logging/analyzer.js +129 -0
- package/rules/common/C029_catch_block_logging/ast-analyzer-backup.js +441 -0
- package/rules/common/C029_catch_block_logging/ast-analyzer-new.js +127 -0
- package/rules/common/C029_catch_block_logging/ast-analyzer.js +133 -0
- package/rules/common/C029_catch_block_logging/cfg-analyzer.js +408 -0
- package/rules/common/C029_catch_block_logging/config.json +59 -0
- package/rules/common/C029_catch_block_logging/dataflow-analyzer.js +454 -0
- package/rules/common/C029_catch_block_logging/multi-language-ast-engine.js +700 -0
- package/rules/common/C029_catch_block_logging/pattern-learning-analyzer.js +568 -0
- package/rules/common/C029_catch_block_logging/semantic-analyzer.js +459 -0
- package/rules/common/C031_validation_separation/analyzer.js +186 -0
- package/rules/common/C041_no_sensitive_hardcode/analyzer.js +292 -0
- package/rules/common/C041_no_sensitive_hardcode/ast-analyzer.js +296 -0
- package/rules/common/C042_boolean_name_prefix/analyzer.js +300 -0
- package/rules/common/C043_no_console_or_print/analyzer.js +431 -0
- package/rules/common/C047_no_duplicate_retry_logic/analyzer.js +590 -0
- package/rules/common/C075_explicit_return_types/analyzer.js +103 -0
- package/rules/common/C076_single_test_behavior/analyzer.js +121 -0
- package/rules/docs/C002_no_duplicate_code.md +57 -0
- package/rules/docs/C031_validation_separation.md +72 -0
- package/rules/index.js +155 -0
- package/rules/migration/converter.js +385 -0
- package/rules/migration/mapping.json +164 -0
- package/rules/parser/constants.js +31 -0
- package/rules/parser/file-config.js +80 -0
- package/rules/parser/rule-parser-simple.js +305 -0
- package/rules/parser/rule-parser.js +527 -0
- package/rules/security/S015_insecure_tls_certificate/analyzer.js +150 -0
- package/rules/security/S015_insecure_tls_certificate/ast-analyzer.js +237 -0
- package/rules/security/S023_no_json_injection/analyzer.js +278 -0
- package/rules/security/S023_no_json_injection/ast-analyzer.js +359 -0
- package/rules/security/S026_json_schema_validation/analyzer.js +251 -0
- package/rules/security/S026_json_schema_validation/config.json +27 -0
- package/rules/security/S027_no_hardcoded_secrets/analyzer.js +436 -0
- package/rules/security/S027_no_hardcoded_secrets/config.json +29 -0
- package/rules/security/S029_csrf_protection/analyzer.js +330 -0
- package/rules/tests/C002_no_duplicate_code.test.js +50 -0
- package/rules/universal/C010/generic.js +0 -0
- package/rules/universal/C010/tree-sitter-analyzer.js +0 -0
- package/rules/utils/ast-utils.js +191 -0
- package/rules/utils/base-analyzer.js +98 -0
- package/rules/utils/pattern-matchers.js +239 -0
- package/rules/utils/rule-helpers.js +264 -0
- package/rules/utils/severity-constants.js +93 -0
- package/scripts/generate_insights.js +188 -0
- package/scripts/merge-reports.js +0 -424
- package/scripts/test-scripts/README.md +0 -22
- package/scripts/test-scripts/test-c041-comparison.js +0 -114
- package/scripts/test-scripts/test-c041-eslint.js +0 -67
- package/scripts/test-scripts/test-eslint-rules.js +0 -146
- package/scripts/test-scripts/test-real-world.js +0 -44
- package/scripts/test-scripts/test-rules-on-real-projects.js +0 -86
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
cons async analyze(files, language, options = {}) {
|
|
3
|
+
// Use Smart Pipeline as primary choice
|
|
4
|
+
if (this.smartPipeline) {
|
|
5
|
+
console.log('🎯 C029: Using Smart Pipeline (3-stage analysis)...');
|
|
6
|
+
return await this.smartPipeline.analyze(files, language, options);
|
|
7
|
+
} else if (this.astAnalyzer) {
|
|
8
|
+
console.log('🚀 C029: Using AST-enhanced analysis...');
|
|
9
|
+
return await this.astAnalyzer.analyze(files, language, options);
|
|
10
|
+
} else {
|
|
11
|
+
console.log('🔍 C029: Using regex-based analysis...');
|
|
12
|
+
return await this.analyzeWithRegex(files, language, options);
|
|
13
|
+
}
|
|
14
|
+
}ire('path');
|
|
15
|
+
const { PatternMatcher } = require('../../utils/pattern-matchers');
|
|
16
|
+
const { RuleHelper } = require('../../utils/rule-helpers');
|
|
17
|
+
|
|
18
|
+
class C029Analyzer {
|
|
19
|
+
constructor() {
|
|
20
|
+
this.ruleId = 'C029';
|
|
21
|
+
this.ruleName = 'Enhanced Catch Block Error Logging';
|
|
22
|
+
this.description = 'Mọi catch block phải log nguyên nhân lỗi đầy đủ và bảo toàn context (Smart Pipeline 3-stage analysis)';
|
|
23
|
+
|
|
24
|
+
// Load Smart Pipeline as primary analyzer
|
|
25
|
+
this.smartPipeline = null;
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
this.smartPipeline = require('./analyzer-smart-pipeline.js');
|
|
29
|
+
console.log('🎯 C029: Smart Pipeline loaded (3-stage: Regex → AST → Data Flow)');
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.warn('⚠️ C029: Smart Pipeline failed, falling back:', error.message);
|
|
32
|
+
|
|
33
|
+
// Fallback to simpler analyzers
|
|
34
|
+
try {
|
|
35
|
+
this.astAnalyzer = require('./ast-analyzer.js');
|
|
36
|
+
console.log('🚀 C029: AST analyzer loaded (hybrid approach)');
|
|
37
|
+
} catch (error) {
|
|
38
|
+
console.warn('⚠️ C029: AST analyzer failed:', error.message);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async analyze(files, language, options = {}) {
|
|
44
|
+
// Use Smart Pipeline as primary choice
|
|
45
|
+
if (this.smartPipeline) {
|
|
46
|
+
console.log('🎯 C029: Using Smart Pipeline (3-stage analysis)...');
|
|
47
|
+
return await this.smartPipeline.analyze(files, language, options);
|
|
48
|
+
} else if (this.astAnalyzer) {
|
|
49
|
+
console.log('🚀 C029: Using AST-enhanced analysis...');
|
|
50
|
+
return await this.astAnalyzer.analyze(files, language, options);
|
|
51
|
+
} else {
|
|
52
|
+
console.log('🔍 C029: Using regex-based analysis...');
|
|
53
|
+
return await this.analyzeWithRegex(files, language, options);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
console.log('🧠 C029: Using Data Flow analysis (IDE-level)...');
|
|
57
|
+
return await this.dataFlowAnalyzer.analyze(files, language, options);
|
|
58
|
+
} else if (this.astAnalyzer) {
|
|
59
|
+
console.log('� C029: Using AST-enhanced analysis...');
|
|
60
|
+
return await this.astAnalyzer.analyze(files, language, options);
|
|
61
|
+
} else {
|
|
62
|
+
console.log('🔍 C029: Using regex-based analysis...');
|
|
63
|
+
return await this.analyzeWithRegex(files, language, options);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async analyzeWithRegex(files, language, options = {}) {
|
|
68
|
+
const violations = [];
|
|
69
|
+
|
|
70
|
+
for (const filePath of files) {
|
|
71
|
+
if (options.verbose) {
|
|
72
|
+
console.log(`🔍 Running pattern analysis on ${path.basename(filePath)}`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
77
|
+
const fileViolations = await this.analyzeFile(filePath, content, language, options);
|
|
78
|
+
violations.push(...fileViolations);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
console.warn(`⚠️ Failed to analyze ${filePath}: ${error.message}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return violations;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async analyzeFile(filePath, content, language, config) {
|
|
88
|
+
switch (language) {
|
|
89
|
+
case 'typescript':
|
|
90
|
+
case 'javascript':
|
|
91
|
+
return this.analyzeTypeScript(filePath, content, config);
|
|
92
|
+
default:
|
|
93
|
+
return [];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async analyzeTypeScript(filePath, content, config) {
|
|
98
|
+
const violations = [];
|
|
99
|
+
const lines = content.split('\n');
|
|
100
|
+
|
|
101
|
+
// Focus on core functionality - only detect truly silent catch blocks
|
|
102
|
+
violations.push(...this.findSilentCatchBlocks(lines, filePath));
|
|
103
|
+
// Disabled strict checks to reduce false positives:
|
|
104
|
+
// violations.push(...this.findInadequateErrorLogging(lines, filePath));
|
|
105
|
+
// violations.push(...this.findMissingErrorContext(lines, filePath));
|
|
106
|
+
|
|
107
|
+
return violations;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Core functionality from ESLint C029 - detect silent catch blocks
|
|
112
|
+
*/
|
|
113
|
+
findSilentCatchBlocks(lines, filePath) {
|
|
114
|
+
const violations = [];
|
|
115
|
+
|
|
116
|
+
lines.forEach((line, index) => {
|
|
117
|
+
const lineNumber = index + 1;
|
|
118
|
+
const trimmedLine = line.trim();
|
|
119
|
+
|
|
120
|
+
// Detect catch block start
|
|
121
|
+
if (this.isCatchBlockStart(trimmedLine)) {
|
|
122
|
+
const catchBlockInfo = this.extractCatchBlockInfo(lines, index);
|
|
123
|
+
|
|
124
|
+
if (catchBlockInfo.isEmpty) {
|
|
125
|
+
violations.push({
|
|
126
|
+
ruleId: this.ruleId,
|
|
127
|
+
file: filePath,
|
|
128
|
+
line: lineNumber,
|
|
129
|
+
column: line.indexOf('catch') + 1,
|
|
130
|
+
message: 'Empty catch block - error is silently ignored',
|
|
131
|
+
severity: 'error',
|
|
132
|
+
code: trimmedLine,
|
|
133
|
+
type: 'empty_catch_block',
|
|
134
|
+
confidence: 1.0,
|
|
135
|
+
suggestion: 'Add error logging or rethrowing in catch block'
|
|
136
|
+
});
|
|
137
|
+
} else if (!catchBlockInfo.hasLoggingOrRethrow) {
|
|
138
|
+
violations.push({
|
|
139
|
+
ruleId: this.ruleId,
|
|
140
|
+
file: filePath,
|
|
141
|
+
line: lineNumber,
|
|
142
|
+
column: line.indexOf('catch') + 1,
|
|
143
|
+
message: 'Catch block must log error or rethrow - silent error handling hides bugs',
|
|
144
|
+
severity: 'error',
|
|
145
|
+
code: trimmedLine,
|
|
146
|
+
type: 'silent_catch_block',
|
|
147
|
+
confidence: 0.9,
|
|
148
|
+
suggestion: 'Add error logging (console.error, logger.error) or rethrow the error'
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
return violations;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* SunLint enhanced functionality - detect inadequate error logging
|
|
159
|
+
*/
|
|
160
|
+
findInadequateErrorLogging(lines, filePath) {
|
|
161
|
+
const violations = [];
|
|
162
|
+
|
|
163
|
+
lines.forEach((line, index) => {
|
|
164
|
+
const lineNumber = index + 1;
|
|
165
|
+
const trimmedLine = line.trim();
|
|
166
|
+
|
|
167
|
+
if (this.isCatchBlockStart(trimmedLine)) {
|
|
168
|
+
const catchBlockInfo = this.extractCatchBlockInfo(lines, index);
|
|
169
|
+
|
|
170
|
+
if (catchBlockInfo.hasLoggingOrRethrow && !catchBlockInfo.hasAdequateLogging) {
|
|
171
|
+
violations.push({
|
|
172
|
+
ruleId: this.ruleId,
|
|
173
|
+
file: filePath,
|
|
174
|
+
line: lineNumber,
|
|
175
|
+
column: line.indexOf('catch') + 1,
|
|
176
|
+
message: 'Error logging should include error message, stack trace, and context',
|
|
177
|
+
severity: 'warning',
|
|
178
|
+
code: trimmedLine,
|
|
179
|
+
type: 'inadequate_error_logging',
|
|
180
|
+
confidence: 0.8,
|
|
181
|
+
suggestion: 'Include error.message, error.stack, and relevant context in error logging'
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
return violations;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* SunLint enhanced functionality - detect missing error context
|
|
192
|
+
* DISABLED: Too strict, causing false positives
|
|
193
|
+
*/
|
|
194
|
+
findMissingErrorContext(lines, filePath) {
|
|
195
|
+
// Disabled to reduce false positives
|
|
196
|
+
return [];
|
|
197
|
+
|
|
198
|
+
/* Original strict implementation:
|
|
199
|
+
const violations = [];
|
|
200
|
+
|
|
201
|
+
lines.forEach((line, index) => {
|
|
202
|
+
const lineNumber = index + 1;
|
|
203
|
+
const trimmedLine = line.trim();
|
|
204
|
+
|
|
205
|
+
if (this.isCatchBlockStart(trimmedLine)) {
|
|
206
|
+
const catchBlockInfo = this.extractCatchBlockInfo(lines, index);
|
|
207
|
+
|
|
208
|
+
if (catchBlockInfo.hasLoggingOrRethrow && !catchBlockInfo.hasContextualLogging) {
|
|
209
|
+
violations.push({
|
|
210
|
+
ruleId: this.ruleId,
|
|
211
|
+
file: filePath,
|
|
212
|
+
line: lineNumber,
|
|
213
|
+
column: line.indexOf('catch') + 1,
|
|
214
|
+
message: 'Error logging should include operational context (function name, input parameters)',
|
|
215
|
+
severity: 'info',
|
|
216
|
+
code: trimmedLine,
|
|
217
|
+
type: 'missing_error_context',
|
|
218
|
+
confidence: 0.7,
|
|
219
|
+
suggestion: 'Include function name, input parameters, and operational context in error logging'
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
return violations;
|
|
226
|
+
*/
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
isCatchBlockStart(line) {
|
|
230
|
+
return line.includes('catch (') || line.includes('catch(');
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
extractCatchBlockInfo(lines, startIndex) {
|
|
234
|
+
const catchBlockLines = [];
|
|
235
|
+
let braceDepth = 0;
|
|
236
|
+
let foundCatchBrace = false;
|
|
237
|
+
|
|
238
|
+
for (let i = startIndex; i < lines.length; i++) {
|
|
239
|
+
const line = lines[i];
|
|
240
|
+
catchBlockLines.push(line);
|
|
241
|
+
|
|
242
|
+
// Check if this is the catch line with opening brace
|
|
243
|
+
if (this.isCatchBlockStart(line.trim())) {
|
|
244
|
+
// Count braces in the catch line itself
|
|
245
|
+
for (const char of line) {
|
|
246
|
+
if (char === '{') {
|
|
247
|
+
foundCatchBrace = true;
|
|
248
|
+
braceDepth = 1; // Start counting from 1 for the catch block
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
} else if (foundCatchBrace) {
|
|
252
|
+
// Count braces in subsequent lines
|
|
253
|
+
for (const char of line) {
|
|
254
|
+
if (char === '{') {
|
|
255
|
+
braceDepth++;
|
|
256
|
+
} else if (char === '}') {
|
|
257
|
+
braceDepth--;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// If we've closed all braces, we're done
|
|
262
|
+
if (braceDepth === 0) {
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const content = catchBlockLines.join('\n').toLowerCase();
|
|
269
|
+
const originalContent = catchBlockLines.join('\n');
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
isEmpty: this.isCatchBlockEmpty(catchBlockLines),
|
|
273
|
+
hasLoggingOrRethrow: this.hasLoggingOrRethrow(content, originalContent),
|
|
274
|
+
hasAdequateLogging: this.hasAdequateErrorLogging(content, originalContent),
|
|
275
|
+
hasContextualLogging: this.hasContextualErrorLogging(content, originalContent)
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
isCatchBlockEmpty(catchBlockLines) {
|
|
280
|
+
if (catchBlockLines.length === 0) return true;
|
|
281
|
+
|
|
282
|
+
// Join all lines and find the content between braces
|
|
283
|
+
const fullContent = catchBlockLines.join('\n');
|
|
284
|
+
|
|
285
|
+
// Find the opening brace and closing brace
|
|
286
|
+
let openBraceIndex = fullContent.indexOf('{');
|
|
287
|
+
let closeBraceIndex = fullContent.lastIndexOf('}');
|
|
288
|
+
|
|
289
|
+
if (openBraceIndex === -1 || closeBraceIndex === -1) {
|
|
290
|
+
return true; // Malformed catch block
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Extract body content between braces
|
|
294
|
+
const bodyContent = fullContent.substring(openBraceIndex + 1, closeBraceIndex).trim();
|
|
295
|
+
|
|
296
|
+
// Remove comments and whitespace
|
|
297
|
+
const cleanedBody = bodyContent
|
|
298
|
+
.replace(/\/\*[\s\S]*?\*\//g, '') // Remove block comments
|
|
299
|
+
.replace(/\/\/.*$/gm, '') // Remove line comments
|
|
300
|
+
.replace(/\s+/g, ' ') // Normalize whitespace
|
|
301
|
+
.trim();
|
|
302
|
+
|
|
303
|
+
return cleanedBody.length === 0;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
hasLoggingOrRethrow(content, originalContent) {
|
|
307
|
+
// Check for throw statements
|
|
308
|
+
if (content.includes('throw ') || content.includes('throw;')) {
|
|
309
|
+
return true;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// Check for test assertions - valid form of error handling
|
|
313
|
+
const testPatterns = [
|
|
314
|
+
'expect(',
|
|
315
|
+
'assert(',
|
|
316
|
+
'tobeinstanceof',
|
|
317
|
+
'toequal(',
|
|
318
|
+
'tohavebeencalled',
|
|
319
|
+
'toBe(',
|
|
320
|
+
'toHaveBeenCalledWith'
|
|
321
|
+
];
|
|
322
|
+
|
|
323
|
+
const hasTestAssertions = testPatterns.some(pattern =>
|
|
324
|
+
content.includes(pattern.toLowerCase()) || originalContent.toLowerCase().includes(pattern.toLowerCase())
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
if (hasTestAssertions) {
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Check for Redux/async thunk error handling
|
|
332
|
+
const reduxErrorHandlers = [
|
|
333
|
+
'handleaxioserror',
|
|
334
|
+
'rejectwithvalue',
|
|
335
|
+
'dispatch(',
|
|
336
|
+
'seterror(',
|
|
337
|
+
'return value',
|
|
338
|
+
'return rejectwithvalue'
|
|
339
|
+
];
|
|
340
|
+
|
|
341
|
+
const hasReduxHandling = reduxErrorHandlers.some(pattern =>
|
|
342
|
+
content.includes(pattern.toLowerCase()) || originalContent.toLowerCase().includes(pattern.toLowerCase())
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
if (hasReduxHandling) {
|
|
346
|
+
return true;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Check for logging patterns (expanded from original)
|
|
350
|
+
const loggingPatterns = [
|
|
351
|
+
'console.error',
|
|
352
|
+
'console.log',
|
|
353
|
+
'console.warn',
|
|
354
|
+
'logger.error',
|
|
355
|
+
'log.error',
|
|
356
|
+
'logger.warn',
|
|
357
|
+
'log.warn',
|
|
358
|
+
'winston.error',
|
|
359
|
+
'bunyan.error',
|
|
360
|
+
'pino.error',
|
|
361
|
+
'.error(',
|
|
362
|
+
'.warn(',
|
|
363
|
+
'.log('
|
|
364
|
+
];
|
|
365
|
+
|
|
366
|
+
// Accept basic error logging - don't require context
|
|
367
|
+
const hasBasicLogging = loggingPatterns.some(pattern =>
|
|
368
|
+
content.includes(pattern) || originalContent.includes(pattern)
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
return hasBasicLogging;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
hasAdequateErrorLogging(content, originalContent) {
|
|
375
|
+
// Check for error properties being logged
|
|
376
|
+
const errorProperties = [
|
|
377
|
+
'error.message',
|
|
378
|
+
'error.stack',
|
|
379
|
+
'err.message',
|
|
380
|
+
'err.stack',
|
|
381
|
+
'e.message',
|
|
382
|
+
'e.stack',
|
|
383
|
+
'error.name',
|
|
384
|
+
'error.cause'
|
|
385
|
+
];
|
|
386
|
+
|
|
387
|
+
const hasErrorProperties = errorProperties.some(prop =>
|
|
388
|
+
content.includes(prop) || originalContent.includes(prop)
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
// Check for comprehensive error logging
|
|
392
|
+
const hasLogging = this.hasLoggingOrRethrow(content, originalContent);
|
|
393
|
+
|
|
394
|
+
return hasLogging && hasErrorProperties;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
hasContextualErrorLogging(content, originalContent) {
|
|
398
|
+
// Check for contextual information in logging
|
|
399
|
+
const contextualPatterns = [
|
|
400
|
+
'function',
|
|
401
|
+
'method',
|
|
402
|
+
'operation',
|
|
403
|
+
'input',
|
|
404
|
+
'parameters',
|
|
405
|
+
'context',
|
|
406
|
+
'state',
|
|
407
|
+
'request',
|
|
408
|
+
'response',
|
|
409
|
+
'userId',
|
|
410
|
+
'sessionId',
|
|
411
|
+
'transactionId'
|
|
412
|
+
];
|
|
413
|
+
|
|
414
|
+
// Check if logging includes contextual information
|
|
415
|
+
const hasContextualInfo = contextualPatterns.some(pattern =>
|
|
416
|
+
content.includes(pattern) || originalContent.includes(pattern)
|
|
417
|
+
);
|
|
418
|
+
|
|
419
|
+
// Check for template literals or string concatenation (indicates contextual logging)
|
|
420
|
+
const hasTemplateLogging = originalContent.includes('${') || originalContent.includes('" + ') || originalContent.includes("' + ");
|
|
421
|
+
|
|
422
|
+
return hasContextualInfo || hasTemplateLogging;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
module.exports = new C029Analyzer();
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* C029 Analyzer - Smart Pipeline Integration
|
|
3
|
+
*
|
|
4
|
+
* This analyzer forwards to the Smart Pipeline for superior accuracy and performance
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const path = require('path');
|
|
9
|
+
|
|
10
|
+
class C029Analyzer {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.ruleId = 'C029';
|
|
13
|
+
this.ruleName = 'Enhanced Catch Block Error Logging';
|
|
14
|
+
this.description = 'Mọi catch block phải log nguyên nhân lỗi đầy đủ và bảo toàn context (Smart Pipeline 3-stage analysis)';
|
|
15
|
+
|
|
16
|
+
// Load Smart Pipeline as primary analyzer
|
|
17
|
+
this.smartPipeline = null;
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
const SmartPipelineClass = require('./analyzer-smart-pipeline.js');
|
|
21
|
+
this.smartPipeline = new SmartPipelineClass();
|
|
22
|
+
console.log('🎯 C029: Smart Pipeline loaded (3-stage: Regex → AST → Data Flow)');
|
|
23
|
+
} catch (error) {
|
|
24
|
+
console.warn('⚠️ C029: Smart Pipeline failed, using fallback:', error.message);
|
|
25
|
+
this.smartPipeline = null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async analyze(files, language, options = {}) {
|
|
30
|
+
// Use Smart Pipeline as primary choice
|
|
31
|
+
if (this.smartPipeline) {
|
|
32
|
+
console.log('🎯 C029: Using Smart Pipeline (3-stage analysis)...');
|
|
33
|
+
return await this.smartPipeline.analyze(files, language, options);
|
|
34
|
+
} else {
|
|
35
|
+
console.log('🔍 C029: Using fallback regex analysis...');
|
|
36
|
+
return await this.analyzeWithRegex(files, language, options);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async analyzeWithRegex(files, language, options = {}) {
|
|
41
|
+
const violations = [];
|
|
42
|
+
|
|
43
|
+
for (const filePath of files) {
|
|
44
|
+
if (options.verbose) {
|
|
45
|
+
console.log(`🔍 C029 Regex: Processing ${path.basename(filePath)}...`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
50
|
+
const fileViolations = await this.analyzeFile(filePath, content, language);
|
|
51
|
+
violations.push(...fileViolations);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.warn(`⚠️ C029: Error processing ${filePath}:`, error.message);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return violations;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async analyzeFile(filePath, content, language) {
|
|
61
|
+
const violations = [];
|
|
62
|
+
const lines = content.split('\n');
|
|
63
|
+
|
|
64
|
+
for (let i = 0; i < lines.length; i++) {
|
|
65
|
+
const line = lines[i];
|
|
66
|
+
|
|
67
|
+
// Simple catch block detection
|
|
68
|
+
if (line.includes('catch') && line.includes('(')) {
|
|
69
|
+
const catchBlock = this.extractCatchBlock(lines, i);
|
|
70
|
+
|
|
71
|
+
if (this.isCatchBlockEmpty(catchBlock.content)) {
|
|
72
|
+
violations.push({
|
|
73
|
+
file: filePath,
|
|
74
|
+
line: i + 1,
|
|
75
|
+
column: line.indexOf('catch') + 1,
|
|
76
|
+
message: 'Empty catch block detected',
|
|
77
|
+
severity: 'error',
|
|
78
|
+
ruleId: this.ruleId,
|
|
79
|
+
type: 'empty_catch'
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return violations;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
extractCatchBlock(lines, startIndex) {
|
|
89
|
+
const content = [];
|
|
90
|
+
let braceCount = 0;
|
|
91
|
+
let inBlock = false;
|
|
92
|
+
|
|
93
|
+
for (let i = startIndex; i < lines.length; i++) {
|
|
94
|
+
const line = lines[i];
|
|
95
|
+
content.push(line);
|
|
96
|
+
|
|
97
|
+
for (const char of line) {
|
|
98
|
+
if (char === '{') {
|
|
99
|
+
braceCount++;
|
|
100
|
+
inBlock = true;
|
|
101
|
+
} else if (char === '}') {
|
|
102
|
+
braceCount--;
|
|
103
|
+
if (braceCount === 0 && inBlock) {
|
|
104
|
+
return { content, endIndex: i };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return { content, endIndex: startIndex };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
isCatchBlockEmpty(content) {
|
|
114
|
+
const blockContent = content.join('\n');
|
|
115
|
+
|
|
116
|
+
// Remove comments and whitespace
|
|
117
|
+
const cleanContent = blockContent
|
|
118
|
+
.replace(/\/\*[\s\S]*?\*\//g, '') // Remove multi-line comments
|
|
119
|
+
.replace(/\/\/.*$/gm, '') // Remove single-line comments
|
|
120
|
+
.replace(/\s+/g, ' ') // Normalize whitespace
|
|
121
|
+
.trim();
|
|
122
|
+
|
|
123
|
+
// Check if only contains catch declaration and braces
|
|
124
|
+
const hasOnlyStructure = /^catch\s*\([^)]*\)\s*\{\s*\}$/.test(cleanContent);
|
|
125
|
+
|
|
126
|
+
return hasOnlyStructure;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
module.exports = C029Analyzer;
|