Skip to content

Commit

Permalink
Merge pull request #59735 from alrz/fix-local-classification
Browse files Browse the repository at this point in the history
Classify pattern variables
  • Loading branch information
CyrusNajmabadi authored Dec 8, 2022
2 parents 23fdd87 + b6d7ed2 commit 92d2ddd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5109,7 +5109,7 @@ await TestInMethodAsync(@"
Punctuation.Comma,
Punctuation.DotDot,
Keyword("var"),
Identifier("end"),
Local("end"),
Punctuation.CloseBracket,
Punctuation.Colon,
ControlKeyword("break"),
Expand All @@ -5135,11 +5135,11 @@ await TestInMethodAsync(@"
Punctuation.OpenCurly,
Punctuation.OpenBracket,
Keyword("var"),
Identifier("start"),
Local("start"),
Punctuation.Comma,
Punctuation.DotDot,
Keyword("var"),
Identifier("end"),
Local("end"),
Punctuation.CloseBracket,
Operators.EqualsGreaterThan,
Number("1"),
Expand All @@ -5160,7 +5160,7 @@ await TestInMethodAsync(@"
Number("1"),
Keyword("is"),
Keyword("var"),
Identifier("x"),
Local("x"),
Punctuation.Semicolon);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2004,7 +2004,7 @@ void Goo(object o)
Field("Name"),
Punctuation.Colon,
Keyword("var"),
Identifier("n"),
Local("n"),
Punctuation.CloseCurly,
Punctuation.CloseParen,
Punctuation.OpenCurly,
Expand Down Expand Up @@ -2102,6 +2102,45 @@ void Goo(object o)
Punctuation.CloseCurly);
}

[Theory, WorkItem(59484, "https://github.com/dotnet/roslyn/issues/59484")]
[CombinatorialData]
public async Task TestPatternVariables(TestHost testHost)
{
await TestAsync(
@"
void M(object o) {
_ = o is [var (x, y), {} z] list;
}
",
testHost,
Keyword("void"),
Method("M"),
Punctuation.OpenParen,
Keyword("object"),
Parameter("o"),
Punctuation.CloseParen,
Punctuation.OpenCurly,
Keyword("_"),
Operators.Equals,
Parameter("o"),
Keyword("is"),
Punctuation.OpenBracket,
Keyword("var"),
Punctuation.OpenParen,
Local("x"),
Punctuation.Comma,
Local("y"),
Punctuation.CloseParen,
Punctuation.Comma,
Punctuation.OpenCurly,
Punctuation.CloseCurly,
Local("z"),
Punctuation.CloseBracket,
Local("list"),
Punctuation.Semicolon,
Punctuation.CloseCurly);
}

[Theory, WorkItem(42368, "https://github.com/dotnet/roslyn/issues/42368")]
[CombinatorialData]
public async Task RelationalPattern(TestHost testHost)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,28 +246,7 @@ private static bool IsVerbatimStringToken(SyntaxToken token)
}
else if (token.Parent is SingleVariableDesignationSyntax singleVariableDesignation && singleVariableDesignation.Identifier == token)
{
var parent = singleVariableDesignation.Parent;

// Handle nested Tuple deconstruction
while (parent.IsKind(SyntaxKind.ParenthesizedVariableDesignation))
{
parent = parent.Parent;
}

// Checking for DeclarationExpression covers the following cases:
// - Out parameters used within a field initializer or within a method. `int.TryParse("1", out var x)`
// - Tuple deconstruction. `var (x, _) = (1, 2);`
//
// Checking for DeclarationPattern covers the following cases:
// - Is patterns. `if (foo is Action action)`
// - Switch patterns. `case int x when x > 0:`
if (parent.IsKind(SyntaxKind.DeclarationExpression) ||
parent.IsKind(SyntaxKind.DeclarationPattern))
{
return ClassificationTypeNames.LocalName;
}

return ClassificationTypeNames.Identifier;
return ClassificationTypeNames.LocalName;
}
else if (token.Parent is ParameterSyntax parameterSyntax && parameterSyntax.Identifier == token)
{
Expand Down

0 comments on commit 92d2ddd

Please sign in to comment.