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

Function isContainedThis

ark/aot/usage.ts:2913–2969  ·  view source on GitHub ↗
(
	node: Node,
	fnKey: string,
	isMethod: boolean,
	ownKeys: ReadonlySet<string>,
	externalKeys: ReadonlySet<string>,
	module: SourceModule,
	seen = new Set<Binding>()
)

Source from the content-addressed store, hash-verified

2911// whether a `this` read inside a literal's own method or getter can neither
2912// leak the literal itself nor detach its property at `fnKey`
2913const isContainedThis = (
2914 node: Node,
2915 fnKey: string,
2916 isMethod: boolean,
2917 ownKeys: ReadonlySet<string>,
2918 externalKeys: ReadonlySet<string>,
2919 module: SourceModule,
2920 seen = new Set<Binding>()
2921): boolean => {
2922 const [operand, parent] = operatorOf(node)
2923 if (parent?.type === "MemberExpression" && parent.object === operand) {
2924 if (isWriteTarget(parent)) return false
2925 const name = memberNameOf(parent)
2926 if (typeof name === "string") {
2927 if (!ownKeys.has(name) && name in Object.prototype) return false
2928 const isDirectCall = isDirectCallMember(parent)
2929 if (isMethod && name === fnKey) return isDirectCall
2930 if (externalKeys.has(name) && isDirectCall) return false
2931 return true
2932 }
2933 // computed read `this[k]` can return `this[fnKey]` unless only converted/logged
2934 return isNonEscapingRead(parent, module)
2935 }
2936 if (parent?.type === "VariableDeclarator" && parent.init === operand) {
2937 if (parent.id.type === "ObjectPattern") {
2938 return parent.id.properties.every(property => {
2939 const key =
2940 property.type === "Property" ? propertyKeyOf(property) : undefined
2941 return (
2942 key !== undefined &&
2943 (!isMethod || key !== fnKey) &&
2944 (ownKeys.has(key) || !(key in Object.prototype))
2945 )
2946 })
2947 }
2948 const binding =
2949 parent.id.type === "Identifier" ?
2950 module.scopes.bindingsById.get(parent.id)
2951 : undefined
2952 if (!binding || seen.has(binding)) return false
2953 seen.add(binding)
2954 return binding.references.every(
2955 reference =>
2956 reference.isWrite ||
2957 isContainedThis(
2958 reference.id,
2959 fnKey,
2960 isMethod,
2961 ownKeys,
2962 externalKeys,
2963 module,
2964 seen
2965 )
2966 )
2967 }
2968 return false
2969}
2970

Callers 1

isContainedFunction · 0.85

Calls 9

operatorOfFunction · 0.90
isWriteTargetFunction · 0.90
memberNameOfFunction · 0.90
propertyKeyOfFunction · 0.90
isDirectCallMemberFunction · 0.85
isNonEscapingReadFunction · 0.85
getMethod · 0.65
hasMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected