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

Function isContained

ark/aot/usage.ts:2817–2909  ·  view source on GitHub ↗
(
	literal: Extract<Node, { type: "ObjectExpression" }>,
	module: SourceModule,
	property: PropertyNode
)

Source from the content-addressed store, hash-verified

2815// another object: neither through references to `literal`'s binding nor through
2816// `this` inside `literal`'s own methods/getters or external methods called on it
2817const isContained = (
2818 literal: Extract<Node, { type: "ObjectExpression" }>,
2819 module: SourceModule,
2820 property: PropertyNode
2821): boolean => {
2822 const fnKey = propertyKeyOf(property)
2823 if (fnKey === undefined) return false
2824 const isMethod = property.kind === "init"
2825 const [operand, declarator] = operatorOf(literal)
2826 if (
2827 declarator?.type !== "VariableDeclarator" ||
2828 declarator.init !== operand ||
2829 declarator.id.type !== "Identifier"
2830 )
2831 return false
2832 const binding = module.scopes.bindingsById.get(declarator.id)
2833 if (!binding || binding.exportNames.length) return false
2834 const ownKeys = new Set<string>()
2835 // keys whose value is not inline, so calling `literal.<key>()` runs external
2836 // code with `this === literal` that could detach `this.<fnKey>`
2837 const externalKeys = new Set<string>()
2838 for (const prop of literal.properties) {
2839 if (prop.type !== "Property") return false
2840 const key = propertyKeyOf(prop)
2841 if (key === undefined) return false
2842 // a non-null __proto__ inherits getters/methods that run with `this === literal`
2843 if (key === "__proto__" && !prop.computed && prop.kind === "init") {
2844 if (isNonNullProtoProperty(prop)) return false
2845 continue
2846 }
2847 ownKeys.add(key)
2848 if (prop !== property && prop.kind === "init") {
2849 const value = unwrapExpression(prop.value)
2850 if (
2851 value.type !== "FunctionExpression" &&
2852 value.type !== "ArrowFunctionExpression" &&
2853 value.type !== "Literal" &&
2854 value.type !== "TemplateLiteral" &&
2855 value.type !== "ObjectExpression" &&
2856 value.type !== "ArrayExpression"
2857 )
2858 externalKeys.add(key)
2859 }
2860 }
2861 const refsContained = binding.references.every(reference => {
2862 if (reference.isWrite) return false
2863 const [read, use] = operatorOf(reference.id)
2864 if (
2865 use?.type !== "MemberExpression" ||
2866 use.object !== read ||
2867 isWriteTarget(use)
2868 )
2869 return false
2870 const name = memberNameOf(use)
2871 if (typeof name !== "string") return false
2872 // e.g. `other.valueOf()`, `other.__lookupGetter__("x")`, `other.__proto__`
2873 if (!ownKeys.has(name) && name in Object.prototype) return false
2874 const isDirectCall = isDirectCallMember(use)

Callers 1

boundLiteralsOfFunction · 0.85

Calls 13

propertyKeyOfFunction · 0.90
operatorOfFunction · 0.90
unwrapExpressionFunction · 0.90
isWriteTargetFunction · 0.90
memberNameOfFunction · 0.90
walkFunction · 0.90
isNonNullProtoPropertyFunction · 0.85
isDirectCallMemberFunction · 0.85
enclosingThisFunctionOfFunction · 0.85
isContainedThisFunction · 0.85
getMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected