Navigation Menu

Skip to content

Commit

Permalink
Ensuring only valid PascalCharacters (#99)
Browse files Browse the repository at this point in the history
* Ensuring only valid PascalCharacters

Fixes #80

Questions: Is it acceptable to only allow upper/lower/number for naming on methods/properties/fields? Any counter examples?

* Updating unit tests to 3.1

Setting pipeline to install .NET Core 3.1
  • Loading branch information
Keboo committed Mar 19, 2020
1 parent ded7fab commit d57bfb4
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 7 deletions.
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<NoWarn>CA2007,CA1815,CA1303,CA1707,CA1305</NoWarn>
</PropertyGroup>

Expand Down
Expand Up @@ -55,7 +55,6 @@ class NativeMethods
VerifyCSharpDiagnostic(test);
}


[TestMethod]
public void FieldWithNamingViolation_FieldMissingLeadingUnderscore_Warning()
{
Expand Down Expand Up @@ -418,6 +417,41 @@ class TypeName
VerifyCSharpDiagnostic(test);
}


[TestMethod]
[Description("Issue 80")]
public void FieldWithNamingViolation_FieldContainsUnderscore_Warning()
{
string test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TypeName
{
public string _My_Field;
}
}";
var expected = new DiagnosticResult
{
Id = "INTL0001",
Message = "Field '_My_Field' should be named _PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
new DiagnosticResultLocation("Test0.cs", 13, 27)
}
};

VerifyCSharpDiagnostic(test, expected);
}


protected override CodeFixProvider GetCSharpCodeFixProvider()
{
return new CodeFixes.NamingFieldPascalUnderscore();
Expand Down
Expand Up @@ -322,6 +322,45 @@ public class Views_Shared__ValidationScriptsPartial : global::Microsoft.AspNetCo
VerifyCSharpDiagnostic(test);
}


[TestMethod]
[Description("Issue 80")]
public void MethodWithNamingViolation_MethodWithUnderscore_Warning()
{
string test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TypeName
{
public string My_Method()
{
return string.Empty;
}
}
}";
var expected = new DiagnosticResult
{
Id = "INTL0003",
Message = "Method 'My_Method' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
new DiagnosticResultLocation("Test0.cs", 13, 27)
}
};

VerifyCSharpDiagnostic(test, expected);
}



protected override CodeFixProvider GetCSharpCodeFixProvider()
{
return new CodeFixes.NamingIdentifierPascal();
Expand Down
Expand Up @@ -265,6 +265,40 @@ public class Views_Shared__ValidationScriptsPartial : global::Microsoft.AspNetCo
VerifyCSharpDiagnostic(test);
}

[TestMethod]
[Description("Issue 80")]
public void PropertyWithNamingViolation_PropertyContainsUnderscore_Warning()
{
string test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TypeName
{
public string My_Property { get; set; }
}
}";
var expected = new DiagnosticResult
{
Id = "INTL0002",
Message = "Property 'My_Property' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
new DiagnosticResultLocation("Test0.cs", 13, 27)
}
};

VerifyCSharpDiagnostic(test, expected);
}


protected override CodeFixProvider GetCSharpCodeFixProvider()
{
return new CodeFixes.NamingIdentifierPascal();
Expand Down
@@ -1,6 +1,7 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using IntelliTect.Analyzer.Naming;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

Expand Down Expand Up @@ -57,7 +58,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context)
}

if (namedTypeSymbol.Name.StartsWith("_", StringComparison.Ordinal) && namedTypeSymbol.Name.Length > 1
&& char.IsUpper(namedTypeSymbol.Name.Skip(1).First()))
&& Casing.IsPascalCase(namedTypeSymbol.Name.Skip(1)))
{
return;
}
Expand Down
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using IntelliTect.Analyzer.Naming;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -66,7 +67,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context)
return;
}

if (char.IsUpper(namedTypeSymbol.Name.First()))
if (Casing.IsPascalCase(namedTypeSymbol.Name))
{
return;
}
Expand Down
@@ -1,6 +1,7 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using IntelliTect.Analyzer.Naming;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

Expand Down Expand Up @@ -44,17 +45,17 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context)
return;
}

if (char.IsUpper(namedTypeSymbol.Name.First()))
if (namedTypeSymbol.ContainingType.IsNativeMethodsClass())
{
return;
}

if (namedTypeSymbol.ContainingType.IsNativeMethodsClass())
if (namedTypeSymbol is IPropertySymbol property && property.IsIndexer)
{
return;
}

if (namedTypeSymbol is IPropertySymbol property && property.IsIndexer)
if (Casing.IsPascalCase(namedTypeSymbol.Name))
{
return;
}
Expand Down
29 changes: 29 additions & 0 deletions IntelliTect.Analyzer/IntelliTect.Analyzer/Naming/Casing.cs
@@ -0,0 +1,29 @@
using System.Collections.Generic;

namespace IntelliTect.Analyzer.Naming
{
internal static class Casing
{
public static bool IsPascalCase(IEnumerable<char> name)
{
bool isFirst = true;
foreach (char character in name)
{
if (isFirst && !char.IsUpper(character))
{
return false;
}

isFirst = false;
if (char.IsUpper(character) ||
char.IsLower(character) ||
char.IsNumber(character))
{
continue;
}
return false;
}
return true;
}
}
}
5 changes: 5 additions & 0 deletions azure-pipelines.yml
Expand Up @@ -14,6 +14,11 @@ pool:

steps:

- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '3.1.x'

- task: NuGetToolInstaller@0
displayName: 'Use latest NuGet'

Expand Down

0 comments on commit d57bfb4

Please sign in to comment.