官术网_书友最值得收藏!

  • 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.
主站蜘蛛池模板: 六枝特区| 崇阳县| 乌恰县| 文登市| 扶风县| 锦屏县| 尤溪县| 烟台市| 吉木乃县| 囊谦县| 新野县| 武川县| 西畴县| 双峰县| 辽源市| 连平县| 新晃| 凉城县| 江山市| 江安县| 都昌县| 贵南县| 凤城市| 崇阳县| 无棣县| 湘乡市| 中方县| 息烽县| 南华县| 定结县| 嘉义县| 左权县| 桂林市| 建始县| 鹤岗市| 深水埗区| 木里| 巴东县| 潞城市| 吉木萨尔县| 临夏市|