Skip to content

Switch expressions assist conversion with OR removes comments #56597

Open
@FMorschel

Description

@FMorschel

From an example posted by @pq in #58862:

switch (character) {
  /// See [General Punctuation]
  /// (http://www.unicode.org/charts/PDF/U2000.pdf).
  case 0x00A0: // No-break space.
  case 0x1680: // Ogham space mark.
  case 0x180E: // Mongolian vowel separator.
  case 0x2000: // En quad.
  case 0x2001: // Em quad.
  case 0x2002: // En space.
  case 0x2003: // Em space.
  case 0x2004: // Three-per-em space.
  case 0x2005: // Four-per-em space.
  case 0x2006: // Six-per-em space.
  case 0x2007: // Figure space.
  case 0x2008: // Punctuation space.
  case 0x2009: // Thin space.
  case 0x200A: // Hair space.
  case 0x200B: // Zero width space.
  case 0x2028: // Line separator.
  case 0x2029: // Paragraph separator.
  case 0x202F: // Narrow no-break space.
  case 0x205F: // Medium mathematical space.
  case 0x3000: // Ideographic space.
  case 0xFEFF: // Zero width no-break space.
    return something;

  default:
    return somethingElse;
}

Turns into the following losing all comments:

return switch (character) {
  /// See [General Punctuation]
  /// (http://www.unicode.org/charts/PDF/U2000.pdf).
  0x00A0 || 0x1680 || 0x180E || 0x2000 || 0x2001 || 0x2002 || 0x2003 || 0x2004 || 0x2005 || 0x2006 || 0x2007 || 0x2008 || 0x2009 || 0x200A || 0x200B || 0x2028 || 0x2029 || 0x202F || 0x205F || 0x3000 || 0xFEFF => // Zero width no-break space.
    1,

  _ => 2
};

After formatted:

return switch (character) {
  /// See [General Punctuation]
  /// (http://www.unicode.org/charts/PDF/U2000.pdf).
  0x00A0 ||
  0x1680 ||
  0x180E ||
  0x2000 ||
  0x2001 ||
  0x2002 ||
  0x2003 ||
  0x2004 ||
  0x2005 ||
  0x2006 ||
  0x2007 ||
  0x2008 ||
  0x2009 ||
  0x200A ||
  0x200B ||
  0x2028 ||
  0x2029 ||
  0x202F ||
  0x205F ||
  0x3000 ||
  0xFEFF => // Zero width no-break space.
    1,
  _ => 2
};

A smaller example:

switch (character) {
  /// See [General Punctuation]
  /// (http://www.unicode.org/charts/PDF/U2000.pdf).
  case 0x00A0: // No-break space.
  case 0xFEFF: // Zero width no-break space.
    return 1;
  default:
    return 2;
}

Turns into:

return switch (character) {
  /// See [General Punctuation]
  /// (http://www.unicode.org/charts/PDF/U2000.pdf).
  0x00A0 || 0xFEFF => // Zero width no-break space.
    1,
  _ => 2
};

I believe it should become something along the lines of:

return switch (character) {
  /// See [General Punctuation]
  /// (http://www.unicode.org/charts/PDF/U2000.pdf).
  0x00A0 || // No-break space.
  0xFEFF => // Zero width no-break space.
    1,
  _ => 2,
};

But I think the result being alone is weird in all of the above so I suggest doing:

return switch (character) {
  /// See [General Punctuation]
  /// (http://www.unicode.org/charts/PDF/U2000.pdf).
  0x00A0 || // No-break space.
  0xFEFF // Zero width no-break space.
    => 1,
  _ => 2,
};

Currently, in the formatter, if you have the above, it formats the => in one line and the result in a new one. Will file an issue there and link here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-assistIssues with analysis server assistsdevexp-quick-fixIssues with analysis server (quick) fixesdevexp-serverIssues related to some aspect of the analysis servertype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions