|
| 1 | +import type Parser from "web-tree-sitter"; |
| 2 | +import { cleanStringValue, getCapture } from "./ast-helpers.js"; |
| 3 | +import type { LangFamily } from "./languages.js"; |
| 4 | +import { CLIENT_NAMES } from "./languages.js"; |
| 5 | +import type { ParserManager } from "./parser-manager.js"; |
| 6 | +import type { DetectionConfig } from "./types.js"; |
| 7 | + |
| 8 | +const POSTHOG_CLASS_NAMES = new Set(["PostHog", "Posthog"]); |
| 9 | +const GO_CONSTRUCTOR_NAMES = new Set(["New", "NewWithConfig"]); |
| 10 | + |
| 11 | +export function getEffectiveClients(config: DetectionConfig): Set<string> { |
| 12 | + const clients = new Set(CLIENT_NAMES); |
| 13 | + for (const name of config.additionalClientNames) { |
| 14 | + clients.add(name); |
| 15 | + } |
| 16 | + return clients; |
| 17 | +} |
| 18 | + |
| 19 | +export function findAliases( |
| 20 | + pm: ParserManager, |
| 21 | + lang: Parser.Language, |
| 22 | + tree: Parser.Tree, |
| 23 | + family: LangFamily, |
| 24 | +): { |
| 25 | + clientAliases: Set<string>; |
| 26 | + destructuredCapture: Set<string>; |
| 27 | + destructuredFlag: Set<string>; |
| 28 | +} { |
| 29 | + const effectiveClients = getEffectiveClients(pm.config); |
| 30 | + const clientAliases = new Set<string>(); |
| 31 | + const destructuredCapture = new Set<string>(); |
| 32 | + const destructuredFlag = new Set<string>(); |
| 33 | + |
| 34 | + // Client aliases: const tracker = posthog |
| 35 | + const aliasQuery = pm.getQuery(lang, family.queries.clientAliases); |
| 36 | + if (aliasQuery) { |
| 37 | + for (const match of aliasQuery.matches(tree.rootNode)) { |
| 38 | + const aliasNode = getCapture(match.captures, "alias"); |
| 39 | + const sourceNode = getCapture(match.captures, "source"); |
| 40 | + if (aliasNode && sourceNode && effectiveClients.has(sourceNode.text)) { |
| 41 | + clientAliases.add(aliasNode.text); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Constructor aliases: new PostHog('phc_...') / posthog.New("token") / PostHog::Client.new(...) |
| 47 | + const constructorQuery = pm.getQuery(lang, family.queries.constructorAliases); |
| 48 | + if (constructorQuery) { |
| 49 | + for (const match of constructorQuery.matches(tree.rootNode)) { |
| 50 | + const aliasNode = getCapture(match.captures, "alias"); |
| 51 | + const classNode = getCapture(match.captures, "class_name"); |
| 52 | + const pkgNode = getCapture(match.captures, "pkg_name"); |
| 53 | + const funcNode = getCapture(match.captures, "func_name"); |
| 54 | + |
| 55 | + if (aliasNode && classNode && POSTHOG_CLASS_NAMES.has(classNode.text)) { |
| 56 | + clientAliases.add(aliasNode.text); |
| 57 | + } |
| 58 | + if ( |
| 59 | + aliasNode && |
| 60 | + pkgNode && |
| 61 | + funcNode && |
| 62 | + pkgNode.text === "posthog" && |
| 63 | + GO_CONSTRUCTOR_NAMES.has(funcNode.text) |
| 64 | + ) { |
| 65 | + clientAliases.add(aliasNode.text); |
| 66 | + } |
| 67 | + const scopeNode = getCapture(match.captures, "scope_name"); |
| 68 | + const methodNameNode = getCapture(match.captures, "method_name"); |
| 69 | + if ( |
| 70 | + aliasNode && |
| 71 | + scopeNode && |
| 72 | + classNode && |
| 73 | + methodNameNode && |
| 74 | + POSTHOG_CLASS_NAMES.has(scopeNode.text) && |
| 75 | + classNode.text === "Client" && |
| 76 | + methodNameNode.text === "new" |
| 77 | + ) { |
| 78 | + clientAliases.add(aliasNode.text); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Destructured methods: const { capture, getFeatureFlag } = posthog |
| 84 | + if (family.queries.destructuredMethods) { |
| 85 | + const destructQuery = pm.getQuery(lang, family.queries.destructuredMethods); |
| 86 | + if (destructQuery) { |
| 87 | + for (const match of destructQuery.matches(tree.rootNode)) { |
| 88 | + const methodNode = getCapture(match.captures, "method_name"); |
| 89 | + const sourceNode = getCapture(match.captures, "source"); |
| 90 | + if (methodNode && sourceNode && effectiveClients.has(sourceNode.text)) { |
| 91 | + const name = methodNode.text; |
| 92 | + if (family.captureMethods.has(name)) { |
| 93 | + destructuredCapture.add(name); |
| 94 | + } |
| 95 | + if (family.flagMethods.has(name)) { |
| 96 | + destructuredFlag.add(name); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return { clientAliases, destructuredCapture, destructuredFlag }; |
| 104 | +} |
| 105 | + |
| 106 | +export function buildConstantMap( |
| 107 | + pm: ParserManager, |
| 108 | + lang: Parser.Language, |
| 109 | + tree: Parser.Tree, |
| 110 | +): Map<string, string> { |
| 111 | + const constants = new Map<string, string>(); |
| 112 | + |
| 113 | + // JS: const/let/var declarations |
| 114 | + const jsQuery = pm.getQuery( |
| 115 | + lang, |
| 116 | + ` |
| 117 | + (lexical_declaration |
| 118 | + (variable_declarator |
| 119 | + name: (identifier) @name |
| 120 | + value: (string (string_fragment) @value))) |
| 121 | +
|
| 122 | + (variable_declaration |
| 123 | + (variable_declarator |
| 124 | + name: (identifier) @name |
| 125 | + value: (string (string_fragment) @value))) |
| 126 | + `, |
| 127 | + ); |
| 128 | + if (jsQuery) { |
| 129 | + for (const match of jsQuery.matches(tree.rootNode)) { |
| 130 | + const nameNode = getCapture(match.captures, "name"); |
| 131 | + const valueNode = getCapture(match.captures, "value"); |
| 132 | + if (nameNode && valueNode) { |
| 133 | + constants.set(nameNode.text, valueNode.text); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Python: simple assignment — NAME = "value" |
| 139 | + const pyQuery = pm.getQuery( |
| 140 | + lang, |
| 141 | + ` |
| 142 | + (expression_statement |
| 143 | + (assignment |
| 144 | + left: (identifier) @name |
| 145 | + right: (string (string_content) @value))) |
| 146 | + `, |
| 147 | + ); |
| 148 | + if (pyQuery) { |
| 149 | + for (const match of pyQuery.matches(tree.rootNode)) { |
| 150 | + const nameNode = getCapture(match.captures, "name"); |
| 151 | + const valueNode = getCapture(match.captures, "value"); |
| 152 | + if (nameNode && valueNode) { |
| 153 | + constants.set(nameNode.text, valueNode.text); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // Go: short var declarations and const declarations |
| 159 | + const goVarQuery = pm.getQuery( |
| 160 | + lang, |
| 161 | + ` |
| 162 | + (short_var_declaration |
| 163 | + left: (expression_list (identifier) @name) |
| 164 | + right: (expression_list (interpreted_string_literal) @value)) |
| 165 | + `, |
| 166 | + ); |
| 167 | + if (goVarQuery) { |
| 168 | + for (const match of goVarQuery.matches(tree.rootNode)) { |
| 169 | + const nameNode = getCapture(match.captures, "name"); |
| 170 | + const valueNode = getCapture(match.captures, "value"); |
| 171 | + if (nameNode && valueNode) { |
| 172 | + constants.set(nameNode.text, cleanStringValue(valueNode.text)); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + const goConstQuery = pm.getQuery( |
| 178 | + lang, |
| 179 | + ` |
| 180 | + (const_declaration |
| 181 | + (const_spec |
| 182 | + name: (identifier) @name |
| 183 | + value: (expression_list (interpreted_string_literal) @value))) |
| 184 | + `, |
| 185 | + ); |
| 186 | + if (goConstQuery) { |
| 187 | + for (const match of goConstQuery.matches(tree.rootNode)) { |
| 188 | + const nameNode = getCapture(match.captures, "name"); |
| 189 | + const valueNode = getCapture(match.captures, "value"); |
| 190 | + if (nameNode && valueNode) { |
| 191 | + constants.set(nameNode.text, cleanStringValue(valueNode.text)); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + // Ruby: assignment — local var or constant |
| 197 | + const rbQuery = pm.getQuery( |
| 198 | + lang, |
| 199 | + ` |
| 200 | + (assignment |
| 201 | + left: (identifier) @name |
| 202 | + right: (string (string_content) @value)) |
| 203 | +
|
| 204 | + (assignment |
| 205 | + left: (constant) @name |
| 206 | + right: (string (string_content) @value)) |
| 207 | + `, |
| 208 | + ); |
| 209 | + if (rbQuery) { |
| 210 | + for (const match of rbQuery.matches(tree.rootNode)) { |
| 211 | + const nameNode = getCapture(match.captures, "name"); |
| 212 | + const valueNode = getCapture(match.captures, "value"); |
| 213 | + if (nameNode && valueNode) { |
| 214 | + constants.set(nameNode.text, valueNode.text); |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + return constants; |
| 220 | +} |
0 commit comments