提交 6156724f 编写于 作者: C Cyrus Najmabadi

Update tests.

上级 be92d952
...@@ -56,8 +56,7 @@ protected override bool SupportsPropertyInitializer(Compilation compilation) ...@@ -56,8 +56,7 @@ protected override bool SupportsPropertyInitializer(Compilation compilation)
if (member is PropertyDeclarationSyntax propertyDeclaration) if (member is PropertyDeclarationSyntax propertyDeclaration)
{ {
var property = (IPropertySymbol)context.SemanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken); AnalyzeProperty(context, propertyDeclaration, analysisResults);
AnalyzeProperty(context, property, analysisResults);
} }
} }
......
...@@ -36,7 +36,6 @@ protected override SyntaxNode GetNodeToRemove(VariableDeclaratorSyntax declarato ...@@ -36,7 +36,6 @@ protected override SyntaxNode GetNodeToRemove(VariableDeclaratorSyntax declarato
var sourceText = await propertyDocument.GetTextAsync(cancellationToken).ConfigureAwait(false); var sourceText = await propertyDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
var getAccessor = propertyDeclaration.AccessorList.Accessors.First(d => d.IsKind(SyntaxKind.GetAccessorDeclaration)); var getAccessor = propertyDeclaration.AccessorList.Accessors.First(d => d.IsKind(SyntaxKind.GetAccessorDeclaration));
var isSingleLine = sourceText.AreOnSameLine(getAccessor.GetFirstToken(), getAccessor.GetLastToken());
var updatedProperty = propertyDeclaration.WithAccessorList(UpdateAccessorList(propertyDeclaration.AccessorList)); var updatedProperty = propertyDeclaration.WithAccessorList(UpdateAccessorList(propertyDeclaration.AccessorList));
...@@ -63,10 +62,7 @@ protected override SyntaxNode GetNodeToRemove(VariableDeclaratorSyntax declarato ...@@ -63,10 +62,7 @@ protected override SyntaxNode GetNodeToRemove(VariableDeclaratorSyntax declarato
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)); .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
} }
if (isSingleLine) updatedProperty = updatedProperty.WithAdditionalAnnotations(SpecializedFormattingAnnotation);
{
updatedProperty = updatedProperty.WithAdditionalAnnotations(SpecializedFormattingAnnotation);
}
return updatedProperty; return updatedProperty;
} }
......
...@@ -35,9 +35,7 @@ int P ...@@ -35,9 +35,7 @@ int P
@"class Class @"class Class
{ {
int P int P { get; }
{
get; }
}"); }");
} }
...@@ -60,9 +58,7 @@ public int P ...@@ -60,9 +58,7 @@ public int P
@"class Class @"class Class
{ {
public int P public int P { get; private set; }
{
get; private set; }
}", }",
CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5)); CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
} }
...@@ -104,9 +100,7 @@ int P ...@@ -104,9 +100,7 @@ int P
@"class Class @"class Class
{ {
int P int P { get; } = 1;
{
get; } = 1;
}"); }");
} }
...@@ -147,9 +141,7 @@ public async Task TestSingleGetterFromProperty() ...@@ -147,9 +141,7 @@ public async Task TestSingleGetterFromProperty()
@"class Class @"class Class
{ {
int P int P { get; }
{
get; }
}"); }");
} }
...@@ -195,9 +187,7 @@ int P ...@@ -195,9 +187,7 @@ int P
@"class Class @"class Class
{ {
int P int P { get; set; }
{
get; set; }
}"); }");
} }
...@@ -220,9 +210,7 @@ int P ...@@ -220,9 +210,7 @@ int P
@"class Class @"class Class
{ {
int P int P { get; }
{
get; }
}"); }");
} }
...@@ -268,9 +256,7 @@ int P ...@@ -268,9 +256,7 @@ int P
@"class Class @"class Class
{ {
int P int P { get; set; }
{
get; set; }
}"); }");
} }
...@@ -503,9 +489,7 @@ int P ...@@ -503,9 +489,7 @@ int P
{ {
int j, k; int j, k;
int P int P { get; }
{
get; }
}"); }");
} }
...@@ -529,9 +513,7 @@ int P ...@@ -529,9 +513,7 @@ int P
{ {
int i, k; int i, k;
int P int P { get; }
{
get; }
}"); }");
} }
...@@ -555,9 +537,7 @@ int P ...@@ -555,9 +537,7 @@ int P
{ {
int i, j; int i, j;
int P int P { get; }
{
get; }
}"); }");
} }
...@@ -586,9 +566,7 @@ int P ...@@ -586,9 +566,7 @@ int P
partial class Class partial class Class
{ {
int P int P { get; }
{
get; }
}"); }");
} }
...@@ -635,9 +613,7 @@ public Class() ...@@ -635,9 +613,7 @@ public Class()
@"class Class @"class Class
{ {
int P int P { get; }
{
get; }
public Class() public Class()
{ {
...@@ -670,9 +646,7 @@ public Class(int P) ...@@ -670,9 +646,7 @@ public Class(int P)
@"class Class @"class Class
{ {
int P int P { get; }
{
get; }
public Class(int P) public Class(int P)
{ {
...@@ -705,9 +679,7 @@ public Class() ...@@ -705,9 +679,7 @@ public Class()
@"class Class @"class Class
{ {
int P int P { get; }
{
get; }
public Class() public Class()
{ {
...@@ -740,9 +712,7 @@ public void Goo() ...@@ -740,9 +712,7 @@ public void Goo()
@"class Class @"class Class
{ {
int P int P { get; set; }
{
get; set; }
public void Goo() public void Goo()
{ {
...@@ -775,9 +745,7 @@ public void Goo() ...@@ -775,9 +745,7 @@ public void Goo()
@"class Class @"class Class
{ {
public int P public int P { get; private set; }
{
get; private set; }
public void Goo() public void Goo()
{ {
...@@ -877,9 +845,7 @@ public async Task Tuple_SingleGetterFromField() ...@@ -877,9 +845,7 @@ public async Task Tuple_SingleGetterFromField()
@"class Class @"class Class
{ {
(int, string) P (int, string) P { get; }
{
get; }
}"); }");
} }
...@@ -902,9 +868,7 @@ public async Task TupleWithNames_SingleGetterFromField() ...@@ -902,9 +868,7 @@ public async Task TupleWithNames_SingleGetterFromField()
@"class Class @"class Class
{ {
(int a, string b) P (int a, string b) P { get; }
{
get; }
}"); }");
} }
...@@ -945,9 +909,7 @@ public async Task TupleWithOneName_SingleGetterFromField() ...@@ -945,9 +909,7 @@ public async Task TupleWithOneName_SingleGetterFromField()
@"class Class @"class Class
{ {
(int a, string) P (int a, string) P { get; }
{
get; }
}"); }");
} }
...@@ -970,9 +932,7 @@ public async Task Tuple_Initializer() ...@@ -970,9 +932,7 @@ public async Task Tuple_Initializer()
@"class Class @"class Class
{ {
(int, string) P (int, string) P { get; } = (1, ""hello"");
{
get; } = (1, ""hello"");
}"); }");
} }
...@@ -1000,9 +960,42 @@ public async Task Tuple_GetterAndSetter() ...@@ -1000,9 +960,42 @@ public async Task Tuple_GetterAndSetter()
@"class Class @"class Class
{ {
(int, string) P (int, string) P { get; set; }
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseAutoProperty)]
public async Task TestFixAllInDocument()
{
await TestInRegularAndScriptAsync(
@"class Class
{
{|FixAllInDocument:int i|};
int P
{ {
get; set; } get
{
return i;
}
}
int j;
int Q
{
get
{
return j;
}
}
}",
@"class Class
{
int P { get; }
int Q { get; }
}"); }");
} }
} }
......
...@@ -53,8 +53,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UseAutoProperty ...@@ -53,8 +53,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UseAutoProperty
Dim propertyDeclaration = TryCast(member, PropertyBlockSyntax) Dim propertyDeclaration = TryCast(member, PropertyBlockSyntax)
If propertyDeclaration IsNot Nothing Then If propertyDeclaration IsNot Nothing Then
Dim [property] = context.SemanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken) AnalyzeProperty(context, propertyDeclaration, analysisResults)
AnalyzeProperty(context, [property], analysisResults)
End If End If
End Sub End Sub
......
...@@ -10,11 +10,6 @@ ...@@ -10,11 +10,6 @@
namespace Microsoft.CodeAnalysis.UseAutoProperty namespace Microsoft.CodeAnalysis.UseAutoProperty
{ {
internal static class Constants
{
public const string SymbolEquivalenceKey = nameof(SymbolEquivalenceKey);
}
internal abstract class AbstractUseAutoPropertyAnalyzer<TPropertyDeclaration, TFieldDeclaration, TVariableDeclarator, TExpression> : internal abstract class AbstractUseAutoPropertyAnalyzer<TPropertyDeclaration, TFieldDeclaration, TVariableDeclarator, TExpression> :
AbstractCodeStyleDiagnosticAnalyzer AbstractCodeStyleDiagnosticAnalyzer
where TPropertyDeclaration : SyntaxNode where TPropertyDeclaration : SyntaxNode
...@@ -31,7 +26,7 @@ protected AbstractUseAutoPropertyAnalyzer() ...@@ -31,7 +26,7 @@ protected AbstractUseAutoPropertyAnalyzer()
} }
public override bool OpenFileOnly(Workspace workspace) => false; public override bool OpenFileOnly(Workspace workspace) => false;
public override DiagnosticAnalyzerCategory GetAnalyzerCategory() => DiagnosticAnalyzerCategory.ProjectAnalysis; public override DiagnosticAnalyzerCategory GetAnalyzerCategory() => DiagnosticAnalyzerCategory.SemanticSpanAnalysis;
protected abstract void RegisterIneligibleFieldsAction( protected abstract void RegisterIneligibleFieldsAction(
List<AnalysisResult> analysisResults, HashSet<IFieldSymbol> ineligibleFields, List<AnalysisResult> analysisResults, HashSet<IFieldSymbol> ineligibleFields,
...@@ -62,8 +57,17 @@ private void AnalyzeSemanticModel(SemanticModelAnalysisContext context) ...@@ -62,8 +57,17 @@ private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
protected abstract void AnalyzeCompilationUnit(SemanticModelAnalysisContext context, SyntaxNode root, List<AnalysisResult> analysisResults); protected abstract void AnalyzeCompilationUnit(SemanticModelAnalysisContext context, SyntaxNode root, List<AnalysisResult> analysisResults);
protected void AnalyzeProperty(SemanticModelAnalysisContext context, IPropertySymbol property, List<AnalysisResult> analysisResults) protected void AnalyzeProperty(SemanticModelAnalysisContext context, TPropertyDeclaration propertyDeclaration, List<AnalysisResult> analysisResults)
{ {
var cancellationToken = context.CancellationToken;
var semanticModel = context.SemanticModel;
var property = semanticModel.GetDeclaredSymbol(propertyDeclaration, cancellationToken) as IPropertySymbol;
if (property == null)
{
return;
}
if (property.IsIndexer) if (property.IsIndexer)
{ {
return; return;
...@@ -104,14 +108,6 @@ protected void AnalyzeProperty(SemanticModelAnalysisContext context, IPropertySy ...@@ -104,14 +108,6 @@ protected void AnalyzeProperty(SemanticModelAnalysisContext context, IPropertySy
return; return;
} }
var cancellationToken = context.CancellationToken;
var propertyDeclaration = property.DeclaringSyntaxReferences[0].GetSyntax(cancellationToken).FirstAncestorOrSelf<TPropertyDeclaration>();
if (propertyDeclaration == null)
{
return;
}
var semanticModel = context.SemanticModel;
var getterField = GetGetterField(semanticModel, property.GetMethod, cancellationToken); var getterField = GetGetterField(semanticModel, property.GetMethod, cancellationToken);
if (getterField == null) if (getterField == null)
{ {
...@@ -272,9 +268,6 @@ private void Process(AnalysisResult result, SemanticModelAnalysisContext context ...@@ -272,9 +268,6 @@ private void Process(AnalysisResult result, SemanticModelAnalysisContext context
var variableDeclarator = result.VariableDeclarator; var variableDeclarator = result.VariableDeclarator;
var nodeToFade = GetNodeToFade(result.FieldDeclaration, variableDeclarator); var nodeToFade = GetNodeToFade(result.FieldDeclaration, variableDeclarator);
var properties = ImmutableDictionary<string, string>.Empty.Add(
Constants.SymbolEquivalenceKey, result.SymbolEquivalenceKey);
// Fade out the field/variable we are going to remove. // Fade out the field/variable we are going to remove.
var diagnostic1 = Diagnostic.Create(UnnecessaryWithoutSuggestionDescriptor, nodeToFade.GetLocation()); var diagnostic1 = Diagnostic.Create(UnnecessaryWithoutSuggestionDescriptor, nodeToFade.GetLocation());
context.ReportDiagnostic(diagnostic1); context.ReportDiagnostic(diagnostic1);
...@@ -284,10 +277,10 @@ private void Process(AnalysisResult result, SemanticModelAnalysisContext context ...@@ -284,10 +277,10 @@ private void Process(AnalysisResult result, SemanticModelAnalysisContext context
// them when performing the code fix. // them when performing the code fix.
IEnumerable<Location> additionalLocations = new Location[] { propertyDeclaration.GetLocation(), variableDeclarator.GetLocation() }; IEnumerable<Location> additionalLocations = new Location[] { propertyDeclaration.GetLocation(), variableDeclarator.GetLocation() };
var diagnostic2 = Diagnostic.Create(HiddenDescriptor, propertyDeclaration.GetLocation(), additionalLocations, properties); var diagnostic2 = Diagnostic.Create(HiddenDescriptor, propertyDeclaration.GetLocation(), additionalLocations);
context.ReportDiagnostic(diagnostic2); context.ReportDiagnostic(diagnostic2);
var diagnostic3 = Diagnostic.Create(HiddenDescriptor, nodeToFade.GetLocation(), additionalLocations, properties); var diagnostic3 = Diagnostic.Create(HiddenDescriptor, nodeToFade.GetLocation(), additionalLocations);
context.ReportDiagnostic(diagnostic3); context.ReportDiagnostic(diagnostic3);
} }
......
...@@ -46,20 +46,17 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context) ...@@ -46,20 +46,17 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
{ {
foreach (var diagnostic in context.Diagnostics) foreach (var diagnostic in context.Diagnostics)
{ {
var equivalenceKey = diagnostic.Properties[Constants.SymbolEquivalenceKey];
context.RegisterCodeFix( context.RegisterCodeFix(
new UseAutoPropertyCodeAction( new UseAutoPropertyCodeAction(
FeaturesResources.Use_auto_property, FeaturesResources.Use_auto_property,
c => ProcessResult(context, diagnostic, c), c => ProcessResultAsync(context, diagnostic, c)),
equivalenceKey),
diagnostic); diagnostic);
} }
return SpecializedTasks.EmptyTask; return SpecializedTasks.EmptyTask;
} }
private async Task<Solution> ProcessResult(CodeFixContext context, Diagnostic diagnostic, CancellationToken cancellationToken) private async Task<Solution> ProcessResultAsync(CodeFixContext context, Diagnostic diagnostic, CancellationToken cancellationToken)
{ {
var locations = diagnostic.AdditionalLocations; var locations = diagnostic.AdditionalLocations;
var propertyLocation = locations[0]; var propertyLocation = locations[0];
...@@ -238,8 +235,8 @@ private async Task<SyntaxNode> FormatAsync(SyntaxNode newRoot, Document document ...@@ -238,8 +235,8 @@ private async Task<SyntaxNode> FormatAsync(SyntaxNode newRoot, Document document
private class UseAutoPropertyCodeAction : CodeAction.SolutionChangeAction private class UseAutoPropertyCodeAction : CodeAction.SolutionChangeAction
{ {
public UseAutoPropertyCodeAction(string title, Func<CancellationToken, Task<Solution>> createChangedSolution, string equivalenceKey) public UseAutoPropertyCodeAction(string title, Func<CancellationToken, Task<Solution>> createChangedSolution)
: base(title, createChangedSolution, equivalenceKey) : base(title, createChangedSolution, title)
{ {
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册