-
Notifications
You must be signed in to change notification settings - Fork 463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
handle params array in the argument list #5082
Merged
jmarolf
merged 14 commits into
dotnet:main
from
jmarolf:bugfix/pass-cancelation-token-with-params-array-overload
Aug 2, 2021
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6b4eec3
handle params array in the argument list
jmarolf 7d36995
Update src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime…
jmarolf 87a36c6
remove unused parameters
jmarolf 144a027
rename types
jmarolf 6588312
rename files
jmarolf a2148bd
manually contruct array in VB case
jmarolf fec6393
remove unused using
jmarolf 2d59819
remove blank line
jmarolf 9b30240
Update src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/Forw…
jmarolf 98c0399
fixing formatting
jmarolf 21bfcd8
update headers
jmarolf ce5aae8
use preview language version
jmarolf 2dbaf2f
prefer `is not null` to `is object`
jmarolf dc448ea
fixup rebase
jmarolf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
233 changes: 233 additions & 0 deletions
233
...ore.Analyzers/Runtime/CSharpForwardCancellationTokenToInvocationsFixer.TypeNameVisitor.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,233 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using Analyzer.Utilities.Lightup; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Simplification; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
using NullableAnnotation = Analyzer.Utilities.Lightup.NullableAnnotation; | ||
|
||
namespace Microsoft.NetCore.CSharp.Analyzers.Runtime | ||
{ | ||
public sealed partial class CSharpForwardCancellationTokenToInvocationsFixer | ||
{ | ||
private class TypeNameVisitor : SymbolVisitor<TypeSyntax> | ||
{ | ||
public static TypeSyntax GetTypeSyntaxForSymbol(INamespaceOrTypeSymbol symbol) | ||
{ | ||
return symbol.Accept(new TypeNameVisitor()).WithAdditionalAnnotations(Simplifier.Annotation); | ||
} | ||
|
||
public override TypeSyntax DefaultVisit(ISymbol symbol) | ||
=> throw new NotImplementedException(); | ||
|
||
public override TypeSyntax VisitAlias(IAliasSymbol symbol) | ||
{ | ||
return AddInformationTo(ToIdentifierName(symbol.Name)); | ||
} | ||
|
||
public override TypeSyntax VisitDynamicType(IDynamicTypeSymbol symbol) | ||
{ | ||
return AddInformationTo(IdentifierName("dynamic")); | ||
} | ||
|
||
public override TypeSyntax VisitNamedType(INamedTypeSymbol symbol) | ||
{ | ||
if (TryCreateNativeIntegerType(symbol, out var typeSyntax)) | ||
return typeSyntax; | ||
|
||
typeSyntax = CreateSimpleTypeSyntax(symbol); | ||
if (!(typeSyntax is SimpleNameSyntax)) | ||
return typeSyntax; | ||
|
||
var simpleNameSyntax = (SimpleNameSyntax)typeSyntax; | ||
if (symbol.ContainingType is not null) | ||
{ | ||
if (symbol.ContainingType.TypeKind != TypeKind.Submission) | ||
{ | ||
var containingTypeSyntax = symbol.ContainingType.Accept(this); | ||
if (containingTypeSyntax is NameSyntax name) | ||
{ | ||
typeSyntax = AddInformationTo( | ||
QualifiedName(name, simpleNameSyntax)); | ||
} | ||
else | ||
{ | ||
typeSyntax = AddInformationTo(simpleNameSyntax); | ||
} | ||
} | ||
} | ||
else if (symbol.ContainingNamespace is not null) | ||
{ | ||
if (symbol.ContainingNamespace.IsGlobalNamespace) | ||
{ | ||
if (symbol.TypeKind != TypeKind.Error) | ||
{ | ||
typeSyntax = AddGlobalAlias(simpleNameSyntax); | ||
} | ||
} | ||
else | ||
{ | ||
var container = symbol.ContainingNamespace.Accept(this)!; | ||
typeSyntax = AddInformationTo(QualifiedName( | ||
(NameSyntax)container, | ||
simpleNameSyntax)); | ||
} | ||
} | ||
|
||
if (symbol.NullableAnnotation() == NullableAnnotation.Annotated && | ||
!symbol.IsValueType) | ||
{ | ||
typeSyntax = AddInformationTo(NullableType(typeSyntax)); | ||
} | ||
|
||
return typeSyntax; | ||
} | ||
|
||
public override TypeSyntax VisitNamespace(INamespaceSymbol symbol) | ||
{ | ||
var syntax = AddInformationTo(ToIdentifierName(symbol.Name)); | ||
if (symbol.ContainingNamespace == null) | ||
{ | ||
return syntax; | ||
} | ||
|
||
if (symbol.ContainingNamespace.IsGlobalNamespace) | ||
{ | ||
return AddGlobalAlias(syntax); | ||
} | ||
else | ||
{ | ||
var container = symbol.ContainingNamespace.Accept(this)!; | ||
return AddInformationTo(QualifiedName( | ||
(NameSyntax)container, | ||
syntax)); | ||
} | ||
} | ||
|
||
public override TypeSyntax VisitTypeParameter(ITypeParameterSymbol symbol) | ||
{ | ||
TypeSyntax typeSyntax = AddInformationTo(ToIdentifierName(symbol.Name)); | ||
if (symbol.NullableAnnotation() == NullableAnnotation.Annotated) | ||
typeSyntax = AddInformationTo(NullableType(typeSyntax)); | ||
|
||
return typeSyntax; | ||
} | ||
|
||
private TypeSyntax CreateSimpleTypeSyntax(INamedTypeSymbol symbol) | ||
{ | ||
if (symbol.IsTupleType && symbol.TupleUnderlyingType != null && !symbol.Equals(symbol.TupleUnderlyingType)) | ||
{ | ||
return CreateSimpleTypeSyntax(symbol.TupleUnderlyingType); | ||
} | ||
|
||
if (string.IsNullOrEmpty(symbol.Name) || symbol.IsAnonymousType) | ||
{ | ||
return CreateSystemObject(); | ||
} | ||
|
||
if (symbol.TypeParameters.Length == 0) | ||
{ | ||
if (symbol.TypeKind == TypeKind.Error && symbol.Name == "var") | ||
{ | ||
return CreateSystemObject(); | ||
} | ||
|
||
return ToIdentifierName(symbol.Name); | ||
} | ||
|
||
var typeArguments = symbol.IsUnboundGenericType | ||
? Enumerable.Repeat((TypeSyntax)OmittedTypeArgument(), symbol.TypeArguments.Length) | ||
: symbol.TypeArguments.Select(t => GetTypeSyntaxForSymbol(t)); | ||
|
||
return GenericName( | ||
ToIdentifierToken(symbol.Name), | ||
TypeArgumentList(SeparatedList(typeArguments))); | ||
} | ||
|
||
private static QualifiedNameSyntax CreateSystemObject() | ||
{ | ||
return QualifiedName( | ||
AliasQualifiedName( | ||
CreateGlobalIdentifier(), | ||
IdentifierName("System")), | ||
IdentifierName("Object")); | ||
} | ||
|
||
private static TTypeSyntax AddInformationTo<TTypeSyntax>(TTypeSyntax syntax) | ||
where TTypeSyntax : TypeSyntax | ||
{ | ||
syntax = syntax.WithLeadingTrivia(ElasticMarker).WithTrailingTrivia(ElasticMarker); | ||
return syntax; | ||
} | ||
|
||
/// <summary> | ||
/// We always unilaterally add "global::" to all named types/namespaces. This | ||
/// will then be trimmed off if possible by the simplifier | ||
/// </summary> | ||
private static TypeSyntax AddGlobalAlias(SimpleNameSyntax syntax) | ||
{ | ||
return AddInformationTo(AliasQualifiedName(CreateGlobalIdentifier(), syntax)); | ||
} | ||
|
||
private static IdentifierNameSyntax ToIdentifierName(string identifier) | ||
=> IdentifierName(ToIdentifierToken(identifier)); | ||
|
||
private static IdentifierNameSyntax CreateGlobalIdentifier() | ||
=> IdentifierName(Token(SyntaxKind.GlobalKeyword)); | ||
|
||
private static bool TryCreateNativeIntegerType(INamedTypeSymbol symbol, [NotNullWhen(true)] out TypeSyntax? syntax) | ||
{ | ||
if (symbol.IsNativeIntegerType()) | ||
{ | ||
syntax = IdentifierName(symbol.SpecialType == SpecialType.System_IntPtr ? "nint" : "nuint"); | ||
return true; | ||
} | ||
|
||
syntax = null; | ||
return false; | ||
} | ||
|
||
private static SyntaxToken ToIdentifierToken(string identifier) | ||
jmarolf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var escaped = EscapeIdentifier(identifier); | ||
|
||
if (escaped.Length == 0 || escaped[0] != '@') | ||
{ | ||
return Identifier(escaped); | ||
} | ||
|
||
var unescaped = identifier.StartsWith("@", StringComparison.Ordinal) | ||
? identifier[1..] | ||
: identifier; | ||
|
||
var token = Identifier( | ||
default, SyntaxKind.None, "@" + unescaped, unescaped, default); | ||
|
||
if (!identifier.StartsWith("@", StringComparison.Ordinal)) | ||
{ | ||
token = token.WithAdditionalAnnotations(Simplifier.Annotation); | ||
} | ||
|
||
return token; | ||
} | ||
|
||
private static string EscapeIdentifier(string identifier) | ||
jmarolf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var nullIndex = identifier.IndexOf('\0'); | ||
if (nullIndex >= 0) | ||
{ | ||
identifier = identifier.Substring(0, nullIndex); | ||
} | ||
|
||
var needsEscaping = SyntaxFacts.GetKeywordKind(identifier) != SyntaxKind.None; | ||
|
||
return needsEscaping ? "@" + identifier : identifier; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible for
symbol.NullableAnnotation()
to returnAnnotated
for value types?