Skip to content

Commit

Permalink
Merge pull request #28 from dotnet-campus/t/lvyi/case
Browse files Browse the repository at this point in the history
更准确判断 PascalCase 命名规则 + 消除所有警告
  • Loading branch information
kkwpsv authored Jul 29, 2020
2 parents d874bfb + 6e837a4 commit 67f160e
Show file tree
Hide file tree
Showing 32 changed files with 152 additions and 113 deletions.
2 changes: 1 addition & 1 deletion build/Version.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>3.2.0</Version>
<Version>3.2.1</Version>
</PropertyGroup>
</Project>
11 changes: 11 additions & 0 deletions src/dotnetCampus.CommandLine.Analyzer/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
; Shipped analyzer releases
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

## Release 3.2

### New Rules
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
DCL101 | dotnetCampus.Naming | Error | <https://github.com/dotnet-campus/dotnetCampus.CommandLine/blob/master/docs/analyzers/DCL101.md>
DCL201 | dotnetCampus.Usage | Hidden | <https://github.com/dotnet-campus/dotnetCampus.CommandLine/blob/master/docs/analyzers/DCL201.md>
DCL202 | dotnetCampus.Usage | Error | <https://github.com/dotnet-campus/dotnetCampus.CommandLine/blob/master/docs/analyzers/DCL202.md>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,18 @@ private void AnalyzeProperty(SyntaxNodeAnalysisContext context)
var argumentList = attributeSyntax.ChildNodes().OfType<AttributeArgumentListSyntax>().FirstOrDefault();
if (argumentList != null)
{
var expressions = argumentList.ChildNodes().OfType<AttributeArgumentSyntax>().Select(x => x.Expression);
foreach (var expressionSyntax in expressions)
var attributeArguments = argumentList.ChildNodes().OfType<AttributeArgumentSyntax>();
foreach (var attributeArgument in attributeArguments)
{
var expressionSyntax = attributeArgument.Expression;
var expression = expressionSyntax.ToString();
var nameEqualsExists = attributeArgument.ChildNodes().OfType<NameEqualsSyntax>().Any();
var longNameEqualsExists = attributeArgument.ChildNodes().OfType<NameEqualsSyntax>().Any(x => x.Name.ToString() == "LongName");
var mayBeLongName = !nameEqualsExists || longNameEqualsExists;
if (expression != null
&& expression.StartsWith("\"", StringComparison.OrdinalIgnoreCase)
&& expression.EndsWith("\"", StringComparison.OrdinalIgnoreCase))
&& expression.EndsWith("\"", StringComparison.OrdinalIgnoreCase)
&& mayBeLongName)
{
var value = expression.Substring(1, expression.Length - 2);
if (value.Length >= 2)
Expand Down
2 changes: 1 addition & 1 deletion src/dotnetCampus.CommandLine/CommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ private static string[] ConvertUrlToArgs(string url)
if (url != null)
{
url = HttpUtility.UrlDecode(url);
var start = url.IndexOf('?');
var start = url.IndexOf('?', StringComparison.OrdinalIgnoreCase);
if (start >= 0 && url != null)
{
var arguments = url.Substring(start + 1);
Expand Down
4 changes: 3 additions & 1 deletion src/dotnetCampus.CommandLine/CommandLineHandlers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#pragma warning disable CA1303 // 请不要将文本作为本地化参数传递

using System;
using System.Diagnostics.Contracts;
using System.Threading.Tasks;

Expand Down
13 changes: 13 additions & 0 deletions src/dotnetCampus.CommandLine/Compatibility/NET45Compatibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#if NETCOREAPP3_0 || NETCOREAPP3_1 || NETCOREAPP5_0 || NET5_0 || NET6_0
#else
namespace System
{
internal static class NET45Compatibility
{
public static int IndexOf(this string @string, char value, StringComparison stringComparison)
{
return @string.IndexOf(value.ToString(), stringComparison);
}
}
}
#endif
2 changes: 1 addition & 1 deletion src/dotnetCampus.CommandLine/Core/SingleOptimizedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class SingleOptimizedList<T> : IReadOnlyList<T>
private List<T>? _restValues;

/// <summary>
/// 创建带有一个值的 <see cref="SingleOptimizedList"/> 的实例。
/// 创建带有一个值的 <see cref="SingleOptimizedList{T}"/> 的实例。
/// </summary>
/// <param name="firstValue"></param>
public SingleOptimizedList(T firstValue)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#pragma warning disable CA1303 // 请不要将文本作为本地化参数传递

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -39,7 +41,7 @@ public ImmutableRuntimeOptionParser(string? verb, IReadOnlyList<PropertyInfo> at
_shortNameDictionary[attribute.ShortName.Value] = propertyInfo;
}

if (string.IsNullOrWhiteSpace(attribute.LongName))
if (attribute.LongName is null || string.IsNullOrWhiteSpace(attribute.LongName))
{
_longNameDictionary[propertyInfo.Name] = propertyInfo;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#pragma warning disable CA1303 // 请不要将文本作为本地化参数传递

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public RuntimeOptionParser(string? verb, IReadOnlyList<PropertyInfo> attributedP
_shortNameDictionary[attribute.ShortName.Value] = propertyInfo;
}

if (string.IsNullOrWhiteSpace(attribute.LongName))
if (attribute.LongName is null || string.IsNullOrWhiteSpace(attribute.LongName))
{
_longNameDictionary[propertyInfo.Name] = propertyInfo;
}
Expand Down
6 changes: 4 additions & 2 deletions src/dotnetCampus.CommandLine/Standard/GnuOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#pragma warning disable CA1303 // 请不要将文本作为本地化参数传递

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
Expand Down Expand Up @@ -147,7 +149,7 @@ private static string GetLocalizedDescription(CommandLineAttribute attribute, Re
: attribute.Description ?? "";

private static string GetLocalizedDescription(CommandLineAttribute attribute, LocalizableStrings resourceManager)
=> !string.IsNullOrWhiteSpace(attribute.LocalizableDescription)
=> attribute.LocalizableDescription != null && !string.IsNullOrWhiteSpace(attribute.LocalizableDescription)
? resourceManager.GetString(attribute.LocalizableDescription, CultureInfo.CurrentUICulture) ?? ""
: attribute.Description ?? "";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#pragma warning disable CA1303 // 请不要将文本作为本地化参数传递

using System;
using System.Collections.Generic;

using dotnetCampus.Cli.Core;
Expand Down Expand Up @@ -143,7 +145,7 @@ private void SetOption(string? option)
return;
}

var valueSplitIndex = option.IndexOf(':');
var valueSplitIndex = option.IndexOf(':', StringComparison.OrdinalIgnoreCase);
if (valueSplitIndex < 0 || valueSplitIndex >= option.Length - 1)
{
// -k value
Expand Down
4 changes: 3 additions & 1 deletion src/dotnetCampus.CommandLine/Utils/CommandLineHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#pragma warning disable CA1303 // 请不要将文本作为本地化参数传递

using System;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Runtime.CompilerServices;
Expand Down
47 changes: 24 additions & 23 deletions src/dotnetCampus.CommandLine/Utils/NamingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,13 @@ internal static class NamingHelper
internal static bool CheckIsPascalCase(string value)
{
var first = value[0];
if (!char.IsUpper(first))
if (char.IsLower(first))
{
return false;
}

foreach (var letter in value)
{
if (!char.IsLetterOrDigit(letter))
{
return false;
}
}

if (value.Length >= 3)
{
var allUpper = value.All(x => char.IsUpper(x));
if (allUpper)
{
return false;
}
}

return true;
var testName = MakePascalCase(value);
return string.Equals(value, testName, StringComparison.Ordinal);
}

internal static string MakePascalCase(string oldName)
Expand All @@ -64,18 +48,27 @@ internal static string MakePascalCase(string oldName)
isWordStart = true;
continue;
}
else if (!char.IsUpper(c))
else if (char.IsLower(c))
{
// 小写字母。
isFirstLetter = false;
isWordStart = false;
builder.Append(char.ToUpperInvariant(c));
}
else
else if (char.IsUpper(c))
{
// 大写字母。
isFirstLetter = false;
isWordStart = false;
builder.Append(c);
}
else
{
// 无大小写,但可作为标识符的字符(对 char 来说也视为字母)。
isFirstLetter = false;
isWordStart = true;
builder.Append(c);
}
}
else
{
Expand All @@ -85,18 +78,26 @@ internal static string MakePascalCase(string oldName)
isWordStart = true;
builder.Append(c);
}
else if (!char.IsUpper(c))
else if (char.IsLower(c))
{
// 小写字母。
builder.Append(isWordStart
? char.ToUpperInvariant(c)
: c);
isWordStart = false;
}
else
else if (char.IsUpper(c))
{
// 大写字母。
isWordStart = false;
builder.Append(c);
}
else
{
// 无大小写,但可作为标识符的字符(对 char 来说也视为字母)。
isWordStart = true;
builder.Append(c);
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/dotnetCampus.CommandLine/ValueAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#pragma warning disable CA1303 // 请不要将文本作为本地化参数传递

using System;

namespace dotnetCampus.Cli
{
Expand Down
4 changes: 3 additions & 1 deletion src/dotnetCampus.CommandLine/VerbAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#pragma warning disable CA1303 // 请不要将文本作为本地化参数传递

using System;

namespace dotnetCampus.Cli
{
Expand Down
8 changes: 2 additions & 6 deletions src/dotnetCampus.CommandLine/dotnetCampus.CommandLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
<Description>dotnetCampus.CommandLine is probably the fastest command line parser in all .NET open-source projects. Parsing a classical command line only takes 1091ns, thus 10 ticks.</Description>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' Or '$(TargetFramework)' == 'netstandard2.0' ">
<WarningLevel>0</WarningLevel>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.0.0" PrivateAssets="all" />
Expand All @@ -24,8 +20,8 @@
<Reference Include="System.Web" />
</ItemGroup>

<ItemGroup>
<ProjectReference Condition=" '$(TargetFramework)' == 'netcoreapp3.1' " Include="..\dotnetCampus.CommandLine.Analyzer\dotnetCampus.CommandLine.Analyzer\dotnetCampus.CommandLine.Analyzer.csproj" ReferenceOutputAssembly="False" />
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
<ProjectReference Include="..\dotnetCampus.CommandLine.Analyzer\dotnetCampus.CommandLine.Analyzer.csproj" ReferenceOutputAssembly="False" />
</ItemGroup>

<Target Name="IncludeAllDependencies" BeforeTargets="_GetPackageFiles">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ComparedOptions
/// 表示通过打开的文件路径。此属性可能为 null,但绝不会是空字符串或空白字符串。
/// </summary>
[Value(0), Option('f', "file")]
public string FilePath { get; set; }
public string? FilePath { get; set; }

/// <summary>
/// 当此参数值为 true 时,表示此进程是从 Cloud 端启动的 Shell 进程。此属性默认值是 false。
Expand All @@ -23,7 +23,7 @@ public class ComparedOptions
/// 表示 Shell 端启动的模式。此属性可能为 null,但绝不会是空字符串或空白字符串。
/// </summary>
[Option('m', "mode")]
public string StartupMode { get; set; }
public string? StartupMode { get; set; }

/// <summary>
/// 表示当前是否是静默方式启动,通常由 Shell 启动 Cloud 时使用。此属性默认值是 false。
Expand All @@ -41,12 +41,12 @@ public class ComparedOptions
/// 表示当前窗口启动时应该安放的位置。此属性可能为 null,但绝不会是空字符串或空白字符串。
/// </summary>
[Option('p', "placement")]
public string Placement { get; set; }
public string? Placement { get; set; }

/// <summary>
/// 表示一个启动会话 Id,用于在多个进程间同步一些信息。此属性可能为 null,但绝不会是空字符串或空白字符串。
/// </summary>
[Option("startup-session")]
public string StartupSession { get; set; }
public string? StartupSession { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void ParseValues()
// Assert
Assert.AreEqual("foo", options.Foo);
Assert.AreEqual(8, options.LongValue);
CollectionAssert.AreEqual(new[] { "x", "y" }, (ICollection)options.Values);
CollectionAssert.AreEqual(new[] { "x", "y" }, (ICollection?)options.Values);
Assert.AreEqual(2, options.Int32Value);
}).WithArguments(
new[] { "8", "x", "y", "2", "-f", "foo" },
Expand All @@ -38,7 +38,7 @@ public void ParseValues()
// Assert
Assert.AreEqual("foo", options.Foo);
Assert.AreEqual(-8, options.LongValue);
CollectionAssert.AreEqual(new[] { "-x", "-y" }, (ICollection)options.Values);
CollectionAssert.AreEqual(new[] { "-x", "-y" }, (ICollection?)options.Values);
Assert.AreEqual(-2, options.Int32Value);
}).WithArguments(
new[] { "-f", "foo", "--", "-8", "-x", "-y", "-2" }
Expand All @@ -53,7 +53,7 @@ public void ParseValues()
// Assert
Assert.AreEqual("foo", options.Section);
Assert.AreEqual(8, options.Count);
CollectionAssert.AreEqual(new[] { "dcl.exe", "--foo", "xyz", "-s", "some", "2" }, (ICollection)options.Args);
CollectionAssert.AreEqual(new[] { "dcl.exe", "--foo", "xyz", "-s", "some", "2" }, (ICollection?)options.Args);
}).WithArguments(
new[] { "-s", "foo", "--", "8", "dcl.exe", "--foo", "xyz", "-s", "some", "2" }
);
Expand Down
Loading

0 comments on commit 67f160e

Please sign in to comment.