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

Function namesReadOf

ark/aot/usage.ts:2168–2228  ·  view source on GitHub ↗
(
	node: Node,
	module: SourceModule,
	ctx: ThisReadsContext,
	seen = new Set<Binding>()
)

Source from the content-addressed store, hash-verified

2166// than at a name, like `read(this)` or `this[k]`, or empty where it's passed
2167// as the receiver of a function proven to have its own `this` reads indexed
2168const namesReadOf = (
2169 node: Node,
2170 module: SourceModule,
2171 ctx: ThisReadsContext,
2172 seen = new Set<Binding>()
2173): ReadonlySet<string> | "any" => {
2174 const [operand, parent] = operatorOf(node)
2175 // e.g. `fn.apply(this, args)` in a debounce, where fn is proven to be an
2176 // indexed function whose own body records every `this` read, unless the
2177 // program may have patched `Function.prototype.call` or `.apply`
2178 if (
2179 !ctx.patchesBuiltins &&
2180 parent?.type === "CallExpression" &&
2181 parent.arguments[0] === operand &&
2182 parent.callee.type === "MemberExpression" &&
2183 forwardingMethods.has(String(memberNameOf(parent.callee))) &&
2184 isForwardingCallee(parent.callee.object, module, ctx)
2185 )
2186 return emptyNames
2187 if (parent?.type === "MemberExpression" && parent.object === operand) {
2188 // a plain assignment like `this.props = props` writes without reading the
2189 // previous value (an object literal's `init` validator property has no setter)
2190 const [memberOp, memberUse] = operatorOf(parent)
2191 if (
2192 memberUse?.type === "AssignmentExpression" &&
2193 memberUse.operator === "=" &&
2194 memberUse.left === memberOp
2195 )
2196 return emptyNames
2197 const name = memberNameOf(parent)
2198 return typeof name === "string" ? new Set([name]) : "any"
2199 }
2200 // e.g. `const self = this` or `const { schema } = this`, read at its references
2201 if (parent?.type === "VariableDeclarator" && parent.init === operand) {
2202 if (parent.id.type === "ObjectPattern") {
2203 const names = new Set<string>()
2204 for (const property of parent.id.properties) {
2205 const name =
2206 property.type === "Property" ? propertyKeyOf(property) : undefined
2207 if (name === undefined) return "any"
2208 names.add(name)
2209 }
2210 return names
2211 }
2212 const binding =
2213 parent.id.type === "Identifier" ?
2214 module.scopes.bindingsById.get(parent.id)
2215 : undefined
2216 if (!binding || seen.has(binding)) return "any"
2217 seen.add(binding)
2218 const names = new Set<string>()
2219 for (const reference of binding.references) {
2220 if (reference.isWrite) continue
2221 const read = namesReadOf(reference.id, module, ctx, seen)
2222 if (read === "any") return "any"
2223 for (const name of read) names.add(name)
2224 }
2225 return names

Callers 1

thisReadsOfFunction · 0.85

Calls 8

operatorOfFunction · 0.90
memberNameOfFunction · 0.90
propertyKeyOfFunction · 0.90
StringInterface · 0.85
isForwardingCalleeFunction · 0.85
getMethod · 0.65
hasMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected