Skip to content

Commit

Permalink
Revert "✨ Support target-typed new expressions"
Browse files Browse the repository at this point in the history
This reverts commit 309d875.
  • Loading branch information
angularsen committed Oct 5, 2022
1 parent c9a0a5c commit ef2e452
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 92 deletions.
86 changes: 28 additions & 58 deletions AssignAll/AssignAll.Test/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,34 @@ private class Foo
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}

[Fact]
public async Task FileWithTopLevelStatements_AddsDiagnostic()
{
var test = @"
// AssignAll enable
var foo = {|#0:new Foo
{
// PropInt and PropString not assigned, diagnostic error
}|#0};
// Add methods and nested types available to top level statements via a partial Program class.
public static partial class Program
{
private class Foo
{
public int PropInt { get; set; }
public string PropString { get; set; }
}
}
";

// Baz does not have AssignAll enabled and should have no diagnostics.
await VerifyCS.VerifyAnalyzerAsync(test,
t => t.TestState.OutputKind = OutputKind.ConsoleApplication,
VerifyCS.Diagnostic("AssignAll").WithLocation(0).WithArguments("Foo", "PropInt, PropString")
);
}

[Fact]
public async Task PropertiesNotAssigned_NoCommentToEnableAnalyzer_AddsNoDiagnostics()
{
Expand Down Expand Up @@ -684,63 +712,5 @@ internal class Derived : Base
var expected = VerifyCS.Diagnostic("AssignAll").WithLocation(0).WithArguments("Derived", "BasePropUnassigned, DerivedPropUnassigned" );
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}

[Fact]
public async Task FileWithTopLevelStatements_AddsDiagnostic()
{
var test = @"
// AssignAll enable
var foo = {|#0:new Foo
{
// PropInt and PropString not assigned, diagnostic error
}|#0};
// Add methods and nested types available to top level statements via a partial Program class.
public static partial class Program
{
private class Foo
{
public int PropInt { get; set; }
public string PropString { get; set; }
}
}
";

// Baz does not have AssignAll enabled and should have no diagnostics.
await VerifyCS.VerifyAnalyzerAsync(test,
t => t.TestState.OutputKind = OutputKind.ConsoleApplication,
VerifyCS.Diagnostic("AssignAll").WithLocation(0).WithArguments("Foo", "PropInt, PropString")
);
}

[Fact]
public async Task TargetTypedNewSyntax_AddsDiagnostic()
{
var test = @"
// AssignAll enable
namespace Samples.ConsoleNet6;
public static class Example005_TargetTypedNew
{
public static void Irrelevant()
{
// This should give analyzer error:
// Missing member assignments in object initializer for type 'Foo'. Properties: PropUnassigned
Foo foo = {|#0:new()
{
PropInt = 1,
}|#0};
}
private class Foo
{
public int PropInt { get; set; }
public string PropString { get; set; }
}
}
";
var expected = VerifyCS.Diagnostic("AssignAll").WithLocation(0).WithArguments("Foo", "PropString");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
}
}
19 changes: 9 additions & 10 deletions AssignAll/AssignAll/AssignAllMembers/ObjectInitializerAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
Expand Down Expand Up @@ -26,13 +25,15 @@ internal void AnalyzeObjectInitializers(SyntaxNodeAnalysisContext ctx)
// TODO Support other means to enable, such as static configuration (analyze all/none by default), attributes on types and members
if (!_regionsToAnalyze.TextSpans.Any(enabledTextSpan => enabledTextSpan.Contains(objectInitializer.SpanStart))) return;

// Only handle initializers immediately following object creation.
// Not sure what any other scenario would be since we are only registered for object initializers,
// not things like list/collection initializers.
if (!(objectInitializer.Parent is BaseObjectCreationExpressionSyntax objectCreation))
// Only handle initializers immediately following object creation,
// not sure what the scenario would be since we are only registered for
// object initializers, not things like list/collection initializers.
if (!(objectInitializer.Parent is ObjectCreationExpressionSyntax objectCreation))
return;

if (!(ctx.SemanticModel.GetTypeInfo(objectCreation).Type is INamedTypeSymbol objectCreationNamedType))
var objectCreationNamedType =
(INamedTypeSymbol) ctx.SemanticModel.GetSymbolInfo(objectCreation.Type).Symbol;
if (objectCreationNamedType == null)
return;

IEnumerable<ISymbol> membersEnumerable = objectCreationNamedType.GetMembers();
Expand Down Expand Up @@ -113,18 +114,16 @@ internal void AnalyzeObjectInitializers(SyntaxNodeAnalysisContext ctx)
// {
// }

private static ImmutableArray<string> GetIgnoredPropertyNames(BaseObjectCreationExpressionSyntax objectCreation)
private static ImmutableArray<string> GetIgnoredPropertyNames(ObjectCreationExpressionSyntax objectCreation)
{
ImmutableArray<string> propertiesByCommentedAssignment =
GetIgnoredPropertyNamesFromCommentedAssignments(objectCreation);
return propertiesByCommentedAssignment;
}

private static ImmutableArray<string> GetIgnoredPropertyNamesFromCommentedAssignments(
BaseObjectCreationExpressionSyntax objectCreation)
ObjectCreationExpressionSyntax objectCreation)
{
if (objectCreation.Initializer == null) throw new InvalidOperationException("No initializer.");

// Case 1: Commented member assignments before one or more actual member assignments
// return new Foo {
// // Prop1 = null,
Expand Down
24 changes: 0 additions & 24 deletions Samples.ConsoleNet6/Example005_TargetTypedNew.cs

This file was deleted.

0 comments on commit ef2e452

Please sign in to comment.