Skip to content

Commit

Permalink
feat: add support for switch expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Meir017 authored and eNeRGy164 committed Aug 29, 2024
1 parent 660de5a commit ed003bf
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/DendroDocs.Tool/Analyzers/BranchingAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ public override void VisitSwitchStatement(SwitchStatementSyntax node)
}
}

public override void VisitSwitchExpression(SwitchExpressionSyntax node)
{
var switchStatement = new Switch();
statements.Add(switchStatement);

switchStatement.Expression = node.GoverningExpression.ToString();

foreach (var arm in node.Arms)
{
var switchSection = new SwitchSection();
switchStatement.Sections.Add(switchSection);

var label = $"{arm.Pattern}{(arm.WhenClause?.Condition is not null ? $" when {arm.WhenClause.Condition}" : string.Empty)}";
switchSection.Labels.Add(label);

var invocationAnalyzer = new InvocationsAnalyzer(semanticModel, switchSection.Statements);
invocationAnalyzer.Visit(arm.Expression);
}
}

private static string Label(SwitchLabelSyntax label)
{
return label switch
Expand Down
6 changes: 6 additions & 0 deletions src/DendroDocs.Tool/Analyzers/InvocationsAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public override void VisitSwitchStatement(SwitchStatementSyntax node)
branchingAnalyzer.Visit(node);
}

public override void VisitSwitchExpression(SwitchExpressionSyntax node)
{
var branchingAnalyzer = new BranchingAnalyzer(semanticModel, statements);
branchingAnalyzer.Visit(node);
}

public override void VisitIfStatement(IfStatementSyntax node)
{
var branchingAnalyzer = new BranchingAnalyzer(semanticModel, statements);
Expand Down
40 changes: 40 additions & 0 deletions tests/DendroDocs.Tool.Tests/Analyzers/BranchingAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,44 @@ private static IEnumerable<object[]> GetSwitchStatements()
yield return new object[] { "switch (Something) { case 1 when true: break; }", 1, new[] { new[] { "1 when true" } } };
yield return new object[] { "switch (Something) { case int value: break; }", 1, new[] { new[] { "int value" } } };
}

[DynamicData(nameof(GetSwitchExpressions), DynamicDataSourceType.Method)]
[TestMethod]
public void ShouldParseSwitchExpressionsCorrectly(string code, int sections, string[][] conditions)
{
// Assign
var source = @$"
public class Test
{{
public int Something;
object Method()
{{
{code}
}}
}}
";

// Act
var types = TestHelper.VisitSyntaxTree(source, "CS8509");

// Assert
types[0].Methods[0].Statements[0].Should().BeOfType<ReturnDescription>();
var @switch = types[0].Methods[0].Statements[1].Should().BeOfType<Switch>().Subject;
@switch.Sections.Should().HaveCount(sections);

for (var i = 0; i < @switch.Sections.Count; i++)
{
@switch.Sections[i].Labels.Should().Equal(conditions[i]);
}
}

private static IEnumerable<object[]> GetSwitchExpressions()
{
yield return new object[] { "return Something switch { _ => false };", 1, new[] { new[] { "_" } } };
yield return new object[] { "return Something switch { 1 => true };", 1, new[] { new[] { "1" } } };
yield return new object[] { "return Something switch { 1 or 2 => true };", 1, new[] { new[] { "1 or 2" } } };
yield return new object[] { "return Something switch { 1 => true, 2 => true };", 2, new[] { new[] { "1" }, new[] { "2" } } };
yield return new object[] { "return Something switch { 1 when true => true, _ => false };", 2, new[] { new[] { "1 when true" }, new[] { "_" } } };
yield return new object[] { "return Something switch { int value => false };", 1, new[] { new[] { "int value" } } };
}
}

0 comments on commit ed003bf

Please sign in to comment.