| 1109 | |
| 1110 | // each unit's declaration, then each creation's, unless a name is declared twice |
| 1111 | const unitTextsOf = ( |
| 1112 | units: UnitModule[], |
| 1113 | creations: Creation[] |
| 1114 | ): UnitTexts | undefined => { |
| 1115 | const specifiersByName = new Map<string, string>() |
| 1116 | const texts: string[] = [] |
| 1117 | for (const unit of units) { |
| 1118 | for (const declaration of unit.declarations) { |
| 1119 | texts.push(unit.module.source.slice(declaration.start, declaration.end)) |
| 1120 | specifiersByName.set(declaredNameOf(declaration), unit.specifier) |
| 1121 | } |
| 1122 | } |
| 1123 | const created = new Set<string>() |
| 1124 | const creators = new Set<string>() |
| 1125 | for (const { name, creator, shared, args } of creations) { |
| 1126 | if (specifiersByName.has(name) || created.has(name)) return |
| 1127 | created.add(name) |
| 1128 | if (shared && specifiersByName.has(shared)) return |
| 1129 | if (shared && !creators.has(shared)) { |
| 1130 | creators.add(shared) |
| 1131 | texts.push(`const ${shared} = (${creator})`) |
| 1132 | } |
| 1133 | texts.push( |
| 1134 | `const ${name} = ${shared ?? `(${creator})`}(${args.join(", ")})` |
| 1135 | ) |
| 1136 | } |
| 1137 | return { texts, specifiersByName, created } |
| 1138 | } |
| 1139 | |
| 1140 | const writeFlat = ( |
| 1141 | units: UnitModule[], |