Skip to content

Commit

Permalink
fix(jsii): support public autoproperties in private constructor (#256)
Browse files Browse the repository at this point in the history
Includes a fix in the .NET generator to add support for private constructors.
  • Loading branch information
rix0rrr authored Oct 10, 2018
1 parent 660ae79 commit 181012e
Show file tree
Hide file tree
Showing 13 changed files with 381 additions and 122 deletions.
8 changes: 8 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,3 +914,11 @@ export class DoNotOverridePrivates {
this.privateProperty = newValue;
}
}

/**
* Class that implements interface properties automatically, but using a private constructor
*/
export class ClassWithPrivateConstructorAndAutomaticProperties implements IInterfaceWithProperties {
private constructor(public readonly readOnlyString: string, public readWriteString: string) {
}
}
37 changes: 36 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,41 @@
}
]
},
"jsii-calc.ClassWithPrivateConstructorAndAutomaticProperties": {
"assembly": "jsii-calc",
"docs": {
"comment": "Class that implements interface properties automatically, but using a private constructor"
},
"fqn": "jsii-calc.ClassWithPrivateConstructorAndAutomaticProperties",
"interfaces": [
{
"fqn": "jsii-calc.IInterfaceWithProperties"
}
],
"kind": "class",
"name": "ClassWithPrivateConstructorAndAutomaticProperties",
"properties": [
{
"immutable": true,
"name": "readOnlyString",
"overrides": {
"fqn": "jsii-calc.IInterfaceWithProperties"
},
"type": {
"primitive": "string"
}
},
{
"name": "readWriteString",
"overrides": {
"fqn": "jsii-calc.IInterfaceWithProperties"
},
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.DefaultedConstructorArgument": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.DefaultedConstructorArgument",
Expand Down Expand Up @@ -3297,5 +3332,5 @@
}
},
"version": "0.7.6",
"fingerprint": "IrPnQp841TiCOiG/Z2z18s0K8pxTwuMglW1UJ2t1zsM="
"fingerprint": "eFasWxN7YC37iWdz+dDbEFTSQCzyangrqP5Nu02rzpw="
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,36 @@ protected MyClass(DeputyProps props): base(props)
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}

[Fact(DisplayName = Prefix + nameof(AllowsPrivateConstructor))]
public void AllowsPrivateConstructor()
{
ClassType classType = new ClassType
(
fullyQualifiedName: "myFqn",
assembly: "myPackage",
name: "myClass",
isAbstract: false
);

string actual = Render(classType);
string expected =
@"namespace MyNamespace
{
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
public class MyClass : DeputyBase
{
protected MyClass(ByRefValue reference): base(reference)
{
}
protected MyClass(DeputyProps props): base(props)
{
}
}
}";
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}

[Fact(DisplayName = Prefix + nameof(IncludesAbstractKeyword))]
public void IncludesAbstractKeyword()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Amazon.JSII.JsonModel.Spec;
using System.Collections.Generic;
using System.Linq;
using Amazon.JSII.JsonModel.Spec;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Generic;
using System.Linq;
using SF = Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace Amazon.JSII.Generator.Class
Expand Down Expand Up @@ -97,37 +97,41 @@ IEnumerable<MemberDeclarationSyntax> CreateConstructors()
{
SyntaxToken typeName = Symbols.GetNameSyntaxToken(Type);

yield return SF.ConstructorDeclaration
(
SF.List<AttributeListSyntax>(),
SF.TokenList(SF.Token(
Type.IsAbstract || Type.Initializer.IsProtected ?
SyntaxKind.ProtectedKeyword :
SyntaxKind.PublicKeyword
)),
typeName,
Type.Initializer.GetParameterListSyntax(Namespaces, Symbols),
SF.ConstructorInitializer
if (Type.Initializer != null)
{
yield return SF.ConstructorDeclaration
(
SyntaxKind.BaseConstructorInitializer,
SF.ArgumentList(
SF.SeparatedList(new[] {
SF.Argument(
SF.ObjectCreationExpression(
SF.Token(SyntaxKind.NewKeyword),
SF.ParseTypeName("DeputyProps"),
SF.ArgumentList(SF.SeparatedList(
new[] { GetBaseArgument() }
)),
null
SF.List<AttributeListSyntax>(),
SF.TokenList(SF.Token(
Type.IsAbstract || Type.Initializer.IsProtected
? SyntaxKind.ProtectedKeyword
: SyntaxKind.PublicKeyword
)),
typeName,
Type.Initializer.GetParameterListSyntax(Namespaces, Symbols),
SF.ConstructorInitializer
(
SyntaxKind.BaseConstructorInitializer,
SF.ArgumentList(
SF.SeparatedList(new[]
{
SF.Argument(
SF.ObjectCreationExpression(
SF.Token(SyntaxKind.NewKeyword),
SF.ParseTypeName("DeputyProps"),
SF.ArgumentList(SF.SeparatedList(
new[] {GetBaseArgument()}
)),
null
)
)
)
})
)
),
SF.Block(),
null
);
})
)
),
SF.Block(),
null
);
}

yield return SF.ConstructorDeclaration
(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Amazon.JSII.JsonModel.Spec;
using System;
using System.Collections.Generic;
using System.Linq;
using Amazon.JSII.JsonModel.Spec;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using SF = Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace Amazon.JSII.Generator
Expand Down Expand Up @@ -44,7 +44,7 @@ IEnumerable<ParameterSyntax> GetParameters()
public static SyntaxToken GetParametersJsonSyntaxToken(this Method method)
{
// Strip docs before serializing.
Parameter[] parameters = (method.Parameters ?? Enumerable.Empty<Parameter>())
Parameter[] parameters = (method?.Parameters ?? Enumerable.Empty<Parameter>())
.Select(p => new Parameter(p.Name, p.Type))
.ToArray();

Expand Down
Loading

0 comments on commit 181012e

Please sign in to comment.