Closing parenthesis of conditional spread removed erroneously
See original GitHub issueI’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})
};
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Conditional Object Properties Using Spread in JavaScript
The line 2 expression omits a name from being applied if the condition fails. This works because the spread operator ignores values without...
Read more >khan academy programming unit test Flashcards - Quizlet
This program uses a conditional to predict the hair type of a baby. IF (fatherAllele = "C" AND motherAllele = "C") { hairType...
Read more >Styling multi-line conditions in 'if' statements? [closed]
You don't need to use 4 spaces on your second conditional line. Maybe use: if (cond1 == 'val1' and cond2 == 'val2' and...
Read more >PHP Coding Standards - WordPress Developer Resources
Put spaces on both sides of the opening and closing parentheses of control structure ... not (real) which is deprecated in PHP 7.4,...
Read more >Linter rules - Dart programming language
The rule will be removed in a future Linter release. avoid_bool_literals_in_conditional_expressions. Avoid bool literals in conditional expressions. This rule ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@fkling @dunnbobcat I’ve reproduced this issue and it only seems to happen with the
flow
parser. Running jscodeshift with the vanilla parser works fine.You can reproduce the issue with AST explorer: https://astexplorer.net/#/gist/8269fb593017340ed9486d0c57a0a3ff/f30217f92bb6d197ef0bbfdb7eb70c006d9eca6a