未验证 提交 553503d9 编写于 作者: M Manish Vasani 提交者: GitHub

Merge pull request #37525 from CyrusNajmabadi/removeMethods

Remove unneeded method
...@@ -49,7 +49,7 @@ private bool IsConstructorInitializerContext(int position, CSharpSyntaxContext c ...@@ -49,7 +49,7 @@ private bool IsConstructorInitializerContext(int position, CSharpSyntaxContext c
if (token.Kind() == SyntaxKind.ColonToken && if (token.Kind() == SyntaxKind.ColonToken &&
token.Parent is ConstructorInitializerSyntax && token.Parent is ConstructorInitializerSyntax &&
token.Parent.IsParentKind(SyntaxKind.ConstructorDeclaration) && token.Parent.IsParentKind(SyntaxKind.ConstructorDeclaration) &&
token.Parent.GetParent().IsParentKind(SyntaxKind.ClassDeclaration)) token.Parent.Parent.IsParentKind(SyntaxKind.ClassDeclaration))
{ {
var constructor = token.GetAncestor<ConstructorDeclarationSyntax>(); var constructor = token.GetAncestor<ConstructorDeclarationSyntax>();
if (constructor.Modifiers.Any(SyntaxKind.StaticKeyword)) if (constructor.Modifiers.Any(SyntaxKind.StaticKeyword))
......
...@@ -121,7 +121,7 @@ protected override bool IsValidContext(int position, CSharpSyntaxContext context ...@@ -121,7 +121,7 @@ protected override bool IsValidContext(int position, CSharpSyntaxContext context
if (token.Kind() == SyntaxKind.CloseBraceToken) if (token.Kind() == SyntaxKind.CloseBraceToken)
{ {
if (token.Parent is TypeDeclarationSyntax && if (token.Parent is TypeDeclarationSyntax &&
!(token.Parent.GetParent() is TypeDeclarationSyntax)) !(token.Parent.Parent is TypeDeclarationSyntax))
{ {
return true; return true;
} }
...@@ -137,7 +137,7 @@ protected override bool IsValidContext(int position, CSharpSyntaxContext context ...@@ -137,7 +137,7 @@ protected override bool IsValidContext(int position, CSharpSyntaxContext context
if (token.Kind() == SyntaxKind.SemicolonToken) if (token.Kind() == SyntaxKind.SemicolonToken)
{ {
if (token.Parent.IsKind(SyntaxKind.DelegateDeclaration) && if (token.Parent.IsKind(SyntaxKind.DelegateDeclaration) &&
!(token.Parent.GetParent() is TypeDeclarationSyntax)) !(token.Parent.Parent is TypeDeclarationSyntax))
{ {
return true; return true;
} }
......
...@@ -79,9 +79,8 @@ private bool IsTypeParameterConstraintContext(CSharpSyntaxContext context) ...@@ -79,9 +79,8 @@ private bool IsTypeParameterConstraintContext(CSharpSyntaxContext context)
// class C<T> : IGoo | // class C<T> : IGoo |
var baseList = token.GetAncestor<BaseListSyntax>(); var baseList = token.GetAncestor<BaseListSyntax>();
if (baseList.GetParent() is TypeDeclarationSyntax) if (baseList?.Parent is TypeDeclarationSyntax typeDecl)
{ {
var typeDecl = baseList.GetParent() as TypeDeclarationSyntax;
if (typeDecl.TypeParameterList != null && if (typeDecl.TypeParameterList != null &&
typeDecl.BaseList.Types.Any(t => token == t.GetLastToken(includeSkipped: true))) typeDecl.BaseList.Types.Any(t => token == t.GetLastToken(includeSkipped: true)))
{ {
......
...@@ -62,7 +62,7 @@ protected override bool IsInAutoPropertyInitializer(ExpressionSyntax expression) ...@@ -62,7 +62,7 @@ protected override bool IsInAutoPropertyInitializer(ExpressionSyntax expression)
protected override bool IsInExpressionBodiedMember(ExpressionSyntax expression) protected override bool IsInExpressionBodiedMember(ExpressionSyntax expression)
{ {
// walk up until we find a nearest enclosing block or arrow expression. // walk up until we find a nearest enclosing block or arrow expression.
for (SyntaxNode node = expression; node != null; node = node.GetParent()) for (SyntaxNode node = expression; node != null; node = node.Parent)
{ {
// If we are in an expression bodied member and if the expression has a block body, then, // If we are in an expression bodied member and if the expression has a block body, then,
// act as if we're in a block context and not in an expression body context at all. // act as if we're in a block context and not in an expression body context at all.
......
...@@ -430,9 +430,9 @@ public static bool IsConstructorOrMethodParameterArgumentContext(this SyntaxToke ...@@ -430,9 +430,9 @@ public static bool IsConstructorOrMethodParameterArgumentContext(this SyntaxToke
if (targetToken.Kind() == SyntaxKind.ColonToken && if (targetToken.Kind() == SyntaxKind.ColonToken &&
targetToken.Parent.IsKind(SyntaxKind.NameColon) && targetToken.Parent.IsKind(SyntaxKind.NameColon) &&
targetToken.Parent.IsParentKind(SyntaxKind.Argument) && targetToken.Parent.IsParentKind(SyntaxKind.Argument) &&
targetToken.Parent.GetParent().IsParentKind(SyntaxKind.ArgumentList)) targetToken.Parent.Parent.IsParentKind(SyntaxKind.ArgumentList))
{ {
var owner = targetToken.Parent.GetParent().GetParent().GetParent(); var owner = targetToken.Parent.Parent.Parent.Parent;
if (owner.IsKind(SyntaxKind.InvocationExpression) || if (owner.IsKind(SyntaxKind.InvocationExpression) ||
owner.IsKind(SyntaxKind.ObjectCreationExpression) || owner.IsKind(SyntaxKind.ObjectCreationExpression) ||
owner.IsKind(SyntaxKind.BaseConstructorInitializer) || owner.IsKind(SyntaxKind.BaseConstructorInitializer) ||
...@@ -564,7 +564,7 @@ private static bool IsAccessorDeclarationContextWorker(ref SyntaxToken targetTok ...@@ -564,7 +564,7 @@ private static bool IsAccessorDeclarationContextWorker(ref SyntaxToken targetTok
// int Goo { set { } private | // int Goo { set { } private |
if (targetToken.Kind() == SyntaxKind.CloseBraceToken && if (targetToken.Kind() == SyntaxKind.CloseBraceToken &&
targetToken.Parent.IsKind(SyntaxKind.Block) && targetToken.Parent.IsKind(SyntaxKind.Block) &&
targetToken.Parent.GetParent() is AccessorDeclarationSyntax) targetToken.Parent.Parent is AccessorDeclarationSyntax)
{ {
return true; return true;
} }
...@@ -579,7 +579,7 @@ private static bool IsAccessorDeclarationContextWorker(ref SyntaxToken targetTok ...@@ -579,7 +579,7 @@ private static bool IsAccessorDeclarationContextWorker(ref SyntaxToken targetTok
// int Goo { [Bar]| // int Goo { [Bar]|
if (targetToken.Kind() == SyntaxKind.CloseBracketToken && if (targetToken.Kind() == SyntaxKind.CloseBracketToken &&
targetToken.Parent.IsKind(SyntaxKind.AttributeList) && targetToken.Parent.IsKind(SyntaxKind.AttributeList) &&
targetToken.Parent.GetParent() is AccessorDeclarationSyntax) targetToken.Parent.Parent is AccessorDeclarationSyntax)
{ {
return true; return true;
} }
...@@ -631,7 +631,7 @@ public static bool IsTypeParameterVarianceContext(this SyntaxToken targetToken) ...@@ -631,7 +631,7 @@ public static bool IsTypeParameterVarianceContext(this SyntaxToken targetToken)
if (targetToken.Kind() == SyntaxKind.CloseBracketToken && if (targetToken.Kind() == SyntaxKind.CloseBracketToken &&
targetToken.Parent.IsKind(SyntaxKind.AttributeList) && targetToken.Parent.IsKind(SyntaxKind.AttributeList) &&
targetToken.Parent.IsParentKind(SyntaxKind.TypeParameter) && targetToken.Parent.IsParentKind(SyntaxKind.TypeParameter) &&
IsGenericInterfaceOrDelegateTypeParameterList(targetToken.Parent.GetParent().GetParent())) IsGenericInterfaceOrDelegateTypeParameterList(targetToken.Parent.Parent.Parent))
{ {
return true; return true;
} }
......
...@@ -142,7 +142,7 @@ public static bool IsAttributeNameContext(this SyntaxTree syntaxTree, int positi ...@@ -142,7 +142,7 @@ public static bool IsAttributeNameContext(this SyntaxTree syntaxTree, int positi
if (token.IsKind(SyntaxKind.SemicolonToken)) if (token.IsKind(SyntaxKind.SemicolonToken))
{ {
if (token.Parent is MemberDeclarationSyntax && if (token.Parent is MemberDeclarationSyntax &&
token.Parent.GetParent() is BaseTypeDeclarationSyntax) token.Parent.Parent is BaseTypeDeclarationSyntax)
{ {
return true; return true;
} }
...@@ -159,7 +159,7 @@ public static bool IsAttributeNameContext(this SyntaxTree syntaxTree, int positi ...@@ -159,7 +159,7 @@ public static bool IsAttributeNameContext(this SyntaxTree syntaxTree, int positi
if (token.IsKind(SyntaxKind.CloseBraceToken)) if (token.IsKind(SyntaxKind.CloseBraceToken))
{ {
if (token.Parent is BaseTypeDeclarationSyntax && if (token.Parent is BaseTypeDeclarationSyntax &&
token.Parent.GetParent() is BaseTypeDeclarationSyntax) token.Parent.Parent is BaseTypeDeclarationSyntax)
{ {
// after a nested type // after a nested type
return true; return true;
...@@ -171,7 +171,7 @@ public static bool IsAttributeNameContext(this SyntaxTree syntaxTree, int positi ...@@ -171,7 +171,7 @@ public static bool IsAttributeNameContext(this SyntaxTree syntaxTree, int positi
} }
else if ( else if (
token.Parent.IsKind(SyntaxKind.Block) && token.Parent.IsKind(SyntaxKind.Block) &&
token.Parent.GetParent() is MemberDeclarationSyntax) token.Parent.Parent is MemberDeclarationSyntax)
{ {
// after a method/operator/etc. // after a method/operator/etc.
return true; return true;
...@@ -191,7 +191,7 @@ public static bool IsAttributeNameContext(this SyntaxTree syntaxTree, int positi ...@@ -191,7 +191,7 @@ public static bool IsAttributeNameContext(this SyntaxTree syntaxTree, int positi
// the parent is the attribute // the parent is the attribute
// the grandparent is the owner of the attribute // the grandparent is the owner of the attribute
// the great-grandparent is the container that the owner is in // the great-grandparent is the container that the owner is in
var container = token.Parent.GetParent().GetParent(); var container = token.Parent.Parent?.Parent;
if (container is BaseTypeDeclarationSyntax) if (container is BaseTypeDeclarationSyntax)
{ {
return true; return true;
...@@ -460,7 +460,7 @@ public static bool IsAttributeNameContext(this SyntaxTree syntaxTree, int positi ...@@ -460,7 +460,7 @@ public static bool IsAttributeNameContext(this SyntaxTree syntaxTree, int positi
} }
else if ( else if (
token.Parent.IsKind(SyntaxKind.Block) && token.Parent.IsKind(SyntaxKind.Block) &&
token.Parent.GetParent() is MemberDeclarationSyntax) token.Parent.Parent is MemberDeclarationSyntax)
{ {
return true; return true;
} }
...@@ -485,7 +485,7 @@ public static bool IsAttributeNameContext(this SyntaxTree syntaxTree, int positi ...@@ -485,7 +485,7 @@ public static bool IsAttributeNameContext(this SyntaxTree syntaxTree, int positi
// the parent is the attribute // the parent is the attribute
// the grandparent is the owner of the attribute // the grandparent is the owner of the attribute
// the great-grandparent is the container that the owner is in // the great-grandparent is the container that the owner is in
var container = token.Parent.GetParent().GetParent(); var container = token.Parent?.Parent?.Parent;
if (container.IsKind(SyntaxKind.CompilationUnit) || if (container.IsKind(SyntaxKind.CompilationUnit) ||
container.IsKind(SyntaxKind.NamespaceDeclaration) || container.IsKind(SyntaxKind.NamespaceDeclaration) ||
container.IsKind(SyntaxKind.ClassDeclaration) || container.IsKind(SyntaxKind.ClassDeclaration) ||
...@@ -572,7 +572,7 @@ public static bool IsAttributeNameContext(this SyntaxTree syntaxTree, int positi ...@@ -572,7 +572,7 @@ public static bool IsAttributeNameContext(this SyntaxTree syntaxTree, int positi
{ {
// the parent is the member // the parent is the member
// the grandparent is the container of the member // the grandparent is the container of the member
var container = token.Parent.GetParent(); var container = token.Parent?.Parent;
// ref $$ // ref $$
// readonly ref $$ // readonly ref $$
......
...@@ -520,11 +520,6 @@ internal static SyntaxNodeOrToken ChildThatContainsPosition(this SyntaxNode self ...@@ -520,11 +520,6 @@ internal static SyntaxNodeOrToken ChildThatContainsPosition(this SyntaxNode self
throw new ArgumentOutOfRangeException(nameof(position)); throw new ArgumentOutOfRangeException(nameof(position));
} }
public static SyntaxNode GetParent(this SyntaxNode node)
{
return node != null ? node.Parent : null;
}
public static (SyntaxToken openBrace, SyntaxToken closeBrace) GetParentheses(this SyntaxNode node) public static (SyntaxToken openBrace, SyntaxToken closeBrace) GetParentheses(this SyntaxNode node)
{ {
switch (node) switch (node)
...@@ -753,7 +748,7 @@ public static ConditionalAccessExpressionSyntax GetInnerMostConditionalAccessExp ...@@ -753,7 +748,7 @@ public static ConditionalAccessExpressionSyntax GetInnerMostConditionalAccessExp
public static bool IsInDeconstructionLeft(this SyntaxNode node, out SyntaxNode deconstructionLeft) public static bool IsInDeconstructionLeft(this SyntaxNode node, out SyntaxNode deconstructionLeft)
{ {
SyntaxNode previous = null; SyntaxNode previous = null;
for (var current = node; current != null; current = current.GetParent()) for (var current = node; current != null; current = current.Parent)
{ {
if ((current is AssignmentExpressionSyntax assignment && previous == assignment.Left && assignment.IsDeconstruction()) || if ((current is AssignmentExpressionSyntax assignment && previous == assignment.Left && assignment.IsDeconstruction()) ||
(current is ForEachVariableStatementSyntax @foreach && previous == @foreach.Variable)) (current is ForEachVariableStatementSyntax @foreach && previous == @foreach.Variable))
......
...@@ -27,24 +27,16 @@ public static bool IsKind<TNode>(this SyntaxNode node, SyntaxKind kind, out TNod ...@@ -27,24 +27,16 @@ public static bool IsKind<TNode>(this SyntaxNode node, SyntaxKind kind, out TNod
} }
public static bool IsParentKind(this SyntaxNode node, SyntaxKind kind) public static bool IsParentKind(this SyntaxNode node, SyntaxKind kind)
{ => CodeAnalysis.CSharpExtensions.IsKind(node?.Parent, kind);
return node != null && CodeAnalysis.CSharpExtensions.IsKind(node.Parent, kind);
}
public static bool IsParentKind(this SyntaxNode node, SyntaxKind kind1, SyntaxKind kind2) public static bool IsParentKind(this SyntaxNode node, SyntaxKind kind1, SyntaxKind kind2)
{ => IsKind(node?.Parent, kind1, kind2);
return node != null && IsKind(node.Parent, kind1, kind2);
}
public static bool IsParentKind(this SyntaxNode node, SyntaxKind kind1, SyntaxKind kind2, SyntaxKind kind3) public static bool IsParentKind(this SyntaxNode node, SyntaxKind kind1, SyntaxKind kind2, SyntaxKind kind3)
{ => IsKind(node?.Parent, kind1, kind2, kind3);
return node != null && IsKind(node.Parent, kind1, kind2, kind3);
}
public static bool IsParentKind(this SyntaxNode node, SyntaxKind kind1, SyntaxKind kind2, SyntaxKind kind3, SyntaxKind kind4) public static bool IsParentKind(this SyntaxNode node, SyntaxKind kind1, SyntaxKind kind2, SyntaxKind kind3, SyntaxKind kind4)
{ => IsKind(node?.Parent, kind1, kind2, kind3, kind4);
return node != null && IsKind(node.Parent, kind1, kind2, kind3, kind4);
}
public static bool IsKind(this SyntaxNode node, SyntaxKind kind1, SyntaxKind kind2) public static bool IsKind(this SyntaxNode node, SyntaxKind kind1, SyntaxKind kind2)
{ {
......
...@@ -762,8 +762,7 @@ public static SyntaxNode GetParent(this SyntaxNode node, bool ascendOutOfTrivia) ...@@ -762,8 +762,7 @@ public static SyntaxNode GetParent(this SyntaxNode node, bool ascendOutOfTrivia)
var parent = node.Parent; var parent = node.Parent;
if (parent == null && ascendOutOfTrivia) if (parent == null && ascendOutOfTrivia)
{ {
var structuredTrivia = node as IStructuredTriviaSyntax; if (node is IStructuredTriviaSyntax structuredTrivia)
if (structuredTrivia != null)
{ {
parent = structuredTrivia.ParentTrivia.Token.Parent; parent = structuredTrivia.ParentTrivia.Token.Parent;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册