facebook / facebook/jscodeshift
Closing parenthesis of conditional spread removed erroneously
- Dominant language
- JavaScript
- Stars
- 10k
- Forks
- 498
- PR merge metrics
- No merged PRs in 30d
Description
I've got a transform to remove empty object spreads like `...{}`:
```
module.exports = {
default: function(file, api, options) {
const j = api.jscodeshift;
const ast = j(file.source);
ast
.find(j.SpreadProperty, {argument: {type: 'ObjectExpression'}})
.filter(p => p.node.argument.properties.length === 0)
.remove();
return ast.toSource();
},
parser: 'flow',
};
```
I'm running it against the following code:
```
let cond = false;
let obj = {
...(cond ? {x: 4} : {y: 5}),
...{},
};
```
The result of the transform is as follows:
```
let cond = false;
let obj = {
...(cond ? {x: 4} : {y: 5}
};
```
You can see that the closing parenthesis of the conditional spread was removed, resulting in broken syntax. I get the same result if I move the empty object spread above the conditional spread.
I attempted to reproduce the issue in `recast` (0.15.0) directly:
```
const recast = require('recast');
const flow = require('recast/parsers/flow');
const builders = recast.types.builders;
recast.run(
function(ast, callback) {
recast.visit(ast, {
visitSpreadElement: function(path) {
const arg = path.node.argument;
if (arg.type === 'ObjectExpression' && arg.properties.length === 0) {
path.prune();
return false;
}
this.traverse(path);
},
});
callback(ast);
},
{
parser: flow
}
);
```
But its output is correct:
```
let cond = false;
let obj = {
...(cond ? {x: 4} : {y: 5})
};
```
Contributor guide
Assessment
This issue has not been assessed yet.