dart-lang / dart_style

An opinionated formatter/linter for Dart code
https://pub.dev/packages/dart_style
BSD 3-Clause "New" or "Revised" License
649 stars 120 forks source link

Treat await expressions as block-formattable if the subexpression is #1531

Closed munificent closed 3 months ago

munificent commented 3 months ago

The formatter treats "block formattable" expressions differently in assignment contexts. That's what lets it produce output like:

variable = function(
  argument,
  another,
);

Instead of the split in the argument list forcing the = to split like:

variable =
    function(
      argument,
      another,
    );

Currently await expressions are not considered block-formattable even if their subexpression is. That means that instead of:

variable = await function(
  argument,
  another,
);

You get:

variable =
    await function(
      argument,
      another,
    );

We should probably allow await expressions to be block formattable if their subexpression is. In particular this diff of applying the new style to shorebird has some examples:

// Before:
    final response = await post(
      Uri.parse(webhookUrl),
      headers: {HttpHeaders.contentTypeHeader: ContentType.json.value},
      body: json.encode(discordPayload),
    );

// After:
    final response =
        await post(Uri.parse(webhookUrl), headers: {
          HttpHeaders.contentTypeHeader: ContentType.json.value,
        }, body: json.encode(discordPayload));

// Before:
  final token = await jwt.verify(
    '<TOKEN>',
    issuer: '<ISSUER>',
    audience: {'<AUDIENCE>'},
    publicKeysUrl: '<PUBLIC_KEYS_URL>',
  );

// After:
  final token =
      await jwt.verify('<TOKEN>', issuer: '<ISSUER>', audience: {
        '<AUDIENCE>',
      }, publicKeysUrl: '<PUBLIC_KEYS_URL>');

// Before:
      final connection = await connectSocket(
        uri.host,
        port: uri.port,
        timeout: _socketOptions.timeout,
      );

// After:
      final connection =
          await connectSocket(
            uri.host,
            port: uri.port,
            timeout: _socketOptions.timeout,
          );
dcharkes commented 3 months ago

Ditto for as expressions.