codemods icon indicating copy to clipboard operation
codemods copied to clipboard

give option to avoid reformatting code or at least respect prettier rc in project root

Open emahuni opened this issue 2 years ago • 6 comments

There are coding conventions that teams follow that this mod is assuming as THE standard. However, it should be to the engineer what to transform. So give them the option to disable reformatting of code by both disabling prettier completely and/or using prettier .prettierrc.js file... in project root.

I had to fork and comment out the line that formats the code.

emahuni avatar Aug 28 '22 08:08 emahuni

Can you be specific about which part you are referring to and what you had to comment out?

derrickmehaffy avatar Aug 29 '22 16:08 derrickmehaffy

So sorry for being so vague, I modified these 2 lines with as minimum impact as possible (though this makes the formatFile function redundant, I didn't want to break anything and then fix it): https://github.com/strapi/codemods/blob/89429aa99a4ebffad35196dc4dfb1dc52905ee61/lib/global/utils/format-code.js#L11

so from this:

const formatFile = async (path) => {
  try {
    const fileContent = await readFile(path, 'utf-8');
    // Format the code with prettier
    return writeFile(
      path, prettier.format(fileContent, {
         filepath: path,
       })
    );
  } catch (error) {
    logger.warn(`Failed to format code, check ${path}`);
  }
};

to this:

const formatFile = async (path) => {
  try {
    const fileContent = await readFile(path, 'utf-8');
    // Format the code with prettier
    return writeFile(
      path, fileContent
//       prettier.format(fileContent, {
//         filepath: path,
//       })
    );
  } catch (error) {
    logger.warn(`Failed to format code, check ${path}`);
  }
};

This was causing a lot of undesired results. We tried to use an rc file and everything and noticed that it's not respecting any of that.

emahuni avatar Aug 30 '22 12:08 emahuni

Good point. I'm starting to think we should probably just remove the formatting and leave that up to the end user. I think the format function is only called in one spot so the clean up should be pretty quick.

markkaylor avatar Aug 30 '22 16:08 markkaylor

Good point. I'm starting to think we should probably just remove the formatting and leave that up to the end user. I think the format function is only called in one spot so the clean up should be pretty quick.

@emahuni are you willing to submit a PR to remove the code formatting? If so I can review it and merge it in as I agree with Mark here that we should just get rid of it.

derrickmehaffy avatar Sep 01 '22 16:09 derrickmehaffy

I have created the pull request

emahuni avatar Sep 02 '22 05:09 emahuni

I didn't remove the prettierrc file, not sure what it is used for, since any edits didn't seem to affect the code formatting I was after. You are free to remove any changes to it or it altogether if you understand why it's there and if it was supposed to work in tandem with the code formatting we are removing.

emahuni avatar Sep 02 '22 05:09 emahuni