Skip to content

Commit c61e2d3

Browse files
NikolayPianikovNikolayPianikov
authored andcommitted
Supporting block-free namespace
1 parent 975c0a6 commit c61e2d3

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
<!--<AnalyzerRoslynVersion Condition="'$(AnalyzerRoslynVersion)'==''">3.8</AnalyzerRoslynVersion>
1818
<AnalyzerRoslynPackageVersion Condition="'$(AnalyzerRoslynPackageVersion)'==''">3.8.0</AnalyzerRoslynPackageVersion>-->
1919

20-
<DefineConstants Condition="'$(AnalyzerRoslynVersion)'=='4.0'">ROSLYN40</DefineConstants>
21-
<DefineConstants Condition="'$(AnalyzerRoslynVersion)'=='3.8'">ROSLYN38</DefineConstants>
20+
<DefineConstants Condition="'$(AnalyzerRoslynVersion)'=='4.0'">$(DefineConstants);ROSLYN40</DefineConstants>
21+
<DefineConstants Condition="'$(AnalyzerRoslynVersion)'=='3.8'">$(DefineConstants);ROSLYN38</DefineConstants>
2222
</PropertyGroup>
2323

2424
<ItemGroup>

Immutype.Tests/Integration/Tests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,31 @@ namespace Sample
6262
{
6363
using System;
6464
65-
[Immutype.TargetAttribute()]
65+
[Immutype.Target]
6666
public record struct Rec(int val);
6767
}".Run(out var generatedCode, new RunOptions { Statements = statements });
6868

6969
// Then
7070
output.ShouldBe(new [] { "Rec { val = 99 }" }, generatedCode);
7171
}
72+
73+
[Fact]
74+
public void ShouldSupportBlockFreeNamespace()
75+
{
76+
// Given
77+
const string statements = "System.Console.WriteLine(new Rec(33).WithVal(99));";
78+
79+
// When
80+
var output = @"
81+
namespace Sample;
82+
using System;
83+
[Immutype.Target]
84+
public record struct Rec(int val);
85+
".Run(out var generatedCode, new RunOptions { Statements = statements });
86+
87+
// Then
88+
output.ShouldBe(new [] { "Rec { val = 99 }" }, generatedCode);
89+
}
7290
#endif
7391

7492
[Fact]

Immutype/Core/ExtensionsFactory.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ public ExtensionsFactory(IMethodsFactory methodsFactory) =>
2020
public IEnumerable<Source> Create(GenerationContext<TypeDeclarationSyntax> context, IReadOnlyList<ParameterSyntax> parameters)
2121
{
2222
var typeDeclarationSyntax = context.Syntax;
23-
var ns = typeDeclarationSyntax.Ancestors().OfType<NamespaceDeclarationSyntax>().Reverse().ToArray();
23+
var ns = typeDeclarationSyntax.Ancestors()
24+
#if ROSLYN38
25+
.OfType<NamespaceDeclarationSyntax>()
26+
#else
27+
.OfType<BaseNamespaceDeclarationSyntax>()
28+
#endif
29+
.Reverse()
30+
.ToArray();
31+
2432
var typeName =
2533
string.Join(".", ns.Select(i => i.Name.ToString())
2634
.Concat(new []{typeDeclarationSyntax.Identifier.Text + typeDeclarationSyntax.TypeParameterList}));

0 commit comments

Comments
 (0)