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

Method reflectionOf

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

Source from the content-addressed store, hash-verified

4930
4931 // e.g. `JSON.stringify(T)`, `Object.keys(T)`, `{ ...T }`
4932 private reflectionOf(
4933 node: Node,
4934 module: SourceModule
4935 ): [read: unknown, objects: Node[]] | undefined {
4936 if (
4937 node.type === "SpreadElement" &&
4938 node.parent?.type === "ObjectExpression"
4939 )
4940 return [undefined, [node.argument]]
4941 if (node.type === "ForInStatement") return [undefined, [node.right]]
4942 // e.g. `${T}` or `"" + T`, which convert it to its string
4943 if (
4944 node.type === "TemplateLiteral" &&
4945 node.parent?.type !== "TaggedTemplateExpression"
4946 )
4947 return ["toString", node.expressions]
4948 if (node.type === "BinaryExpression" && node.operator === "+")
4949 return ["toString", [node.left, node.right]]
4950 if (node.type === "AssignmentExpression" && node.operator === "+=")
4951 return ["toString", [node.right]]
4952 if (node.type === "BinaryExpression" && node.operator === "in") {
4953 const key = node.left.type === "Literal" ? node.left.value : undefined
4954 return [key, [node.right]]
4955 }
4956 if (node.type !== "CallExpression") return
4957 const callee = unwrapChain(node.callee)
4958 const args = node.arguments.filter(arg => arg.type !== "SpreadElement")
4959 if (
4960 callee.type === "Identifier" &&
4961 callee.name === "String" &&
4962 !module.scopes.byId.get(callee)?.binding
4963 )
4964 return ["toString", args.slice(0, 1)]
4965 if (callee.type !== "MemberExpression") return
4966 const global = unwrapChain(callee.object)
4967 if (global.type !== "Identifier" || module.scopes.byId.get(global)?.binding)
4968 return
4969 const name = memberNameOf(callee)
4970 const [object, key] = args
4971 return (
4972 global.name === "JSON" && name === "stringify" ?
4973 ["toJSON", args.slice(0, 1)]
4974 : (
4975 global.name === "Reflect" &&
4976 name === "get" &&
4977 key?.type === "Literal" &&
4978 typeof key.value === "string"
4979 ) ?
4980 [key.value, [object]]
4981 : (
4982 (global.name === "Object" &&
4983 !(typeof name === "string" && name in nonReflectingObjectMethods)) ||
4984 global.name === "Reflect"
4985 ) ?
4986 [undefined, args]
4987 : undefined
4988 )
4989 }

Callers 1

unavailableReadOfMethod · 0.95

Calls 4

unwrapChainFunction · 0.90
filterMethod · 0.80
memberNameOfFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected