Skip to content

Commit

Permalink
Updating C# language feature code.
Browse files Browse the repository at this point in the history
  • Loading branch information
GinoCanessa committed Jun 2, 2023
1 parent 9d014e6 commit 61e61bc
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 104 deletions.
42 changes: 30 additions & 12 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
end_of_line = crlf
dotnet_style_prefer_inferred_tuple_names = true:suggestion

[project.json]
indent_size = 2
Expand Down Expand Up @@ -53,15 +67,15 @@ dotnet_style_predefined_type_for_member_access = true:suggestion
# name all constant fields using PascalCase
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.required_modifiers = const
dotnet_naming_style.pascal_case_style.capitalization = pascal_case

# static fields should have s_ prefix
dotnet_naming_rule.static_fields_should_have_prefix.severity = suggestion
dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields
dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style
dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style
dotnet_naming_symbols.static_fields.applicable_kinds = field
dotnet_naming_symbols.static_fields.required_modifiers = static
dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected
Expand All @@ -71,7 +85,7 @@ dotnet_naming_style.static_prefix_style.capitalization = camel_case
# internal and private fields should be _camelCase
dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion
dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields
dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
dotnet_naming_symbols.private_internal_fields.applicable_kinds = field
dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal
dotnet_naming_style.camel_case_underscore_style.required_prefix = _
Expand All @@ -80,7 +94,7 @@ dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case
# Code style defaults
csharp_using_directive_placement = outside_namespace:suggestion
dotnet_sort_system_directives_first = true
csharp_prefer_braces = true:refactoring
csharp_prefer_braces = true:silent
csharp_preserve_single_line_blocks = true:none
csharp_preserve_single_line_statements = false:none
csharp_prefer_static_local_function = true:suggestion
Expand All @@ -106,14 +120,14 @@ dotnet_style_prefer_conditional_expression_over_return = true:refactoring
csharp_prefer_simple_default_expression = true:suggestion

# Expression-bodied members
csharp_style_expression_bodied_methods = true:refactoring
csharp_style_expression_bodied_constructors = true:refactoring
csharp_style_expression_bodied_operators = true:refactoring
csharp_style_expression_bodied_properties = true:refactoring
csharp_style_expression_bodied_indexers = true:refactoring
csharp_style_expression_bodied_accessors = true:refactoring
csharp_style_expression_bodied_lambdas = true:refactoring
csharp_style_expression_bodied_local_functions = true:refactoring
csharp_style_expression_bodied_methods = true:silent
csharp_style_expression_bodied_constructors = true:silent
csharp_style_expression_bodied_operators = true:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = true:silent

# Pattern matching
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
Expand Down Expand Up @@ -155,6 +169,10 @@ csharp_space_between_square_brackets = false

# Analyzers
dotnet_code_quality.ca1802.api_surface = private, internal
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_allow_embedded_statements_on_same_line_experimental = true:silent

# C++ Files
[*.{cpp,h,in}]
Expand Down
8 changes: 2 additions & 6 deletions src/FhirCodeGenBlazor/Pages/ScriptPackage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ else
<MudExpansionPanel Text="Advanced Filtering" IsInitiallyExpanded="true">
<MudGrid>
<MudItem xs="12">
<StandaloneCodeEditor @ref="_editor" Id="script-code-editor" ConstructionOptions="EditorConstructionOptions" OnDidInit="EditorOnDidInit" />
<StandaloneCodeEditor @ref="_editor" Id="script-code-editor" ConstructionOptions="EditorConstructionOptions" />
</MudItem>
<MudItem xs="12">
For details and examples, please see the
Expand Down Expand Up @@ -68,7 +68,7 @@ else
</MudExpansionPanel>
</MudExpansionPanels>

<StandaloneCodeEditor @ref="_resultEditor" Id="script-result-editor" ConstructionOptions="ResultConstructionOptions" OnDidInit="EditorOnDidInit" />
<StandaloneCodeEditor @ref="_resultEditor" Id="script-result-editor" ConstructionOptions="ResultConstructionOptions" />

}

Expand Down Expand Up @@ -199,10 +199,6 @@ else
}
}

