Skip to content
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

Shortcomings in Syntax Tree API - no common base class or interface for MethodDeclarationSyntax, FieldDeclarationSyntax etc. for important common properties #10455

Closed
gmamaladze opened this issue Apr 11, 2016 · 2 comments
Labels
Area-Compilers Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request
Milestone

Comments

@gmamaladze
Copy link

Version Used:
Visual Studio 2015 Update 2

Shortcomings in Syntax Tree API:
While writing code for a code refactoring very often you need to perform similar checks or modifications on members containing actual code: methods, property accessors, indexer accessors etc. Most of them have common properties like Body, Modifiers, AttributeLists, ArgumentList.

There is no common base class or a common interface which would expose these common properties. That's why you end up with a lot of code duplication, with all consequences as shown in example below:

private static bool IsPrivate(MethodDeclarationSyntax method)
{
    return method.Modifiers.Any(IsPrivateModifier);
}

private static bool IsPrivate(FieldDeclarationSyntax field)
{
    return field.Modifiers.Any(IsPrivateModifier);
}

private static bool IsPrivate(PropertyDeclarationSyntax property)
{
    return property.Modifiers.Any(IsPrivateModifier);
}

private static bool IsPrivate(IndexerDeclarationSyntax indexer)
{
    return indexer.Modifiers.Any(IsPrivateModifier);
}

private static bool IsPrivateModifier(SyntaxToken modifier)
{
    return modifier.Kind() == SyntaxKind.PrivateKeyword;
}

Suggestion:

  • One way would be to provide a common interface for all "method like" SyntaxNode-s.
  • Another way is to provide adapter classes to adapt "method like" SyntaxNode-s to some interface which provides uniform and convenient access to properties like Modifiers, AttributeLists, Body, ArgumentList. This solution will have less implementation and runtime impact.

Related to: #9908

@Pilchie Pilchie added Area-Compilers Concept-API This issue involves adding, removing, clarification, or modification of an API. labels Apr 11, 2016
@jaredpar jaredpar added this to the 2.0 (RTM) milestone Apr 11, 2016
@jaredpar jaredpar modified the milestones: 2.1, 2.0 (RTM) Jul 23, 2016
@ap0llo
Copy link

ap0llo commented Aug 16, 2016

Should I give this issue a try or is there already someone working on this?

@CyrusNajmabadi
Copy link
Member

These share MemberDeclarationSyntax which do expose things like Modifiers. Closing out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request
Projects
None yet
Development

No branches or pull requests

5 participants