|
| 1 | +/** |
| 2 | + * C# — AST analysis rules. |
| 3 | + */ |
| 4 | + |
| 5 | +import { makeCfgRules, makeDataflowRules } from '../shared.js'; |
| 6 | + |
| 7 | +// ─── Complexity ─────────────────────────────────────────────────────────── |
| 8 | + |
| 9 | +export const complexity = { |
| 10 | + branchNodes: new Set([ |
| 11 | + 'if_statement', |
| 12 | + 'else_clause', |
| 13 | + 'for_statement', |
| 14 | + 'foreach_statement', |
| 15 | + 'while_statement', |
| 16 | + 'do_statement', |
| 17 | + 'catch_clause', |
| 18 | + 'conditional_expression', |
| 19 | + 'switch_statement', |
| 20 | + ]), |
| 21 | + caseNodes: new Set(['switch_section']), |
| 22 | + logicalOperators: new Set(['&&', '||', '??']), |
| 23 | + logicalNodeType: 'binary_expression', |
| 24 | + optionalChainType: 'conditional_access_expression', |
| 25 | + nestingNodes: new Set([ |
| 26 | + 'if_statement', |
| 27 | + 'for_statement', |
| 28 | + 'foreach_statement', |
| 29 | + 'while_statement', |
| 30 | + 'do_statement', |
| 31 | + 'catch_clause', |
| 32 | + 'conditional_expression', |
| 33 | + 'switch_statement', |
| 34 | + ]), |
| 35 | + functionNodes: new Set([ |
| 36 | + 'method_declaration', |
| 37 | + 'constructor_declaration', |
| 38 | + 'lambda_expression', |
| 39 | + 'local_function_statement', |
| 40 | + ]), |
| 41 | + ifNodeType: 'if_statement', |
| 42 | + elseNodeType: null, |
| 43 | + elifNodeType: null, |
| 44 | + elseViaAlternative: true, |
| 45 | + switchLikeNodes: new Set(['switch_statement']), |
| 46 | +}; |
| 47 | + |
| 48 | +// ─── Halstead ───────────────────────────────────────────────────────────── |
| 49 | + |
| 50 | +export const halstead = { |
| 51 | + operatorLeafTypes: new Set([ |
| 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 | + 'is', |
| 88 | + 'as', |
| 89 | + 'new', |
| 90 | + 'typeof', |
| 91 | + 'sizeof', |
| 92 | + 'nameof', |
| 93 | + 'if', |
| 94 | + 'else', |
| 95 | + 'for', |
| 96 | + 'foreach', |
| 97 | + 'while', |
| 98 | + 'do', |
| 99 | + 'switch', |
| 100 | + 'case', |
| 101 | + 'return', |
| 102 | + 'throw', |
| 103 | + 'break', |
| 104 | + 'continue', |
| 105 | + 'try', |
| 106 | + 'catch', |
| 107 | + 'finally', |
| 108 | + 'await', |
| 109 | + 'yield', |
| 110 | + '.', |
| 111 | + '?.', |
| 112 | + ',', |
| 113 | + ';', |
| 114 | + ':', |
| 115 | + '=>', |
| 116 | + '->', |
| 117 | + ]), |
| 118 | + operandLeafTypes: new Set([ |
| 119 | + 'identifier', |
| 120 | + 'integer_literal', |
| 121 | + 'real_literal', |
| 122 | + 'string_literal', |
| 123 | + 'character_literal', |
| 124 | + 'verbatim_string_literal', |
| 125 | + 'interpolated_string_text', |
| 126 | + 'true', |
| 127 | + 'false', |
| 128 | + 'null', |
| 129 | + 'this', |
| 130 | + 'base', |
| 131 | + ]), |
| 132 | + compoundOperators: new Set([ |
| 133 | + 'invocation_expression', |
| 134 | + 'element_access_expression', |
| 135 | + 'object_creation_expression', |
| 136 | + ]), |
| 137 | + skipTypes: new Set(['type_argument_list', 'type_parameter_list']), |
| 138 | +}; |
| 139 | + |
| 140 | +// ─── CFG ────────────────────────────────────────────────────────────────── |
| 141 | + |
| 142 | +export const cfg = makeCfgRules({ |
| 143 | + ifNode: 'if_statement', |
| 144 | + elseViaAlternative: true, |
| 145 | + forNodes: new Set(['for_statement', 'foreach_statement']), |
| 146 | + whileNode: 'while_statement', |
| 147 | + doNode: 'do_statement', |
| 148 | + switchNode: 'switch_statement', |
| 149 | + caseNode: 'switch_section', |
| 150 | + tryNode: 'try_statement', |
| 151 | + catchNode: 'catch_clause', |
| 152 | + finallyNode: 'finally_clause', |
| 153 | + returnNode: 'return_statement', |
| 154 | + throwNode: 'throw_statement', |
| 155 | + breakNode: 'break_statement', |
| 156 | + continueNode: 'continue_statement', |
| 157 | + blockNode: 'block', |
| 158 | + labeledNode: 'labeled_statement', |
| 159 | + functionNodes: new Set([ |
| 160 | + 'method_declaration', |
| 161 | + 'constructor_declaration', |
| 162 | + 'lambda_expression', |
| 163 | + 'local_function_statement', |
| 164 | + ]), |
| 165 | +}); |
| 166 | + |
| 167 | +// ─── Dataflow ───────────────────────────────────────────────────────────── |
| 168 | + |
| 169 | +export const dataflow = makeDataflowRules({ |
| 170 | + functionNodes: new Set([ |
| 171 | + 'method_declaration', |
| 172 | + 'constructor_declaration', |
| 173 | + 'lambda_expression', |
| 174 | + 'local_function_statement', |
| 175 | + ]), |
| 176 | + returnNode: 'return_statement', |
| 177 | + varDeclaratorNode: 'variable_declarator', |
| 178 | + varNameField: 'name', |
| 179 | + assignmentNode: 'assignment_expression', |
| 180 | + callNode: 'invocation_expression', |
| 181 | + callFunctionField: 'function', |
| 182 | + callArgsField: 'arguments', |
| 183 | + memberNode: 'member_access_expression', |
| 184 | + memberObjectField: 'expression', |
| 185 | + memberPropertyField: 'name', |
| 186 | + awaitNode: 'await_expression', |
| 187 | + argumentWrapperType: 'argument', |
| 188 | + mutatingMethods: new Set(['Add', 'Remove', 'Clear', 'Insert', 'Sort', 'Reverse', 'Push', 'Pop']), |
| 189 | + extractParamName(node) { |
| 190 | + if (node.type === 'parameter') { |
| 191 | + const nameNode = node.childForFieldName('name'); |
| 192 | + return nameNode ? [nameNode.text] : null; |
| 193 | + } |
| 194 | + if (node.type === 'identifier') return [node.text]; |
| 195 | + return null; |
| 196 | + }, |
| 197 | +}); |
| 198 | + |
| 199 | +// ─── AST Node Types ─────────────────────────────────────────────────────── |
| 200 | + |
| 201 | +export const astTypes = null; |
0 commit comments