I've been writing some transforms to help me refactor the code base. What I'm doing is basically adding a new property to some object expressions. Why does jscodeshift/recast
remove trailing commas from those objects? How can I prevent that?
Recast has a trailing comma option:
// If you want to print trailing commas in object literals,
// array expressions, functions calls and function definitions pass true
// for this option.
trailingComma: false,
You can pass recast printing options to jscodeshift's to toSource
method as explained here:
return x.toSource({trailingComma: true});
Now that the issue in comments below has been merged, you can also use it this way for more granularity:
trailingComma: {
objects: true,
arrays: true,
functions: false,
}
Why does jscodeshift/recast remove trailing commas from those objects?
Recast cannot remove something that doesn't exist. Recast operates on the AST of the code. Punctuators such as the comma to separate properties doesn't exist in the AST.