Skip to content

Complex nested cascades sometimes extend past line length unnecessarily #811

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

Closed
cpelling opened this issue May 2, 2019 · 3 comments
Closed

Comments

@cpelling
Copy link
Contributor

cpelling commented May 2, 2019

The following code is formatted by dartfmt as shown. Note that the last line of actual code is longer than 80 characters.

void main() async {
  group('my group', () {
    setUp(() async {
      final requestHandler = FakeRequestHandler()
        ..when(withServiceName(Ng2ProtoFooBarBazService.serviceName))
            .thenRespond(FooBarBazListResponse()
              ..entities.add(FooBarBaz()
                ..fooBarBazEntityId = entityId
                ..name = 'Test entity'
                ..baseFooBarBazId = baseFooBarBazId
                ..entityFooBarBazId = entityFooBarBazId))
        ..when(allOf(withServiceName(Ng2ProtoFooBarBazService.serviceName),
                hasFooBarBazId(baseFooBarBazId)))
            .thenRespond(FooBarBazListResponse()..entities.add(baseFooBarBaz))
        ..when(allOf(withServiceName(Ng2ProtoFooBarBazService.serviceName),
                hasFooBarBazId(entityFooBarBazId)))
            .thenRespond(FooBarBazListResponse()..entities.add(entityFooBarBaz))
        ..when(allOf(withServiceName(Ng2ProtoFooBarBazService.serviceName),
                hasFooBarBazIds(Set.from([baseFooBarBazId, entityFooBarBazId]))))
            .thenRespond(
                FooBarBazListResponse()..entities.addAll([baseFooBarBaz, entityFooBarBaz]));
    });
  });
}

The output I'd expect is more like:

void main() async {
  group('my group', () {
    setUp(() async {
      final requestHandler = FakeRequestHandler()
        ..when(withServiceName(Ng2ProtoFooBarBazService.serviceName))
            .thenRespond(FooBarBazListResponse()
              ..entities.add(FooBarBaz()
                ..fooBarBazEntityId = entityId
                ..name = 'Test entity'
                ..baseFooBarBazId = baseFooBarBazId
                ..entityFooBarBazId = entityFooBarBazId))
        ..when(allOf(withServiceName(Ng2ProtoFooBarBazService.serviceName),
                hasFooBarBazId(baseFooBarBazId)))
            .thenRespond(FooBarBazListResponse()..entities.add(baseFooBarBaz))
        ..when(allOf(withServiceName(Ng2ProtoFooBarBazService.serviceName),
                hasFooBarBazId(entityFooBarBazId)))
            .thenRespond(FooBarBazListResponse()..entities.add(entityFooBarBaz))
        ..when(allOf(withServiceName(Ng2ProtoFooBarBazService.serviceName),
                hasFooBarBazIds(Set.from([baseFooBarBazId, entityFooBarBazId]))))
            .thenRespond(FooBarBazListResponse()
              ..entities.addAll([baseFooBarBaz, entityFooBarBaz]));
    });
  });
}

In other words, I'd expect the final when().thenRespond() chain to wrap more like the first one does.

There's some interesting interactions going on within this expression which are hard to figure out from a black-box perspective. Simplifying the above code any more than this (for example, removing any of the when() calls) fixes the issue.

@natebosch
Copy link
Member

https://github.com/dart-lang/dart_style/wiki/FAQ#why-did-the-formatter-only-format-some-of-my-code

You might want to break this up into multiple expressions.

@munificent
Copy link
Member

I'm going to keep this open so we can use it as a regression test if we optimize how cascades are formatted.

@munificent
Copy link
Member

Here's another (sanitized) example:

aaaa() {
  {
    {
      {
        aaaaa aaaaa = AaaAaaaa.aaaaaaa().aaaaaaa((a) => a
          ..aaaaAaaaa
                  .aaaaaaaaaaaAaAaaa[AaaaAaaaaaaaaaAaaa_Aaaa.AAA_AAAAAAAAAAA_AAAAA_AAAAAAA.aaaa] =
              aaaa
          ..aaaaAaaaa.aaaaaaaaaaaAaAaaa[AaaaAaaaaaaaaaAaaa_Aaaa
              .AAA_AAAAAAAAAAA_AAAAAAAA_AAAA_AAAAAAAAA.aaaa] = aaaa
          ..aaaaAaaaa.aaaaaaaaaaaAaAaaa[AaaaAaaaaaaaaaAaaa_Aaaa
              .AAA_AAAAAAAAAAA_AAAAAAA_AAAA_AAAAAAAAA.aaaa] = aaaa
          ..aaaaAaaaa.aaaaaaaaaaaAaAaaa[AaaaAaaaaaaaaaAaaa_Aaaa
              .AAA_AAAAAAAAAAA_AAAAAA_AAAAAAA_AAA_AAAAAAAAAAA_AAAAA.aaaa] = aaaa
          ..aaaaaaaaAaaaaaAaaaa.aaaaaaa(AaaaaaaaAaaaaaAaaaaAaaaa.aaaaaaa())
          ..aaaaaaaaAaaaa.aaaaaaaa =
              (Aaaaaaaa()..aaaaaaaaAaaa = (AaaaaaaaAaaa()..aaAaaaaaa = aaaaa)));
      }
    }
  }
}

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

No branches or pull requests

3 participants