Skip to content

Commit

Permalink
TokenType: Tests and support for scoped type declarations (#8061)
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-strecker-sonarsource authored Sep 25, 2023
1 parent d2f9054 commit 6869c7c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion analyzers/src/SonarAnalyzer.CFG/ShimLayer/SyntaxKindEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static class SyntaxKindEx
public const SyntaxKind Utf8StringLiteralToken = (SyntaxKind)8520;
public const SyntaxKind Utf8SingleLineRawStringLiteralToken = (SyntaxKind)8521;
public const SyntaxKind Utf8MultiLineRawStringLiteralToken = (SyntaxKind)8522;
public const SyntaxKind PragmaChecksumDirectiveTrivia = (SyntaxKind)8560;
public const SyntaxKind ConflictMarkerTrivia = (SyntaxKind)8564;
public const SyntaxKind IsPatternExpression = (SyntaxKind)8657;
public const SyntaxKind RangeExpression = (SyntaxKind)8658;
Expand Down Expand Up @@ -87,6 +88,6 @@ public static class SyntaxKindEx
public const SyntaxKind InterpolatedSingleLineRawStringStartToken = (SyntaxKind)9072;
public const SyntaxKind InterpolatedMultiLineRawStringStartToken = (SyntaxKind)9073;
public const SyntaxKind InterpolatedRawStringEndToken = (SyntaxKind)9074;
public const SyntaxKind PragmaChecksumDirectiveTrivia = (SyntaxKind)8560;
public const SyntaxKind ScopedType = (SyntaxKind)9075;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ private static bool IsInTypeContext(SimpleNameSyntax name) =>
{ RawKind: (int)SyntaxKindEx.FileScopedNamespaceDeclaration } x => ((FileScopedNamespaceDeclarationSyntaxWrapper)x).Name == name,
{ RawKind: (int)SyntaxKindEx.TupleElement } x => ((TupleElementSyntaxWrapper)x).Type == name,
{ RawKind: (int)SyntaxKindEx.RefType } x => ((RefTypeSyntaxWrapper)x).Type == name,
{ RawKind: (int)SyntaxKindEx.ScopedType } x => ((ScopedTypeSyntaxWrapper)x).Type == name,
_ => false,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1286,4 +1286,47 @@ public void M(object o)
}
}
""", allowSemanticModel);

[DataTestMethod]
[DataRow("[k:scoped] ref S s2 = ref s1;", false)]
[DataRow("scoped ref [t:S] s2 = ref s1;", false)]
[DataRow("ref [t:S] s2 = ref s1;", false)]
[DataRow("scoped [t:S] s2 = s1;", false)]
[DataRow("[k:scoped] [k:ref] [k:readonly] [t:S] [u:s2] = [k:ref] [u:s1];", false)]
[DataRow("[k:int] [u:scoped] = 1;", false)]
public void IdentifierToken_Scoped_Local(string localDeclaration, bool allowSemanticModel) =>
ClassifierTestHarness.AssertTokenTypes($$"""
using System;
public ref struct S { }
public class C
{
public void M(ref S s1)
{
{{localDeclaration}}
}
}
""", allowSemanticModel);

[DataTestMethod]
[DataRow("[k:scoped] ref S s", false)]
[DataRow("scoped ref [t:S] s", false)]
[DataRow("ref [t:S] s", false)]
[DataRow("scoped [t:S] s", false)]
[DataRow("[k:scoped] [k:ref] [t:S] [u:s]", false)]
[DataRow("[k:int] [u:scoped]", false)]
public void IdentifierToken_Scoped_Parameter(string parameterDeclaration, bool allowSemanticModel) =>
ClassifierTestHarness.AssertTokenTypes($$"""
using System;
public ref struct S { }
public class C
{
public void M({{parameterDeclaration}})
{
}
}
""", allowSemanticModel);
}

0 comments on commit 6869c7c

Please sign in to comment.