| 1254 | |
| 1255 | // e.g. `tools.map(tool => tool.inputSchema)`, passing each element on |
| 1256 | private arrayMethod( |
| 1257 | name: string, |
| 1258 | call: Extract<Node, { type: "CallExpression" }>, |
| 1259 | module: SourceModule, |
| 1260 | held: Held |
| 1261 | ): void { |
| 1262 | const element: Held = { ...held, path: held.path.slice(1) } |
| 1263 | const returns = arrayMethods[name] |
| 1264 | if (returns === "unfollowed") |
| 1265 | return this.lose(call, module, `reduces it with .${name}`) |
| 1266 | if (returns === "convert") { |
| 1267 | this.convert(held, call, module) |
| 1268 | return |
| 1269 | } |
| 1270 | if (returns === "flat") { |
| 1271 | // flattens each leading array of its elements, to any depth, where one |
| 1272 | // flattened fewer times than the call's depth may be held deeper than it is |
| 1273 | const depth = flatDepthOf(call) |
| 1274 | for (let i = 0; i <= held.path.length; i++) { |
| 1275 | const path = held.path.slice(i) |
| 1276 | this.push( |
| 1277 | call, |
| 1278 | module, |
| 1279 | path[0] === anyKey && i < depth ? |
| 1280 | { ...held, path, unflattened: true } |
| 1281 | : { ...held, path } |
| 1282 | ) |
| 1283 | if (held.path[i] !== anyKey) break |
| 1284 | } |
| 1285 | return |
| 1286 | } |
| 1287 | if (returns === "holder") this.push(call, module, held) |
| 1288 | else if (returns === "element") this.push(call, module, element) |
| 1289 | this.callback( |
| 1290 | call, |
| 1291 | module, |
| 1292 | element, |
| 1293 | returns === "mapped" ? [anyKey] : undefined, |
| 1294 | 0, |
| 1295 | name === "flatMap" |
| 1296 | ) |
| 1297 | // flatMap flattens what its callback returns by one level, so a callback |
| 1298 | // returning an element rather than an array of them holds it at the same |
| 1299 | // depth |
| 1300 | if (name === "flatMap") this.callback(call, module, element, [], 0) |
| 1301 | } |
| 1302 | |
| 1303 | // a callback passed what its call reads, whose returns the call holds at wrap |
| 1304 | private callback( |