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

Method writeFrom

ark/aot/graph.ts:5253–5316  ·  view source on GitHub ↗
(node: Node, module: SourceModule)

Source from the content-addressed store, hash-verified

5251 }
5252
5253 private writeFrom(node: Node, module: SourceModule): Node | undefined {
5254 for (;;) {
5255 const [operand, parent] = operatorOf(node)
5256 switch (parent?.type) {
5257 case "MemberExpression": {
5258 if (parent.object !== operand) return
5259 if (isWriteTarget(parent)) return writeOf(parent)
5260 const [callee, call] = operatorOf(parent)
5261 if (
5262 call?.type === "CallExpression" &&
5263 call.callee === callee &&
5264 (memberNameOf(parent) as never) in mutatingMethods
5265 )
5266 return call
5267 // e.g. `o.a.b = 1`, writing what `o` holds
5268 node = parent
5269 continue
5270 }
5271 case "CallExpression": {
5272 const index = parent.arguments.indexOf(operand as never)
5273 if (index === -1) return
5274 if (index === 0 && isObjectWrite(parent.callee, module)) return parent
5275 // e.g. `addB(o)` with `addB = o => { o.b = "number" }`
5276 const fn = this.functionOf(parent.callee, module)
5277 const param = fn && runtimeParamsOf(fn[0])[index]
5278 const written =
5279 fn &&
5280 param?.type === "Identifier" &&
5281 fn[1].scopes.bindingsById.get(param)
5282 return written && this.writesThrough(written, fn![1]).length ?
5283 parent
5284 : undefined
5285 }
5286 // e.g. `const alias = o`, through which the program writes `o`
5287 case "VariableDeclarator": {
5288 const alias =
5289 parent.init === operand && parent.id.type === "Identifier" ?
5290 module.scopes.bindingsById.get(parent.id)
5291 : undefined
5292 return alias && this.writesThrough(alias, module)[0]
5293 }
5294 // e.g. `for (const field of fields) field.x = 1`
5295 case "ForOfStatement": {
5296 const declaration = parent.left
5297 const id =
5298 (
5299 parent.right === operand &&
5300 declaration.type === "VariableDeclaration" &&
5301 declaration.declarations[0].id.type === "Identifier"
5302 ) ?
5303 declaration.declarations[0].id
5304 : undefined
5305 const element = id && module.scopes.bindingsById.get(id)
5306 return element && this.writesThrough(element, module)[0]
5307 }
5308 case "ConditionalExpression":
5309 case "LogicalExpression":
5310 node = parent

Callers 1

writesThroughMethod · 0.95

Calls 9

functionOfMethod · 0.95
writesThroughMethod · 0.95
operatorOfFunction · 0.90
isWriteTargetFunction · 0.90
runtimeParamsOfFunction · 0.90
writeOfFunction · 0.85
isObjectWriteFunction · 0.85
memberNameOfFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected