You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to add a way for a human to have input on a codemod. This expands the range of what codemods can do.
For instance, consider the shift from CJS to ESM:
functionA(){}functionB(){}functionC(){}module.exports={ A, B, C};// Option 1: The three values are standaloneexportfunctionA(){}exportfunctionB(){}exportfunctionC(){}// Option 2: The three values are a single unitconst_export={A, B, C}exportdefault_export;
Determining which of the two options are preferable would be programmatically prohibitively difficult, if not impossible. But I don't want to fall back to having to handle these cases entirely by hand. If the codemod knows which of the two cases it is, it can still do the rest of the work for me.
My dream is that this is a low-friction task where the user can quickly crank through a bunch of files, when questions are easy for humans to answer but hard for machines.
My suggested API is:
asyncfunctiontransformer(file,api){// ...awaitapi.prompt(node,{type: 'multiselect',name: 'namedExports',message: 'Choose the exports that should be converted to named exports.',choices: exportNames});}
api.prompt is a wrapper around prompts. It applies some codemod-specific logic, like a syntax highlighted snippet of the relevant node.
Longer demo:
I have a branch that produces a demo for this. I created it in a single day, so it's very proof-of-concept. If this fits with your vision of jscodeshift, I'll work to get it into a mergeable state, including exhaustive tests and docs.
This could become very complicated. I'd like to keep it as simple as possible and start small. Perhaps it would be labeled as an experimental API at first.
I have a separate PR to prompts that would make this better.
prompts relies on nothing else being written to the terminal. My branch has some work to ensure that only one part of the code writes to the terminal at once, but it only works with --run-in-band.
Features I'd like to add that are not in the demo:
Undo: choose a different answer for a question you've already answered
Support multiple questions per file
Support for more than Node v13
The demo codemod I have is both too simplistic and also produces incorrect code. 😄
The text was updated successfully, but these errors were encountered:
I'd like to add a way for a human to have input on a codemod. This expands the range of what codemods can do.
For instance, consider the shift from CJS to ESM:
Determining which of the two options are preferable would be programmatically prohibitively difficult, if not impossible. But I don't want to fall back to having to handle these cases entirely by hand. If the codemod knows which of the two cases it is, it can still do the rest of the work for me.
My dream is that this is a low-friction task where the user can quickly crank through a bunch of files, when questions are easy for humans to answer but hard for machines.
My suggested API is:
api.prompt
is a wrapper aroundprompts
. It applies some codemod-specific logic, like a syntax highlighted snippet of the relevantnode
.Longer demo:
I have a branch that produces a demo for this. I created it in a single day, so it's very proof-of-concept. If this fits with your vision of jscodeshift, I'll work to get it into a mergeable state, including exhaustive tests and docs.
Implementation Notes
prompts
that would make this better.prompts
relies on nothing else being written to the terminal. My branch has some work to ensure that only one part of the code writes to the terminal at once, but it only works with--run-in-band
.The text was updated successfully, but these errors were encountered: