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

Skip running StyleCop rules on generated files and elements #670

Merged
merged 17 commits into from
Apr 22, 2015

Conversation

AArnott
Copy link
Contributor

@AArnott AArnott commented Apr 21, 2015

Fixes #555

@AArnott
Copy link
Contributor Author

AArnott commented Apr 21, 2015

Obviously this needs to be applied to all rules and not just SA1600. I didn't want to go too far before getting feedback as this is my first contribution. Please let me know what you think, including:

  1. Should I replace all uses of RegisterSyntaxNodeAction with RegisterSyntaxNodeActionHonorExclusions?
  2. How do you like the name of the RegisterSyntaxNodeActionHonorExclusions method?
  3. Do I need to add tests for this to every single analyzer or is it enough that it's tested once in the context of one of them?

@sharwell
Copy link
Member

I'm primarily concerned about the performance implications of this. In the proposed implementation, the check would be run once for each item analyzed, as opposed to once per file. When you consider all the other analyzers we have here, we could be talking about multiple calls to check for exclusions for every token in the input, including trivia tokens.

@AArnott
Copy link
Contributor Author

AArnott commented Apr 21, 2015

Good point. But how else can we skip an entire file? At the time the analyzer registers itself, we don't have a file context to check.

@sharwell
Copy link
Member

No idea. Best guess right now is it might be possible to use RegisterSyntaxTreeAction to get a callback that runs before the RegisterSyntaxNodeAction callbacks (I don't know when, if ever, this order is guaranteed to be correct for this use case), and "attach" a flag to the tree (e.g. using ConditionalWeakTable) that later SyntaxNode handlers look for.

If it worked, this would allow the result to be shared across all analyzers in the solution. If it worked.

@sharwell
Copy link
Member

Oh hey, it would certainly work if we treat the ConditionalWeakTable as an opportunistic cache, i.e. simply recompute the value any time it's absent and we need it. The check and conditional update could be inside the action you register with RegisterSyntaxNodeActionHonorExclusions.

@AArnott
Copy link
Contributor Author

AArnott commented Apr 21, 2015

Cool. I'll try that out and see if I can test its effectiveness as well.

@AArnott
Copy link
Contributor Author

AArnott commented Apr 21, 2015

What does the 'do not merge' label mean? Is that a bad thing?

@sharwell
Copy link
Member

No, it just means issues were identified during the last code review (it saves me time when looking through the list of outstanding PRs).

@@ -60,7 +60,7 @@ public override void Initialize(AnalysisContext context)
context.RegisterSyntaxTreeAction(this.HandleSyntaxTree);

// handle nameof (which appears as an invocation expression??)
context.RegisterSyntaxNodeAction(this.HandleInvocationExpressionSyntax, SyntaxKind.InvocationExpression);
context.RegisterSyntaxNodeActionHonorExclusions(this.HandleInvocationExpressionSyntax, SyntaxKind.InvocationExpression);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗ We're also going to need to handle RegisterSyntaxTreeAction above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. I had used a search and replace for the first method, so I missed this other one.

There were several "return false" statements that would skip storing the negative result in the cache, resulting in repeated checks. Moving the entire check logic into another method allowed me to keep the logic simple but guarantee storage of the result.

private static bool IsOnGeneratedFile(this string filePath) =>
Regex.IsMatch(filePath, @"(^service|^TemporaryGeneratedFile_.*|^assemblyinfo|^assemblyattributes|\.(g\.i|g|designer|generated|assemblyattributes))\.(cs|vb)$",
RegexOptions.IgnoreCase);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Add RegexOptions.ExplicitCapture.

@sharwell
Copy link
Member

@AArnott pro-tip: GitHub does not send me a notification if you push more commits. You have to actually comment afterwards for it to notify me. 👍 (or break the build and have AppVeyor email me 😉)

@AArnott
Copy link
Contributor Author

AArnott commented Apr 22, 2015

@sharwell thanks for that. I wasn't aware. :)

sharwell added a commit that referenced this pull request Apr 22, 2015
Skip running StyleCop rules on generated files and elements
@sharwell sharwell merged commit a8fa0ec into DotNetAnalyzers:master Apr 22, 2015
@sharwell sharwell added this to the 1.0.0 Alpha 5 milestone Apr 22, 2015
@sharwell sharwell self-assigned this Apr 22, 2015
@pdelvo
Copy link
Member

pdelvo commented Apr 22, 2015

I love this! I think it would be nice if the changes made to GeneratedCodeAnalysisExtensions would be contributed back to code cracker as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Analyzers will report in auto generated files
3 participants