Repository navigation
Excluding code in the context of a GeneratedCodeAttribute #33
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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cbef812
Excluding code in the context of a GeneratedCodeAttribute from diagno…
afscrome 867d2ae
Refactored IsInGeneratedCodeContext, adding support for recognising t…
afscrome 102ba47
Also recognising GeneratedCode attributes on field initalisers, opera…
afscrome 89b49a8
Removed unnessescary newlines.
afscrome 68b6cb5
Ignoring code in Constructors marked with GeneratedCode
afscrome e3bfd7f
Refactored the IsInGeneratedCodeContext to better cover all node type…
afscrome db68485
Using SemanticModel to ensure the attribute is GeneratedCode attribut…
afscrome 264f963
Merge remote-tracking branch 'DustinCampbell/master'
afscrome 3d244e7
Moved location of BeginsWithAutoGeneratedComment to make diffs clearer.
afscrome b24dfc9
Refactored Removed SyntaxNode argument from IsGeneratedCode(this Synt…
afscrome efb8775
Added missing tests for excluding code with CompilerGenerated attribu…
afscrome bff4de1
GetterOnlyAutoProperties are now correctly ignored in the context of …
afscrome 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 hidden or 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 hidden or 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,14 @@ | ||
| using Microsoft.CodeAnalysis; | ||
| using System.Collections.Immutable; | ||
|
|
||
| namespace CSharpEssentials.Tests | ||
| { | ||
| class Constants | ||
| { | ||
| public static readonly ImmutableList<MetadataReference> CompilerGeneratedAttributeReferences = ImmutableList.Create<MetadataReference>( | ||
| MetadataReference.CreateFromFile(typeof(System.CodeDom.Compiler.GeneratedCodeAttribute).Assembly.Location), | ||
| MetadataReference.CreateFromFile(typeof(object).Assembly.Location) | ||
| ); | ||
|
|
||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
|
|
||
| namespace CSharpEssentials | ||
| { | ||
|
|
@@ -211,19 +212,58 @@ private static bool BeginsWithAutoGeneratedComment(SyntaxTree tree, Cancellation | |
| return false; | ||
| } | ||
|
|
||
| public static bool IsGeneratedCode(this SyntaxTree tree, CancellationToken cancellationToken) | ||
| public static bool IsGeneratedCode(this SyntaxNode node, SemanticModel model, CancellationToken cancellationToken) | ||
| { | ||
| if (IsGeneratedCodeFile(tree.FilePath)) | ||
| { | ||
| return true; | ||
| return IsGeneratedCodeFile(node.SyntaxTree.FilePath) | ||
| || BeginsWithAutoGeneratedComment(node.SyntaxTree, cancellationToken) | ||
| || IsInGeneratedCodeAttributeContext(node, model, cancellationToken); | ||
| } | ||
|
|
||
| public static bool IsGeneratedCode(this SyntaxNodeAnalysisContext context) | ||
| => IsGeneratedCode(context.Node, context.SemanticModel, context.CancellationToken); | ||
|
|
||
| private static bool IsInGeneratedCodeAttributeContext(SyntaxNode node, SemanticModel model, CancellationToken cancelationToken) | ||
| { | ||
| while(node != null) | ||
| { | ||
| var typeDecleration = node as BaseTypeDeclarationSyntax; | ||
| if (typeDecleration != null && ContainsGeneratedCodeAttribute(typeDecleration.AttributeLists, model, cancelationToken)) | ||
| return true; | ||
|
|
||
| var methodDecleration = node as BaseMethodDeclarationSyntax; | ||
| if (methodDecleration != null && ContainsGeneratedCodeAttribute(methodDecleration.AttributeLists, model, cancelationToken)) | ||
| return true; | ||
|
|
||
| var propertyDecleration = node as BasePropertyDeclarationSyntax; | ||
| if (propertyDecleration != null && ContainsGeneratedCodeAttribute(propertyDecleration.AttributeLists, model, cancelationToken)) | ||
| return true; | ||
|
|
||
| var fieldDecleration = node as BaseFieldDeclarationSyntax; | ||
| if (fieldDecleration != null && ContainsGeneratedCodeAttribute(fieldDecleration.AttributeLists, model, cancelationToken)) | ||
| return true; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about structs? |
||
|
|
||
| node = node.Parent; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| if (BeginsWithAutoGeneratedComment(tree, cancellationToken)) | ||
|
|
||
| private static bool ContainsGeneratedCodeAttribute(SyntaxList<AttributeListSyntax> attributeLists, SemanticModel model, CancellationToken cancellationToken) | ||
| { | ||
| foreach (var attributeList in attributeLists) | ||
| { | ||
| return true; | ||
| } | ||
| foreach (var attribute in attributeList.Attributes) | ||
| { | ||
| var attributeType = model.GetTypeInfo(attribute, cancellationToken).Type; | ||
|
|
||
| if (attributeType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == "global::System.CodeDom.Compiler.GeneratedCodeAttribute") | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
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.
What about fields initializers, properties, indexers, operators, and conversions?