Skip to content

Commit

Permalink
Merge pull request #337 from las-nsc/las/keyword-types-with-short-names
Browse files Browse the repository at this point in the history
Keyword types when using `--compactTypeNames`
  • Loading branch information
mganss authored Jun 8, 2022
2 parents 50adb75 + efef426 commit 4df6db1
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Microsoft.CSharp;

using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -172,8 +174,7 @@ public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel
var classModel = typeModel as ClassModel;
var propBackingFieldName = propertyModel.Name.ToBackingField(classModel?.Configuration.PrivateMemberPrefix);

if (!IsValidIdentifier(propBackingFieldName.ToLower()))
propBackingFieldName = "@" + propBackingFieldName;
propBackingFieldName = CSharp.CreateEscapedIdentifier(propBackingFieldName);

if (classModel == null)
return propBackingFieldName;
Expand Down Expand Up @@ -214,7 +215,7 @@ public static string GetUniquePropertyName(this TypeModel tm, string name)

internal static string NormalizeNewlines(string text) => NormalizeNewlinesRegex.Replace(text, "$1\r\n");

private static readonly Predicate<string> IsValidIdentifier = new Microsoft.CSharp.CSharpCodeProvider().IsValidIdentifier;
private static readonly CSharpCodeProvider CSharp = new();

internal static Uri CreateUri(string uri) => string.IsNullOrEmpty(uri) ? null : new Uri(uri);

Expand Down Expand Up @@ -255,7 +256,9 @@ public static bool IsUsingNamespace(string namespaceName, GeneratorConfiguration

public static CodeTypeReference CreateTypeReference(Type type, GeneratorConfiguration conf)
{
if (IsUsingNamespace(type.Namespace, conf))
// If the type is a keyword it will prefix it with an @ to make it a valid identifier.
var isKeyword = CSharp.CreateEscapedIdentifier(CSharp.GetTypeOutput(new(type)))[0] == '@';
if (!isKeyword && IsUsingNamespace(type.Namespace, conf))
{
var typeRef = new CodeTypeReference(type.Name, conf.CodeTypeReferenceOptions);

Expand Down

0 comments on commit 4df6db1

Please sign in to comment.