Skip to content

Commit

Permalink
Correct handling of getter expressions, fix #52 (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
amis92 authored Mar 22, 2019
1 parent 8f50f54 commit ad11d61
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<LangVersion>7.3</LangVersion>
<CodeGenerationRoslynPackagesVersion>0.4.88</CodeGenerationRoslynPackagesVersion>
<Version>0.4.0</Version>
<PackageVersion>0.4.0-manual</PackageVersion>
<Version>0.4.1</Version>
<PackageVersion>0.4.1-manual</PackageVersion>
<AssemblyVersion></AssemblyVersion>
<FileVersion></FileVersion>
<InformationalVersion></InformationalVersion>
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Build in Release, publish to Nuget

image: Visual Studio 2017
version: 0.4.0-{branch}-build{build}
version: 0.4.1-{branch}-build{build}
skip_tags: true
clone_depth: 1
configuration:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<PropertyGroup>
<Description>Contains code generators for [Record]-marked types. Creates backing code (constructor, With- mutators, deconstructor to tuple, Builder nested type etc.) for immutable record classes. See project site for installation instructions and more details.</Description>
<PackageReleaseNotes>Updated reference of CodeGeneration.Roslyn to v0.4.88.</PackageReleaseNotes>
<PackageReleaseNotes>Fixed handling of get-only expression body properties (e.g. `int Count { get => 0; }`).</PackageReleaseNotes>
<PackageTags>record immutable attribute generator generators generation amadevus recordgenerator code codegen codegenerator codegeneration</PackageTags>
<NoPackageAnalysis>true</NoPackageAnalysis>
</PropertyGroup>
Expand Down
12 changes: 7 additions & 5 deletions src/Amadevus.RecordGenerator.Generators/SyntaxExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Generic;
using System.Linq;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using System.Collections.Generic;

namespace Amadevus.RecordGenerator.Generators
{
Expand Down Expand Up @@ -64,7 +64,7 @@ public static MethodDeclarationSyntax WithParameters(this MethodDeclarationSynta
return syntax.WithParameterList(ParameterList(SeparatedList(parameters)));
}

public static MethodDeclarationSyntax WithParameters(this MethodDeclarationSyntax syntax, params ParameterSyntax[] parameters)
public static MethodDeclarationSyntax WithParameters(this MethodDeclarationSyntax syntax, params ParameterSyntax[] parameters)
{
return syntax.WithParameterList(ParameterList(SeparatedList(parameters)));
}
Expand Down Expand Up @@ -106,14 +106,16 @@ public static bool IsNamed(this AttributeSyntax attribute, string name)

public static bool HasOnlyGetterWithNoBody(this PropertyDeclarationSyntax pdSyntax)
{
return pdSyntax.AccessorList is AccessorListSyntax accList
return pdSyntax.AccessorList is AccessorListSyntax accList
? accList.Accessors.Count == 1 && accList.Accessors.Single().IsGetterWithNoBody()
: false;
}

public static bool IsGetterWithNoBody(this AccessorDeclarationSyntax accessor)
{
return accessor.Kind() == SyntaxKind.GetAccessorDeclaration && accessor.Body == null;
return accessor.Kind() == SyntaxKind.GetAccessorDeclaration
&& accessor.Body is null
&& accessor.ExpressionBody is null;
}

public static bool IsPublic(this PropertyDeclarationSyntax pdSyntax)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<iconUrl>$iconUrl$</iconUrl>
<requireLicenseAcceptance>$requireLicenseAcceptance$</requireLicenseAcceptance>
<description>Code generator for [Record]-marked types. Automatically creates backing code (constructor, With- mutators, deconstructor to tuple, Builder nested type etc.) for immutable record classes. This package references all required and recommended (analyzers) packages. See project site for installation instructions and more details.</description>
<releaseNotes>Remove DevelopmentDependency flag, drop target framework. Essentially an aggregate of dependency packages for simple installation. Updated reference of CodeGeneration.Roslyn to v0.4.88.</releaseNotes>
<releaseNotes>Fixed handling of get-only expression body properties (e.g. `int Count { get => 0; }`).</releaseNotes>
<copyright>$copyright$</copyright>
<tags>record immutable attribute generator generation amadevus recordgenerator code codegen codegenerator codegeneration</tags>
<dependencies>
Expand Down
16 changes: 15 additions & 1 deletion test/Amadevus.RecordGenerator.TestsBase/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@
public partial class Item
{
public string Id { get; }

public string Name { get; }

// below properties should not be included in record's Descriptor

public string CalculatedDirectExpressionBody => Id + Name;

public string CalculatedAccessorExpressionBody
{
get => Id + Name;
}

public string CalculatedAccessorBlockBody
{
get { return Id + Name; }
}
}
}

0 comments on commit ad11d61

Please sign in to comment.