-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
jsx-indent behavior with { bool && jsx } syntax #540
Comments
Can you give me your eslint, babel-eslint and eslint-plugin-react versions before and after the upgrade? |
Ok nevermind, I wasn't using jsx-indent before... (Sorry about the false alarm). So my issue would be "Is there a way to disable jsx-indent for this use case?" (Renaming accordingly). |
@MoOx whether you surround the |
Sorry but I don't really get what you are explaining. Why jsx-indent is warning me about non jsx indentation? I would expect that jsx-indent rule only warn for indentation inside jsx nodes, like it's explained in the doc. Btw, this seems consistent {
condition1 &&
condition2
} But this doesn't {
condition1 &&
condition2
} I don't understand why the first JSX level need to be indented, seems like a bug to me. |
OK so, I agree with your "condition1" and "condition2" analogy - but that's not the same thing. Imagine that instead of "condition2", you had a multiline array: // bad
{
condition1 && [
1,
2,
]
}
// good
{
condition1 && [
1,
2,
]
} The reason this is the right analogy is because every JSX expression has implicit parens around them - the fact that they're optional to type, like semicolons, doesn't mean they aren't always semantically present (like semicolons). In other words, your original example, with parens added: // bad
{
head.title && (
<h1>
{ head.title }
</h1>
)
}
// good
{
head.title && (
<h1>
{ head.title }
</h1>
)
} |
I totally understand that this code is not "good" {
condition1 && [
1,
2,
]
} But I found this is opinionated to assume the same behavior for the JSX I provided, as I consider to be transformed as this {
head.title &&
React.createElement("h1", ...)
} And not {
head.title &&
React.createElement("h1",)
} I think if you got parenthesis, it's fine to yell for this code {
head.title && (
<h1>
{ head.title }
</h1>
)
} But not on this one {
head.title &&
(<h1>
{ head.title }
</h1>)
} And eslint will not yell for code like this if (
condition &&
condition2
) {
// stuff
} On all the different react projects with different teams (note: I am freelance) I worked for the past 3 years, we indented this way, and not using parenthesis, so it's feel like I am stuck and cannot use this rule at all. Can we at least provide the user the choice via an option? I get your point for the "it lints everything in jsx", which make totally sense now you are highlighting this point, but we should provide flexibility (or maybe just not assume too much :p). |
…in-react#540), the rule is for now disabled. If re-enabled, will be done in a major release.
Well. My first intent when writing the If I remember correctly the rule assume that a So... I'm divided here. Both @ljharb explanation and @MoOx expected behavior makes sense to me. |
@MoOx I agree that with parens, either of the two styles would be appropriate: {
foo &&
(<h1>
bar
</h1>)
}
{
foo && (
<h1>
bar
</h1>
)
} // your preferred style without parens
{
foo &&
<h1>
bar
</h1>
} Without parens, I think your preferred style (the latter) is uncommon and unidiomatic, but does make subjective sense - only while omitting the parens. I don't mind adding an option for this indentation, but I'd want it to only allow it when parens around the jsx element are omitted. Thoughts? |
I really thing all 3 examples above are valid by default, but an option would be fine. Note that this code is valid with eslint /* eslint indent: ["error", 2] */
var foo, bar, baz;
if (
foo &&
(bar)
) baz()
if (
foo && (
bar
)
) baz()
if (
foo &&
bar
) baz() Note: It seems eslint indent rules does not even read indentation in if() condition. This code is valid too /* eslint indent: ["error", 2] */
var foo, bar, baz;
if (
foo &&
(bar)
) baz() Tried on the demo http://eslint.org/demo/ |
Conditionals are different than jsx |
About about this /* eslint indent: ["error", 2] */
var foo, bar;
foo &&
(bar());
foo && (
bar()
);
foo &&
bar();
foo &&
bar(); |
I think you've established they're not parallel :-) However I think that's a gap in |
Linting is also about coding style and preferences, that why I was asking to allow this syntax to not be considered as an error. Enforcing coding style is weird imo. |
Also, for example, in our project we indent only markup in jsx and not expressions. For example <div>
{condition &&
<div>Hello world!</div>
}
</div> Would be awesome to be able to provide options for that kind of formatting. |
Ternaries are also an issue. We'd like to do: <div>
{isCondition
? <div>
some content
</div>
: <span />
}
</div> But this currently errors, instead wanting: <div>
{isCondition
? <div>
some content
</div>
: <span />
}
</div> Which IMO is much less readable, since the opening and closing |
Can this really be it's own rule instead of relying on secondary indent rules?
|
This rule leads to some odd indentation around ternary operators and logic operators, but there is an active bug for it so it should be ok to leave the rule on and wait for a fix. See jsx-eslint/eslint-plugin-react#540
This rule leads to some odd indentation around ternary operators and logic operators, but there is an active bug for it so it should be ok to leave the rule on and wait for a fix. See jsx-eslint/eslint-plugin-react#540
Any update on this? As @billyjanitsch pointed out, the way this enforces indentation for ternaries makes no sense. |
The recent commits listed in this PR has now broken syntax like this:
It expects all instances of |
This rule leads to some odd indentation around ternary operators and logic operators, but there is an active bug for it so it should be ok to leave the rule on and wait for a fix. See jsx-eslint/eslint-plugin-react#540
This rule leads to some odd indentation around ternary operators and logic operators, but there is an active bug for it so it should be ok to leave the rule on and wait for a fix. See jsx-eslint/eslint-plugin-react#540
The referenced commit 42037c4 only fixes one case, but doesn't address many other cases such as the one mentioned in #540 (comment). |
@billyjanitsch do you mind filing a new issue so that it can be more easily addressed? |
I just upgraded to v4.3 and got issue on this code
But the change is weird:
Is this on purpose? I don't see any change in the changelog about that. I also added some other rules it should not interfere right?
The text was updated successfully, but these errors were encountered: