@sun-asterisk/sunlint 1.2.0 → 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.
Files changed (77) hide show
  1. package/config/rule-analysis-strategies.js +18 -2
  2. package/engines/eslint-engine.js +9 -11
  3. package/engines/heuristic-engine.js +63 -31
  4. package/package.json +2 -1
  5. package/rules/README.md +252 -0
  6. package/rules/common/C002_no_duplicate_code/analyzer.js +65 -0
  7. package/rules/common/C002_no_duplicate_code/config.json +23 -0
  8. package/rules/common/C003_no_vague_abbreviations/analyzer.js +418 -0
  9. package/rules/common/C003_no_vague_abbreviations/config.json +35 -0
  10. package/rules/common/C006_function_naming/analyzer.js +504 -0
  11. package/rules/common/C006_function_naming/config.json +86 -0
  12. package/rules/common/C006_function_naming/smart-analyzer.js +503 -0
  13. package/rules/common/C010_limit_block_nesting/analyzer.js +389 -0
  14. package/rules/common/C012_command_query_separation/analyzer.js +481 -0
  15. package/rules/common/C012_command_query_separation/ast-analyzer.js +495 -0
  16. package/rules/common/C013_no_dead_code/analyzer.js +206 -0
  17. package/rules/common/C014_dependency_injection/analyzer.js +338 -0
  18. package/rules/common/C017_constructor_logic/analyzer.js +314 -0
  19. package/rules/common/C019_log_level_usage/analyzer.js +362 -0
  20. package/rules/common/C019_log_level_usage/config.json +121 -0
  21. package/rules/common/C029_catch_block_logging/analyzer-backup.js +426 -0
  22. package/rules/common/C029_catch_block_logging/analyzer-fixed.js +130 -0
  23. package/rules/common/C029_catch_block_logging/analyzer-multi-tech.js +487 -0
  24. package/rules/common/C029_catch_block_logging/analyzer-simple.js +110 -0
  25. package/rules/common/C029_catch_block_logging/analyzer-smart-pipeline.js +755 -0
  26. package/rules/common/C029_catch_block_logging/analyzer.js +129 -0
  27. package/rules/common/C029_catch_block_logging/ast-analyzer-backup.js +441 -0
  28. package/rules/common/C029_catch_block_logging/ast-analyzer-new.js +127 -0
  29. package/rules/common/C029_catch_block_logging/ast-analyzer.js +133 -0
  30. package/rules/common/C029_catch_block_logging/cfg-analyzer.js +408 -0
  31. package/rules/common/C029_catch_block_logging/config.json +59 -0
  32. package/rules/common/C029_catch_block_logging/dataflow-analyzer.js +454 -0
  33. package/rules/common/C029_catch_block_logging/multi-language-ast-engine.js +700 -0
  34. package/rules/common/C029_catch_block_logging/pattern-learning-analyzer.js +568 -0
  35. package/rules/common/C029_catch_block_logging/semantic-analyzer.js +459 -0
  36. package/rules/common/C031_validation_separation/analyzer.js +186 -0
  37. package/rules/common/C041_no_sensitive_hardcode/analyzer.js +292 -0
  38. package/rules/common/C041_no_sensitive_hardcode/ast-analyzer.js +296 -0
  39. package/rules/common/C042_boolean_name_prefix/analyzer.js +300 -0
  40. package/rules/common/C043_no_console_or_print/analyzer.js +431 -0
  41. package/rules/common/C047_no_duplicate_retry_logic/analyzer.js +590 -0
  42. package/rules/common/C075_explicit_return_types/analyzer.js +103 -0
  43. package/rules/common/C076_single_test_behavior/analyzer.js +121 -0
  44. package/rules/docs/C002_no_duplicate_code.md +57 -0
  45. package/rules/docs/C031_validation_separation.md +72 -0
  46. package/rules/index.js +155 -0
  47. package/rules/migration/converter.js +385 -0
  48. package/rules/migration/mapping.json +164 -0
  49. package/rules/parser/constants.js +31 -0
  50. package/rules/parser/file-config.js +80 -0
  51. package/rules/parser/rule-parser-simple.js +305 -0
  52. package/rules/parser/rule-parser.js +527 -0
  53. package/rules/security/S015_insecure_tls_certificate/analyzer.js +150 -0
  54. package/rules/security/S015_insecure_tls_certificate/ast-analyzer.js +237 -0
  55. package/rules/security/S023_no_json_injection/analyzer.js +278 -0
  56. package/rules/security/S023_no_json_injection/ast-analyzer.js +359 -0
  57. package/rules/security/S026_json_schema_validation/analyzer.js +251 -0
  58. package/rules/security/S026_json_schema_validation/config.json +27 -0
  59. package/rules/security/S027_no_hardcoded_secrets/analyzer.js +436 -0
  60. package/rules/security/S027_no_hardcoded_secrets/config.json +29 -0
  61. package/rules/security/S029_csrf_protection/analyzer.js +330 -0
  62. package/rules/tests/C002_no_duplicate_code.test.js +50 -0
  63. package/rules/universal/C010/generic.js +0 -0
  64. package/rules/universal/C010/tree-sitter-analyzer.js +0 -0
  65. package/rules/utils/ast-utils.js +191 -0
  66. package/rules/utils/base-analyzer.js +98 -0
  67. package/rules/utils/pattern-matchers.js +239 -0
  68. package/rules/utils/rule-helpers.js +264 -0
  69. package/rules/utils/severity-constants.js +93 -0
  70. package/scripts/generate_insights.js +188 -0
  71. package/scripts/merge-reports.js +0 -424
  72. package/scripts/test-scripts/README.md +0 -22
  73. package/scripts/test-scripts/test-c041-comparison.js +0 -114
  74. package/scripts/test-scripts/test-c041-eslint.js +0 -67
  75. package/scripts/test-scripts/test-eslint-rules.js +0 -146
  76. package/scripts/test-scripts/test-real-world.js +0 -44
  77. package/scripts/test-scripts/test-rules-on-real-projects.js +0 -86
