Skip to content

Commit 288f21e

Browse files
NikolayPianikovNikolayPianikov
authored andcommitted
Add null check for add
1 parent de2fc26 commit 288f21e

File tree

11 files changed

+123
-19
lines changed

11 files changed

+123
-19
lines changed

Immutype.Tests/Integration/Tests.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,28 @@ public record Rec(int[] vals);
289289
output.ShouldBe(new [] { "33,99,44" }, generatedCode);
290290
}
291291

292+
[Fact]
293+
public void ShouldCreateRecordAddSingleArrayDefault()
294+
{
295+
// Given
296+
const string statements = "System.Console.WriteLine(string.Join(',', new Rec().AddVals(99, 44).vals));";
297+
298+
// When
299+
var output = @"
300+
namespace Sample
301+
{
302+
using System;
303+
using System.Collections.Generic;
304+
using Immutype;
305+
306+
[Target]
307+
public record Rec(int[] vals = default);
308+
}".Run(out var generatedCode, new RunOptions { Statements = statements });
309+
310+
// Then
311+
output.ShouldBe(new [] { "99,44" }, generatedCode);
312+
}
313+
292314
[Fact]
293315
public void ShouldCreateStructWithSingleValue()
294316
{
@@ -431,6 +453,28 @@ public record Rec(ImmutableArray<int> vals);
431453
// Then
432454
output.ShouldBe(new [] { "22,55,99,44" }, generatedCode);
433455
}
456+
457+
[Fact]
458+
public void ShouldSupportImmutableArrayWhenDefault()
459+
{
460+
// Given
461+
const string statements = "System.Console.WriteLine(string.Join(',', new Rec().AddVals(22, 55).AddVals(99,44).vals));";
462+
463+
// When
464+
var output = @"
465+
namespace Sample
466+
{
467+
using System;
468+
using System.Collections.Immutable;
469+
using Immutype;
470+
471+
[Target]
472+
public record Rec(ImmutableArray<int> vals = default);
473+
}".Run(out var generatedCode, new RunOptions { Statements = statements });
474+
475+
// Then
476+
output.ShouldBe(new [] { "22,55,99,44" }, generatedCode);
477+
}
434478

435479
[Fact]
436480
public void ShouldSupportImmutableQueue()
@@ -454,6 +498,29 @@ public record Rec(ImmutableQueue<int> vals);
454498
output.ShouldBe(new [] { "22,55,99,44" }, generatedCode);
455499
}
456500

501+
[Fact]
502+
public void ShouldSupportImmutableQueueWhenDefault()
503+
{
504+
// Given
505+
const string statements = "System.Console.WriteLine(string.Join(',', new Rec().WithVals(22, 55).AddVals(99,44).vals));";
506+
507+
// When
508+
var output = @"
509+
namespace Sample
510+
{
511+
using System;
512+
using System.Collections.Immutable;
513+
using Immutype;
514+
515+
[Target]
516+
public record Rec(ImmutableQueue<int> vals = default);
517+
}".Run(out var generatedCode, new RunOptions { Statements = statements });
518+
519+
// Then
520+
// Then
521+
output.ShouldBe(new [] { "22,55,99,44" }, generatedCode);
522+
}
523+
457524
[Fact]
458525
public void ShouldSupportImmutableStack()
459526
{
@@ -606,6 +673,28 @@ public record Rec(HashSet<int> vals);
606673
output.ShouldBe(new [] { "55,99,44" }, generatedCode);
607674
}
608675

