- Roslyn Cookbook
- Manish Vasani
- 189字
- 2021-07-15 17:07:33
How it works...
Syntax tree analyzers register callbacks to analyze syntax of all source files in the compilation. Our analysis works by getting the roots of the syntax tree and then operating on all the descendant syntax nodes of the roots which are of type StatementSyntax. First, we note that a block statement is itself an aggregate statement, and by definition has curly braces, so we skip past these.
// Skip analyzing block statements.
if (statement is BlockSyntax)
{
continue;
}
We then perform syntactic checks for the parent of statement syntax. If the parent of the statement is also a statement, but not a block with curly braces, then we report a diagnostic on the first syntax token of the statement recommending usage of curly braces.
// Report issue for all statements that are nested within a statement,
// but not a block statement.
if (statement.Parent is StatementSyntax && !(statement.Parent is BlockSyntax))
{
var diagnostic = Diagnostic.Create(Rule, statement.GetFirstToken().GetLocation());
syntaxTreeContext.ReportDiagnostic(diagnostic);
}
SyntaxTreeAnalysisContext provided to syntax tree actions does not expose the semantic model for the source file, hence no semantic analysis can be performed within a syntax tree action.
推薦閱讀
- Mastering Zabbix(Second Edition)
- Visual Basic 6.0程序設計計算機組裝與維修
- Building a RESTful Web Service with Spring
- JavaScript 網頁編程從入門到精通 (清華社"視頻大講堂"大系·網絡開發視頻大講堂)
- 軟件測試項目實戰之性能測試篇
- 正則表達式經典實例(第2版)
- Unity 5.x By Example
- 焊接機器人系統操作、編程與維護
- ASP.NET開發與應用教程
- Service Mesh實戰:基于Linkerd和Kubernetes的微服務實踐
- IDA Pro權威指南(第2版)
- Java并發編程:核心方法與框架
- 深度學習程序設計實戰
- Python機器學習開發實戰
- Java Web開發教程:基于Struts2+Hibernate+Spring