Hello everyone, I have a small problem, I have created a code for the validation of the prefix that must have certain types of variables, I have managed to make the rule visible in the Analyze File, but when I execute it, it does not detect the variables that are wrong created, example, I have the variable name of type string, this variable according to the rule that I have created must start with “str_” but the rule does not detect the error.
This is the code
using System;
using System.Collections.Generic;
using UiPath.Studio.Activities.Api;
using UiPath.Studio.Activities.Api.Analyzer;
using UiPath.Studio.Analyzer.Models;
using UiPath.Studio.Activities.Api.Analyzer.Rules;
namespace MaximumVariableLength
{
public class VariableNamingConventionRuleRepository : IRegisterAnalyzerConfiguration
{
public void Initialize(IAnalyzerConfigurationService workflowAnalyzerConfigService)
{
if (!workflowAnalyzerConfigService.HasFeature(“WorkflowAnalyzerV4”))
return;
var variableNamingConventionRule = new Rule<IActivityModel>("VariableNamingConventionRule", "DG-RUL-001", InspectVariableNamePrefix);
variableNamingConventionRule.DefaultErrorLevel = System.Diagnostics.TraceLevel.Warning;
workflowAnalyzerConfigService.AddRule<IActivityModel>(variableNamingConventionRule);
}
private InspectionResult InspectVariableNamePrefix(IActivityModel activityToInspect, Rule configuredRule)
{
var messageList = new List<InspectionMessage>();
foreach (var variable in activityToInspect.Variables)
{
if (variable.Type == "System.Data.DataTable" && !variable.DisplayName.StartsWith("dt_"))
{
messageList.Add(new InspectionMessage()
{
Message = $"Variable {variable.DisplayName} of type DataTable should start with 'dt_' prefix."
});
}
else if (variable.Type == "System.String" && !variable.DisplayName.StartsWith("str_"))
{
messageList.Add(new InspectionMessage()
{
Message = $"Variable {variable.DisplayName} of type String should start with 'str_' prefix."
});
}
else if (variable.Type == "System.Int32" && !variable.DisplayName.StartsWith("int_"))
{
messageList.Add(new InspectionMessage()
{
Message = $"Variable {variable.DisplayName} of type Integer should start with 'int_' prefix."
});
}
}
if (messageList.Count > 0)
{
return new InspectionResult()
{
HasErrors = true,
InspectionMessages = messageList,
RecommendationMessage = "Fix your variable naming",
ErrorLevel = configuredRule.ErrorLevel
};
}
return new InspectionResult { HasErrors = false };
}
}
}