Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing cursor position/selection range to custom codemod that operates on entire file #7

Open
jedwards1211 opened this issue Jan 16, 2020 · 1 comment

Comments

@jedwards1211
Copy link

jedwards1211 commented Jan 16, 2020

I'm in the process of switching from Atom to VSCode. I had a bunch of custom codemods at my disposal in Atom that I'm planning to get working in VSCode, for instance: https://github.com/jedwards1211/material-ui-codemorphs

For convenience, my addStyles codemod in that package doesn't require the user to select the component to be modified; instead it can just determine which component contains the cursor position by using my https://github.com/jedwards1211/jscodeshift-paths-in-range package.

Furthermore, it wouldn't be sufficient for addStyles to transform only the selected code, because it also has to add import statements to the top of the file, and potentially rename some identifiers. But it has to know the cursor position/selection to determine which component to modify.

My own codemods for adding block statements to arrow functions also worked this way; it's a lot less hassle to just put the cursor inside the function than selecting the whole thing.

I was planning on making an extension for running codemods, then I found your extension. This wouldn't currently satisfy my needs, but I figured I would talk to you about this before making my own extension. Basically it would have to pass the selection range (or cursor position) to the transform via the options argument. This will make it possible to do more complex transforms in the future than just operating on selected code.

@jedwards1211
Copy link
Author

jedwards1211 commented Jan 16, 2020

Also btw, I was thinking it would be more convenient to be able to map codemods to top-level VSCode commands (and even keyboard shortcuts), rather than having to select the codemod after running the "Run codemod on selection" command.

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

No branches or pull requests

1 participant