( units: UnitModule[], creations: Creation[] )
| 1138 | } |
| 1139 | |
| 1140 | const writeFlat = ( |
| 1141 | units: UnitModule[], |
| 1142 | creations: Creation[] |
| 1143 | ): Flat | undefined => { |
| 1144 | const unitTexts = unitTextsOf(units, creations) |
| 1145 | if (!unitTexts) return |
| 1146 | const { texts, specifiersByName, created } = unitTexts |
| 1147 | const inlined = withTemplatesInlined(texts.join("\n")) |
| 1148 | if (!inlined) return |
| 1149 | const { code } = inlined |
| 1150 | // an inlined template is no longer the function's to return |
| 1151 | for (const name of inlined.names) specifiersByName.delete(name) |
| 1152 | const { program, errors } = parseUnits(code) |
| 1153 | if (errors.length) return |
| 1154 | const scopes = new ModuleScopes(program) |
| 1155 | const top = scopes.root.bindings |
| 1156 | const templates = new Map<string, number>() |
| 1157 | const declarations = (program.body as Node[]).map(statement => |
| 1158 | declarationOf(statement, scopes, templates, created) |
| 1159 | ) |
| 1160 | const factories = declarations.filter( |
| 1161 | (d): d is Factory => d.kind === "factory" |
| 1162 | ) |
| 1163 | if (!factories.length) return |
| 1164 | const names = new Set<string>() |
| 1165 | const globals = new Set<string>() |
| 1166 | const bindingsNamed = new Map<string, Binding[]>() |
| 1167 | const bindingsIn = new Map<Node, Binding[]>() |
| 1168 | for (const binding of new Set(scopes.bindingsById.values())) { |
| 1169 | names.add(binding.name) |
| 1170 | pushTo(bindingsNamed, binding.name, binding) |
| 1171 | pushTo(bindingsIn, binding.scope.node, binding) |
| 1172 | } |
| 1173 | for (const reference of scopes.references) { |
| 1174 | if (reference.binding) continue |
| 1175 | names.add(reference.id.name) |
| 1176 | globals.add(reference.id.name) |
| 1177 | } |
| 1178 | const values = new Map<string, string>() |
| 1179 | const holders = new Map<string, string>() |
| 1180 | const fresh = (base: string) => { |
| 1181 | let name = base |
| 1182 | for (let i = 1; names.has(name) || values.has(name); i++) |
| 1183 | name = `${base}$${i}` |
| 1184 | names.add(name) |
| 1185 | return name |
| 1186 | } |
| 1187 | const isFree = (name: string) => |
| 1188 | !values.has(name) && !(name in top) && !globals.has(name) |
| 1189 | // what a factory's parameter reads of the unit it's passed |
| 1190 | const memberReads = new Map<Binding, string>() |
| 1191 | for (const d of factories) { |
| 1192 | for (const [i, param] of d.fn.params.entries()) { |
| 1193 | if (param.type !== "ObjectPattern") continue |
| 1194 | const unit = (d.call.arguments[i] as { name: string }).name |
| 1195 | for (const p of param.properties as never as Member[]) { |
| 1196 | memberReads.set( |
| 1197 | scopes.bindingsById.get(p.value)!, |
no test coverage detected