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

Function writeIssueFeaturesInto

ark/aot/features.ts:62–148  ·  view source on GitHub ↗
(
	id: string,
	code: string,
	issues: IssueFeatures
)

Source from the content-addressed store, hash-verified

60 * Undefined where the module reads none.
61 */
62export const writeIssueFeaturesInto = (
63 id: string,
64 code: string,
65 issues: IssueFeatures
66): string | undefined => {
67 if (!namesIssueFeature(code)) return
68 const module = new SourceModule(id, code, id)
69 if (module.parseErrors.length) return
70 const { scopes } = module
71 const edits: Edit[] = []
72 const valueByReference = new Map<Node, string>()
73 // each import's specifiers that are written in, by its declaration
74 const writtenByImport = new Map<Node, Set<Node>>()
75 for (const binding of Object.values(scopes.root.bindings)) {
76 const specifier = binding.declaration
77 if (binding.kind !== "import" || specifier.type !== "ImportSpecifier")
78 continue
79 const declaration = specifier.parent as Node | undefined
80 if (declaration?.type !== "ImportDeclaration") continue
81 const imported =
82 specifier.imported.type === "Identifier" ?
83 specifier.imported.name
84 : specifier.imported.value
85 if (
86 !hasOwn(issues, imported) ||
87 !exportsFeatures(declaration.source.value) ||
88 binding.writes.length
89 )
90 continue
91 // in parentheses, which esbuild reads as meant: it warns that `&&` after
92 // a bare `false` always returns it, where the module isn't a dependency's
93 const value = `(${issues[imported as keyof IssueFeatures]})`
94 let isExported = false
95 for (const { id } of binding.references) {
96 // `export { appliesValues }`, as arktype's runtime exports
97 // @ark/schema's: still the binding
98 if ((id as Node).parent?.type === "ExportSpecifier") isExported = true
99 else valueByReference.set(id, value)
100 }
101 if (isExported) continue
102 let written = writtenByImport.get(declaration)
103 if (!written) writtenByImport.set(declaration, (written = new Set()))
104 written.add(specifier)
105 }
106 if (!valueByReference.size && !writtenByImport.size) return
107 // `{ writesText }` holds the key too
108 const shorthand = new Set<Node>()
109 walk(module.program, node => {
110 if (
111 node.type === "Property" &&
112 node.shorthand &&
113 valueByReference.has(node.value)
114 ) {
115 shorthand.add(node.value)
116 edits.push([
117 node.start,
118 node.end,
119 `${code.slice(node.value.start, node.value.end)}: ${valueByReference.get(node.value)}`

Callers 4

loadIssueFeaturesFunction · 0.90
loadEsbuildModulesFunction · 0.90
writtenFunction · 0.90
prune.test.tsFile · 0.90

Calls 12

hasOwnFunction · 0.90
walkFunction · 0.90
namesIssueFeatureFunction · 0.85
exportsFeaturesFunction · 0.85
filterMethod · 0.80
sortMethod · 0.80
getMethod · 0.65
mapMethod · 0.65
setMethod · 0.45
addMethod · 0.45
hasMethod · 0.45
pushMethod · 0.45

Tested by 1

writtenFunction · 0.72