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 removes `\r`s from inside strings #1504

Closed DanTup closed 3 months ago

DanTup commented 4 months ago

Not sure if this is intended, but feels like a bug. When using the tall experiment, carriage returns seem to be removed from the inside of strings.

Here's a repro:

Future<void> test_newlines() async {
  var formatter = dart_style.DartFormatter();
  var tallFormatter = dart_style.DartFormatter(
    experimentFlags: [
      'tall-style',
    ],
  );
  // The whole document uses \r\n, once inside the string and leading/trailing the code
  var code = '\r\nvar a = """\r\ntest\r\n""";\r\n';
  print(jsonEncode(code));
  print(jsonEncode(formatter.format(code)));
  print(jsonEncode(tallFormatter.format(code)));
}

And here's the output:

"\r\nvar a = \"\"\"\r\ntest\r\n\"\"\";\r\n"

"var a = \"\"\"\r\ntest\r\n\"\"\";\r\n"

"var a = \"\"\"\ntest\n\"\"\";\r\n"
               ^     ^ missing \r

The \r in the surrounding code was kept (at least at the end, the leading whitespace was removed as part of formatting), but inside the strings it was removed.