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

Issue 31 no new from functions or methods #32

Merged
merged 2 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 30 additions & 0 deletions src/Vogen/CreationUsingImplicitNewAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
};
}

ancestor = syntax.Ancestors(false)
.FirstOrDefault(a => a.IsKind(SyntaxKind.MethodDeclaration));

if (ancestor is MethodDeclarationSyntax methodSyntax)
{
TypeSyntax t = methodSyntax.ReturnType;
INamedTypeSymbol? voClass = VoFilter.TryGetValueObjectClass(ctx, t);

return voClass == null ? null : new FoundItem
{
VoClass = voClass,
Location = t.GetLocation()
};
}

ancestor = syntax.Ancestors(false)
.FirstOrDefault(a => a.IsKind(SyntaxKind.LocalFunctionStatement));

if (ancestor is LocalFunctionStatementSyntax localFunctionStatementSyntax)
{
TypeSyntax t = localFunctionStatementSyntax.ReturnType;
INamedTypeSymbol? voClass = VoFilter.TryGetValueObjectClass(ctx, t);

return voClass == null ? null : new FoundItem
{
VoClass = voClass,
Location = t.GetLocation()
};
}

return null;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Testbench/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

using Vogen;

// var c =Foo.GetCustomer();
var c = GetCustomer();
CustomerId GetCustomer() => CustomerId.From(123);

Console.ReadLine();

[ValueObject(typeof(int))]
public partial struct CustomerId { }

class Foo {
// public static CustomerId GetCustomer() => default;
}



103 changes: 103 additions & 0 deletions tests/Vogen.IntegrationTests/SnapshotTests/DisallowNewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,108 @@ public partial struct CustomerId
diagnostic.ToString().Should().Be("(10,1): error VOG010: Type 'CustomerId' cannot be constructed with 'new' as it is prohibited.");
}

[Fact]
public void No_creation_using_implicit_new_from_method()
{
// The source code to test
var source = @"using Vogen;

var c =Foo.GetCustomer();

Console.ReadLine();

[ValueObject(typeof(int))]
public partial struct CustomerId { }

class Foo {
public static CustomerId GetCustomer() => new();
}

";

var (diagnostics, _) = TestHelper.GetGeneratedOutput<CreationUsingImplicitNewAnalyzer>(source);

diagnostics.Should().HaveCount(1);
Diagnostic diagnostic = diagnostics.Single();

diagnostic.Id.Should().Be("VOG010");
diagnostic.ToString().Should().Be("(11,19): error VOG010: Type 'CustomerId' cannot be constructed with 'new' as it is prohibited.");
}

[Fact]
public void Disallow_new_from_method()
{
// The source code to test
var source = @"using Vogen;

var c =Foo.GetCustomer();

Console.ReadLine();

[ValueObject(typeof(int))]
public partial struct CustomerId { }

class Foo {
public static CustomerId GetCustomer() => new CustomerId();
}

";

var (diagnostics, _) = TestHelper.GetGeneratedOutput<CreationUsingNewAnalyzer>(source);

diagnostics.Should().HaveCount(1);
Diagnostic diagnostic = diagnostics.Single();

diagnostic.Id.Should().Be("VOG010");
diagnostic.ToString().Should().Be("(11,51): error VOG010: Type 'CustomerId' cannot be constructed with 'new' as it is prohibited.");
}

[Fact]
public void Disallow_implicit_new_from_local_function()
{
// The source code to test
var source = @"using Vogen;

var c = GetCustomer();
CustomerId GetCustomer() => new();

Console.ReadLine();

[ValueObject(typeof(int))]
public partial struct CustomerId { }
";

var (diagnostics, _) = TestHelper.GetGeneratedOutput<CreationUsingImplicitNewAnalyzer>(source);

diagnostics.Should().HaveCount(1);
Diagnostic diagnostic = diagnostics.Single();

diagnostic.Id.Should().Be("VOG010");
diagnostic.ToString().Should().Be("(4,1): error VOG010: Type 'CustomerId' cannot be constructed with 'new' as it is prohibited.");
}

[Fact]
public void Disallow_new_from_local_function()
{
// The source code to test
var source = @"using Vogen;

var c = GetCustomer();
CustomerId GetCustomer() => new CustomerId();

Console.ReadLine();

[ValueObject(typeof(int))]
public partial struct CustomerId { }
";

var (diagnostics, _) = TestHelper.GetGeneratedOutput<CreationUsingNewAnalyzer>(source);

diagnostics.Should().HaveCount(1);
Diagnostic diagnostic = diagnostics.Single();

diagnostic.Id.Should().Be("VOG010");
diagnostic.ToString().Should().Be("(4,33): error VOG010: Type 'CustomerId' cannot be constructed with 'new' as it is prohibited.");
}

}