I am using jscodeshift to transform function calls:
foo() --> foo({uid: ... label: ...})
const newArgObj = j.objectExpression([
j.property(
'init',
j.identifier('uid'),
j.literal(getUID()),
),
j.property(
'init',
j.identifier('label'),
j.literal('bar'),
)
]);
node.arguments = [newArgObj];
...
return callExpressions.toSource({quote: 'single'});
The problem is objectExpression is always pretty-printed:
foo({
uid: 'LBL_btBeZETZ',
label: 'bar'
})
How to prevent that and get something like:
foo({uid: 'LBL_btBeZETZ', label: 'bar'})
ok, it is impossible, take a look at recast's printer.js source:
case "ObjectExpression":
case "ObjectPattern":
case "ObjectTypeAnnotation":
...
var len = 0;
fields.forEach(function(field) {
len += n[field].length;
});
var oneLine = (isTypeAnnotation && len === 1) || len === 0;
var parts = [oneLine ? "{" : "{\n"];
...
if (!oneLine) {
lines = lines.indent(options.tabWidth);
}
...
parts.push(oneLine ? "}" : "\n}");
WORKAROUND (at least in my simple case) - you can use raw string:
const rawCode = `{uid: '${getUID()}', label: 'bar' }`;
node.arguments = [rawCode];
or
node.arguments = [j.jsxText(rawCode)];
Both will give you:
foo({uid: 'LBL_btBeZETZ', label: "bar" })