dart-lang / dart_style

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

Formatter adds invalid commas in representation fields of extension types #1505

Closed DanTup closed 3 months ago

DanTup commented 4 months ago

I noticed this when accidentally running formatted code through the formatter.

Given this code (taken from the SDK):

extension type JSExportedDartFunction._(
  JSExportedDartFunctionRepType _jsExportedDartFunction
) implements JSFunction {}

The tall formatter produces this:

extension type JSExportedDartFunction._(
  JSExportedDartFunctionRepType _jsExportedDartFunction,
) implements JSFunction {}

If you try to format this code again, it fails with:

  Could not format because the source could not be parsed:

  line 2, column 56 of .: The representation field can't have a trailing comma.

Here's a repro that formats the code twice with/without tall mode. Only the very last line (re-formatting after already formatting with tall mode) fails:

  Future<void> test_newlines() async {
    var formatter = dart_style.DartFormatter();
    var tallFormatter = dart_style.DartFormatter(
      experimentFlags: [
        'tall-style',
      ],
    );
    var code = r'''
extension type JSExportedDartFunction._(
  JSExportedDartFunctionRepType _jsExportedDartFunction
) implements JSFunction {}
''';
    print(jsonEncode(code));
    print(jsonEncode(formatter.format(code)));
    print(jsonEncode(formatter.format(formatter.format(code))));
    print(jsonEncode(tallFormatter.format(code)));
    print(jsonEncode(tallFormatter.format(tallFormatter.format(code))));
  }
munificent commented 3 months ago

Oh, wow. Today I learned that extension types don't allow a trailing comma in the representation clause. :)

I'll fix that. Thanks for the issue!