Open jefflloyd opened 4 years ago
I'm seeing another issue with ObjectPattern
where a newline is inserted when I don't expect it. I don't know if it's related to this issue.
I'm also running into this issue on ObjectExpression
. I've tried both calling remove()
directly on a property reference and filtering directly the properties
object on the node. It seems to happen when a key's value contains a newline, such as a multiline object definition.
This AST Explorer demonstrates the issue.
({
key1: 'value',
key2: 'value',
key3: {
key: 1
},
key4: {
key: 2
},
})
export default function transform(file, api) {
const j = api.jscodeshift;
const { source } = file;
const root = j(source);
const obj = root.find(j.ObjectExpression).get(0).parentPath.value;
obj.properties = obj.properties.filter((_, i) => i % 2)
return root.toSource();
}
(({
key2: 'value',
key4: {
key: 2
}
}))
(Notice that newline appearing above).
Hi, all. I'm trying to run jscodeshift to modify Properties of an ObjectExpression. The code I'm running (and the files I'm running it on) are more complex, but here is a simple example to illustrate the problem I'm seeing. Given an input file like:
And a transform:
I would expect the resulting file to look like:
But instead, this is the result:
The transformation was successful (the "zero" property was added as expected), but the newline between properties "two" and "three" was removed. The newlines are not always just collapsed; with more complicated inputs, newlines are sometimes added between properties that didn't have one before, too. This is important to us, as our developers have separated various properties into groups for ease of development, and the tool is unfortunately messing with those groups and making the diff hard to understand. Ideally, we would expect that whitespace in unaffected areas of the code would be preserved.
Thanks for taking the time to look at this! If you need any additional information, please let me know.