facebook / facebook/jscodeshift
[Feature proposal] Allow reporting arbitrary data from codemods
- Dominant language
- JavaScript
- Stars
- 10k
- Forks
- 498
- PR merge metrics
- No merged PRs in 30d
Description
I'm programmatically invoking `jscodeshift` to perform some codemods; this is part of a process that will be invoked across a large number of repos to automatically make changes, commit them, and open corresponding PRs. As part of this process, I'd like to be able to identify files that might need additional human attention before the PR is merged. I know how to use `jscodeshift` to do actually identify such files; the issue comes with reporting and aggregating this information across all files on all workers.
## Proposal
This is a very rough idea of what this API might look like, but it captures the gist of what I want to do. Names of functions, properties, etc. can of course be changed.
I propose adding a new function to the `api` object; let's call it `data`. This will take an arbitrary value:
```js
module.exports = (fileInfo, api) => {
...
if (condition) {
api.data({
lineNumber: 123,
reason: 'could_not_convert',
})
}
}
```
Using the existing IPC method, the Worker could then notify the Runner that this codemod has added data. This can then be associated with the current file and be stored while `jscodeshift` is running, and be included as part of the object that the `run` function ultimately resolves to:
```js
{
stats: ...,
timeElapsed: ...
data: [{
'file': 'myfile.txt',
'data': {
lineNumber: 123,
msg: 'could_not_convert',
}]
}
```
It would then be possible to aggregate this data after the codemod has completed so that this could be displayed cleanly to the user in a PR:
```js
const results = await Runner.run(...);
const couldNotConvert = results.data.reduce((acc, curr) => {
if (curr.data.msg === 'could_not_convert') {
acc.push(curr.file);
}
);
console.log('The following files require human attention:');
couldNotConvert.map(console.log);
```
I'm willing to work on this feature if it would be accepted by the maintainers. This should be fully backwards-compatible, as it's only adding additional functions/properties to existing objects.
Contributor guide
Assessment
This issue has not been assessed yet.