Skip to content

Commit

Permalink
fix(jsii): Defaulted parameters were not rendered as optional (#234)
Browse files Browse the repository at this point in the history
Parameters with a default value were not represented as optional in the assembly
documents due to an oversight when re-writing `jsii`. There was also no coverage
in the test corpus for this use-case, so I've added some.

Fixes #233
  • Loading branch information
RomainMuller authored Sep 17, 2018
1 parent b664805 commit 578bf9c
Show file tree
Hide file tree
Showing 12 changed files with 551 additions and 4 deletions.
18 changes: 17 additions & 1 deletion packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,22 @@ export class RuntimeTypeChecking {
public methodWithOptionalArguments(arg1: number, arg2: string, arg3?: Date) {
arg1; arg2; arg3;
}

public methodWithDefaultedArguments(arg1: number = 2, arg2: string, arg3: Date = new Date()) {
arg1; arg2; arg3;
}
}

export class OptionalConstructorArgument {
public constructor(public readonly arg1: number,
public readonly arg2: string,
public readonly arg3?: Date) {}
}

export class DefaultedConstructorArgument {
public constructor(public readonly arg1: number = 2,
public readonly arg2: string,
public readonly arg3: Date = new Date()) {}
}

export namespace DerivedClassHasNoProperties {
Expand Down Expand Up @@ -865,4 +881,4 @@ export class AbstractClassReturner {
abstractProperty: 'hello-abstract-property'
}
}
}
}
135 changes: 134 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,60 @@
}
]
},
"jsii-calc.DefaultedConstructorArgument": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.DefaultedConstructorArgument",
"initializer": {
"initializer": true,
"parameters": [
{
"name": "arg1",
"type": {
"optional": true,
"primitive": "number"
}
},
{
"name": "arg2",
"type": {
"primitive": "string"
}
},
{
"name": "arg3",
"type": {
"optional": true,
"primitive": "date"
}
}
]
},
"kind": "class",
"name": "DefaultedConstructorArgument",
"properties": [
{
"immutable": true,
"name": "arg1",
"type": {
"primitive": "number"
}
},
{
"immutable": true,
"name": "arg2",
"type": {
"primitive": "string"
}
},
{
"immutable": true,
"name": "arg3",
"type": {
"primitive": "date"
}
}
]
},
"jsii-calc.DerivedClassHasNoProperties.Base": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.DerivedClassHasNoProperties.Base",
Expand Down Expand Up @@ -2073,6 +2127,60 @@
],
"name": "ObjectRefsInCollections"
},
"jsii-calc.OptionalConstructorArgument": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.OptionalConstructorArgument",
"initializer": {
"initializer": true,
"parameters": [
{
"name": "arg1",
"type": {
"primitive": "number"
}
},
{
"name": "arg2",
"type": {
"primitive": "string"
}
},
{
"name": "arg3",
"type": {
"optional": true,
"primitive": "date"
}
}
]
},
"kind": "class",
"name": "OptionalConstructorArgument",
"properties": [
{
"immutable": true,
"name": "arg1",
"type": {
"primitive": "number"
}
},
{
"immutable": true,
"name": "arg2",
"type": {
"primitive": "string"
}
},
{
"immutable": true,
"name": "arg3",
"type": {
"optional": true,
"primitive": "date"
}
}
]
},
"jsii-calc.OverrideReturnsObject": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.OverrideReturnsObject",
Expand Down Expand Up @@ -2270,6 +2378,31 @@
},
"kind": "class",
"methods": [
{
"name": "methodWithDefaultedArguments",
"parameters": [
{
"name": "arg1",
"type": {
"optional": true,
"primitive": "number"
}
},
{
"name": "arg2",
"type": {
"primitive": "string"
}
},
{
"name": "arg3",
"type": {
"optional": true,
"primitive": "date"
}
}
]
},
{
"docs": {
"comment": "Used to verify verification of number of method arguments."
Expand Down Expand Up @@ -3070,5 +3203,5 @@
}
},
"version": "0.7.5",
"fingerprint": "XrmsNUcNdYiHEC6BRVT5XoeVmYQZzjYgiu6MyibgOwk="
"fingerprint": "PrLv57d+5ukv/bps1DvjB9DpM5DS6TpCEld13gQUTe8="
}
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,60 @@
}
]
},
"jsii-calc.DefaultedConstructorArgument": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.DefaultedConstructorArgument",
"initializer": {
"initializer": true,
"parameters": [
{
"name": "arg1",
"type": {
"optional": true,
"primitive": "number"
}
},
{
"name": "arg2",
"type": {
"primitive": "string"
}
},
{
"name": "arg3",
"type": {
"optional": true,
"primitive": "date"
}
}
]
},
"kind": "class",
"name": "DefaultedConstructorArgument",
"properties": [
{
"immutable": true,
"name": "arg1",
"type": {
"primitive": "number"
}
},
{
"immutable": true,
"name": "arg2",
"type": {
"primitive": "string"
}
},
{
"immutable": true,
"name": "arg3",
"type": {
"primitive": "date"
}
}
]
},
"jsii-calc.DerivedClassHasNoProperties.Base": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.DerivedClassHasNoProperties.Base",
Expand Down Expand Up @@ -2073,6 +2127,60 @@
],
"name": "ObjectRefsInCollections"
},
"jsii-calc.OptionalConstructorArgument": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.OptionalConstructorArgument",
"initializer": {
"initializer": true,
"parameters": [
{
"name": "arg1",
"type": {
"primitive": "number"
}
},
{
"name": "arg2",
"type": {
"primitive": "string"
}
},
{
"name": "arg3",
"type": {
"optional": true,
"primitive": "date"
}
}
]
},
"kind": "class",
"name": "OptionalConstructorArgument",
"properties": [
{
"immutable": true,
"name": "arg1",
"type": {
"primitive": "number"
}
},
{
"immutable": true,
"name": "arg2",
"type": {
"primitive": "string"
}
},
{
"immutable": true,
"name": "arg3",
"type": {
"optional": true,
"primitive": "date"
}
}
]
},
"jsii-calc.OverrideReturnsObject": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.OverrideReturnsObject",
Expand Down Expand Up @@ -2270,6 +2378,31 @@
},
"kind": "class",
"methods": [
{
"name": "methodWithDefaultedArguments",
"parameters": [
{
"name": "arg1",
"type": {
"optional": true,
"primitive": "number"
}
},
{
"name": "arg2",
"type": {
"primitive": "string"
}
},
{
"name": "arg3",
"type": {
"optional": true,
"primitive": "date"
}
}
]
},
{
"docs": {
"comment": "Used to verify verification of number of method arguments."
Expand Down Expand Up @@ -3070,5 +3203,5 @@
}
},
"version": "0.7.5",
"fingerprint": "XrmsNUcNdYiHEC6BRVT5XoeVmYQZzjYgiu6MyibgOwk="
"fingerprint": "PrLv57d+5ukv/bps1DvjB9DpM5DS6TpCEld13gQUTe8="
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Amazon.JSII.Runtime.Deputy;
using System;

namespace Amazon.JSII.Tests.CalculatorNamespace
{
[JsiiClass(typeof(DefaultedConstructorArgument), "jsii-calc.DefaultedConstructorArgument", "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"number\",\"optional\":true}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg3\",\"type\":{\"primitive\":\"date\",\"optional\":true}}]")]
public class DefaultedConstructorArgument : DeputyBase
{
public DefaultedConstructorArgument(double? arg1, string arg2, DateTime? arg3): base(new DeputyProps(new object[]{arg1, arg2, arg3}))
{
}

protected DefaultedConstructorArgument(ByRefValue reference): base(reference)
{
}

protected DefaultedConstructorArgument(DeputyProps props): base(props)
{
}

[JsiiProperty("arg1", "{\"primitive\":\"number\"}")]
public virtual double Arg1
{
get => GetInstanceProperty<double>();
}

[JsiiProperty("arg2", "{\"primitive\":\"string\"}")]
public virtual string Arg2
{
get => GetInstanceProperty<string>();
}

[JsiiProperty("arg3", "{\"primitive\":\"date\"}")]
public virtual DateTime Arg3
{
get => GetInstanceProperty<DateTime>();
}
}
}
Loading

0 comments on commit 578bf9c

Please sign in to comment.