Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recast is adding extra newlines in ObjectExpressions #242

Open
kumarharsh opened this issue Dec 27, 2015 · 8 comments
Open

Recast is adding extra newlines in ObjectExpressions #242

kumarharsh opened this issue Dec 27, 2015 · 8 comments

Comments

@kumarharsh
Copy link

Refer to the following fiddle: http://astexplorer.net/#/27xKOBJJGN

When I have an object with a structure such as this:

const styles = {
  base: {
    height: '100%',
  },
  ribbon: {
    backgroundColor: 'black',
  },
};

and I do anything with the body of the ObjectExpression like this:

export default function(file, api) {
  const j = api.jscodeshift;
  window.j = j;
  const { statement, statements } = j.template;
  return j(file.source)
    .find(j.VariableDeclarator)
    .replaceWith(p => {
        p.node.init = j.objectExpression(p.node.init.properties);
        return p.node;
    })
    .toSource();
};

then the following output is gotten:

const styles = {
  base: {
    height: '100%',
  },

  ribbon: {
    backgroundColor: 'black',
  }
};

see that extra newline after base and before ribbon. These lines are added after every property, which is adding unnecessary changes to my codebase. Is there a way to suppress that? or is that a bug?

@kumarharsh
Copy link
Author

Thinking about this a little, I'm inclined to believe that this is also related to #240. Essentially, the printer is forcefully adding one extra newline after each property in an objectExpression. So, if there is none, then there will be one. If there's one, then there will be two. But if there's two or more, then nothing happens.

@kumarharsh
Copy link
Author

This comes from

recast/lib/printer.js

Lines 624 to 627 in 827ae9f

// Add an extra line break if the previous object property
// had a multi-line value.
parts.push(separator + (multiLine ? "\n\n" : "\n"));
allowBreak = !multiLine;

From what I've seen, the practice of writing code as I've given in the first code snippet above is quite common and, at risk of sounding too narrow-minded, no one writes an extra newline in their code separating the properties of an objectExpression, not even Facebook.

@petrfelzmann
Copy link

#353

@pksjce
Copy link

pksjce commented Jan 10, 2017

+1
I faced the same issue! My PR linked above is not an informed fix but it fixes this particular issue while not breaking any test cases. I also wrote a failing test case for this and made it oss as per of that PR

@nickretallack
Copy link

I found a ton of examples of cases where this happens. I originally filed a bug here: zxbodya/flowts#11

It seems any property that spans multiple lines causes extra newlines, even if it's just because it has a comment.

@danielo515
Copy link

danielo515 commented Jun 12, 2021

It is happening something similar to me. This code, for example:

  const shortProperty = (j, identifier) => {
    return { ...j.objectProperty(identifier, identifier), shorthand: true };
  };


export default function (file, api) {
  const j = api.jscodeshift;

  const call = j.variableDeclaration("const", [
      j.variableDeclarator(
        j.objectPattern([shortProperty(j,j.identifier("__"))]),
        j.callExpression(j.identifier("usePulyglot"), [
          j.identifier("phrases")
        ])
      )
    ])
  
    return j(call).toSource();
}

Produces this output:

const {
  __
} = usePulyglot(phrases); = 99

But I expected this instead:

const { __ } = usePulyglot(phrases); = 99

Here is a playground:

https://astexplorer.net/#/gist/eeee224efd6c389deb8a82a338e4fbe7/latest

@villesau
Copy link
Contributor

Made a fix for this: #1262

@yhancsx
Copy link

yhancsx commented Feb 17, 2023

I have similar problem

image

https://astexplorer.net/#/gist/eeee224efd6c389deb8a82a338e4fbe7/af669315e6eead72e30a403b05298edd685fd864

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants