( query: QueryIR, )
| 150 | * @returns A map from collection ID to the set of all aliases referencing that collection |
| 151 | */ |
| 152 | export function extractCollectionAliases( |
| 153 | query: QueryIR, |
| 154 | ): Map<string, Set<string>> { |
| 155 | const aliasesById = new Map<string, Set<string>>() |
| 156 | |
| 157 | function recordAlias(source: any) { |
| 158 | if (!source) return |
| 159 | |
| 160 | if (source.type === `collectionRef`) { |
| 161 | const { id } = source.collection |
| 162 | const existing = aliasesById.get(id) |
| 163 | if (existing) { |
| 164 | existing.add(source.alias) |
| 165 | } else { |
| 166 | aliasesById.set(id, new Set([source.alias])) |
| 167 | } |
| 168 | } else if (source.type === `queryRef`) { |
| 169 | traverse(source.query) |
| 170 | } else if (source.type === `unionFrom`) { |
| 171 | for (const childSource of source.sources) { |
| 172 | recordAlias(childSource) |
| 173 | } |
| 174 | } else if (source.type === `unionAll`) { |
| 175 | for (const branch of source.queries) { |
| 176 | traverse(branch) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | function traverseSelect(select: any) { |
| 182 | for (const [key, value] of Object.entries(select)) { |
| 183 | if (typeof key === `string` && key.startsWith(`__SPREAD_SENTINEL__`)) { |
| 184 | continue |
| 185 | } |
| 186 | if (value instanceof IncludesSubquery) { |
| 187 | traverse(value.query) |
| 188 | } else if (value instanceof ConditionalSelect) { |
| 189 | traverseConditionalSelect(value) |
| 190 | } else if (isNestedSelectObject(value)) { |
| 191 | traverseSelect(value) |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | function traverseConditionalSelect(conditional: ConditionalSelect) { |
| 197 | for (const branch of conditional.branches) { |
| 198 | traverseSelectValue(branch.value) |
| 199 | } |
| 200 | if (conditional.defaultValue !== undefined) { |
| 201 | traverseSelectValue(conditional.defaultValue) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | function traverseSelectValue(value: any) { |
| 206 | if (value instanceof IncludesSubquery) { |
| 207 | traverse(value.query) |
| 208 | } else if (value instanceof ConditionalSelect) { |
| 209 | traverseConditionalSelect(value) |
no test coverage detected