Skip to content

refactor: use @jssg/utils for common codemod transformations - #187

Merged
schultzp2020 merged 1 commit into
mainfrom
kanban/refactor-the-codemods-to-use-jssg-utils-ff7626
Nov 3, 2026
Merged

schultzp2020 merged 1 commit into
mainfrom
kanban/refactor-the-codemods-to-use-jssg-utils-ff7626

Conversation

@schultzp2020

Copy link
Copy Markdown
Contributor

Summary

Every common AST transformation the codemods hand-rolled — import add/remove/rename/resolve, import binding resolution, reference renaming, indent detection — now uses the shared helpers from @jssg/utils (javascript/imports, plus the new javascript/bindings and javascript/style modules), with all recipe packages standardized on ^0.0.11.

 codemods/<recipe>/scripts/<name>.ts
-  findImportStatementsFrom(rootNode, source)      // ~150 duplicated lines per script
-  getDefaultImportName(imp)
-  getNamedImportLocalName(imp, name)
-  pruneBarrelImportSpecifiers(imp, names, edits)
-  collectXImports(rootNode) / addBuiImport(...)
+  getImport / getAllImports / listImports / addImport / removeImport / updateImport
+  importBindingOf / resolveDefinition / renameReferences
+  getLineIndent
  • 41 scripts across nine recipe packages migrated (all 23 import-touching mui4→BUI scripts, v1.45/47/48/50/51/52/54/55), net −3,925 lines.
  • Kept hand-rolled where no util models the behavior, each with a reason: in-place import moves with re-export handling (move-connections-backend-imports), collision-aware rename orchestration (rename-connections-public-api — it does adopt renameReferences for the reference rewrite), single-specifier expansion (migrate-collapsible-to-accordion), mid-line indent math (migrate-policy-query-user), and namespace member renames (rename-header-to-plugin-header).
  • Carries a type-only yarn patch (.yarn/patches/, registered in package.json resolutions.patchedDependencies) for two TS7 assertion failures in @jssg/utils@0.0.11's raw .ts source; documented in CONTRIBUTING alongside a new "Prefer shared codemod utils" section (check the utils first, bump deps for new helpers, bespoke logic only with justification).
  • Fixes found on the way: the v1.54 typed-extension fixture type-check was broken on main (it imports @backstage/frontend-plugin-api and zod, which were never declared — now devDependencies of that recipe), and a latent wrong-node lookup in the mui4 styles codemod (uncovered by the new util types, not hit by goldens).

Evidence

  • Before: 48 scripts with hand-rolled import manipulation; the v1.54 fixture tsc stage failed on pristine main (verified by stashing the branch changes and re-running).
  • After: yarn test — exit 0, 92 jssg fixture suites plus all workflow golden and node --test suites; yarn lint and yarn format:check — clean; the four v1.55 fixture tsc stages pass with the patch applied. 8 expected.tsx fixtures updated, each formatting-only and individually reviewed: the utils remove an import statement together with its trailing newline (the old code left a blank line) and append merged specifiers in source order instead of re-sorting them.

Merge Danger

Door: two-way.

Blast Radius: codemod internals only — no workflow, manifest, or public API changes; output differences are limited to the formatting-only fixture diffs above. Publishing is covered by a changeset (patch bumps for the ten affected recipes).

How to test changes / Special notes for the reviewer

yarn test && yarn lint && yarn format:check. Suggested review order: the five exemplar scripts in codemods/misc/mui4-to-bui-migration-recipe/scripts/ (button, avatar, skeleton, alert, chip) establish the pattern — the rest are mechanical applications of it; then CONTRIBUTING.md, the changeset, and the two-line patch file. Reviewer focus worth spending: the fixture diffs under tests/*/merge-existing-bui/ and the partially-migrated scripts listed above.

Replace hand-rolled import manipulation (add/remove/rename/resolve,
binding resolution, reference renaming, indent detection) in 41 codemod
scripts across nine recipe packages with the shared helpers from
@jssg/utils/javascript/{imports,bindings,style}, standardizing every
recipe on ^0.0.11. Behavior-preserving: all fixture suites, metrics, and
workflow goldens pass; the only fixture diffs are formatting-only.

Logic with no util equivalent stays hand-rolled (in-place import moves
with re-exports, collision-aware rename orchestration, single-specifier
expansion, namespace member renames). CONTRIBUTING gains a 'Prefer
shared codemod utils' section.

Also fixes the pre-existing v1.54 typed-extension fixture type-check
(the packages it imports are now declared devDependencies) and a latent
node lookup bug in the mui4 styles codemod. Carries a type-only yarn
patch for two TS7 assertion failures in @jssg/utils 0.0.11's raw .ts
source, to be dropped once upstream ships compiled declarations.
@schultzp2020
schultzp2020 merged commit 3e0b443 into main Nov 3, 2026
2 checks passed
@schultzp2020
schultzp2020 deleted the kanban/refactor-the-codemods-to-use-jssg-utils-ff7626 branch November 4, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant