Skip to content

Commit

Permalink
Improve parsing of argument to value (#11)
Browse files Browse the repository at this point in the history
Keep old way of working when possible.
Only switch to ToFullString if FQN doesn't work.
  • Loading branch information
MichielOda authored Nov 21, 2023
1 parent 1584f28 commit 277f146
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Common/RoslynHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -843,11 +843,24 @@ public static bool TryParseValue(ExpressionSyntax expression, SemanticModel sema
case ObjectCreationExpressionSyntax oces:
// Examples: new DateTime(...); new MyClass();

object o;
try
{
// Will work when it's defined in the same project
o = GetFullyQualifiedName(semanticModel, oces);
}
catch
{
// Backup for when it's defined in another project/semantic model
// Will change the outcome as we can't retrieve the FQN.
o = oces.ToFullString();
}

value = new Value
{
Type = Value.ValueType.Unknown,
HasNotChanged = true,
Object = oces.ToFullString(),
Object = o,
};
succeeded = true;
break;
Expand Down
52 changes: 52 additions & 0 deletions CommonTests/CheckCallingMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public static class MyOtherConstants
string programText = @"
namespace MyNamespace
{
using System;
using MyOtherNamespace;
//using Skyline.DataMiner.Scripting;
Expand Down Expand Up @@ -122,6 +124,9 @@ public static void Run(int randomValue)
Method16(MyOtherConstants.TriggerQueue.Dequeue());
Method17(MyOtherConstants.MyDoubleConst);
Method18(new DateTime(2008, 8, 29, 19, 27, 15));
Method19(MyOtherConstants.TriggerQueue);
}
private static void Method1(object o) { }
Expand All @@ -141,6 +146,8 @@ private static void Method14(object o) { }
private static void Method15(object o) { }
private static void Method16(object o) { }
private static void Method17(object o) { }
private static void Method18(object o) { }
private static void Method19(object o) { }
}
}";

Expand Down Expand Up @@ -191,6 +198,9 @@ void CheckCallingMethod(CallingMethodClass callingMethod, SemanticModel semantic
// Using type from another project
results.Add(Method16(callingMethod, semanticModel, solution));
results.Add(Method17(callingMethod, semanticModel, solution));

results.Add(Method18(callingMethod, semanticModel, solution));
results.Add(Method19(callingMethod, semanticModel, solution));
}

string Method1(CallingMethodClass callingMethod, SemanticModel semanticModel, Solution solution)
Expand Down Expand Up @@ -563,6 +573,48 @@ string Method17(CallingMethodClass callingMethod, SemanticModel semanticModel, S

return String.Empty;
}

string Method18(CallingMethodClass callingMethod, SemanticModel semanticModel, Solution solution)
{
if (callingMethod.Name != "Method18")
{
return null;
}

try
{
callingMethod.Arguments[0].TryParseToValue(semanticModel, solution, out Value value).Should().BeTrue();
value.Type.Should().Be(Value.ValueType.Unknown);
value.Object.Should().BeEquivalentTo("System.DateTime");
}
catch (AssertFailedException e)
{
return $"[Method18] {e.Message}";
}

return String.Empty;
}

string Method19(CallingMethodClass callingMethod, SemanticModel semanticModel, Solution solution)
{
if (callingMethod.Name != "Method19")
{
return null;
}

try
{
callingMethod.Arguments[0].TryParseToValue(semanticModel, solution, out Value value).Should().BeTrue();
value.Type.Should().Be(Value.ValueType.Unknown);
value.Object.Should().BeEquivalentTo("new Queue<int>()");
}
catch (AssertFailedException e)
{
return $"[Method19] {e.Message}";
}

return String.Empty;
}
}

private class QActionAnalyzer : CSharpAnalyzerBase
Expand Down

0 comments on commit 277f146

Please sign in to comment.