-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ToLowerInvariant Or ToUpperInvariant Analyzer (#2186)
- Loading branch information
1 parent
5b0b341
commit 334bb06
Showing
28 changed files
with
1,152 additions
and
5 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...arp/Microsoft.NetCore.Analyzers/Runtime/CSharpSpecifyCultureForToLowerAndToUpper.Fixer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Composition; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Editing; | ||
using Microsoft.CodeAnalysis.Formatting; | ||
using Microsoft.NetCore.Analyzers.Runtime; | ||
|
||
namespace Microsoft.NetCore.CSharp.Analyzers.Runtime | ||
{ | ||
[ExportCodeFixProvider(LanguageNames.CSharp), Shared] | ||
public class CSharpSpecifyCultureForToLowerAndToUpperFixer : SpecifyCultureForToLowerAndToUpperFixerBase | ||
{ | ||
protected override bool ShouldFix(SyntaxNode node) | ||
{ | ||
return node.IsKind(SyntaxKind.IdentifierName) && | ||
(node.Parent?.IsKind(SyntaxKind.SimpleMemberAccessExpression) == true || node.Parent?.IsKind(SyntaxKind.MemberBindingExpression) == true); | ||
} | ||
|
||
protected override async Task<Document> SpecifyCurrentCultureAsync(Document document, SyntaxGenerator generator, SyntaxNode root, SyntaxNode node, CancellationToken cancellationToken) | ||
{ | ||
if (node.IsKind(SyntaxKind.IdentifierName) && node.Parent?.FirstAncestorOrSelf<InvocationExpressionSyntax>() is InvocationExpressionSyntax invocation) | ||
{ | ||
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); | ||
if (model.GetSymbolInfo((IdentifierNameSyntax)node, cancellationToken).Symbol is IMethodSymbol methodSymbol && methodSymbol.Parameters.Length == 0) | ||
{ | ||
var newArg = generator.Argument(CreateCurrentCultureMemberAccess(generator, model)).WithAdditionalAnnotations(Formatter.Annotation); | ||
var newInvocation = invocation.AddArgumentListArguments((ArgumentSyntax)newArg).WithAdditionalAnnotations(Formatter.Annotation); | ||
var newRoot = root.ReplaceNode(invocation, newInvocation); | ||
return document.WithSyntaxRoot(newRoot); | ||
} | ||
} | ||
|
||
return document; | ||
} | ||
|
||
protected override Task<Document> UseInvariantVersionAsync(Document document, SyntaxGenerator generator, SyntaxNode root, SyntaxNode node) | ||
{ | ||
if (node.IsKind(SyntaxKind.IdentifierName)) | ||
{ | ||
if (node.Parent is MemberAccessExpressionSyntax memberAccess) | ||
{ | ||
var replacementMethodName = GetReplacementMethodName(memberAccess.Name.Identifier.Text); | ||
var newMemberAccess = memberAccess.WithName((SimpleNameSyntax)generator.IdentifierName(replacementMethodName)).WithAdditionalAnnotations(Formatter.Annotation); | ||
var newRoot = root.ReplaceNode(memberAccess, newMemberAccess); | ||
return Task.FromResult(document.WithSyntaxRoot(newRoot)); | ||
} | ||
|
||
if (node.Parent is MemberBindingExpressionSyntax memberBinding) | ||
{ | ||
var replacementMethodName = GetReplacementMethodName(memberBinding.Name.Identifier.Text); | ||
var newMemberBinding = memberBinding.WithName((SimpleNameSyntax)generator.IdentifierName(replacementMethodName)).WithAdditionalAnnotations(Formatter.Annotation); | ||
var newRoot = root.ReplaceNode(memberBinding, newMemberBinding); | ||
return Task.FromResult(document.WithSyntaxRoot(newRoot)); | ||
} | ||
} | ||
|
||
return Task.FromResult(document); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...rs/CSharp/Microsoft.NetCore.Analyzers/Runtime/CSharpSpecifyCultureForToLowerAndToUpper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Diagnostics; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.NetCore.Analyzers.Runtime; | ||
|
||
namespace Microsoft.NetCore.CSharp.Analyzers.Runtime | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class CSharpSpecifyCultureForToLowerAndToUpperAnalyzer : SpecifyCultureForToLowerAndToUpperAnalyzer | ||
{ | ||
protected override Location GetMethodNameLocation(SyntaxNode invocationNode) | ||
{ | ||
Debug.Assert(invocationNode.IsKind(SyntaxKind.InvocationExpression)); | ||
|
||
var invocation = (InvocationExpressionSyntax)invocationNode; | ||
if (invocation.Expression is MemberAccessExpressionSyntax memberAccess) | ||
{ | ||
return memberAccess.Name.GetLocation(); | ||
} | ||
else if (invocation.Expression is MemberBindingExpressionSyntax memberBinding) | ||
{ | ||
return memberBinding.Name.GetLocation(); | ||
} | ||
return invocation.GetLocation(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...zers/Core/Microsoft.NetCore.Analyzers/Runtime/SpecifyCultureForToLowerAndToUpper.Fixer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Immutable; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Analyzer.Utilities; | ||
using Analyzer.Utilities.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.Editing; | ||
|
||
namespace Microsoft.NetCore.Analyzers.Runtime | ||
{ | ||
public abstract class SpecifyCultureForToLowerAndToUpperFixerBase : CodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(SpecifyCultureForToLowerAndToUpperAnalyzer.RuleId); | ||
|
||
public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
var node = root.FindNode(context.Span); | ||
|
||
if (ShouldFix(node)) | ||
{ | ||
var generator = SyntaxGenerator.GetGenerator(context.Document); | ||
|
||
var title = MicrosoftNetCoreAnalyzersResources.SpecifyCurrentCulture; | ||
context.RegisterCodeFix(CodeAction.Create(title, | ||
async ct => await SpecifyCurrentCultureAsync(context.Document, generator, root, node, ct).ConfigureAwait(false), | ||
equivalenceKey: nameof(MicrosoftNetCoreAnalyzersResources.SpecifyCurrentCulture)), | ||
context.Diagnostics); | ||
|
||
title = MicrosoftNetCoreAnalyzersResources.UseInvariantVersion; | ||
context.RegisterCodeFix(CodeAction.Create(title, | ||
async ct => await UseInvariantVersionAsync(context.Document, generator, root, node).ConfigureAwait(false), | ||
equivalenceKey: nameof(MicrosoftNetCoreAnalyzersResources.UseInvariantVersion)), | ||
context.Diagnostics); | ||
} | ||
} | ||
|
||
protected abstract bool ShouldFix(SyntaxNode node); | ||
|
||
protected abstract Task<Document> SpecifyCurrentCultureAsync(Document document, SyntaxGenerator generator, SyntaxNode root, SyntaxNode node, CancellationToken cancellationToken); | ||
|
||
protected static SyntaxNode CreateCurrentCultureMemberAccess(SyntaxGenerator generator, SemanticModel model) | ||
{ | ||
var cultureInfoType = model.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemGlobalizationCultureInfo)!; | ||
return generator.MemberAccessExpression( | ||
generator.TypeExpressionForStaticMemberAccess(cultureInfoType), | ||
generator.IdentifierName("CurrentCulture")); | ||
} | ||
|
||
protected abstract Task<Document> UseInvariantVersionAsync(Document document, SyntaxGenerator syntaxGenerator, SyntaxNode root, SyntaxNode node); | ||
|
||
protected static string GetReplacementMethodName(string currentMethodName) => currentMethodName switch | ||
{ | ||
SpecifyCultureForToLowerAndToUpperAnalyzer.ToLowerMethodName => "ToLowerInvariant", | ||
SpecifyCultureForToLowerAndToUpperAnalyzer.ToUpperMethodName => "ToUpperInvariant", | ||
_ => currentMethodName, | ||
}; | ||
|
||
public sealed override FixAllProvider GetFixAllProvider() | ||
{ | ||
return WellKnownFixAllProviders.BatchFixer; | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SpecifyCultureForToLowerAndToUpper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Immutable; | ||
using Analyzer.Utilities; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.NetAnalyzers; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace Microsoft.NetCore.Analyzers.Runtime | ||
{ | ||
using static MicrosoftNetCoreAnalyzersResources; | ||
|
||
public abstract class SpecifyCultureForToLowerAndToUpperAnalyzer : AbstractGlobalizationDiagnosticAnalyzer | ||
{ | ||
internal const string RuleId = "CA1311"; | ||
|
||
internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create( | ||
RuleId, | ||
CreateLocalizableResourceString(nameof(SpecifyCultureForToLowerAndToUpperTitle)), | ||
CreateLocalizableResourceString(nameof(SpecifyCultureForToLowerAndToUpperMessage)), | ||
DiagnosticCategory.Globalization, | ||
RuleLevel.IdeHidden_BulkConfigurable, | ||
description: CreateLocalizableResourceString(nameof(SpecifyCultureForToLowerAndToUpperDescription)), | ||
isPortedFxCopRule: false, | ||
isDataflowRule: false); | ||
|
||
internal const string ToLowerMethodName = "ToLower"; | ||
internal const string ToUpperMethodName = "ToUpper"; | ||
protected abstract Location GetMethodNameLocation(SyntaxNode invocationNode); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); | ||
|
||
protected override void InitializeWorker(CompilationStartAnalysisContext context) | ||
{ | ||
context.RegisterOperationAction(operationContext => | ||
{ | ||
var operation = (IInvocationOperation)operationContext.Operation; | ||
IMethodSymbol methodSymbol = operation.TargetMethod; | ||
|
||
if (methodSymbol.ContainingType.SpecialType == SpecialType.System_String && | ||
!methodSymbol.IsStatic && | ||
IsToLowerOrToUpper(methodSymbol.Name) && | ||
//picking the correct overload | ||
methodSymbol.Parameters.Length == 0) | ||
{ | ||
operationContext.ReportDiagnostic(Diagnostic.Create(Rule, GetMethodNameLocation(operation.Syntax))); | ||
} | ||
}, OperationKind.Invocation); | ||
} | ||
|
||
private static bool IsToLowerOrToUpper(string methodName) | ||
{ | ||
return methodName == ToLowerMethodName || methodName == ToUpperMethodName; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.