Skip to content

Commit 8e8ddb0

Browse files
Merge pull request #62947 from CyrusNajmabadi/banAPI2
Ban SyntaxEditor constructor that takes HostWorkspaceServices.
2 parents d81ce62 + a45078d commit 8e8ddb0

File tree

10 files changed

+30
-20
lines changed

10 files changed

+30
-20
lines changed

eng/config/BannedSymbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAna
3434
M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAnalysis.Document,Microsoft.CodeAnalysis.SyntaxAnnotation,Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload that takes SimplifierOptions
3535
M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAnalysis.Document,Microsoft.CodeAnalysis.Text.TextSpan,Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload that takes SimplifierOptions
3636
M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAnalysis.Document,System.Collections.Generic.IEnumerable{Microsoft.CodeAnalysis.Text.TextSpan},Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload that takes SimplifierOptions
37+
M:Microsoft.CodeAnalysis.Editing.SyntaxEditor.#ctor(Microsoft.CodeAnalysis.SyntaxNode,Microsoft.CodeAnalysis.Host.HostWorkspaceServices); Use overload that takes HostSolutionServices instead

src/Analyzers/CSharp/CodeFixes/InlineDeclaration/CSharpInlineDeclarationCodeFixProvider.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,11 @@ await editor.ApplyExpressionLevelSemanticEditsAsync(
8080

8181
return (t.invocationOrCreation, additionalNodesToTrack.ToImmutable());
8282
},
83-
(_1, _2, _3) => true,
83+
(_, _, _) => true,
8484
(semanticModel, currentRoot, t, currentNode)
8585
=> ReplaceIdentifierWithInlineDeclaration(
86-
options, semanticModel, currentRoot, t.declarator,
87-
t.identifier, currentNode, declarationsToRemove, document.Project.Solution.Workspace.Services,
88-
cancellationToken),
86+
document, options, semanticModel, currentRoot, t.declarator,
87+
t.identifier, currentNode, declarationsToRemove, cancellationToken),
8988
cancellationToken).ConfigureAwait(false);
9089
}
9190

@@ -106,17 +105,17 @@ private static (VariableDeclaratorSyntax declarator, IdentifierNameSyntax identi
106105
}
107106

