facebook / facebook/jscodeshift

Newlines being unexpectedly removed / reordered

Open
#364 2 comments 3 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
10k
Forks
498
PR merge metrics
No merged PRs in 30d

Description

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:

```
export default {
one: "1",
two: "2",

three: "3"
}
```

And a transform:

```
module.exports = function(fileInfo, api) {
const j = api.jscodeshift;

return api.jscodeshift(fileInfo.source)
.find(j.ObjectExpression)
.forEach(path => {
const properties = path.get('properties');

properties.insertAt(
0,
j.property('init', j.identifier("zero"), j.literal("0"))
);
})
.toSource();
}
```

I would expect the resulting file to look like:

```
export default {
zero: "0",
one: "1",
two: "2",

three: "3"
};
```

But instead, this is the result:

```
export default {
zero: "0",
one: "1",
two: "2",
three: "3"
};
```

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.