codemod icon indicating copy to clipboard operation
codemod copied to clipboard

Is there a way to disable rewriting a file?

Open kumarharsh opened this issue 5 years ago • 7 comments

I'm using codemods to do searches across files for complicated things which a regular grep can't fulfill (such as: does any file have a component <XYZ> which has these two particular attributes). When my codemod finds files matching the said properties, I output the list to another file. I don't mutate the AST in any way (these search transforms are just a bunch of if-else checks and a console.error() to write the filename to another file).

One issue while doing this is that codemod modifies all the files which it touches. It forcefully transforms line endings, adds or removes some braces here and there, etc. Then I have to do a git checkout -- . always after running the codemod.

Is there a way to disable the codemod from writing the file it touches? I'd like it to just read the file, run the non-mutating transform and just let the file be.

kumarharsh avatar Dec 26 '18 14:12 kumarharsh

Have you tried it with --dry-run?

eventualbuddha avatar Dec 27 '18 01:12 eventualbuddha

That's it! Perfect. I couldn't find anything in the readme for it, but it's there in the cli tool. The flag is called --dry. Thanks 👍

kumarharsh avatar Dec 27 '18 08:12 kumarharsh

Actually, on further thought, I still think there is a significant use-case for not touching unmodified files. Consider that my codemod changes only some files, but the codemod would still overwrite every single file it reads. So the developer will still have to spend additional time to figure out which files to commit, and which ones to checkout.

Would it be possible to add some functionality for the codemod cli to figure out when to overwrite files? For example, if the visitor function explicitly returns false, then don't modify the file.

kumarharsh avatar Dec 27 '18 11:12 kumarharsh

So you have a codemod which doesn't alter the AST, but the files are still being modified? I've seen things like that and they're typically bugs in recast. Can you provide a couple examples of the unwanted diffs?

eventualbuddha avatar Dec 27 '18 15:12 eventualbuddha

Sure, check out this in ASTExplorer: https://astexplorer.net/#/gist/63a503b3b38c148a06d6438bad35af65/e81a51e13fc87be488af7801bb441351e12f7f91

kumarharsh avatar Dec 28 '18 08:12 kumarharsh

This seems to happen even with something as small as this:

class A {
  a = {};
}

which becomes this:

class A {
  a = {};}

Something about reprinting class properties is probably broken in recast. I'll take a look later if I get time (cc @benjamn).

eventualbuddha avatar Jan 03 '19 23:01 eventualbuddha

I have had similar problems with recast for quite a long time. There are also issues like the LF -> CRLF conversion which recast can't do much about even if it worked well.

I was thinking whether it'd be possible to have a flag which doesn't even call recast on the code, so the file won't even be touched.

kumarharsh avatar Jan 04 '19 09:01 kumarharsh

Closing since, though this remains an issue, the cause of it is not in codemod.

eventualbuddha avatar Dec 24 '22 14:12 eventualbuddha