108107
private static SyntaxNode ReplaceIdentifierWithInlineDeclaration(
108+
Document document,
109109
CSharpCodeFixOptionsProvider options, SemanticModel semanticModel,
110110
SyntaxNode currentRoot, VariableDeclaratorSyntax declarator,
111111
IdentifierNameSyntax identifier, SyntaxNode currentNode,
112112
HashSet<StatementSyntax> declarationsToRemove,
113-
HostWorkspaceServices services,
114113
CancellationToken cancellationToken)
115114
{
116115
declarator = currentRoot.GetCurrentNode(declarator);
117116
identifier = currentRoot.GetCurrentNode(identifier);
118117

119-
var editor = new SyntaxEditor(currentRoot, services);
118+
var editor = document.GetSyntaxEditor(currentRoot);
120119
var sourceText = currentRoot.GetText();
121120

122121
var declaration = (VariableDeclarationSyntax)declarator.Parent;

src/Analyzers/CSharp/CodeFixes/UseDeconstruction/CSharpUseDeconstructionCodeFixProvider.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,18 @@ protected override Task FixAllAsync(
5555
return editor.ApplyMethodBodySemanticEditsAsync(
5656
document, nodesToProcess,
5757
(semanticModel, node) => true,
58-
(semanticModel, currentRoot, node) => UpdateRoot(semanticModel, currentRoot, node, document.Project.Solution.Workspace.Services, cancellationToken),
58+
(semanticModel, currentRoot, node) => UpdateRoot(document, semanticModel, currentRoot, node, cancellationToken),
5959
cancellationToken);
6060
}
6161

62-
private SyntaxNode UpdateRoot(SemanticModel semanticModel, SyntaxNode root, SyntaxNode node, HostWorkspaceServices services, CancellationToken cancellationToken)
62+
private SyntaxNode UpdateRoot(
63+
Document document,
64+
SemanticModel semanticModel,
65+
SyntaxNode root,
66+
SyntaxNode node,
67+
CancellationToken cancellationToken)
6368
{
64-
var editor = new SyntaxEditor(root, services);
69+
var editor = document.GetSyntaxEditor(root);
6570

6671
// We use the callback form of ReplaceNode because we may have nested code that
6772
// needs to be updated in fix-all situations. For example, nested foreach statements.

src/Analyzers/Core/CodeFixes/RemoveUnusedParametersAndValues/AbstractRemoveUnusedValuesCodeFixProvider.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -795,12 +795,9 @@ private async Task<SyntaxNode> ReplaceDiscardDeclarationsWithAssignmentsAsync(Sy
795795
{
796796
var service = document.GetLanguageService<IReplaceDiscardDeclarationsWithAssignmentsService>();
797797
if (service == null)
798-
{
799798
return memberDeclaration;
800-
}
801799

802-
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
803-
return await service.ReplaceAsync(memberDeclaration, semanticModel, document.Project.Solution.Workspace.Services, cancellationToken).ConfigureAwait(false);
800+
return await service.ReplaceAsync(document, memberDeclaration, cancellationToken).ConfigureAwait(false);
804801
}
805802

806803
/// <summary>

src/Features/Core/Portable/ConvertAutoPropertyToFullProperty/AbstractConvertAutoPropertyToFullPropertyCodeRefactoringProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ private async Task<Document> ExpandToFullPropertyAsync(
109109
: new SyntaxNode[] { newGetAccessor, newSetAccessor })
110110
.WithLeadingTrivia(property.GetLeadingTrivia());
111111
fullProperty = ConvertPropertyToExpressionBodyIfDesired(info, fullProperty);
112-
var editor = new SyntaxEditor(root, services);
112+
113+
var editor = document.GetSyntaxEditor(root);
113114
editor.ReplaceNode(property, fullProperty.WithAdditionalAnnotations(Formatter.Annotation));
114115

115116
// add backing field, plus initializer if it exists

src/Workspaces/Core/Portable/Editing/SyntaxEditor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class SyntaxEditor
2626
/// </summary>
2727
[Obsolete("Use SyntaxEditor(SyntaxNode, HostWorkspaceServices)")]
2828
public SyntaxEditor(SyntaxNode root, Workspace workspace)
29-
: this(root, (workspace ?? throw new ArgumentNullException(nameof(workspace))).Services)
29+
: this(root, (workspace ?? throw new ArgumentNullException(nameof(workspace))).Services.SolutionServices)
3030
{
3131
}
3232

src/Workspaces/CoreTest/Editing/SyntaxEditorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private void VerifySyntax<TSyntax>(SyntaxNode node, string expectedText) where T
3636
}
3737

3838
private SyntaxEditor GetEditor(SyntaxNode root)
39-
=> new SyntaxEditor(root, EmptyWorkspace.Services);
39+
=> new SyntaxEditor(root, EmptyWorkspace.Services.SolutionServices);
4040

4141
[Fact]
4242
public void TestReplaceNode()

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpReplaceDiscardDeclarationsWithAssignmentsService.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Microsoft.CodeAnalysis.Host.Mef;
1919
using Microsoft.CodeAnalysis.PooledObjects;
2020
using Microsoft.CodeAnalysis.ReplaceDiscardDeclarationsWithAssignments;
21+
using Microsoft.CodeAnalysis.Shared.Extensions;
2122
using Roslyn.Utilities;
2223

2324
namespace Microsoft.CodeAnalysis.CSharp.ReplaceDiscardDeclarationsWithAssignments
@@ -33,9 +34,13 @@ public CSharpReplaceDiscardDeclarationsWithAssignmentsService()
3334
{
3435
}
3536

36-
public Task<SyntaxNode> ReplaceAsync(SyntaxNode memberDeclaration, SemanticModel semanticModel, HostWorkspaceServices services, CancellationToken cancellationToken)
37+
public async Task<SyntaxNode> ReplaceAsync(
38+
Document document,
39+
SyntaxNode memberDeclaration,
40+
CancellationToken cancellationToken)
3741
{
38-
var editor = new SyntaxEditor(memberDeclaration, services);
42+
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
43+
var editor = document.GetSyntaxEditor(memberDeclaration);
3944
foreach (var child in memberDeclaration.DescendantNodes())
4045
{
4146
switch (child)
@@ -102,7 +107,7 @@ public Task<SyntaxNode> ReplaceAsync(SyntaxNode memberDeclaration, SemanticModel
102107
}
103108
}
104109

105-
return Task.FromResult(editor.GetChangedRoot());
110+
return editor.GetChangedRoot();
106111
}
107112

108113
private static bool IsDiscardDeclaration(VariableDeclaratorSyntax variable)

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Extensions/DocumentExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ public static SyntaxEditor GetSyntaxEditor(this Document document, SyntaxNode ro
218218
{
219219
#if CODE_STYLE
220220
// Remove once Solution.Services becomes public: https://github.com/dotnet/roslyn/issues/62914
221+
#pragma warning disable RS0030 // Do not used banned APIs. This is the shim method to use until the api becomes public.
221222
return new SyntaxEditor(root, document.Project.Solution.Workspace.Services);
223+
#pragma warning restore RS0030 // Do not used banned APIs
222224
#else
223225
return new SyntaxEditor(root, document.Project.Solution.Services);
224226
#endif

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/ReplaceDiscardDeclarationsWithAssignments/IReplaceDiscardDeclarationsWithAssignmentsService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ internal interface IReplaceDiscardDeclarationsWithAssignmentsService : ILanguage
2929
/// where the code transformation wants to generate new discard assignment(s), which would be a compiler error.
3030
/// This method replaces such discard variable declarations with discard assignments.
3131
/// </summary>
32-
Task<SyntaxNode> ReplaceAsync(SyntaxNode memberDeclaration, SemanticModel semanticModel, HostWorkspaceServices services, CancellationToken cancellationToken);
32+
Task<SyntaxNode> ReplaceAsync(Document document, SyntaxNode memberDeclaration, CancellationToken cancellationToken);
3333
}
3434
}

0 commit comments

Comments
 (0)