Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify code using SyntaxFactory. #72901

Merged
merged 80 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
3497bfb
Use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
1919a87
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
242cbd8
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
c9b956d
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
f51c4fc
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
0c0560c
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
f60511a
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
765f9df
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
c5d4aea
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
c24ee0e
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
509e523
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
9126e74
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
92f7ae6
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
6c1c58a
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
ed97a19
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
2f57b3e
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
534837f
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
fc6811c
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
7f06031
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
3a85fde
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
104e4a9
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
ea10eb2
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
6e7ab05
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
fbae3b0
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
4857be4
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
6670b53
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
ea6d346
use SyntaxFactory
CyrusNajmabadi Apr 6, 2024
570ffda
Merge branch 'singleton' into syntaxFactory
CyrusNajmabadi Apr 6, 2024
7bcb95a
simplify
CyrusNajmabadi Apr 6, 2024
3ca6701
simplify
CyrusNajmabadi Apr 6, 2024
5e46569
Merge branch 'main' into syntaxFactory
CyrusNajmabadi Apr 7, 2024
26094af
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
83f07ac
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
0149770
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
7a5c3c3
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
29e24e7
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
5f6cc40
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
75a6680
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
18c6414
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
e1fc6c2
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
86a6118
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
d1feb82
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
6350289
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
4086af1
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
81ce1b3
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
d237da8
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
b3e9391
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
59c7437
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
689cbda
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
c86bddf
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
77e2584
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
863d327
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
4d231c5
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
895804b
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
d4dff21
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
6170b9f
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
d728d82
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
35b2dc4
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
ee28692
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
f2422a7
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
2145c6d
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
3315954
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
176e288
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
1cca695
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
7bb5c7a
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
2fc5890
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
0620114
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
e54e0f8
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
5f2076e
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
d574609
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
6ed35e5
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
2c9b3a3
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
c9addc3
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
a889ce8
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
50826b2
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
b3e8e63
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
3bb5dd9
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
70bdbd8
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
7cff5bd
use SyntaxFactory
CyrusNajmabadi Apr 7, 2024
68a5c76
REvert
CyrusNajmabadi Apr 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace Microsoft.CodeAnalysis.CSharp.UseCollectionExpression;

