Skip to content
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
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