|
| 1 | + |
| 2 | + |
| 3 | +import { RuleDescription, SeverityLevel, COMMON_TAGS } from '@salesforce/code-analyzer-engine-api'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Known ApexGuru rules with descriptions and metadata. |
| 7 | + * |
| 8 | + * This list should be updated when Salesforce adds new ApexGuru rules. |
| 9 | + * Violations for rules NOT in this list will be mapped to the fallback "apexguru-other" rule. |
| 10 | + */ |
| 11 | +export const APEXGURU_RULES: RuleDescription[] = [ |
| 12 | + // ================================================================================================================= |
| 13 | + // PERFORMANCE RULES - HIGH SEVERITY (CRITICAL - RECOMMENDED) |
| 14 | + // ================================================================================================================= |
| 15 | + |
| 16 | + { |
| 17 | + name: 'SoqlInALoop', |
| 18 | + severityLevel: SeverityLevel.High, |
| 19 | + tags: [COMMON_TAGS.RECOMMENDED, COMMON_TAGS.CATEGORIES.PERFORMANCE, COMMON_TAGS.LANGUAGES.APEX], |
| 20 | + description: 'SOQL query inside a loop causes performance issues and can hit governor limits', |
| 21 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_soql_in_loop.htm&type=5'] |
| 22 | + }, |
| 23 | + |
| 24 | + { |
| 25 | + name: 'DmlInALoop', |
| 26 | + severityLevel: SeverityLevel.High, |
| 27 | + tags: [COMMON_TAGS.RECOMMENDED, COMMON_TAGS.CATEGORIES.PERFORMANCE, COMMON_TAGS.LANGUAGES.APEX], |
| 28 | + description: 'DML statement inside a loop causes performance issues and can hit governor limits', |
| 29 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_dml_in_loop.htm&type=5'] |
| 30 | + }, |
| 31 | + |
| 32 | + // ================================================================================================================= |
| 33 | + // PERFORMANCE RULES - MODERATE SEVERITY (CRITICAL - RECOMMENDED) |
| 34 | + // ================================================================================================================= |
| 35 | + |
| 36 | + { |
| 37 | + name: 'SoqlWithoutAWhereClauseOrLimitStatement', |
| 38 | + severityLevel: SeverityLevel.Moderate, |
| 39 | + tags: [COMMON_TAGS.RECOMMENDED, COMMON_TAGS.CATEGORIES.PERFORMANCE, COMMON_TAGS.LANGUAGES.APEX], |
| 40 | + description: 'SOQL query without WHERE clause or LIMIT statement can cause performance issues and heap size exceptions', |
| 41 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_soql_without_where_clause_or_limit_statement.htm&type=5'] |
| 42 | + }, |
| 43 | + |
| 44 | + { |
| 45 | + name: 'SoqlWithWildcardFilter', |
| 46 | + severityLevel: SeverityLevel.Moderate, |
| 47 | + tags: [COMMON_TAGS.RECOMMENDED, COMMON_TAGS.CATEGORIES.PERFORMANCE, COMMON_TAGS.LANGUAGES.APEX], |
| 48 | + description: 'SOQL query using LIKE with leading wildcard is inefficient and cannot use indexes', |
| 49 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_soql_with_wildcard_filter.htm&type=5'] |
| 50 | + }, |
| 51 | + |
| 52 | + { |
| 53 | + name: 'SchemaGetGlobalDescribeNotEfficient', |
| 54 | + severityLevel: SeverityLevel.Moderate, |
| 55 | + tags: [COMMON_TAGS.RECOMMENDED, COMMON_TAGS.CATEGORIES.PERFORMANCE, COMMON_TAGS.LANGUAGES.APEX], |
| 56 | + description: 'Using Schema.getGlobalDescribe() causes unnecessary overhead and decreases performance', |
| 57 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_schema_getglobaldescribe_not_efficient.htm&type=5'] |
| 58 | + }, |
| 59 | + |
| 60 | + // ================================================================================================================= |
| 61 | + // PERFORMANCE RULES - MODERATE SEVERITY (PERFORMANCE ONLY - NOT RECOMMENDED) |
| 62 | + // ================================================================================================================= |
| 63 | + |
| 64 | + { |
| 65 | + name: 'Soql Aggregation', |
| 66 | + severityLevel: SeverityLevel.Moderate, |
| 67 | + tags: [COMMON_TAGS.CATEGORIES.PERFORMANCE, COMMON_TAGS.LANGUAGES.APEX], |
| 68 | + description: 'Manual aggregation in Apex instead of using SOQL aggregate functions causes performance issues', |
| 69 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_aggregating_in_apex.htm&type=5'] |
| 70 | + }, |
| 71 | + |
| 72 | + { |
| 73 | + name: 'SoqlWithApexFilter', |
| 74 | + severityLevel: SeverityLevel.Moderate, |
| 75 | + tags: [COMMON_TAGS.CATEGORIES.PERFORMANCE, COMMON_TAGS.LANGUAGES.APEX], |
| 76 | + description: 'Filtering records in Apex instead of using SOQL WHERE clause causes performance issues', |
| 77 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_soql_with_apex_filter.htm&type=5'] |
| 78 | + }, |
| 79 | + |
| 80 | + { |
| 81 | + name: 'CopyingListOrSetElementsUsingAForLoop', |
| 82 | + severityLevel: SeverityLevel.Moderate, |
| 83 | + tags: [COMMON_TAGS.CATEGORIES.PERFORMANCE, COMMON_TAGS.LANGUAGES.APEX], |
| 84 | + description: 'Copying list or set elements using a for loop is inefficient - use addAll() instead', |
| 85 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_copying_elements_with_for_loop.htm&type=5'] |
| 86 | + }, |
| 87 | + |
| 88 | + { |
| 89 | + name: 'Redundant Soql', |
| 90 | + severityLevel: SeverityLevel.Moderate, |
| 91 | + tags: [COMMON_TAGS.CATEGORIES.PERFORMANCE, COMMON_TAGS.LANGUAGES.APEX], |
| 92 | + description: 'Multiple identical SOQL queries cause unnecessary database round trips', |
| 93 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_redundant_soql.htm&type=5'] |
| 94 | + }, |
| 95 | + |
| 96 | + { |
| 97 | + name: 'SoqlWithNegativeExpressions', |
| 98 | + severityLevel: SeverityLevel.Moderate, |
| 99 | + tags: [COMMON_TAGS.CATEGORIES.PERFORMANCE, COMMON_TAGS.LANGUAGES.APEX], |
| 100 | + description: 'SOQL queries using negative expressions (NOT IN, !=) don\'t use indexes and cause full table scans', |
| 101 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_soql_with_negative_expressions.htm&type=5'] |
| 102 | + }, |
| 103 | + |
| 104 | + { |
| 105 | + name: 'SObjectMapInAForLoop', |
| 106 | + severityLevel: SeverityLevel.Moderate, |
| 107 | + tags: [COMMON_TAGS.CATEGORIES.PERFORMANCE, COMMON_TAGS.LANGUAGES.APEX], |
| 108 | + description: 'Building Map<Id, SObject> using .put() in a for loop is inefficient - use map constructor or putAll()', |
| 109 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_sobject_map_in_for_loop.htm&type=5'] |
| 110 | + }, |
| 111 | + |
| 112 | + // ================================================================================================================= |
| 113 | + // BEST PRACTICES - LOW SEVERITY (RECOMMENDED) |
| 114 | + // ================================================================================================================= |
| 115 | + |
| 116 | + { |
| 117 | + name: 'UsingTheTestMethodKeyword', |
| 118 | + severityLevel: SeverityLevel.Low, |
| 119 | + tags: [COMMON_TAGS.RECOMMENDED, COMMON_TAGS.CATEGORIES.BEST_PRACTICES, COMMON_TAGS.LANGUAGES.APEX], |
| 120 | + description: 'The testMethod keyword is deprecated - use @isTest annotation instead', |
| 121 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_test_case_antipattern_using_testmethod.htm&type=5'] |
| 122 | + }, |
| 123 | + |
| 124 | + // ================================================================================================================= |
| 125 | + // BEST PRACTICES - LOW SEVERITY (NOT RECOMMENDED) |
| 126 | + // ================================================================================================================= |
| 127 | + |
| 128 | + { |
| 129 | + name: 'SortingInApex', |
| 130 | + severityLevel: SeverityLevel.Low, |
| 131 | + tags: [COMMON_TAGS.CATEGORIES.BEST_PRACTICES, COMMON_TAGS.LANGUAGES.APEX], |
| 132 | + description: 'Sorting records in Apex wastes CPU time and can exceed governor limits - use ORDER BY in SOQL', |
| 133 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_sorting_in_apex.htm&type=5'] |
| 134 | + }, |
| 135 | + |
| 136 | + { |
| 137 | + name: 'BusyLoopDelay', |
| 138 | + severityLevel: SeverityLevel.Low, |
| 139 | + tags: [COMMON_TAGS.CATEGORIES.BEST_PRACTICES, COMMON_TAGS.LANGUAGES.APEX], |
| 140 | + description: 'Using empty loops to delay execution wastes CPU time - use System.enqueueJob with delay parameter', |
| 141 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_busy_loop_delay.htm&type=5'] |
| 142 | + }, |
| 143 | + |
| 144 | + { |
| 145 | + name: 'SoqlWithUnusedFields', |
| 146 | + severityLevel: SeverityLevel.Low, |
| 147 | + tags: [COMMON_TAGS.CATEGORIES.BEST_PRACTICES, COMMON_TAGS.LANGUAGES.APEX], |
| 148 | + description: 'SOQL query selecting unused fields increases resource consumption unnecessarily', |
| 149 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_soql_with_unused_fields.htm&type=5'] |
| 150 | + }, |
| 151 | + |
| 152 | + // ================================================================================================================= |
| 153 | + // FALLBACK RULE |
| 154 | + // ================================================================================================================= |
| 155 | + |
| 156 | + { |
| 157 | + name: 'apexguru-other', |
| 158 | + severityLevel: SeverityLevel.Moderate, |
| 159 | + tags: [COMMON_TAGS.RECOMMENDED, COMMON_TAGS.CATEGORIES.BEST_PRACTICES, COMMON_TAGS.LANGUAGES.APEX], |
| 160 | + description: 'Other ApexGuru rules - covers new rules added by Salesforce that are not yet explicitly declared', |
| 161 | + resourceUrls: ['https://help.salesforce.com/s/articleView?id=xcloud.apexguru.htm'] |
| 162 | + } |
| 163 | +]; |
| 164 | + |
| 165 | +/** |
| 166 | + * Helper to check if a rule name is known |
| 167 | + */ |
| 168 | +export function isKnownRule(ruleName: string): boolean { |
| 169 | + return APEXGURU_RULES.some(rule => rule.name === ruleName); |
| 170 | +} |
| 171 | + |
| 172 | +/** |
| 173 | + * Fallback rule name for unknown violations |
| 174 | + */ |
| 175 | +export const FALLBACK_RULE_NAME = 'apexguru-other'; |
0 commit comments