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

Hug elements inside of JSXExpressionContainer #213

Merged
merged 1 commit into from Jan 19, 2017

Conversation

vjeux
Copy link
Contributor

@vjeux vjeux commented Jan 15, 2017

Fixes #197
Fixes #196

@@ -1098,6 +1098,14 @@ function genericPrintNoParens(path, options, print) {
case "JSXSpreadAttribute":
return concat([ "{...", path.call(print, "argument"), "}" ]);
case "JSXExpressionContainer":
const shouldIndent =
n.expression.type !== 'ArrayExpression' &&
n.expression.type !== 'ObjectExpression';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll probably want to add more things here over time

@vjeux vjeux force-pushed the hug_jsx_expression branch 2 times, most recently from 03ae06b to 0438805 Compare January 15, 2017 03:25

<Something>
{items.map(
item => (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one will be fixed by #211

@vjeux vjeux changed the title Hug objects and arrays inside of JSXExpressionContainer Hug objects, arrays and functions inside of JSXExpressionContainer Jan 15, 2017
@vjeux vjeux changed the title Hug objects, arrays and functions inside of JSXExpressionContainer Hug elements inside of JSXExpressionContainer Jan 15, 2017

if (!shouldIndent) {
return concat(["{", path.call(print, "expression"), "}"]);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I get what the shouldIndent logic is? Take this example:

<Something>
  {() => (
      <SomethingElse>
        <span />
      </SomethingElse>
    )}
</Something>;

I thought that the indent below is the indentation for the opening { on the line 2. It's not clear to me why this bug is happening, but it's also getting late so I might not be thinking clearly... Thanks for fixing these JSX bugs, they've annoyed me as well!

I'd like to look at this again tomorrow before merging so I can understand what's happening.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not really sure what's going on here and it would be nice to find out, instead of blindly sending a fix that happens to fix it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is good. The comment below about the group will fix other bugs.

@jlongster
Copy link
Member

Sorry I haven't merged this yet. I will go through this again tomorrow (there's something I want to understand here). I'm finishing up some other work right now.

@jlongster
Copy link
Member

What's happening is the JSX code has hard lines, so it breaks across lines. The previous code assumed all code was on one line, and added indentation for itself when it breaks. This pattern doesn't work when the inner contents forces itself across lines because it'll inherit an extra indentation level.

This bug will happen with anything that breaks itself up. For example, blocks have hard lines so we can repro with this:

<Something>
  {() => {
      return 5;
    }}
</Something>;

If the inner contents may break themselves up, you need to use multilineGroup. I'll comment where the change that.

Otherwise, your PR looks good. It's a bit unrelated actually, just exposed this bug.

if (!shouldIndent) {
return concat(["{", path.call(print, "expression"), "}"]);
}

return group(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be a multilineGroup.

But this has me wondering: Should JSXExpressionContainer ever include lines and break the expression to next line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great find, it also fixes a few more snapshots :)

@@ -200,13 +200,13 @@ var Example = React.createClass({
var ok_void = (
<Example
func={() => {
}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay

@jlongster jlongster merged commit a4dd760 into prettier:master Jan 19, 2017
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 21, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants