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

Update tests.

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