MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / findConstructorCalls

Function findConstructorCalls

scripts/migrate-schema-class.ts:219–245  ·  view source on GitHub ↗
(
  source: ts.SourceFile,
  migrated: ReadonlySet<string>,
  aliases: ReadonlyMap<string, string>,
)

Source from the content-addressed store, hash-verified

217 * origin) is in the migrated set. Rewrites to `<LocalName>.make(...)` so
218 * the local binding stays consistent. */
219const findConstructorCalls = (
220 source: ts.SourceFile,
221 migrated: ReadonlySet<string>,
222 aliases: ReadonlyMap<string, string>,
223): Edit[] => {
224 const edits: Edit[] = [];
225 const visit = (node: ts.Node) => {
226 if (ts.isNewExpression(node) && ts.isIdentifier(node.expression)) {
227 const localName = node.expression.text;
228 const importedName = aliases.get(localName) ?? localName;
229 if (migrated.has(importedName)) {
230 const startOfNew = node.getStart(source);
231 const expressionEnd = node.expression.getEnd();
232 // Replace `new ClassName` with `ClassName.make` using the local
233 // binding name so the rewrite respects `import { X as Y }`.
234 edits.push({
235 start: startOfNew,
236 end: expressionEnd,
237 replacement: `${localName}.make`,
238 });
239 }
240 }
241 ts.forEachChild(node, visit);
242 };
243 visit(source);
244 return edits;
245};
246
247const applyEdits = (text: string, edits: Edit[]): string => {
248 // Apply in reverse order so positions stay valid.

Callers 1

Calls 1

visitFunction · 0.70

Tested by

no test coverage detected