@@ -0,0 +1,300 @@
1
+ /**
2
+ * Heuristic analyzer for: C042 – Boolean variable names should start with proper prefixes
3
+ * Purpose: Detect boolean variables that don't follow naming conventions
4
+ */
5
+
6
+ class C042Analyzer {
7
+ constructor() {
8
+ this.ruleId = 'C042';
9
+ this.ruleName = 'Boolean Variable Naming';
10
+ this.description = 'Boolean variable names should start with is, has, should, can, will, must, may, or check';
11
+
12
+ // User requested to add "check" prefix
13
+ this.booleanPrefixes = [
14
+ 'is', 'has', 'should', 'can', 'will', 'must', 'may', 'check',
15
+ 'are', 'were', 'was', 'could', 'might', 'shall', 'need', 'want'
16
+ ];
17
+
18
+ // Common non-boolean patterns to ignore (user feedback)
19
+ this.ignoredPatterns = [
20
+ // Fallback/default patterns: var = value || fallback
21
+ /\w+\s*=\s*\w+\s*\|\|\s*[^|]/,
22
+ // Assignment patterns that are clearly not boolean
23
+ /\w+\s*=\s*['"`][^'"`]*['"`]/, // String assignments
24
+ /\w+\s*=\s*\d+/, // Number assignments
25
+ /\w+\s*=\s*\{/, // Object assignments
26
+ /\w+\s*=\s*\[/, // Array assignments
27
+ ];
28
+
29
+ // Variables that commonly aren't boolean but might look like it
30
+ this.commonNonBooleans = [
31
+ 'value', 'result', 'data', 'config', 'name', 'id', 'key',
32
+ 'path', 'url', 'src', 'href', 'text', 'message', 'error',
33
+ 'response', 'request', 'params', 'options', 'settings'
34
+ ];
35
+ }
36
+
37
+ async analyze(files, language, options = {}) {
38
+ const violations = [];
39
+
40
+ for (const filePath of files) {
41
+ if (options.verbose) {
42
+ console.log(`🔍 Running C042 analysis on ${require('path').basename(filePath)}`);
43
+ }
44
+
45
+ try {
46
+ const content = require('fs').readFileSync(filePath, 'utf8');
47
+ const fileViolations = this.analyzeFile(content, filePath);
48
+ violations.push(...fileViolations);
49
+ } catch (error) {
50
+ console.warn(`⚠️ Failed to analyze ${filePath}: ${error.message}`);
51
+ }
52
+ }
53
+
54
+ return violations;
55
+ }
56
+
57
+ analyzeFile(content, filePath) {
58
+ const violations = [];
59
+ const lines = content.split('\n');
60
+
61
+ for (let i = 0; i < lines.length; i++) {
62
+ const line = lines[i].trim();
63
+
64
+ // Skip empty lines, comments, imports, and type declarations
65
+ if (!line || line.startsWith('//') || line.startsWith('/*') ||
66
+ line.startsWith('import') || line.startsWith('export') ||
67
+ line.startsWith('declare') || line.startsWith('*')) {
68
+ continue;
69
+ }
70
+
71
+ const lineViolations = this.analyzeDeclaration(line, i + 1);
72
+ lineViolations.forEach(violation => {
73
+ violations.push({
74
+ ...violation,
75
+ file: filePath
76
+ });
77
+ });
78
+ }
79
+
80
+ return violations;
81
+ }
82
+
83
+ analyzeDeclaration(line, lineNumber) {
84
+ const violations = [];
85
+
86
+ // Match variable declarations with boolean values
87
+ const booleanAssignments = this.findBooleanAssignments(line);
88
+
89
+ for (const assignment of booleanAssignments) {
90
+ const { varName, isBooleanValue, hasPrefix, actualValue } = assignment;
91
+
92
+ // Case 1: Variable is assigned a boolean value but doesn't have proper prefix
93
+ if (isBooleanValue && !hasPrefix) {
94
+ // Skip if this matches user feedback patterns (false positives)
95
+ if (this.shouldSkipBooleanVariableCheck(varName, line, actualValue)) {
96
+ continue;
97
+ }
98
+
99
+ violations.push({
100
+ line: lineNumber,
101
+ column: line.indexOf(varName) + 1,
102
+ message: `Boolean variable '${varName}' should start with a descriptive prefix like 'is', 'has', 'should', 'can', or 'check'. Consider: ${this.generateSuggestions(varName).join(', ')}.`,
103
+ severity: 'warning',
104
+ ruleId: this.ruleId
105
+ });
106
+ }
107
+
108
+ // Case 2: Variable has boolean prefix but is assigned a non-boolean value
109
+ else if (hasPrefix && !isBooleanValue && actualValue && this.isDefinitelyNotBoolean(actualValue)) {
110
+ // Only skip very basic cases for prefix misuse
111
+ if (varName.length <= 2) {
112
+ continue;
113
+ }
114
+
115
+ const prefix = this.extractPrefix(varName);
116
+ violations.push({
117
+ line: lineNumber,
118
+ column: line.indexOf(varName) + 1,
119
+ message: `Variable '${varName}' uses boolean prefix '${prefix}' but is assigned a non-boolean value. Consider renaming or changing the value.`,
120
+ severity: 'warning',
121
+ ruleId: this.ruleId
122
+ });
123
+ }
124
+ }
125
+
126
+ return violations;
127
+ }
128
+
129
+ findBooleanAssignments(line) {
130
+ const assignments = [];
131
+
132
+ // Match declaration patterns only (avoid duplicates)
133
+ const patterns = [
134
+ // let/const/var varName = value
135
+ /(?:let|const|var)\s+(\w+)\s*(?::\s*\w+\s*)?=\s*(.+?)(?:;|$)/g,
136
+ ];
137
+
138
+ for (const pattern of patterns) {
139
+ let match;
140
+ const seenVariables = new Set(); // Avoid duplicates
141
+
142
+ while ((match = pattern.exec(line)) !== null) {
143
+ const varName = match[1];
144
+ const value = match[2].trim();
145
+
146
+ // Skip if already processed
147
+ if (seenVariables.has(varName)) {
148
+ continue;
149
+ }
150
+ seenVariables.add(varName);
151
+
152
+ // Skip destructuring and complex patterns
153
+ if (varName.includes('[') || varName.includes('{') || value.includes('{') || value.includes('[')) {
154
+ continue;
155
+ }
156
+
157
+ const isBooleanValue = this.isBooleanValue(value);
158
+ const hasPrefix = this.hasBooleanPrefix(varName);
159
+
160
+ assignments.push({
161
+ varName,
162
+ isBooleanValue,
163
+ hasPrefix,
164
+ actualValue: value
165
+ });
166
+ }
167
+ }
168
+
169
+ return assignments;
170
+ }
171
+
172
+ isBooleanValue(value) {
173
+ const trimmedValue = value.trim();
174
+
175
+ // Direct boolean literals
176
+ if (trimmedValue === 'true' || trimmedValue === 'false') {
177
+ return true;
178
+ }
179
+
180
+ // Boolean expressions that clearly result in boolean
181
+ const booleanExpressions = [
182
+ /\w+\s*[<>!=]=/, // Comparisons
183
+ /\w+\s*(&&|\|\|)/, // Logical operations (but not fallback patterns)
184
+ /^\!\w+/, // Negation
185
+ /instanceof\s+/, // instanceof
186
+ /\.test\(/, // regex.test()
187
+ /\.includes\(/, // array.includes()
188
+ /\.hasOwnProperty\(/, // hasOwnProperty
189
+ /\.some\(/, // array.some()
190
+ /\.every\(/, // array.every()
191
+ /typeof\s+.*\s*===/, // typeof checks
192
+ /Math\.random\(\)\s*[<>]/, // Math.random() comparisons
193
+ /\.length\s*[<>!=]=/, // Length comparisons
194
+ ];
195
+
196
+ // Exclude fallback patterns (user feedback - these are NOT boolean)
197
+ if (trimmedValue.includes('||')) {
198
+ // Check if it's a boolean expression or just a fallback
199
+ // If the || is followed by a non-boolean value, it's likely a fallback
200
+ const parts = trimmedValue.split('||');
201
+ if (parts.length === 2) {
202
+ const fallback = parts[1].trim();
203
+ // If fallback is clearly not boolean, this is not a boolean assignment
204
+ if (this.isDefinitelyNotBoolean(fallback) || /^\d+$/.test(fallback) || /^['"`]/.test(fallback)) {
205
+ return false;
206
+ }
207
+ }
208
+ }
209
+
210
+ return booleanExpressions.some(pattern => pattern.test(trimmedValue));
211
+ }
212
+
213
+ hasBooleanPrefix(varName) {
214
+ const lowerName = varName.toLowerCase();
215
+ return this.booleanPrefixes.some(prefix =>
216
+ lowerName.startsWith(prefix.toLowerCase()) &&
217
+ lowerName.length > prefix.length
218
+ );
219
+ }
220
+
221
+ extractPrefix(varName) {
222
+ const lowerName = varName.toLowerCase();
223
+ for (const prefix of this.booleanPrefixes) {
224
+ if (lowerName.startsWith(prefix.toLowerCase())) {
225
+ return prefix;
226
+ }
227
+ }
228
+ return '';
229
+ }
230
+
231
+ shouldSkipBooleanVariableCheck(varName, line, value) {
232
+ // Skip very short names
233
+ if (varName.length <= 2) {
234
+ return true;
235
+ }
236
+
237
+ // Skip common non-boolean variable names
238
+ if (this.commonNonBooleans.includes(varName.toLowerCase())) {
239
+ return true;
240
+ }
241
+
242
+ // Skip user feedback patterns (fallback/default patterns)
243
+ if (this.ignoredPatterns.some(pattern => pattern.test(line))) {
244
+ return true;
245
+ }
246
+
247
+ // Skip function parameters and loop variables
248
+ if (line.includes('function') || line.includes('for') || line.includes('=>')) {
249
+ return true;
250
+ }
251
+
252
+ return false;
253
+ }
254
+
255
+ isDefinitelyNotBoolean(value) {
256
+ const trimmedValue = value.trim();
257
+
258
+ // String literals (including single quotes)
259
+ if (trimmedValue.match(/^['"`][^'"`]*['"`]$/)) {
260
+ return true;
261
+ }
262
+
263
+ // Number literals
264
+ if (trimmedValue.match(/^\d+(\.\d+)?$/)) {
265
+ return true;
266
+ }
267
+
268
+ // Object/array literals
269
+ if (trimmedValue.startsWith('{') || trimmedValue.startsWith('[')) {
270
+ return true;
271
+ }
272
+
273
+ // null, undefined
274
+ if (trimmedValue === 'null' || trimmedValue === 'undefined') {
275
+ return true;
276
+ }
277
+
278
+ // Common non-boolean patterns
279
+ if (trimmedValue.includes('new ') || trimmedValue.includes('function')) {
280
+ return true;
281
+ }
282
+
283
+ return false;
284
+ }
285
+
286
+ generateSuggestions(varName) {
287
+ const suggestions = [];
288
+ const baseName = varName.replace(/^(is|has|should|can|will|must|may|check)/i, '');
289
+ const capitalizedBase = baseName.charAt(0).toUpperCase() + baseName.slice(1);
290
+
291
+ // Generate a few reasonable suggestions
292
+ suggestions.push(`is${capitalizedBase}`);
293
+ suggestions.push(`has${capitalizedBase}`);
294
+ suggestions.push(`should${capitalizedBase}`);
295
+
296
+ return suggestions.slice(0, 3); // Limit to 3 suggestions
297
+ }
298
+ }
299
+
300
+ module.exports = C042Analyzer;