- 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.
推薦閱讀
- 觸·心:DT時代的大數據精準營銷
- Kubernetes實戰
- 自己動手寫Java虛擬機
- JIRA 7 Administration Cookbook(Second Edition)
- Web交互界面設計與制作(微課版)
- Visual C++串口通信技術詳解(第2版)
- Silverlight魔幻銀燈
- Banana Pi Cookbook
- Visual Basic程序設計與應用實踐教程
- C# 8.0核心技術指南(原書第8版)
- Learning Node.js for .NET Developers
- Hands-On Robotics Programming with C++
- Java服務端研發知識圖譜
- 美麗洞察力:從化妝品行業看顧客需求洞察
- JavaScript Mobile Application Development