From 8208b67dba0439bdeea3b2c16207e3ffb8dd62f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9F=91=A8=F0=9F=8F=BC=E2=80=8D=F0=9F=92=BB=20Romain=20M?= =?UTF-8?q?arcadier-Muller?= Date: Mon, 17 Sep 2018 16:47:04 +0200 Subject: [PATCH] fix(jsii): Defaulted parameters were not rendered as optional 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 --- packages/jsii-calc/lib/compliance.ts | 6 ++++- packages/jsii-calc/test/assembly.jsii | 27 ++++++++++++++++++- .../.jsii | 27 ++++++++++++++++++- .../RuntimeTypeChecking.cs | 6 +++++ .../tests/calculator/RuntimeTypeChecking.java | 8 ++++++ .../expected.jsii-calc/sphinx/jsii-calc.rst | 10 +++++++ packages/jsii/lib/assembler.ts | 6 ++++- 7 files changed, 86 insertions(+), 4 deletions(-) diff --git a/packages/jsii-calc/lib/compliance.ts b/packages/jsii-calc/lib/compliance.ts index 4d62821557..bd508db548 100644 --- a/packages/jsii-calc/lib/compliance.ts +++ b/packages/jsii-calc/lib/compliance.ts @@ -240,6 +240,10 @@ 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 namespace DerivedClassHasNoProperties { @@ -865,4 +869,4 @@ export class AbstractClassReturner { abstractProperty: 'hello-abstract-property' } } -} \ No newline at end of file +} diff --git a/packages/jsii-calc/test/assembly.jsii b/packages/jsii-calc/test/assembly.jsii index fd17e047fa..20fc3552c7 100644 --- a/packages/jsii-calc/test/assembly.jsii +++ b/packages/jsii-calc/test/assembly.jsii @@ -2270,6 +2270,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." @@ -3070,5 +3095,5 @@ } }, "version": "0.7.5", - "fingerprint": "XrmsNUcNdYiHEC6BRVT5XoeVmYQZzjYgiu6MyibgOwk=" + "fingerprint": "Ywp3EW+cv1wVMiKbYcuDGIc5pIYI3RQtcVzwVBMg0aM=" } diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/.jsii b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/.jsii index fd17e047fa..20fc3552c7 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/.jsii +++ b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/.jsii @@ -2270,6 +2270,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." @@ -3070,5 +3095,5 @@ } }, "version": "0.7.5", - "fingerprint": "XrmsNUcNdYiHEC6BRVT5XoeVmYQZzjYgiu6MyibgOwk=" + "fingerprint": "Ywp3EW+cv1wVMiKbYcuDGIc5pIYI3RQtcVzwVBMg0aM=" } diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/RuntimeTypeChecking.cs b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/RuntimeTypeChecking.cs index c3da746ef9..4f6a82c414 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/RuntimeTypeChecking.cs +++ b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon/JSII/Tests/CalculatorNamespace/RuntimeTypeChecking.cs @@ -18,6 +18,12 @@ protected RuntimeTypeChecking(DeputyProps props): base(props) { } + [JsiiMethod("methodWithDefaultedArguments", null, "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"number\",\"optional\":true}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg3\",\"type\":{\"primitive\":\"date\",\"optional\":true}}]")] + public virtual void MethodWithDefaultedArguments(double? arg1, string arg2, DateTime? arg3) + { + InvokeInstanceVoidMethod(new object[]{arg1, arg2, arg3}); + } + /// Used to verify verification of number of method arguments. [JsiiMethod("methodWithOptionalArguments", null, "[{\"name\":\"arg1\",\"type\":{\"primitive\":\"number\"}},{\"name\":\"arg2\",\"type\":{\"primitive\":\"string\"}},{\"name\":\"arg3\",\"type\":{\"primitive\":\"date\",\"optional\":true}}]")] public virtual void MethodWithOptionalArguments(double arg1, string arg2, DateTime? arg3) diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/RuntimeTypeChecking.java b/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/RuntimeTypeChecking.java index 55c7e39c86..b48aeca4a9 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/RuntimeTypeChecking.java +++ b/packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/RuntimeTypeChecking.java @@ -11,6 +11,14 @@ public RuntimeTypeChecking() { software.amazon.jsii.JsiiEngine.getInstance().createNewObject(this); } + public void methodWithDefaultedArguments(@javax.annotation.Nullable final java.lang.Number arg1, final java.lang.String arg2, @javax.annotation.Nullable final java.time.Instant arg3) { + this.jsiiCall("methodWithDefaultedArguments", Void.class, java.util.stream.Stream.concat(java.util.stream.Stream.concat(java.util.stream.Stream.of(arg1), java.util.stream.Stream.of(java.util.Objects.requireNonNull(arg2, "arg2 is required"))), java.util.stream.Stream.of(arg3)).toArray()); + } + + public void methodWithDefaultedArguments(@javax.annotation.Nullable final java.lang.Number arg1, final java.lang.String arg2) { + this.jsiiCall("methodWithDefaultedArguments", Void.class, java.util.stream.Stream.concat(java.util.stream.Stream.of(arg1), java.util.stream.Stream.of(java.util.Objects.requireNonNull(arg2, "arg2 is required"))).toArray()); + } + /** * Used to verify verification of number of method arguments. */ diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/sphinx/jsii-calc.rst b/packages/jsii-pacmak/test/expected.jsii-calc/sphinx/jsii-calc.rst index 291eaeebfa..86ce9856b2 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/sphinx/jsii-calc.rst +++ b/packages/jsii-pacmak/test/expected.jsii-calc/sphinx/jsii-calc.rst @@ -2417,6 +2417,16 @@ RuntimeTypeChecking + .. py:method:: methodWithDefaultedArguments([arg1, arg2, [arg3]]) + + :param arg1: + :type arg1: number or undefined + :param arg2: + :type arg2: string + :param arg3: + :type arg3: date or undefined + + .. py:method:: methodWithOptionalArguments(arg1, arg2, [arg3]) Used to verify verification of number of method arguments. diff --git a/packages/jsii/lib/assembler.ts b/packages/jsii/lib/assembler.ts index e130fe0db6..1511018f80 100644 --- a/packages/jsii/lib/assembler.ts +++ b/packages/jsii/lib/assembler.ts @@ -618,14 +618,18 @@ export class Assembler implements Emitter { if (LOG.isTraceEnabled()) { LOG.trace(`Processing parameter: ${colors.cyan(paramSymbol.name)}`); } + const paramDeclaration = paramSymbol.valueDeclaration as ts.ParameterDeclaration; const parameter: spec.Parameter = { name: paramSymbol.name, type: await this._typeReference(this._typeChecker.getTypeAtLocation(paramSymbol.valueDeclaration), paramSymbol.valueDeclaration), - variadic: !!(paramSymbol.valueDeclaration as ts.ParameterDeclaration).dotDotDotToken + variadic: !!paramDeclaration.dotDotDotToken }; if (parameter.variadic) { parameter.type = (parameter.type as spec.CollectionTypeReference).collection.elementtype; } + if (paramDeclaration.initializer != null) { + parameter.type.optional = true; + } this._visitDocumentation(paramSymbol, parameter); return parameter; }