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

Add support for public properties defined in a private constructor #256

Merged
merged 2 commits into from
Oct 10, 2018
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
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