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
Please include the ability to configure indent on conditional expressions #12674
Comments
It would be really helpful to us in evaluating your proposal if you include what the rule will actually allow/disallow using code examples. Some specific questions I have:
const a = b
? c
: d;
const a = b ?
c :
d;
const a = b ? c :
d;
const a = b
? c :
d;
const a = (
b || c
) ? d :
{
e: true
};
I personally think a more generic option that configures the indentation of multiline expressions might be more useful (see #12248). |
Ah, I found it! Sorry, thought it was an issue. #12556 Do the proposed changes in that PR satisfy your use case? |
Thank you for your prompt reply! Yeah, a generalized multiline expression config would be better. I was under the impression that was already happening for most expressions, though. In most cases, it seems to correctly handle my multiline cases...i.e. for (blah in blahs) {
const thisStatementIsMultiline =
someReallyLongStuffThatDeservesItsOwnLine();
} This seems to be handled correctly. However, in looking through the codebase to make sure I have everything I'm telling you correct, I did run across an odd case: function isAVuexModule() {
return (
_.isPlainObject(options) &&
_.isPlainObject(options.state) &&
_.isPlainObject(options.getters) &&
_.isPlainObject(options.mutations) &&
_.isPlainObject(options.actions)
);
} This is unexpected in several ways (I recognize that not all of these are relevant to this PR):
Whatever solution we come to also ought to fix this; so, I agree, doing this just for Conditional Expressions is not the best way to proceed. What I would expect in this scenario is: function isAVuexModule() {
return (_.isPlainObject(options) &&
_.isPlainObject(options.state) &&
_.isPlainObject(options.getters) &&
_.isPlainObject(options.mutations) &&
_.isPlainObject(options.actions));
} (To be clear, the behavior I want is not to have the continuations line up with the argument on the first line. It's all about 8 spaces aka double the normal indent) Now to answer your questions: // Ideally, one this short would be on one line. If it's broken, it would be
// the following.
// The first 4 should look like this
const a = b ?
c :
d;
const a = (b || c) ?
d :
{e: true};
// Or, if the inline object is too long for one line
const a = (b || c) ?
d :
{
e : true
};
var a = foo ?
bar :
baz ? qux : boop; With var a = foo ?
bar :
baz ? qux : boop; If var a = foo ?
bar :
baz ?
qux :
boop; Does that help? |
Just looked at that PR you linked. I don't think it addresses the issue, though it is important. Correct me if I'm wrong, but it seems like that PR would allow for the following: // Format...
foo ?
bar => {
function();
} :
baz;
// Into...
foo ?
bar => {
function();
} :
baz; This seems important; the latter seems like the correct behavior. However, what I'm looking for is the following: // Format...
foo ?
bar => {
function();
} :
baz;
// Into...
foo ?
bar => {
function();
} :
baz; |
These seem like two separate issues to me - do you mind creating a separate issue for the latter one? I don't think we're quite on the same page. The It sounds like what you're looking for is the following: /*eslint indent: ["error", 4, { "ConditionalExpression": 2 }]*/
// Incorrect
foo ?
bar => {
function();
} :
baz;
// Correct
foo ?
bar => {
function();
} :
baz; /*eslint indent: ["error", 4, { "ConditionalExpression": 2, "flatTernaryExpressions": false }]*/
// Incorrect
var a =
foo ? bar :
baz ? qux :
boop;
// Correct
var a =
foo ? bar :
baz ? qux :
boop; /*eslint indent: ["error", 4, { "ConditionalExpression": 2, "flatTernaryExpressions": true }]*/
// Inorrect
var a =
foo ? bar :
baz ? qux :
boop;
// Correct
var a =
foo ? bar :
baz ? qux :
boop; |
Unfortunately, it looks like there wasn't enough interest from the team Thanks for contributing to ESLint and we appreciate your understanding. |
What rule do you want to change?
indent
Does this change cause the rule to produce more or fewer warnings?
more
How will the change be implemented? (New option, new default behavior, etc.)?
new option
Please provide some example code that this change will affect:
Existing:
Desired (Example):
What does the rule currently do for this code?
The only way to configure indent behavior for conditional (ternary) expressions is to ignore them completely
What will the rule do after it's changed?
Conditional expressions will be configurable, just as every other kind of expression. At the very least, it should be possible to automatically bring conditional expressions into conformance with the rest of the code base, which it does not currently seem to do.
Are you willing to submit a pull request to implement this change?
I have no idea how to do that. Maybe if you can give me enough of a description on how to make it happen, I could give it a shot, but I am not familiar at all with JS, so my code might be less than great.
The text was updated successfully, but these errors were encountered: