Skip to content

Commit

Permalink
Making the variable naming watch out for key words because of course …
Browse files Browse the repository at this point in the history
…someone tried to do just that
  • Loading branch information
jeremydmiller committed Jun 18, 2024
1 parent 430b00b commit 4f4676a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/CodegenTests/Codegen/VariableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ public void default_arg_name_of_inner_interface()
Variable.DefaultArgName<HyperdriveMotivator.IInnerThing>()
.ShouldBe("innerThing");
}

[Fact]
public void custom_lock_classes()
{
Variable.DefaultArgName<Lock>().ShouldBe("@lock");
}

}

Expand Down Expand Up @@ -156,4 +162,7 @@ public class InnerThing
public interface IInnerThing
{
}
}
}

// Because of course, someone did this
public class Lock{}
8 changes: 8 additions & 0 deletions src/JasperFx.CodeGeneration/Model/Variable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace JasperFx.CodeGeneration.Model;

public class Variable
{
private static readonly string[] _reservedNames = new string[]
{ "lock", "switch", "case", "if", "base", "catch", "class", "continue", "default" };

private Frame? _frame;

public Variable(Type variableType) : this(variableType, DefaultArgName(variableType))
Expand Down Expand Up @@ -85,6 +88,11 @@ public static Variable For<T>(string? variableName = null)

public static string SanitizeVariableName(string variableName)
{
if (_reservedNames.Contains(variableName))
{
return "@" + variableName;
}

return variableName.Replace('<', '_').Replace('>', '_');
}

Expand Down

0 comments on commit 4f4676a

Please sign in to comment.