Skip to main content
Experimental. This can change in a minor release of the CLI, but not in a patch. Pin the CLI version (npx codemod@<version>) if you build on it.
A text search for a function’s name also finds other functions with the same name. With semantic analysis, each use of a name can tell you where it was declared, through imports, barrel files (index.ts files that re-export) and tsconfig path aliases. You’ll rename the shared toMoment to toDayjs everywhere it is used, and leave a different toMoment alone. You need Node.js 22.18 or later and Git.

1. The project

A small monorepo. toMoment is declared in packages/shared/src/dates.ts and re-exported by the barrel packages/shared/src/index.ts. api imports it through a relative path to the barrel, web through the @shared alias. web/src/legacy.ts has its own toMoment, which must stay.
packages/shared/src/dates.ts
packages/shared/src/index.ts
packages/shared/src/format.ts
packages/api/src/orders.ts
packages/web/src/app.ts
packages/web/src/legacy.ts
tsconfig.json

2. The workflow

workflow.ts
  • semanticAnalysis: "workspace" lets definition() look into other files of the project. The default, "file", only sees the file itself.
  • containing: "toMoment" skips files that don’t mention the name, so only those files are parsed.
  • node.definition() returns where a name is declared: def.root is that file and def.node the declaration. It follows re-exports, export * and path aliases to any depth.
  • A use of an imported name resolves to its import first (kind: "local", an import_specifier); calling definition() on that follows the import to the exported function.
  • The callback renames a name only when it leads to dates.ts, and logs where each one leads.

3. Dry run

Commit the project, then preview:
Every use of the shared function leads to dates.ts:3, through the barrel and the alias. Both names in legacy.ts lead to legacy.ts:2, so they stay. Files run in parallel, so the log lines can come in another order.

4. Apply it

Next

  • Semantic analysis: definition(), references() and the analysis modes.
  • To find every use from the declaration instead, call references() on the declared name.