Skip to main content
A transform can return data along with its edit. The data can be any JSON value, and the workflow gets every file’s data back to analyze however it likes. This is useful for:
  • Codebase analysis: find patterns, prop usages, component instances
  • Migration tracking: record what your codemod rewrote, grouped by the shape of the change
  • Read-only codemods: gather insights without modifying code

When to return data

For standalone JSSG tests, the data is snapshotted in data.json next to the fixtures (see Testing).

Basic usage

Return { edit, data } instead of a string:
  • edit: the new file content, or null/missing to leave the file unchanged. Returning a plain string or null still works.
  • data: any JSON value, including null. To skip a file’s data, leave data out or set it to undefined.
  • Data that isn’t JSON (a function, a BigInt) is an error, and so are other keys on the result, so a typo doesn’t go unnoticed.

In a workflow

In workflow.ts, per-file logic is a forEachFile callback, and what it returns is collected into the step’s outputs, in path order, leaving out files that returned nothing:
A jssg transform’s { edit, data } is what codemod jssg run and codemod jssg test report, and what workflow.yaml’s js-ast-grep steps collect into their outputs.

Testing

codemod jssg test snapshots the data next to input and expected as data.json, in the same [{ file, data }] shape. A missing data.json is written on the first run; --update-snapshots rewrites a changed one.
In Codemod 2.0, useMetricAtom from codemod/metrics and the CLI’s metric totals were removed. Return what you counted and add it up in the workflow.