Skip to content
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

Some remaining little bugs related to C#12 collection expressions #1047

Closed
fgimian opened this issue Nov 29, 2023 · 2 comments · Fixed by #1054
Closed

Some remaining little bugs related to C#12 collection expressions #1047

fgimian opened this issue Nov 29, 2023 · 2 comments · Fixed by #1054
Milestone

Comments

@fgimian
Copy link

fgimian commented Nov 29, 2023

Hey there, found a few little issues while migrating a project to .NET 8 today 😄

Here's some sample code:

List<string> items =
[
    // My item
    "Hello",
];

items.AddRange(
    [
        "More really long words",
        "which span multiple lines",
        "are added here",
        "and something bad",
        "happens"
    ]
);

class MyClass
{
    private readonly List<string> _items;

    public MyClass()
    {
        _items = [];
        _items ??= [];
    }
}

And reformatted via the CSharpier CLI as follows:

~\source\ConsoleApp1 via .NET v8.0.100 🎯 net8.0
🕙 [ 06:57:50 PM ] ❯ dotnet csharpier --version
0.26.3+6bcc339686b935c644c660a45dc2722ef3567303

~\source\ConsoleApp1 via .NET v8.0.100 🎯 net8.0
🕙 [ 06:57:54 PM ] ❯ dotnet csharpier .
Error ./Program.cs - Failed syntax tree validation.
  ----------------------------- Original: Around Line 1 -----------------------------
  List<string> items =
  [
      // My item
      "Hello",
  ];

  items.AddRange(
      [
  ----------------------------- Formatted: Around Line 0 -----------------------------
  List<string> items = [// My item
      "Hello",];

  items.AddRange(

      [
          "More really long words",
          "which span multiple lines",
Formatted 1 files in 274ms.

The result is as follows:

List<string> items = [// My item
    "Hello",];

items.AddRange(

    [
        "More really long words",
        "which span multiple lines",
        "are added here",
        "and something bad",
        "happens"
    ]
);

class MyClass
{
    private readonly List<string> _items;

    public MyClass()
    {
        _items =  [];
        _items ??=  [];
    }
}

Here's the same code with comments for each issue:

// CSharpier should have kept the original formatting here.
List<string> items = [// My item
    "Hello",];

// An additional empty line was added below.
items.AddRange(

    [
        "More really long words",
        "which span multiple lines",
        "are added here",
        "and something bad",
        "happens"
    ]
);

class MyClass
{
    private readonly List<string> _items;

    public MyClass()
    {
        // Two spaces were added after the = sign in this scenario.
        _items =  [];
        _items ??=  [];
    }
}

Any help is greatly appreciated
Fotis

@belav
Copy link
Owner

belav commented Dec 1, 2023

Thanks for catching all of these! I'll have an update out shortly

shocklateboy92 added a commit that referenced this issue Dec 2, 2023
* Fixing a number of bugs with collection expressions

closes #1049
closes #1047

* Found a few more edge cases

* and another

---------

Co-authored-by: Lasath Fernando <[email protected]>
@fgimian
Copy link
Author

fgimian commented Dec 2, 2023

Thank you so much for sorting these out so quickly! 🚀

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

Successfully merging a pull request may close this issue.

2 participants