MCPcopy Create free account
hub / github.com/arktypeio/arktype / bindPattern

Method bindPattern

ark/aot/readers.ts:1504–1588  ·  view source on GitHub ↗
(pattern: Node, module: SourceModule, held: Held)

Source from the content-addressed store, hash-verified

1502
1503 // a const, let, param or destructured binding
1504 private bindPattern(pattern: Node, module: SourceModule, held: Held): void {
1505 switch (pattern.type) {
1506 case "Identifier": {
1507 // a declaration, or a binding an assignment writes
1508 const binding =
1509 module.scopes.bindingsById.get(pattern) ??
1510 module.scopes.byId.get(pattern)?.binding
1511 if (!binding) {
1512 return this.lose(
1513 pattern,
1514 module,
1515 "stores it on the global object, which code outside the bundle may read"
1516 )
1517 }
1518 const helds = this.heldsByBinding.get(binding)
1519 if (helds) helds.add(shapeKeyOf(held))
1520 else this.heldsByBinding.set(binding, new Set([shapeKeyOf(held)]))
1521 this.readBinding(binding, module, held)
1522 return
1523 }
1524 case "AssignmentPattern":
1525 return this.bindPattern(pattern.left, module, held)
1526 case "MemberExpression":
1527 return this.write(pattern, module, held)
1528 case "ObjectPattern":
1529 for (const property of pattern.properties) {
1530 if (property.type === "RestElement") {
1531 if (!held.path.length)
1532 this.lose(property, module, "copies its members")
1533 else this.bindPattern(property.argument, module, held)
1534 continue
1535 }
1536 const key =
1537 propertyKeyOf(property) ??
1538 (property.computed ? constantOf(property.key, module) : undefined)
1539 if (!held.path.length) {
1540 const read = this.readMember(key, held, property, module)
1541 if (read) this.bindPattern(property.value, module, read)
1542 continue
1543 }
1544 const [first, ...rest] = held.path
1545 if (key === undefined || first === anyKey || first === key)
1546 this.bindPattern(property.value, module, { ...held, path: rest })
1547 }
1548 return
1549 case "ArrayPattern":
1550 if (!held.path.length) return
1551 for (const [i, element] of pattern.elements.entries()) {
1552 if (!element) continue
1553 if (element.type === "RestElement")
1554 this.bindPattern(element.argument, module, held)
1555 else if (held.path[0] === anyKey || held.path[0] === String(i)) {
1556 this.bindPattern(element, module, {
1557 ...held,
1558 path: held.path.slice(1)
1559 })
1560 }
1561 }

Callers 6

readAtMethod · 0.95
writeMethod · 0.95
followFieldMethod · 0.95
callbackMethod · 0.95
passMethod · 0.95
bindLeftMethod · 0.95

Calls 12

loseMethod · 0.95
readBindingMethod · 0.95
writeMethod · 0.95
readMemberMethod · 0.95
followFieldMethod · 0.95
propertyKeyOfFunction · 0.90
shapeKeyOfFunction · 0.85
constantOfFunction · 0.85
StringInterface · 0.85
getMethod · 0.65
addMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected