-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69280 from CyrusNajmabadi/useCollectionExpression4
Add support for generating the `.. x ? [y] : []` pattern in a collection-expression
- Loading branch information
Showing
22 changed files
with
1,103 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...yzers/CSharp/Analyzers/UseCollectionInitializer/CSharpUseCollectionInitializerAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.UseCollectionInitializer; | ||
using Roslyn.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.UseCollectionInitializer; | ||
|
||
internal sealed class CSharpUseCollectionInitializerAnalyzer : AbstractUseCollectionInitializerAnalyzer< | ||
ExpressionSyntax, | ||
StatementSyntax, | ||
BaseObjectCreationExpressionSyntax, | ||
MemberAccessExpressionSyntax, | ||
InvocationExpressionSyntax, | ||
ExpressionStatementSyntax, | ||
ForEachStatementSyntax, | ||
IfStatementSyntax, | ||
VariableDeclaratorSyntax, | ||
CSharpUseCollectionInitializerAnalyzer> | ||
{ | ||
protected override bool IsComplexElementInitializer(SyntaxNode expression) | ||
=> expression.IsKind(SyntaxKind.ComplexElementInitializerExpression); | ||
|
||
protected override void GetPartsOfForeachStatement( | ||
ForEachStatementSyntax statement, | ||
out SyntaxToken identifier, | ||
out ExpressionSyntax expression, | ||
out IEnumerable<StatementSyntax> statements) | ||
{ | ||
identifier = statement.Identifier; | ||
expression = statement.Expression; | ||
statements = ExtractEmbeddedStatements(statement.Statement); | ||
} | ||
|
||
protected override void GetPartsOfIfStatement( | ||
IfStatementSyntax statement, | ||
out ExpressionSyntax condition, | ||
out IEnumerable<StatementSyntax> whenTrueStatements, | ||
out IEnumerable<StatementSyntax>? whenFalseStatements) | ||
{ | ||
condition = statement.Condition; | ||
whenTrueStatements = ExtractEmbeddedStatements(statement.Statement); | ||
whenFalseStatements = statement.Else != null ? ExtractEmbeddedStatements(statement.Else.Statement) : null; | ||
} | ||
|
||
private static IEnumerable<StatementSyntax> ExtractEmbeddedStatements(StatementSyntax embeddedStatement) | ||
=> embeddedStatement is BlockSyntax block ? block.Statements : SpecializedCollections.SingletonEnumerable(embeddedStatement); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.