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

Code with cascading calls inside an assert is moved too soon to a new line. #1119

Closed
Jonas-Sander opened this issue Jun 16, 2022 · 1 comment

Comments

@Jonas-Sander
Copy link

Actual

class Foo {
  final List<Foo> subsections;
  final bool aBoolWithSomeVeryLongName;

  Foo({
    this.subsections,
    this.aBoolWithSomeVeryLongName,
  }) : assert(subsections
                .where((element) => element.aBoolWithSomeVeryLongName)
                .length <=
            1);
}

Expected
(Or something along these lines)

class Foo {
  final List<Foo> subsections;
  final bool aBoolWithSomeVeryLongName;

  Foo({
    this.subsections,
    this.aBoolWithSomeVeryLongName,
  }) : assert(subsections
                .where((element) => element.aBoolWithSomeVeryLongName)
                .length <= 1);
}
@munificent
Copy link
Member

I ran this code through the forthcoming tall style and it gives you:

class Foo {
  final List<Foo> subsections;
  final bool aBoolWithSomeVeryLongName;

  Foo({this.subsections, this.aBoolWithSomeVeryLongName})
    : assert(
        subsections
                .where((element) => element.aBoolWithSomeVeryLongName)
                .length <=
            1,
      );
}

I admit that's still a little funny looking. But that's mostly a consequence of the structure of the code. The formatter has a very simple rule that if either operand of a binary operator contains a newline then we split at the operator too. That helps clarify precedence when you have nested binary operators of different kinds and generally avoids the operator getting buried in the middle of a pile of large code.

But in this case, it looks weird because you have one really large operand before <= (the entire subsections...length expression) and one really short one after (1). So it splits at the <= and the 1 looks a little lonely.

Aside from that, I think the new formatter is doing what I expect here and looks OK. (If this were my code, I'd probably make a utility function for the subsections...length part and call that from the assert.)

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

2 participants