Skip to content

Commit

Permalink
Fixing issue with a trailing comma being added after a trailing comme…
Browse files Browse the repository at this point in the history
…nt (belav#1370)

closes belav#1354

---------

Co-authored-by: Lasath Fernando <devel@lasath.org>
  • Loading branch information
2 people authored and pisolofin committed Nov 16, 2024
1 parent c1a6156 commit 7f30193
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var someObject = new SomeObject()
{
Property1 = 1,
Property2 = 2, // Trailing Comment
};

var someObject = new SomeObject()
{
Property1 = 1,
Property2 = [], // Trailing Comment
};

var someObject = new SomeObject()
{
Property1 = 1,
Property2 =
[ /* this formatting isn't ideal, but this probably won't happen in the real world */
], // Trailing Comment
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var someObject = new SomeObject()
{
Property1 = 1,
Property2 = 2 // Trailing Comment
};

var someObject = new SomeObject()
{
Property1 = 1,
Property2 = [] // Trailing Comment
};

var someObject = new SomeObject()
{
Property1 = 1,
Property2 = [/* this formatting isn't ideal, but this probably won't happen in the real world */ ] // Trailing Comment
};
10 changes: 10 additions & 0 deletions Src/CSharpier/SyntaxPrinter/FormattingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,19 @@ internal class FormattingContext
// we also need to keep track if we move around usings with disabledText
public bool ReorderedUsingsWithDisabledText { get; set; }

public TrailingCommaContext? TrailingComma { get; set; }

public FormattingContext WithSkipNextLeadingTrivia()
{
this.SkipNextLeadingTrivia = true;
return this;
}

public FormattingContext WithTrailingComma(SyntaxTrivia syntaxTrivia, Doc doc)
{
this.TrailingComma = new TrailingCommaContext(syntaxTrivia, doc);
return this;
}

public record TrailingCommaContext(SyntaxTrivia TrailingComment, Doc PrintedTrailingComma);
}
20 changes: 19 additions & 1 deletion Src/CSharpier/SyntaxPrinter/SeparatedSyntaxList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,30 @@ private static Doc Print<T>(
continue;
}

var firstTrailingComment = list[x]
.GetTrailingTrivia()
.FirstOrDefault(o => o.IsComment());

// we want a trailing comma, but we need to get it printed in place before a trailing comment
// shove it in the context so the token printing can pick it up and put it in place
if (
x >= list.SeparatorCount
&& closingToken is not null
&& firstTrailingComment != default
)
{
context.WithTrailingComma(
firstTrailingComment,
TrailingComma.Print(closingToken.Value, context, true)
);
}

docs.Add(printFunc(list[x], context));

// if the syntax tree doesn't have a trailing comma but we want want, then add it
if (x >= list.SeparatorCount)
{
if (closingToken != null)
if (closingToken != null && firstTrailingComment == default)
{
docs.Add(TrailingComma.Print(closingToken.Value, context));
}
Expand Down
10 changes: 10 additions & 0 deletions Src/CSharpier/SyntaxPrinter/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ or SyntaxKind.InterpolatedRawStringEndToken
var trailingTrivia = PrintTrailingTrivia(syntaxToken);
if (trailingTrivia != Doc.Null)
{
if (
context.TrailingComma is not null
&& syntaxToken.TrailingTrivia.FirstOrDefault(o => o.IsComment())
== context.TrailingComma.TrailingComment
)
{
docs.Add(context.TrailingComma.PrintedTrailingComma);
context.TrailingComma = null;
}

docs.Add(trailingTrivia);
}
}
Expand Down
17 changes: 10 additions & 7 deletions Src/CSharpier/SyntaxPrinter/TrailingComma.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ namespace CSharpier.SyntaxPrinter;

internal static class TrailingComma
{
public static Doc Print(SyntaxToken closingToken, FormattingContext context)
public static Doc Print(
SyntaxToken closingToken,
FormattingContext context,
bool skipIfBreak = false
)
{
if (!context.UsePrettierStyleTrailingCommas)
{
return Doc.Null;
}

return closingToken.LeadingTrivia.Any(o => o.IsDirective)
? Doc.Null
: Doc.IfBreak(
Token.Print(SyntaxFactory.Token(SyntaxKind.CommaToken), context),
Doc.Null
);
var printedToken = Token.Print(SyntaxFactory.Token(SyntaxKind.CommaToken), context);

return closingToken.LeadingTrivia.Any(o => o.IsDirective) ? Doc.Null
: skipIfBreak ? printedToken
: Doc.IfBreak(printedToken, Doc.Null);
}
}

0 comments on commit 7f30193

Please sign in to comment.