jscodeshift icon indicating copy to clipboard operation
jscodeshift copied to clipboard

Incorrect behavior after comment removal

Open ameyms opened this issue 2 years ago • 0 comments

See this example.

I am trying to remove some comments which can appear before or after object properties. And I want to preserve the location (leading/trailing) of other comments. However, after running the codemod, the comments that should be untouched are rearranged.

Code:


const opts = {
  // Good comment
  // REMOVE_ME
  banner: true,
  // REMOVE_ME
  // Nice
};

CodeMod:

export default function transformer(file, api, { buildFlag1 }) {
  const j = api.jscodeshift;
  return j(file.source)
    .find(j.Comment)
    .filter((path) => path.value.value.trim() === "REMOVE_ME")
    .forEach((path) => {
      console.log(path.node);
      path.prune();
    })
    .toSource();
}

module.exports.parser = "flow";

Expected


const opts = {
  // Good comment
  banner: true,
  // Nice
};

Actual


const opts = {
  // Good comment
  // Nice
  banner: true,
};

🐞 Is this a bug in jscodeshift?

ameyms avatar Aug 10 '21 22:08 ameyms