676+
[Fact]
677+
public void ShouldSupportHashSetWithDefault()
678+
{
679+
// Given
680+
const string statements = "System.Console.WriteLine(string.Join(',', new Rec().AddVals(55).AddVals(99, 55, 44).vals));";
681+
682+
// When
683+
var output = @"
684+
namespace Sample
685+
{
686+
using System;
687+
using System.Collections.Generic;
688+
using Immutype;
689+
690+
[Target]
691+
public record Rec(HashSet<int> vals = default);
692+
}".Run(out var generatedCode, new RunOptions { Statements = statements });
693+
694+
// Then
695+
output.ShouldBe(new [] { "55,99,44" }, generatedCode);
696+
}
697+
609698
[Fact]
610699
public void ShouldSupportISet()
611700
{

Immutype.UsageScenarios.Tests/README_TEMPLATE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class SampleScenario
2727
public void Run()
2828
{
2929
var john = new Person("John", false, 15)
30-
.WithFriends(
30+
.AddFriends(
3131
new Person("David").WithAge(16),
3232
new Person("James").WithAge(17)
3333
.WithFriends(new Person("Tyler").WithAge(16)));
@@ -215,7 +215,7 @@ public class Set
215215
public void Run()
216216
{
217217
var john = new Person("John",15)
218-
.WithFriends(
218+
.AddFriends(
219219
new Person("David").WithAge(16),
220220
new Person("David").WithAge(16),
221221
new Person("James").WithAge(17)

Immutype.UsageScenarios.Tests/SampleScenario.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class SampleScenario
2929
public void Run()
3030
{
3131
var john = new Person("John", false, 15)
32-
.WithFriends(
32+
.AddFriends(
3333
new Person("David").WithAge(16),
3434
new Person("James").WithAge(17)
3535
.WithFriends(new Person("Tyler").WithAge(16)));

Immutype.UsageScenarios.Tests/Set.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class Set
2828
public void Run()
2929
{
3030
var john = new Person("John",15)
31-
.WithFriends(
31+
.AddFriends(
3232
new Person("David").WithAge(16),
3333
new Person("David").WithAge(16),
3434
new Person("James").WithAge(17)

Immutype/Core/DataContainerFactory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Immutype.Core
44
{
55
using System.Collections.Generic;
6-
using System.Collections.Immutable;
76
using Microsoft.CodeAnalysis.CSharp;
87
using Microsoft.CodeAnalysis.CSharp.Syntax;
98

Immutype/Core/MethodAddRemoveFactory.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public IEnumerable<MethodDeclarationSyntax> Create(TypeDeclarationSyntax targetD
6464

6565
private IEnumerable<ArgumentSyntax> CreateArguments(string enumerableMethod, TypeDeclarationSyntax owner, ParameterSyntax thisParameter, IEnumerable<ParameterSyntax> parameters, ParameterSyntax currentParameter, ParameterSyntax arrayParameter)
6666
{
67-
foreach (ParameterSyntax parameter in parameters)
67+
foreach (var parameter in parameters)
6868
{
6969
if (parameter == currentParameter)
7070
{
@@ -87,6 +87,12 @@ private ExpressionSyntax CreateExpression(string enumerableMethod, ExpressionSyn
8787
ExpressionSyntax? result = default;
8888
if (thisExpression != default)
8989
{
90+
var defaultExpression = CreateExpression(enumerableMethod, default, currentParameterType, arrayParameter);
91+
thisExpression = SyntaxFactory.ParenthesizedExpression(SyntaxFactory.ConditionalExpression(
92+
SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression, thisExpression, SyntaxFactory.DefaultExpression(currentParameterType!)),
93+
defaultExpression,
94+
thisExpression));
95+
9096
result = SyntaxFactory.InvocationExpression(
9197
SyntaxFactory.MemberAccessExpression(
9298
SyntaxKind.SimpleMemberAccessExpression,
@@ -99,7 +105,16 @@ private ExpressionSyntax CreateExpression(string enumerableMethod, ExpressionSyn
99105
{
100106
case NullableTypeSyntax nullableTypeSyntax:
101107
var defaultExpression = CreateExpression(enumerableMethod, default, nullableTypeSyntax.ElementType, arrayParameter);
102-
thisExpression = SyntaxFactory.ParenthesizedExpression(SyntaxFactory.BinaryExpression(SyntaxKind.CoalesceExpression, thisExpression!, defaultExpression));
108+
if (thisExpression != default)
109+
{
110+
thisExpression = SyntaxFactory.ParenthesizedExpression(SyntaxFactory.BinaryExpression(SyntaxKind.CoalesceExpression, thisExpression!, defaultExpression));
111+
}
112+
else
113+
{
114+
thisExpression = SyntaxFactory.DefaultExpression(nullableTypeSyntax.ElementType);
115+
}
116+
117+
// ReSharper disable once TailRecursiveCall
103118
return CreateExpression(enumerableMethod, thisExpression, nullableTypeSyntax.ElementType, arrayParameter);
104119

105120
case GenericNameSyntax genericNameSyntax:
@@ -122,8 +137,11 @@ private ExpressionSyntax CreateExpression(string enumerableMethod, ExpressionSyn
122137
else
123138
{
124139
return SyntaxFactory.InvocationExpression(
125-
SyntaxFactory.GenericName(nameof(Enumerable.Empty))
126-
.AddTypeArgumentListArguments(arrayTypeSyntax.ElementType));
140+
SyntaxFactory.MemberAccessExpression(
141+
SyntaxKind.SimpleMemberAccessExpression,
142+
SyntaxFactory.IdentifierName("System.Array"),
143+
SyntaxFactory.GenericName(nameof(Enumerable.Empty))
144+
.AddTypeArgumentListArguments(arrayTypeSyntax.ElementType)));
127145
}
128146
}
129147

Immutype/Core/MethodWithFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public IEnumerable<MethodDeclarationSyntax> Create(TypeDeclarationSyntax targetD
3030
var argumentParameter = currentParameter.WithDefault(default);
3131
var newArgumentParameter = argumentParameter;
3232
var arguments = new List<ArgumentSyntax>();
33-
foreach (ParameterSyntax parameter in curParameters)
33+
foreach (var parameter in curParameters)
3434
{
3535
if (parameter == currentParameter)
3636
{
@@ -101,7 +101,7 @@ private ExpressionSyntax CreateWithExpression(TypeSyntax? currentParameterType,
101101
break;
102102
}
103103

104-
return result!;
104+
return result;
105105
}
106106
}
107107
}

Immutype/Core/SyntaxNodeFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public IEnumerable<StatementSyntax> CreateGuards(ParameterSyntax parameter)
8282
SyntaxFactory.ThrowStatement(
8383
SyntaxFactory.ObjectCreationExpression(
8484
SyntaxFactory.IdentifierName(
85-
SyntaxFactory.Identifier($"System.ArgumentNullException"))).
85+
SyntaxFactory.Identifier("System.ArgumentNullException"))).
8686
AddArgumentListArguments(
8787
SyntaxFactory.Argument(
8888
SyntaxFactory.LiteralExpression(

Immutype/Core/TypeSyntaxFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public bool IsAccepted(TypeDeclarationSyntax typeDeclarationSyntax)
3535

3636
return typeDeclarationSyntax switch
3737
{
38-
RecordDeclarationSyntax recordDeclarationSyntax => recordDeclarationSyntax.ParameterList is { Parameters: { Count: > 0 } },
38+
RecordDeclarationSyntax recordDeclarationSyntax => recordDeclarationSyntax.ParameterList is { Parameters.Count: > 0 },
3939
_ => typeDeclarationSyntax.Members.OfType<ConstructorDeclarationSyntax>().Any(i => i.ParameterList.Parameters.Count > 0)
4040
};
4141
}

Immutype/SourceGenerator4.0.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#if ROSLYN40
22
namespace Immutype
33
{
4-
using Immutype;
5-
using Immutype.Core;
64
using Microsoft.CodeAnalysis;
75
using Microsoft.CodeAnalysis.CSharp.Syntax;
86

@@ -22,8 +20,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2220
var sourceBuilder = Composer.ResolveISourceBuilder();
2321
var typeSyntaxFilter = Composer.ResolveITypeSyntaxFilter();
2422
var changes = context.SyntaxProvider.CreateSyntaxProvider(
25-
(node, token) => node is TypeDeclarationSyntax typeDeclarationSyntax && typeSyntaxFilter.IsAccepted(typeDeclarationSyntax),
26-
(syntaxContext, token) => (TypeDeclarationSyntax)syntaxContext.Node)
23+
(node, _) => node is TypeDeclarationSyntax typeDeclarationSyntax && typeSyntaxFilter.IsAccepted(typeDeclarationSyntax),
24+
(syntaxContext, _) => (TypeDeclarationSyntax)syntaxContext.Node)
2725
.Collect();
2826

2927
context.RegisterSourceOutput(changes, (ctx, syntax) =>

0 commit comments

Comments
 (0)