Skip to content

Commit

Permalink
Prettier style trailing commas (#1284)
Browse files Browse the repository at this point in the history
This pull request addresses the issue described in [Issue
#668](#668) and [Issue #1285
](#1285).
A flag UsePrettierStyleTrailingCommas is introduced which ensures a
trailing comma for anonymous object/collection/initializer/switch
expressions, list patterns and enum declarations in the case a line
break occurs.

I hope the implementation of the feature isn't too invasive. Obviously
it would be easier to implement the feature non-configurable.

I'm not sure how to deal with the tests, for now I enabled the flag and
adjusted the corresponding tests.

Thanks for any feedback!

---------

Co-authored-by: Bela VanderVoort <bela.vandervoort@optimizely.com>
  • Loading branch information
dawust and belav authored Jun 16, 2024
1 parent d388ca4 commit bc331cd
Show file tree
Hide file tree
Showing 38 changed files with 1,910 additions and 1,351 deletions.
3 changes: 2 additions & 1 deletion Src/CSharpier.FakeGenerators/SyntaxNodeComparerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,9 @@ private static void GenerateMethod(StringBuilder sourceBuilder, Type type)
);
sourceBuilder.AppendLine($" if (result.IsInvalid) return result;");

// Omit the last separator when comparing the original node with the formatted node, as it legitimately may be added or removed
sourceBuilder.AppendLine(
$" result = this.CompareLists(originalNode.{propertyName}.GetSeparators().ToList(), formattedNode.{propertyName}.GetSeparators().ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span);"
$" result = this.CompareLists(originalNode.{propertyName}.GetSeparators().Take(originalNode.{propertyName}.Count() - 1).ToList(), formattedNode.{propertyName}.GetSeparators().Take(formattedNode.{propertyName}.Count() - 1).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span);"
);
sourceBuilder.AppendLine($" if (result.IsInvalid) return result;");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class ClassName
{
One = "One",
Two = "Two",
ThreeThreeThree = "ThreeThreeThree"
ThreeThreeThree = "ThreeThreeThree",
};

public dynamic Value = new { NoName };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ public class ClassName
elementSelector: static o => new Entity
{
o.LongName_______________________,
o.LongName_______________________
o.LongName_______________________,
}
);

this.GroupBy_____________(
keySelector: static o => new Entity
{
o.LongName_______________________,
o.LongName_______________________
o.LongName_______________________,
},
resultSelector: static o => new Entity
{
o.LongName_______________________,
o.LongName_______________________
o.LongName_______________________,
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ class ClassName
private static readonly char[] initializerIndents = new char[]
{
Path.DirectorySeparatorChar,
Path.AltDirectorySeparatorChar
Path.AltDirectorySeparatorChar,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ string[] c =
"________________________",
"________________________",
"________________________",
"________________________"
"________________________",
];

int[][] d =
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
[7, 8, 9],
];

var a = new A { B = [1, 2, 3] };
Expand All @@ -33,7 +33,7 @@ items.AddRange(
"which span multiple lines",
"are added here",
"and something bad",
"happens"
"happens",
]
);

Expand All @@ -43,15 +43,15 @@ someObject = new SomeObject
Property2 =
[
LongValue________________________________________________,
LongValue________________________________________________
LongValue________________________________________________,
],
Property3 = "three"
Property3 = "three",
};

someValue =
[
LongValue________________________________________________,
LongValue________________________________________________
LongValue________________________________________________,
];

CallMethod(parameter1, parameterName: [1, 2]);
Expand All @@ -61,7 +61,7 @@ CallMethod(
parameterName:
[
LongValue________________________________________________,
LongValue________________________________________________
LongValue________________________________________________,
]
);

Expand Down Expand Up @@ -116,7 +116,7 @@ class MyClass
ArgConstants.NoInteractivity,
ArgConstants.UseProgramMain,
ArgConstants.NoHttps,
ArgConstants.Empty
ArgConstants.Empty,
],
];

Expand All @@ -138,7 +138,7 @@ class MyClass
DayOfWeek.Wednesday,
DayOfWeek.Thursday,
DayOfWeek.Friday,
DayOfWeek.Saturday
DayOfWeek.Saturday,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public enum SubCode
DoNotKeepLine,

[ForceLine]
ForceLine
ForceLine,
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public enum SubCode

DoNotKeepLine,
[ForceLine]
ForceLine
ForceLine,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,34 @@ enum AnEnum
{
One,
Two,
Three
Three,
}

public enum E
{
A,
B = A,
C = 2 + A,
}

public enum EnumWithDirective
{
A,
B = A,
C = 2 + A
#if DEBUG
,
D = 4,
#endif
}

public enum EnumWithDirective2
{
A,
B = A,
C = 2 + A,
#if DEBUG
D = 4,
#endif
E = 5,
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ClassName
)
{
Property1 = 1,
Property2 = 2
Property2 = 2,
};

private LotsOfParametersToCauseBreaking ShortFieldName = new LotsOfParametersToCauseBreaking(
Expand All @@ -44,7 +44,7 @@ public class ClassName
Property2 = false,
Property3 = false,
Property4 = false,
Property5 = false
Property5 = false,
};

// leading comments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public class ClassName
public enum MeetingLocation
{
Café,
Restaurant
Restaurant,
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
public enum MeetingLocation
{
Café,
Restaurant
Restaurant,
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ClassName
int[,] array = new[,]
{
{ 1, 2 },
{ 3, 4 }
{ 3, 4 },
};

private string[] SomeArray = new[]
Expand All @@ -25,7 +25,7 @@ class ClassName
{
"someLongValue",
"anotherLongValue",
"yetAnotherLongValue"
"yetAnotherLongValue",
};

return TheArrayInHereShouldAlsoNotBreak(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class ClassName
Dictionary<int, string> dictionary = new Dictionary<int, string>
{
[1] = "one",
[2] = "two_______________________"
[2] = "two_______________________",
};
}
Loading

0 comments on commit bc331cd

Please sign in to comment.