using static UseCollectionExpressionHelpers;
using static SyntaxFactory;
using FluentState = UpdateExpressionState<ExpressionSyntax, StatementSyntax>;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
Expand All @@ -42,7 +43,7 @@ internal sealed partial class CSharpUseCollectionExpressionForFluentDiagnosticAn
nameof(Array),
nameof(Span<int>),
nameof(ReadOnlySpan<int>),
nameof(List<int>),
nameof(System.Collections.Generic.List<int>),
nameof(HashSet<int>),
nameof(LinkedList<int>),
nameof(Queue<int>),
Expand Down Expand Up @@ -293,7 +294,7 @@ void AddFinalMatch(ExpressionSyntax expression)
{
// Remove any whitespace around the `.`, making the singly-wrapped fluent expression into a single line.
matchesInReverse.Add(new CollectionExpressionMatch<ArgumentSyntax>(
SyntaxFactory.Argument(innerInvocation.WithExpression(
Argument(innerInvocation.WithExpression(
memberAccess.Update(
memberAccess.Expression.WithoutTrailingTrivia(),
memberAccess.OperatorToken.WithoutTrivia(),
Expand All @@ -302,7 +303,7 @@ void AddFinalMatch(ExpressionSyntax expression)
return;
}

matchesInReverse.Add(new CollectionExpressionMatch<ArgumentSyntax>(SyntaxFactory.Argument(expression), UseSpread: true));
matchesInReverse.Add(new CollectionExpressionMatch<ArgumentSyntax>(Argument(expression), UseSpread: true));
}

// We only want to offer this feature when the original collection was list-like (as opposed to being something
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.UseCollectionInitializer;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.UseCollectionInitializer;

Expand Down Expand Up @@ -40,5 +39,5 @@ public void GetPartsOfIfStatement(
}

private static IEnumerable<StatementSyntax> ExtractEmbeddedStatements(StatementSyntax embeddedStatement)
=> embeddedStatement is BlockSyntax block ? block.Statements : SpecializedCollections.SingletonEnumerable(embeddedStatement);
=> embeddedStatement is BlockSyntax block ? [.. block.Statements] : [embeddedStatement];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed in pervious pr.

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeGeneration;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
Expand All @@ -17,6 +16,8 @@

namespace Microsoft.CodeAnalysis.CSharp.UseExpressionBody;

using static SyntaxFactory;

/// <summary>
/// Helper class that allows us to share lots of logic between the diagnostic analyzer and the
/// code refactoring provider. Those can't share a common base class due to their own inheritance
Expand Down Expand Up @@ -160,7 +161,7 @@ protected bool TryConvertToExpressionBodyForBaseProperty(
if (getAccessor?.ExpressionBody != null &&
BlockSyntaxExtensions.MatchesPreference(getAccessor.ExpressionBody.Expression, conversionPreference))
{
arrowExpression = SyntaxFactory.ArrowExpressionClause(getAccessor.ExpressionBody.Expression);
arrowExpression = ArrowExpressionClause(getAccessor.ExpressionBody.Expression);
semicolonToken = getAccessor.SemicolonToken;
return true;
}
Expand All @@ -180,7 +181,7 @@ public bool CanOfferUseBlockBody(

expressionBody = GetExpressionBody(declaration);
if (expressionBody?.TryConvertToBlock(
SyntaxFactory.Token(SyntaxKind.SemicolonToken), false, block: out _) != true)
Token(SyntaxKind.SemicolonToken), false, block: out _) != true)
{
fixesError = false;
return false;
Expand Down Expand Up @@ -296,13 +297,13 @@ protected TDeclaration WithAccessorList(SemanticModel semanticModel, TDeclaratio
CreateReturnStatementForExpression(semanticModel, declaration),
out var block);

var accessor = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration);
var accessor = AccessorDeclaration(SyntaxKind.GetAccessorDeclaration);
accessor = block != null
? accessor.WithBody(block)
: accessor.WithExpressionBody(expressionBody)
.WithSemicolonToken(semicolonToken);

return WithAccessorList(declaration, SyntaxFactory.AccessorList([accessor]));
return WithAccessorList(declaration, AccessorList([accessor]));
}

protected virtual TDeclaration WithAccessorList(TDeclaration declaration, AccessorListSyntax accessorListSyntax)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Microsoft.CodeAnalysis.CSharp.UsePatternCombinators;

using static SyntaxFactory;

/// <summary>
/// Base class to represent a pattern constructed from various checks
/// </summary>
Expand Down Expand Up @@ -48,12 +50,12 @@ internal sealed class Type : AnalyzedPattern
// semantics, and for 'C.X' could be a compile error.
//
// So lets create a pattern syntax and make sure the result is the same
var dummyStatement = SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(
var dummyStatement = ExpressionStatement(AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
SyntaxFactory.IdentifierName("_"),
SyntaxFactory.IsPatternExpression(
IdentifierName("_"),
IsPatternExpression(
binaryExpression.Left,
SyntaxFactory.ConstantPattern(SyntaxFactory.ParenthesizedExpression(binaryExpression.Right.WithAdditionalAnnotations(s_annotation)))
ConstantPattern(ParenthesizedExpression(binaryExpression.Right.WithAdditionalAnnotations(s_annotation)))
)
));

Expand Down Expand Up @@ -149,7 +151,7 @@ private Binary(AnalyzedPattern leftPattern, AnalyzedPattern rightPattern, bool i
return null;

var compareTarget = target == leftTarget ? rightTarget : leftTarget;
if (!SyntaxFactory.AreEquivalent(target.Syntax, compareTarget.Syntax))
if (!AreEquivalent(target.Syntax, compareTarget.Syntax))
return null;

return new Binary(leftPattern, rightPattern, isDisjunctive, token, target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace Microsoft.CodeAnalysis.CSharp.ConvertNamespace;

using static SyntaxFactory;

internal static class ConvertNamespaceTransform
{
public static Task<Document> ConvertAsync(Document document, BaseNamespaceDeclarationSyntax baseNamespace, CSharpSyntaxFormattingOptions options, CancellationToken cancellationToken)
Expand Down Expand Up @@ -271,7 +273,7 @@ private static SyntaxNode ReplaceWithBlockScopedNamespace(
if (triviaBeforeSplit.Length > 0)
{
if (needsAdditionalLineEnding)
triviaBeforeSplit = triviaBeforeSplit.Append(SyntaxFactory.EndOfLine(lineEnding));
triviaBeforeSplit = triviaBeforeSplit.Append(EndOfLine(lineEnding));

converted = converted.WithCloseBraceToken(converted.CloseBraceToken.WithPrependedLeadingTrivia(triviaBeforeSplit));
}
Expand Down Expand Up @@ -320,7 +322,7 @@ private static bool HasLeadingBlankLine(
private static FileScopedNamespaceDeclarationSyntax ConvertNamespaceDeclaration(NamespaceDeclarationSyntax namespaceDeclaration)
{
// If the open-brace token has any special trivia, then move them to after the semicolon.
var semiColon = SyntaxFactory.Token(SyntaxKind.SemicolonToken)
var semiColon = Token(SyntaxKind.SemicolonToken)
.WithoutTrivia()
.WithTrailingTrivia(namespaceDeclaration.Name.GetTrailingTrivia())
.WithAppendedTrailingTrivia(namespaceDeclaration.OpenBraceToken.LeadingTrivia);
Expand All @@ -329,7 +331,7 @@ private static FileScopedNamespaceDeclarationSyntax ConvertNamespaceDeclaration(
semiColon = semiColon.WithAppendedTrailingTrivia(namespaceDeclaration.OpenBraceToken.TrailingTrivia);

// Move trivia after the original name token to now be after the new semicolon token.
var fileScopedNamespace = SyntaxFactory.FileScopedNamespaceDeclaration(
var fileScopedNamespace = FileScopedNamespaceDeclaration(
namespaceDeclaration.AttributeLists,
namespaceDeclaration.Modifiers,
namespaceDeclaration.NamespaceKeyword,
Expand Down Expand Up @@ -379,16 +381,16 @@ private static FileScopedNamespaceDeclarationSyntax ConvertNamespaceDeclaration(
private static NamespaceDeclarationSyntax ConvertFileScopedNamespace(ParsedDocument document, FileScopedNamespaceDeclarationSyntax fileScopedNamespace, string lineEnding, NewLinePlacement newLinePlacement)
{
var nameSyntax = fileScopedNamespace.Name.WithAppendedTrailingTrivia(fileScopedNamespace.SemicolonToken.LeadingTrivia)
.WithAppendedTrailingTrivia(newLinePlacement.HasFlag(NewLinePlacement.BeforeOpenBraceInTypes) ? SyntaxFactory.EndOfLine(lineEnding) : SyntaxFactory.Space);
var openBraceToken = SyntaxFactory.Token(SyntaxKind.OpenBraceToken).WithoutLeadingTrivia().WithTrailingTrivia(fileScopedNamespace.SemicolonToken.TrailingTrivia);
.WithAppendedTrailingTrivia(newLinePlacement.HasFlag(NewLinePlacement.BeforeOpenBraceInTypes) ? EndOfLine(lineEnding) : Space);
var openBraceToken = Token(SyntaxKind.OpenBraceToken).WithoutLeadingTrivia().WithTrailingTrivia(fileScopedNamespace.SemicolonToken.TrailingTrivia);

if (openBraceToken.TrailingTrivia is not [.., SyntaxTrivia(SyntaxKind.EndOfLineTrivia)])
{
openBraceToken = openBraceToken.WithAppendedTrailingTrivia(SyntaxFactory.EndOfLine(lineEnding));
openBraceToken = openBraceToken.WithAppendedTrailingTrivia(EndOfLine(lineEnding));
}

FileScopedNamespaceDeclarationSyntax adjustedFileScopedNamespace;
var closeBraceToken = SyntaxFactory.Token(SyntaxKind.CloseBraceToken).WithoutLeadingTrivia().WithoutTrailingTrivia();
var closeBraceToken = Token(SyntaxKind.CloseBraceToken).WithoutLeadingTrivia().WithoutTrailingTrivia();

// Normally the block scoped namespace will have a newline after the closing brace. The only exception to
// this occurs when there are no tokens after the closing brace and the document with a file scoped
Expand All @@ -397,28 +399,28 @@ private static NamespaceDeclarationSyntax ConvertFileScopedNamespace(ParsedDocum
if (!fileScopedNamespace.GetLastToken().GetNextTokenOrEndOfFile().IsKind(SyntaxKind.EndOfFileToken)
|| document.Text.Lines.GetLinePosition(document.Text.Length).Character == 0)
{
closeBraceToken = closeBraceToken.WithAppendedTrailingTrivia(SyntaxFactory.EndOfLine(lineEnding));
closeBraceToken = closeBraceToken.WithAppendedTrailingTrivia(EndOfLine(lineEnding));
adjustedFileScopedNamespace = fileScopedNamespace;
}
else
{
// Make sure the body of the file scoped namespace ends with a trailing new line (so the closing brace
// of the converted block-body namespace appears on its own line), but don't add a new line after the
// closing brace.
adjustedFileScopedNamespace = fileScopedNamespace.WithAppendedTrailingTrivia(SyntaxFactory.EndOfLine(lineEnding));
adjustedFileScopedNamespace = fileScopedNamespace.WithAppendedTrailingTrivia(EndOfLine(lineEnding));
}

// If the file scoped namespace is indented, also indent the newly added braces to match
var outerIndentation = document.Text.GetLeadingWhitespaceOfLineAtPosition(fileScopedNamespace.SpanStart);
if (outerIndentation.Length > 0)
{
if (newLinePlacement.HasFlag(NewLinePlacement.BeforeOpenBraceInTypes))
openBraceToken = openBraceToken.WithLeadingTrivia(openBraceToken.LeadingTrivia.Add(SyntaxFactory.Whitespace(outerIndentation)));
openBraceToken = openBraceToken.WithLeadingTrivia(openBraceToken.LeadingTrivia.Add(Whitespace(outerIndentation)));

closeBraceToken = closeBraceToken.WithLeadingTrivia(closeBraceToken.LeadingTrivia.Add(SyntaxFactory.Whitespace(outerIndentation)));
closeBraceToken = closeBraceToken.WithLeadingTrivia(closeBraceToken.LeadingTrivia.Add(Whitespace(outerIndentation)));
}

var namespaceDeclaration = SyntaxFactory.NamespaceDeclaration(
var namespaceDeclaration = NamespaceDeclaration(
adjustedFileScopedNamespace.AttributeLists,
adjustedFileScopedNamespace.Modifiers,
adjustedFileScopedNamespace.NamespaceKeyword,
Expand Down
Loading
Loading