question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Closing parenthesis of conditional spread removed erroneously

See original GitHub issue

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})
};

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kpsuperplanecommented, Sep 10, 2018

@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.

1reaction
rubennortecommented, Jun 28, 2018
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found