Creation of rule for prefix in variables

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 };
    }
}

}

Test to log the variable.Type. It might contain other things than just the type. When I tested I had to use split to get only the type part.

var type = variable.Type.Split(',')[0];

Thank you very much, it worked!!
Believe me I would never have found the solution without your help.
According to what you explained to me, I want to create a rule that validates if there are message boxes within the project and having this code

private InspectionResult CheckForMessageBox(IActivityModel activityToInspect, Rule configuredRule)
{
if (activityToInspect.Type == “System.Activities.Statements.MessageBox”)
{
var message = $“The activity {activityToInspect.DisplayName} is a Message Box and it should be avoided in your automation”;

            return new InspectionResult()
            {
                HasErrors = true,
                InspectionMessages = new[] { new InspectionMessage() { Message = message } },
                RecommendationMessage = configuredRule.RecommendationMessage,
                ErrorLevel = configuredRule.ErrorLevel
            };
        }

I should change it to

private InspectionResult CheckForMessageBox(IActivityModel activityToInspect, Rule configuredRule)
{
var type = activityToInspect.Type.Split(‘,’)[0];
if (type.equals(“System.Activities.Statements.MessageBox”))
{

I just tried it and it didn’t work for me, the message box was not recognized in my project

Check the content of activityToInspect.Type e.g. by writing the content to a text file. Then inspect the file. Verify if the activity is actually called “System.Activities.Statements.MessageBox” or something else.

File.AppendAllText("c:\\temp\\log.txt", activityToInspect.Type)

thank you!
I hope I’m not a bother, but could you give me an idea of how to validate that the annotation that comes by default in the ReFramework does present changes?

That does not have this text
[Process title]
[Process description]
[Additional information (e.g., author, contact information and applications involved and required external setup)]

(I already checked with ChatGPT and it’s more what confuses me)

I have never validated the annotation text, so unfortunately I can’t help you there.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.