private async Task EditorOnDidInit()
{
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged
/// resources.
Expand Down
3 changes: 2 additions & 1 deletion src/FhirCodeGenBlazor/Services/SpecManagerWebService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using Microsoft.Health.Fhir.SpecManager.Manager;
using System.Collections;
using System.Diagnostics.CodeAnalysis;

namespace FhirCodeGenBlazor.Services;

Expand Down Expand Up @@ -134,7 +135,7 @@ bool IReadOnlyDictionary<string, FhirVersionInfo>.ContainsKey(string key) =>
/// <see cref="T:System.Collections.Generic.IReadOnlyDictionary`2" /> interface contains an
/// element that has the specified key; otherwise, <see langword="false" />.
/// </returns>
bool IReadOnlyDictionary<string, FhirVersionInfo>.TryGetValue(string key, out FhirVersionInfo value) =>
bool IReadOnlyDictionary<string, FhirVersionInfo>.TryGetValue(string key, [MaybeNullWhen(false)] out FhirVersionInfo value) =>
FhirManager.Current.InfoByDirective.TryGetValue(key, out value);

/// <summary>Returns an enumerator that iterates through the collection.</summary>
Expand Down
11 changes: 2 additions & 9 deletions src/Microsoft.Health.Fhir.CodeGenCommon/Models/FhirComplex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,7 @@ public void AddRootElement(FhirElement element)
/// <param name="element">The element.</param>
public void AddContextElement(string element)
{
if (_contextElements == null)
{
_contextElements = new List<string>();
}

_contextElements ??= new List<string>();
_contextElements.Add(element);
}

Expand Down Expand Up @@ -839,12 +835,9 @@ public FhirComplex DeepCopy(
valueSetReferences,
typeMapByPath);

if (typeMapByPath != null)
{
typeMapByPath.Add(
typeMapByPath?.Add(
node.Path,
new FhirNodeInfo(FhirNodeInfo.FhirNodeType.Component, node));
}

complex.Components.Add(
kvp.Key,
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.Health.Fhir.CodeGenCommon/Models/FhirElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public FhirElement(
foreach (string code in codeValues)
{
string clean = code.Trim();
if (clean.Contains(" ", StringComparison.Ordinal))
if (clean.Contains(' ', StringComparison.Ordinal))
{
clean = clean.Substring(0, clean.IndexOf(" ", StringComparison.Ordinal));
}
Expand Down Expand Up @@ -681,7 +681,7 @@ public FhirElement CopyWith(
/// <returns>A copy of this object.</returns>
public object Clone()
{
return this.CopyWith();
return CopyWith();
}

/// <summary>Deep copy.</summary>
Expand Down
16 changes: 4 additions & 12 deletions src/Microsoft.Health.Fhir.CodeGenCommon/Models/FhirTypeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,13 @@ public string TypeForExport(
return primitiveTypeMap[_baseTypeName];
}

string baseType;

// Resources cannot inherit patterns, but they are listed that way today
// see https://chat.fhir.org/#narrow/stream/179177-conformance/topic/Inheritance.20and.20Cardinality.20Changes
switch (_baseTypeName)
string baseType = _baseTypeName switch
{
case "CanonicalResource":
case "MetadataResource":
baseType = "DomainResource";
break;

default:
baseType = _baseTypeName;
break;
}
"CanonicalResource" or "MetadataResource" => "DomainResource",
_ => _baseTypeName,
};

string type = FhirUtils.ToConvention(
baseType,
Expand Down
53 changes: 30 additions & 23 deletions src/Microsoft.Health.Fhir.CodeGenCommon/Models/FhirUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,27 @@
using System.Text.RegularExpressions;
using Microsoft.Health.Fhir.CodeGenCommon.Extensions;
using static Microsoft.Health.Fhir.CodeGenCommon.Models.FhirTypeBase;
using System;

namespace Microsoft.Health.Fhir.CodeGenCommon.Models;

/// <summary>A fhir utilities.</summary>
public abstract class FhirUtils
public abstract partial class FhirUtils
{
/// <summary>The RegEx remove duplicate lines.</summary>
private const string _regexRemoveDuplicateLinesDefinition = "__+";
///// <summary>The RegEx remove duplicate lines.</summary>
//private const string RegexRemoveDuplicateLinesDefinition = "__+";

/// <summary>(Immutable) The RegEx remove duplicate whitespace.</summary>
private const string _regexRemoveDuplicateWhitespaceDefinition = "\\s+";
///// <summary>(Immutable) The RegEx remove duplicate whitespace.</summary>
//private const string RegexRemoveDuplicateWhitespaceDefinition = "\\s+";

/// <summary>The RegEx remove duplicate lines.</summary>
private static Regex _regexRemoveDuplicateLines = new Regex(_regexRemoveDuplicateLinesDefinition);
private static readonly Regex _regexRemoveDuplicateLines = RegexRemoveDuplicateLines();

/// <summary>The RegEx remove duplicate whitespace.</summary>
private static Regex _regexRemoveDuplicateWhitespace = new Regex(_regexRemoveDuplicateWhitespaceDefinition);
private static readonly Regex _regexRemoveDuplicateWhitespace = RegexRemoveDuplicateWhitespace();

/// <summary>The RegEx ASCII escaping.</summary>
private static Regex _regexAsciiEscaping = new Regex("[^ -~]+");
private static readonly Regex _regexAsciiEscaping = RegexAsciiEscapingR();

/// <summary>(Immutable) The underscore.</summary>
public static readonly Dictionary<char[], string> ReplacementsWithUnderscores = new(ReplacementComparer.Default)
Expand Down Expand Up @@ -171,11 +172,8 @@ public abstract class FhirUtils
/// <summary>A replacement comparer.</summary>
private class ReplacementComparer : IEqualityComparer<char[]>
{
/// <summary>The default.</summary>
private static ReplacementComparer _default = new();

/// <summary>Gets the default.</summary>
public static ReplacementComparer Default => _default;
public static ReplacementComparer Default { get; } = new();

/// <summary>Determines whether the specified objects are equal.</summary>
/// <param name="a">The first object of type <paramref name="T" /> to compare.</param>
Expand Down Expand Up @@ -219,7 +217,7 @@ public static string ToConvention(
{
string value;

if (concatenatePath && (!string.IsNullOrEmpty(path)))
if (concatenatePath && !string.IsNullOrEmpty(path))
{
value = path;
}
Expand Down Expand Up @@ -395,7 +393,7 @@ public static void SanitizeForCode(

name = input.Trim();

if (name.Contains(" "))
if (name.Contains(' ', StringComparison.Ordinal))
{
name = name.Substring(0, name.IndexOf(' '));
}
Expand Down Expand Up @@ -490,19 +488,19 @@ public static string SanitizeForProperty(

if (value.StartsWith("http://hl7.org/fhir/", StringComparison.Ordinal))
{
value = "FHIR_" + value.Substring(20);
value = string.Concat("FHIR_", value.AsSpan(20));
}
else if (value.StartsWith("http://hl7.org/fhirpath/", StringComparison.Ordinal))
{
value = "FHIRPath_" + value.Substring(24);
value = string.Concat("FHIRPath_", value.AsSpan(24));
}
else if (value.StartsWith("http://terminology.hl7.org/", StringComparison.Ordinal))
{
value = "THO_" + value.Substring(27);
value = string.Concat("THO_", value.AsSpan(27));
}
else if (value.StartsWith("http://hl7.org/", StringComparison.Ordinal))
{
value = "HL7_" + value.Substring(15);
value = string.Concat("HL7_", value.AsSpan(15));
}
else if (value.StartsWith("https://"))
{
Expand All @@ -514,15 +512,15 @@ public static string SanitizeForProperty(
}
else if (value.StartsWith("urn:oid:"))
{
value = "OID_" + value.Substring(8);
value = string.Concat("OID_", value.AsSpan(8));
}
else if (value.StartsWith("urn:uuid:"))
{
value = "UUID_" + value.Substring(9);
value = string.Concat("UUID_", value.AsSpan(9));
}
else if (value.StartsWith('/'))
{
value = "Per" + value.Substring(1);
value = string.Concat("Per", value.AsSpan(1));
}

if (checkForGmt)
Expand All @@ -533,7 +531,7 @@ public static string SanitizeForProperty(
}
}

replacements = replacements ?? ReplacementsWithUnderscores;
replacements ??= ReplacementsWithUnderscores;

char[] chars = value.Normalize(NormalizationForm.FormD).ToCharArray();

Expand Down Expand Up @@ -639,7 +637,7 @@ public static string SanitizeForProperty(

// need to check for all digits or underscores, or reserved word
if (RequiresAlpha(value) ||
((reservedWords != null) && reservedWords.Contains(value)))
reservedWords != null && reservedWords.Contains(value))
{
if (value[0] == '_')
{
Expand All @@ -651,4 +649,13 @@ public static string SanitizeForProperty(

return value;
}

[GeneratedRegex("__+")]
private static partial Regex RegexRemoveDuplicateLines();

[GeneratedRegex("\\s+")]
private static partial Regex RegexRemoveDuplicateWhitespace();

[GeneratedRegex("[^ -~]+")]
private static partial Regex RegexAsciiEscapingR();
}
Loading

0 comments on commit 61e61bc

Please sign in to comment.