Expose the kind-specific modifiers of the symbols in the XPath queries of MA0240

Merged
ME
2mo ago
+274-9

What changed

The symbol attributes of MA0240 were the same for every kind of symbol, so the modifiers of a parameter or of a field could not be queried from the syntax tree (semantic:) or from the operations (@Parameter…, @Field…, @Local…). They are now added according to the kind of the symbol:

Symbolsemantic: attributesOperation attributes (property P)
ParameterRefKind, IsParams, IsOptional@PRefKind, @PIsParams, @PIsOptional
FieldIsConst, IsReadOnly@PIsConst, @PIsReadOnly
LocalRefKind, IsConst@PRefKind, @PIsConst

Like IsAsync and Arity, an attribute is not present when the modifier does not apply to the symbol, and is false when it applies and is not set.

Examples:

//Parameter[@semantic:RefKind='Out']
//operation:ParameterReference[@ParameterRefKind='Out']
//operation:FieldReference[@FieldIsReadOnly='false' and @FieldIsConst='false']

Notes for reviewers

  • RefKind.RefReadOnly is an alias of RefKind.In, and Enum.ToString does not choose the same name on every runtime (.NET returns In, .NET Framework in Visual Studio may not). XPathAttributeFormatter.GetRefKindName always returns In, and the existing @RefKind attribute of the symbol: elements now uses it too, so the value is the same on every runtime.
  • IsReadOnly is not exposed for properties and methods, nor RefKind for methods and properties, as the Roslyn properties mean something else there (no setter, readonly member, by-ref return). They remain available on the symbol: elements.
  • The documentation of MA0240 describes the new attributes.

Tests

New tests in DoNotUseBannedSyntaxAnalyzerTests for the semantic attributes, the operation attributes, the absence of the attributes for the other kinds of symbols, and @RefKind='In' on the symbol tree. DoNotUseBannedSyntaxAnalyzerTests passes on Roslyn 4.8, 4.14, 5.0, 5.6 and 5.9, and dotnet run --project src/DocumentationGenerator reports no changes.