Skip to content

Commit e5c4507

Browse files
Meir017eNeRGy164
authored andcommitted
feat: add support for switch expressions
1 parent 2df1944 commit e5c4507

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

Diff for: src/DendroDocs.Tool/Analyzers/BranchingAnalyzer.cs

+20
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ public override void VisitSwitchStatement(SwitchStatementSyntax node)
5757
}
5858
}
5959

60+
public override void VisitSwitchExpression(SwitchExpressionSyntax node)
61+
{
62+
var switchStatement = new Switch();
63+
statements.Add(switchStatement);
64+
65+
switchStatement.Expression = node.GoverningExpression.ToString();
66+
67+
foreach (var arm in node.Arms)
68+
{
69+
var switchSection = new SwitchSection();
70+
switchStatement.Sections.Add(switchSection);
71+
72+
var label = $"{arm.Pattern}{(arm.WhenClause?.Condition is not null ? $" when {arm.WhenClause.Condition}" : string.Empty)}";
73+
switchSection.Labels.Add(label);
74+
75+
var invocationAnalyzer = new InvocationsAnalyzer(semanticModel, switchSection.Statements);
76+
invocationAnalyzer.Visit(arm.Expression);
77+
}
78+
}
79+
6080
private static string Label(SwitchLabelSyntax label)
6181
{
6282
return label switch

Diff for: src/DendroDocs.Tool/Analyzers/InvocationsAnalyzer.cs

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public override void VisitSwitchStatement(SwitchStatementSyntax node)
3636
branchingAnalyzer.Visit(node);
3737
}
3838

39+
public override void VisitSwitchExpression(SwitchExpressionSyntax node)
40+
{
41+
var branchingAnalyzer = new BranchingAnalyzer(semanticModel, statements);
42+
branchingAnalyzer.Visit(node);
43+
}
44+
3945
public override void VisitIfStatement(IfStatementSyntax node)
4046
{
4147
var branchingAnalyzer = new BranchingAnalyzer(semanticModel, statements);

Diff for: tests/DendroDocs.Tool.Tests/Analyzers/BranchingAnalyzerTests.cs

+40
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,44 @@ private static IEnumerable<object[]> GetSwitchStatements()
7979
yield return new object[] { "switch (Something) { case 1 when true: break; }", 1, new[] { new[] { "1 when true" } } };
8080
yield return new object[] { "switch (Something) { case int value: break; }", 1, new[] { new[] { "int value" } } };
8181
}
82+
83+
[DynamicData(nameof(GetSwitchExpressions), DynamicDataSourceType.Method)]
84+
[TestMethod]
85+
public void ShouldParseSwitchExpressionsCorrectly(string code, int sections, string[][] conditions)
86+
{
87+
// Assign
88+
var source = @$"
89+
public class Test
90+
{{
91+
public int Something;
92+
object Method()
93+
{{
94+
{code}
95+
}}
96+
}}
97+
";
98+
99+
// Act
100+
var types = TestHelper.VisitSyntaxTree(source, "CS8509");
101+
102+
// Assert
103+
types[0].Methods[0].Statements[0].Should().BeOfType<ReturnDescription>();
104+
var @switch = types[0].Methods[0].Statements[1].Should().BeOfType<Switch>().Subject;
105+
@switch.Sections.Should().HaveCount(sections);
106+
107+
for (var i = 0; i < @switch.Sections.Count; i++)
108+
{
109+
@switch.Sections[i].Labels.Should().Equal(conditions[i]);
110+
}
111+
}
112+
113+
private static IEnumerable<object[]> GetSwitchExpressions()
114+
{
115+
yield return new object[] { "return Something switch { _ => false };", 1, new[] { new[] { "_" } } };
116+
yield return new object[] { "return Something switch { 1 => true };", 1, new[] { new[] { "1" } } };
117+
yield return new object[] { "return Something switch { 1 or 2 => true };", 1, new[] { new[] { "1 or 2" } } };
118+
yield return new object[] { "return Something switch { 1 => true, 2 => true };", 2, new[] { new[] { "1" }, new[] { "2" } } };
119+
yield return new object[] { "return Something switch { 1 when true => true, _ => false };", 2, new[] { new[] { "1 when true" }, new[] { "_" } } };
120+
yield return new object[] { "return Something switch { int value => false };", 1, new[] { new[] { "int value" } } };
121+
}
82122
}

0 commit comments

Comments
 (0)