diff --git a/src/TypeShim.E2E/TypeShim.E2E.Wasm/Constructors.cs b/src/TypeShim.E2E/TypeShim.E2E.Wasm/Constructors.cs new file mode 100644 index 00000000..fd404056 --- /dev/null +++ b/src/TypeShim.E2E/TypeShim.E2E.Wasm/Constructors.cs @@ -0,0 +1,54 @@ +using System; + +namespace TypeShim.E2E.Wasm; + +[TSExport] +public class IntConstructor(int i) +{ + public int Value { get; } = i; +} + +[TSExport] +public class StringConstructor(string s) +{ + public string Value { get; } = s; +} + +[TSExport] +public class MultipleConstructor(int i, string s) +{ + public int IntValue { get; } = i; + public string StringValue { get; } = s; +} + +[TSExport] +public class ExportedClassConstructor(ExportedClass e) +{ + public ExportedClass Value { get; } = e; +} + +[TSExport] +public class ExportedClassMultipleConstructor(ExportedClass e, ExportedClass f) +{ + public ExportedClass Value { get; } = e; + public ExportedClass Value2 { get; } = f; +} + +[TSExport] +public class ExportedClassArrayConstructor(ExportedClass[] e) +{ + public ExportedClass[] Value { get; } = e; +} + +[TSExport] +public class ExportedClassActionConstructor(Action e) +{ + public Action Value { get; } = e; +} + +[TSExport] +public class IntStringMixedConstructor(int i) +{ + public int Value { get; } = i; + public required string StringValue { get; set; } +} \ No newline at end of file diff --git a/src/TypeShim.E2E/vitest/package-lock.json b/src/TypeShim.E2E/vitest/package-lock.json index 385e82c3..26340f74 100644 --- a/src/TypeShim.E2E/vitest/package-lock.json +++ b/src/TypeShim.E2E/vitest/package-lock.json @@ -201,6 +201,7 @@ } ], "license": "MIT", + "peer": true, "engines": { "node": ">=18" }, @@ -244,6 +245,7 @@ } ], "license": "MIT", + "peer": true, "engines": { "node": ">=18" } @@ -1158,6 +1160,7 @@ "integrity": "sha512-o4PXJQidqJl82ckFaXUeoAW+XysPLauYI43Abki5hABd853iMhitooc6znOnczgbTYmEP6U6/y1ZyKAIsvMKGg==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@babel/code-frame": "^7.10.4", "@babel/runtime": "^7.12.5", @@ -1206,6 +1209,7 @@ "integrity": "sha512-/rpCXHlCWeqClNBwUhDcusJxXYDjZTyE8v5oTO7WbL8eij2nKhUeU89/6xgjU7N4/Vh3He0BtyhJdQbDyhiXAw==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "undici-types": "~7.16.0" } @@ -1227,6 +1231,7 @@ "integrity": "sha512-AHDanTP4Ed6J5R6wRBcWRQ+AxgMnNJxsbaa229nFQz5KOMFZqlW11QkIDoLgCjBOpQ1+c78lTN5jVxO8ME+S4w==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@testing-library/dom": "^10.4.0", "@testing-library/user-event": "^14.5.2", @@ -1917,6 +1922,7 @@ "integrity": "sha512-mjzqwWRD9Y1J1KUi7W97Gja1bwOOM5Ug0EZ6UDK3xS7j7mndrkwozHtSblfomlzyB4NepioNt+B2sOSzczVgtQ==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@acemir/cssom": "^0.9.28", "@asamuzakjp/dom-selector": "^6.7.6", @@ -2609,6 +2615,7 @@ "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", "dev": true, "license": "Apache-2.0", + "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" @@ -2640,6 +2647,7 @@ "integrity": "sha512-o5a9xKjbtuhY6Bi5S3+HvbRERmouabWbyUcpXXUA1u+GNUKoROi9byOJ8M0nHbHYHkYICiMlqxkg1KkYmm25Sw==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "esbuild": "^0.21.3", "postcss": "^8.4.43", @@ -2723,6 +2731,7 @@ "integrity": "sha512-MSmPM9REYqDGBI8439mA4mWhV5sKmDlBKWIYbA3lRb2PTHACE0mgKwA8yQ2xq9vxDTuk4iPrECBAEW2aoFXY0Q==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@vitest/expect": "2.1.9", "@vitest/mocker": "2.1.9", diff --git a/src/TypeShim.E2E/vitest/src/constructors.test.ts b/src/TypeShim.E2E/vitest/src/constructors.test.ts new file mode 100644 index 00000000..c2ca7dba --- /dev/null +++ b/src/TypeShim.E2E/vitest/src/constructors.test.ts @@ -0,0 +1,122 @@ +import { describe, test, expect, beforeEach } from 'vitest'; +import { + IntConstructor, + StringConstructor, + MultipleConstructor, + ExportedClass, + ExportedClassConstructor, + ExportedClassMultipleConstructor, + ExportedClassArrayConstructor, + ExportedClassActionConstructor, + IntStringMixedConstructor +} from '@typeshim/e2e-wasm-lib'; + +describe('Constructors Test', () => { + test('IntConstructor with int parameter', async () => { + const instance = new IntConstructor(100); + expect(instance.Value).toBe(100); + }); + + test('IntConstructor with different values', async () => { + const instance1 = new IntConstructor(0); + expect(instance1.Value).toBe(0); + + const instance2 = new IntConstructor(-50); + expect(instance2.Value).toBe(-50); + + const instance3 = new IntConstructor(2147483647); + expect(instance3.Value).toBe(2147483647); + }); + + test('StringConstructor with string parameter', async () => { + const instance = new StringConstructor('hello'); + expect(instance.Value).toBe('hello'); + }); + + test('StringConstructor with different strings', async () => { + const instance1 = new StringConstructor(''); + expect(instance1.Value).toBe(''); + + const instance2 = new StringConstructor('test string'); + expect(instance2.Value).toBe('test string'); + + const instance3 = new StringConstructor('special chars: !@#$%'); + expect(instance3.Value).toBe('special chars: !@#$%'); + }); + + test('MultipleConstructor with int and string parameters', async () => { + const instance = new MultipleConstructor(42, 'test'); + expect(instance.IntValue).toBe(42); + expect(instance.StringValue).toBe('test'); + }); + + test('MultipleConstructor with various values', async () => { + const instance1 = new MultipleConstructor(0, ''); + expect(instance1.IntValue).toBe(0); + expect(instance1.StringValue).toBe(''); + + const instance2 = new MultipleConstructor(-100, 'negative'); + expect(instance2.IntValue).toBe(-100); + expect(instance2.StringValue).toBe('negative'); + }); + + test('ExportedClassConstructor with ExportedClass parameter', async () => { + const exported = new ExportedClass({ Id: 1 }); + const instance = new ExportedClassConstructor(exported); + expect(instance.Value).toBe(exported); + }); + + test('ExportedClassMultipleConstructor with multiple ExportedClass parameters', async () => { + const exported1 = new ExportedClass({ Id: 1 }); + const exported2 = new ExportedClass({ Id: 2 }); + const instance = new ExportedClassMultipleConstructor(exported1, exported2); + expect(instance.Value).toBe(exported1); + expect(instance.Value2).toBe(exported2); + }); + + test('ExportedClassArrayConstructor with ExportedClass array parameter', async () => { + const exported1 = new ExportedClass({ Id: 1 }); + const exported2 = new ExportedClass({ Id: 2 }); + const exported3 = new ExportedClass({ Id: 3 }); + const array = [exported1, exported2, exported3]; + const instance = new ExportedClassArrayConstructor(array); + expect(instance.Value).toStrictEqual(array); + expect(instance.Value.length).toBe(3); + }); + + test('ExportedClassArrayConstructor with empty array', async () => { + const array: ExportedClass[] = []; + const instance = new ExportedClassArrayConstructor(array); + expect(instance.Value).toStrictEqual(array); + expect(instance.Value.length).toBe(0); + }); + + test('ExportedClassActionConstructor with Action parameter', async () => { + let callCount = 0; + const action = (obj: ExportedClass) => { + callCount++; + }; + const instance = new ExportedClassActionConstructor(action); + instance.Value(new ExportedClass({ Id: 1 })) + expect(callCount).toBe(1); + instance.Value(new ExportedClass({ Id: 2 })) + expect(callCount).toBe(2); + }); + + test('ExportedClassActionConstructor action is callable', async () => { + let receivedValue: ExportedClass | null = null; + const action = (obj: ExportedClass) => { + receivedValue = obj; + }; + const instance = new ExportedClassActionConstructor(action); + const exported = new ExportedClass({ Id: 1 }); + instance.Value(exported); + expect(receivedValue).toBe(exported); + }); + + test('IntStringMixedConstructor with int and string parameters', () =>{ + const instance = new IntStringMixedConstructor(42, { StringValue: 'test' }); + expect(instance.Value).toBe(42); + expect(instance.StringValue).toBe('test'); + }) +}); \ No newline at end of file diff --git a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Constructors.cs b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Constructors.cs index 44679c3e..dd3a7d4e 100644 --- a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Constructors.cs +++ b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Constructors.cs @@ -161,12 +161,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsInt32Nullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsInt32Nullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -192,12 +192,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsInt32Nullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsInt32Nullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } } @@ -239,12 +239,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsInt32Nullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsInt32Nullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -263,12 +263,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsInt32Nullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsInt32Nullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } } @@ -310,12 +310,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] int p1, [JSMarshalAs] double p2, [JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] int p1, [JSMarshalAs] double p2, [JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1(p1, p2) { - P1 = jsObject.GetPropertyAsInt32Nullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsInt32Nullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -379,12 +379,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] int p1, [JSMarshalAs] double p2, [JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] int p1, [JSMarshalAs] double p2, [JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1(p1, p2) { - P1 = jsObject.GetPropertyAsInt32Nullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsInt32Nullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -479,6 +479,79 @@ public static C1 FromObject(object obj) """); } + + [Test] + public void CSharpInteropClass_ParameterizedConstructor_WithUserClassAction_ConvertsCorrectly() + { + SyntaxTree userClass = CSharpSyntaxTree.ParseText(""" + using System; + using System.Threading.Tasks; + namespace N1; + [TSExport] + public class MyClass + { + public void M1() + { + } + } + """); + + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(""" + using System; + using System.Threading.Tasks; + namespace N1; + [TSExport] + public class C1(Action p1) + { + public int P1 => 1; + } + """); + SymbolExtractor symbolExtractor = new([CSharpFileInfo.Create(syntaxTree), CSharpFileInfo.Create(userClass)], TestFixture.TargetingPackRefDir); + List exportedClasses = [.. symbolExtractor.ExtractAllExportedSymbols()]; + Assert.That(exportedClasses, Has.Count.EqualTo(2)); + INamedTypeSymbol classSymbol = exportedClasses.First(); + + InteropTypeInfoCache typeCache = new(); + ClassInfo classInfo = new ClassInfoBuilder(classSymbol, typeCache).Build(); + ClassInfo userClassInfo = new ClassInfoBuilder(exportedClasses.Last(), typeCache).Build(); + RenderContext renderContext = new(classInfo, [classInfo, userClassInfo], RenderOptions.CSharp); + string interopClass = new CSharpInteropClassRenderer(classInfo, renderContext, new JSObjectMethodResolver([])).Render(); + + AssertEx.EqualOrDiff(interopClass, """ + #nullable enable + // TypeShim generated TypeScript interop definitions + using System; + using System.Runtime.InteropServices.JavaScript; + using System.Threading.Tasks; + namespace N1; + public partial class C1Interop + { + [JSExport] + [return: JSMarshalAs] + public static object ctor([JSMarshalAs>] Action p1) + { + Action typed_p1 = (MyClass arg0) => p1(arg0); + return new C1(typed_p1); + } + [JSExport] + [return: JSMarshalAs] + public static int get_P1([JSMarshalAs] object instance) + { + C1 typed_instance = (C1)instance; + return typed_instance.P1; + } + public static C1 FromObject(object obj) + { + return obj switch + { + C1 instance => instance, + _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), + }; + } + } + + """); + } [Test] public void CSharpInteropClass_ParameterlessConstructor_WithUserClassPropertyThatIsNotInitializerCompatible_ConvertsCorrectly() @@ -527,12 +600,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = UserClassInterop.FromObject(jsObject.GetPropertyAsObjectNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))), + P1 = UserClassInterop.FromObject(initializer.GetPropertyAsObjectNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer))), }; } [JSExport] @@ -559,12 +632,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = UserClassInterop.FromObject(jsObject.GetPropertyAsObjectNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))), + P1 = UserClassInterop.FromObject(initializer.GetPropertyAsObjectNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer))), }; } } diff --git a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Delegates.cs b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Delegates.cs index 36cdfef0..f648a4a8 100644 --- a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Delegates.cs +++ b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Delegates.cs @@ -1026,10 +1026,10 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; - Func tmpP1 = jsObject.GetPropertyAsObjectObjectFunctionNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)); + using var _ = initializer; + Func tmpP1 = initializer.GetPropertyAsObjectObjectFunctionNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)); return new C1() { P1 = (MyClass arg0) => MyClassInterop.FromObject(tmpP1(arg0)), @@ -1060,10 +1060,10 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; - Func tmpP1 = jsObject.GetPropertyAsObjectObjectFunctionNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)); + using var _ = initializer; + Func tmpP1 = initializer.GetPropertyAsObjectObjectFunctionNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)); return new C1() { P1 = (MyClass arg0) => MyClassInterop.FromObject(tmpP1(arg0)), @@ -1108,12 +1108,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsVoidActionNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsVoidActionNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -1139,12 +1139,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsVoidActionNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsVoidActionNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } } @@ -1186,12 +1186,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsCharVoidActionNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsCharVoidActionNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -1217,12 +1217,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsCharVoidActionNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsCharVoidActionNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } } diff --git a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Properties.cs b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Properties.cs index c1648587..06b4453f 100644 --- a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Properties.cs +++ b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Properties.cs @@ -57,12 +57,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = MyClassInterop.FromObject(jsObject.GetPropertyAsObjectNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))), + P1 = MyClassInterop.FromObject(initializer.GetPropertyAsObjectNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer))), }; } [JSExport] @@ -89,12 +89,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = MyClassInterop.FromObject(jsObject.GetPropertyAsObjectNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))), + P1 = MyClassInterop.FromObject(initializer.GetPropertyAsObjectNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer))), }; } } @@ -214,12 +214,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsObjectNullable("P1") is { } P1Val ? MyClassInterop.FromObject(P1Val) : null, + P1 = initializer.GetPropertyAsObjectNullable("P1") is { } P1Val ? MyClassInterop.FromObject(P1Val) : null, }; } [JSExport] @@ -246,12 +246,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsObjectNullable("P1") is { } P1Val ? MyClassInterop.FromObject(P1Val) : null, + P1 = initializer.GetPropertyAsObjectNullable("P1") is { } P1Val ? MyClassInterop.FromObject(P1Val) : null, }; } } @@ -293,12 +293,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsObjectNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsObjectNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -324,12 +324,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsObjectNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsObjectNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } } @@ -371,12 +371,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsInt32ArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsInt32ArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -402,12 +402,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsInt32ArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsInt32ArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } } @@ -449,12 +449,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsInt32ArraySegmentNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsInt32ArraySegmentNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -480,12 +480,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsInt32ArraySegmentNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsInt32ArraySegmentNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } } @@ -541,12 +541,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = Array.ConvertAll(jsObject.GetPropertyAsObjectArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), e => MyClassInterop.FromObject(e)), + P1 = Array.ConvertAll(initializer.GetPropertyAsObjectArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), e => MyClassInterop.FromObject(e)), }; } [JSExport] @@ -573,12 +573,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = Array.ConvertAll(jsObject.GetPropertyAsObjectArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), e => MyClassInterop.FromObject(e)), + P1 = Array.ConvertAll(initializer.GetPropertyAsObjectArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), e => MyClassInterop.FromObject(e)), }; } } @@ -634,12 +634,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = Array.ConvertAll(jsObject.GetPropertyAsObjectNullableArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), e => e is { } eVal ? MyClassInterop.FromObject(eVal) : null), + P1 = Array.ConvertAll(initializer.GetPropertyAsObjectNullableArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), e => e is { } eVal ? MyClassInterop.FromObject(eVal) : null), }; } [JSExport] @@ -666,12 +666,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = Array.ConvertAll(jsObject.GetPropertyAsObjectNullableArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), e => e is { } eVal ? MyClassInterop.FromObject(eVal) : null), + P1 = Array.ConvertAll(initializer.GetPropertyAsObjectNullableArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), e => e is { } eVal ? MyClassInterop.FromObject(eVal) : null), }; } } @@ -726,12 +726,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsObjectArrayNullable("P1") is { } P1Val ? Array.ConvertAll(P1Val, e => MyClassInterop.FromObject(e)) : null, + P1 = initializer.GetPropertyAsObjectArrayNullable("P1") is { } P1Val ? Array.ConvertAll(P1Val, e => MyClassInterop.FromObject(e)) : null, }; } [JSExport] @@ -758,12 +758,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsObjectArrayNullable("P1") is { } P1Val ? Array.ConvertAll(P1Val, e => MyClassInterop.FromObject(e)) : null, + P1 = initializer.GetPropertyAsObjectArrayNullable("P1") is { } P1Val ? Array.ConvertAll(P1Val, e => MyClassInterop.FromObject(e)) : null, }; } } @@ -819,12 +819,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsObjectNullableArrayNullable("P1") is { } P1Val ? Array.ConvertAll(P1Val, e => e is { } eVal ? MyClassInterop.FromObject(eVal) : null) : null, + P1 = initializer.GetPropertyAsObjectNullableArrayNullable("P1") is { } P1Val ? Array.ConvertAll(P1Val, e => e is { } eVal ? MyClassInterop.FromObject(eVal) : null) : null, }; } [JSExport] @@ -851,12 +851,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsObjectNullableArrayNullable("P1") is { } P1Val ? Array.ConvertAll(P1Val, e => e is { } eVal ? MyClassInterop.FromObject(eVal) : null) : null, + P1 = initializer.GetPropertyAsObjectNullableArrayNullable("P1") is { } P1Val ? Array.ConvertAll(P1Val, e => e is { } eVal ? MyClassInterop.FromObject(eVal) : null) : null, }; } } @@ -912,12 +912,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = (jsObject.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => MyClassInterop.FromObject(t.Result), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), + P1 = (initializer.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer))).ContinueWith(t => MyClassInterop.FromObject(t.Result), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), }; } [JSExport] @@ -944,12 +944,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = (jsObject.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => MyClassInterop.FromObject(t.Result), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), + P1 = (initializer.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer))).ContinueWith(t => MyClassInterop.FromObject(t.Result), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), }; } } @@ -1005,12 +1005,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = (jsObject.GetPropertyAsObjectNullableTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => t.Result is { } tVal ? MyClassInterop.FromObject(tVal) : null, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), + P1 = (initializer.GetPropertyAsObjectNullableTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer))).ContinueWith(t => t.Result is { } tVal ? MyClassInterop.FromObject(tVal) : null, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), }; } [JSExport] @@ -1037,12 +1037,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = (jsObject.GetPropertyAsObjectNullableTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => t.Result is { } tVal ? MyClassInterop.FromObject(tVal) : null, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), + P1 = (initializer.GetPropertyAsObjectNullableTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer))).ContinueWith(t => t.Result is { } tVal ? MyClassInterop.FromObject(tVal) : null, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), }; } } @@ -1098,12 +1098,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsObjectNullableTaskNullable("P1")?.ContinueWith(t => t.Result is { } tVal ? MyClassInterop.FromObject(tVal) : null, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), + P1 = initializer.GetPropertyAsObjectNullableTaskNullable("P1")?.ContinueWith(t => t.Result is { } tVal ? MyClassInterop.FromObject(tVal) : null, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), }; } [JSExport] @@ -1130,12 +1130,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsObjectNullableTaskNullable("P1")?.ContinueWith(t => t.Result is { } tVal ? MyClassInterop.FromObject(tVal) : null, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), + P1 = initializer.GetPropertyAsObjectNullableTaskNullable("P1")?.ContinueWith(t => t.Result is { } tVal ? MyClassInterop.FromObject(tVal) : null, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), }; } } @@ -1178,12 +1178,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = ({{typeName}}[])jsObject.GetPropertyAsObjectArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = ({{typeName}}[])initializer.GetPropertyAsObjectArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -1210,12 +1210,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = ({{typeName}}[])jsObject.GetPropertyAsObjectArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = ({{typeName}}[])initializer.GetPropertyAsObjectArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } } diff --git a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Snapshots.cs b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Snapshots.cs index d70fce64..50489caa 100644 --- a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Snapshots.cs +++ b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_Snapshots.cs @@ -15,7 +15,7 @@ internal class CSharpInteropClassRendererTests_Snapshots [TestCase("string", "string", "JSType.String", "GetPropertyAsStringNullable")] [TestCase("double", "double", "JSType.Number", "GetPropertyAsDoubleNullable")] [TestCase("bool", "bool", "JSType.Boolean", "GetPropertyAsBooleanNullable")] - public void CSharpInteropClass_SupportedPropertyType_GeneratesFromJSObjectMethod(string typeExpression, string interopTypeExpression, string jsType, string jsObjectMethod) + public void CSharpInteropClass_SupportedPropertyType_GeneratesFromJSObjectMethod(string typeExpression, string interopTypeExpression, string jsType, string initializerMethod) { SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(""" using System; @@ -49,12 +49,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.{{jsObjectMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.{{initializerMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -80,19 +80,19 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.{{jsObjectMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.{{initializerMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } } """.Replace("{{typeExpression}}", interopTypeExpression) .Replace("{{jsType}}", jsType) - .Replace("{{jsObjectMethod}}", jsObjectMethod)); + .Replace("{{initializerMethod}}", initializerMethod)); } [TestCase("Version")] @@ -131,13 +131,13 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = ({{typeName}}[])jsObject.GetPropertyAsObjectArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), - P2 = jsObject.GetPropertyAsInt32Nullable("P2") ?? throw new ArgumentException("Non-nullable property 'P2' missing or of invalid type", nameof(jsObject)), + P1 = ({{typeName}}[])initializer.GetPropertyAsObjectArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), + P2 = initializer.GetPropertyAsInt32Nullable("P2") ?? throw new ArgumentException("Non-nullable property 'P2' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -178,13 +178,13 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = ({{typeName}}[])jsObject.GetPropertyAsObjectArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), - P2 = jsObject.GetPropertyAsInt32Nullable("P2") ?? throw new ArgumentException("Non-nullable property 'P2' missing or of invalid type", nameof(jsObject)), + P1 = ({{typeName}}[])initializer.GetPropertyAsObjectArrayNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), + P2 = initializer.GetPropertyAsInt32Nullable("P2") ?? throw new ArgumentException("Non-nullable property 'P2' missing or of invalid type", nameof(initializer)), }; } } @@ -228,13 +228,13 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = (jsObject.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => ({{typeName}})t.Result, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), - P2 = jsObject.GetPropertyAsInt32Nullable("P2") ?? throw new ArgumentException("Non-nullable property 'P2' missing or of invalid type", nameof(jsObject)), + P1 = (initializer.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer))).ContinueWith(t => ({{typeName}})t.Result, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), + P2 = initializer.GetPropertyAsInt32Nullable("P2") ?? throw new ArgumentException("Non-nullable property 'P2' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -275,13 +275,13 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = (jsObject.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => ({{typeName}})t.Result, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), - P2 = jsObject.GetPropertyAsInt32Nullable("P2") ?? throw new ArgumentException("Non-nullable property 'P2' missing or of invalid type", nameof(jsObject)), + P1 = (initializer.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer))).ContinueWith(t => ({{typeName}})t.Result, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously), + P2 = initializer.GetPropertyAsInt32Nullable("P2") ?? throw new ArgumentException("Non-nullable property 'P2' missing or of invalid type", nameof(initializer)), }; } } diff --git a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemDateTimeParameterType.cs b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemDateTimeParameterType.cs index fb2ee8b2..99a23210 100644 --- a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemDateTimeParameterType.cs +++ b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemDateTimeParameterType.cs @@ -112,7 +112,7 @@ public static C1 FromObject(object obj) [TestCase("DateTime", "global::System.DateTime", "GetPropertyAsDateTimeNullable")] [TestCase("DateTimeOffset", "global::System.DateTimeOffset", "GetPropertyAsDateTimeOffsetNullable")] - public void CSharpInteropClass_InstanceProperty_ForDateTimeParameterType(string typeName, string interopTypeExpression, string jsObjectMethod) + public void CSharpInteropClass_InstanceProperty_ForDateTimeParameterType(string typeName, string interopTypeExpression, string initializerMethod) { SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(""" using System; @@ -145,12 +145,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.{{jsObjectMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.{{initializerMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -176,16 +176,16 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.{{jsObjectMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.{{initializerMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } } -""".Replace("{{typeExpression}}", interopTypeExpression).Replace("{{jsObjectMethod}}", jsObjectMethod)); +""".Replace("{{typeExpression}}", interopTypeExpression).Replace("{{initializerMethod}}", initializerMethod)); } } diff --git a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemNumericParameterType.cs b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemNumericParameterType.cs index 7ecb3146..fe674a3c 100644 --- a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemNumericParameterType.cs +++ b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemNumericParameterType.cs @@ -209,7 +209,7 @@ public static C1 FromObject(object obj) [TestCase("double", "double", "GetPropertyAsDoubleNullable")] [TestCase("IntPtr", "nint", "GetPropertyAsIntPtrNullable")] [TestCase("nint", "nint", "GetPropertyAsIntPtrNullable")] - public void CSharpInteropClass_InstanceProperty_WithSupportedNumericParameterType(string typeExpression, string interopTypeExpression, string jsObjectMethod) + public void CSharpInteropClass_InstanceProperty_WithSupportedNumericParameterType(string typeExpression, string interopTypeExpression, string initializerMethod) { SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(""" using System; @@ -242,12 +242,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.{{jsObjectMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.{{initializerMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -273,16 +273,16 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.{{jsObjectMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.{{initializerMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } } -""".Replace("{{typeExpression}}", interopTypeExpression).Replace("{{jsObjectMethod}}", jsObjectMethod)); +""".Replace("{{typeExpression}}", interopTypeExpression).Replace("{{initializerMethod}}", initializerMethod)); } } diff --git a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemStringParameterType.cs b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemStringParameterType.cs index 1c06841a..a2e2290b 100644 --- a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemStringParameterType.cs +++ b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemStringParameterType.cs @@ -117,7 +117,7 @@ public static C1 FromObject(object obj) [TestCase("String", "string", "GetPropertyAsStringNullable")] [TestCase("char", "char", "GetPropertyAsCharNullable")] [TestCase("Char", "char", "GetPropertyAsCharNullable")] - public void CSharpInteropClass_InstanceProperty_WithStringParameterType(string typeName, string interopTypeExpression, string jsObjectMethod) + public void CSharpInteropClass_InstanceProperty_WithStringParameterType(string typeName, string interopTypeExpression, string initializerMethod) { SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(""" using System; @@ -149,12 +149,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.{{jsObjectMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.{{initializerMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -180,16 +180,16 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.{{jsObjectMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.{{initializerMethod}}("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } } -""".Replace("{{typeExpression}}", interopTypeExpression).Replace("{{jsObjectMethod}}", jsObjectMethod)); +""".Replace("{{typeExpression}}", interopTypeExpression).Replace("{{initializerMethod}}", initializerMethod)); } } diff --git a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemTaskParameterType.cs b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemTaskParameterType.cs index d0af4f0d..e25f4d3e 100644 --- a/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemTaskParameterType.cs +++ b/src/TypeShim.Generator.Tests/CSharp/CSharpInteropClassRendererTests_SystemTaskParameterType.cs @@ -371,12 +371,12 @@ public partial class C1Interop { [JSExport] [return: JSMarshalAs] - public static object ctor([JSMarshalAs] JSObject jsObject) + public static object ctor([JSMarshalAs] JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } [JSExport] @@ -402,12 +402,12 @@ public static C1 FromObject(object obj) _ => throw new ArgumentException($"Invalid object type {obj?.GetType().ToString() ?? "null"}", nameof(obj)), }; } - public static C1 FromJSObject(JSObject jsObject) + public static C1 FromJSObject(JSObject initializer) { - using var _ = jsObject; + using var _ = initializer; return new C1() { - P1 = jsObject.GetPropertyAsTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject)), + P1 = initializer.GetPropertyAsTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(initializer)), }; } } diff --git a/src/TypeShim.Generator.Tests/Parsing/SyntaxTreeParsingTests_Methods.cs b/src/TypeShim.Generator.Tests/Parsing/SyntaxTreeParsingTests_Methods.cs index 9d2e2fc5..28d7ecef 100644 --- a/src/TypeShim.Generator.Tests/Parsing/SyntaxTreeParsingTests_Methods.cs +++ b/src/TypeShim.Generator.Tests/Parsing/SyntaxTreeParsingTests_Methods.cs @@ -75,7 +75,7 @@ namespace N1; [TSExport] public class C1 { - public void M1(JSObject jsObject) + public void M1(JSObject initializer) { } } diff --git a/src/TypeShim.Generator.Tests/TypeScript/TypeScriptJSDocTests.cs b/src/TypeShim.Generator.Tests/TypeScript/TypeScriptJSDocTests.cs index 2bbd2532..3c153c5d 100644 --- a/src/TypeShim.Generator.Tests/TypeScript/TypeScriptJSDocTests.cs +++ b/src/TypeShim.Generator.Tests/TypeScript/TypeScriptJSDocTests.cs @@ -228,10 +228,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer })); } /** @@ -2172,10 +2172,10 @@ public C1() {} export class C1 extends ProxyBase { /** * Initializes a new instance. - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer })); } public get Name(): string { diff --git a/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_Char.cs b/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_Char.cs index 4def7f7b..1396ab52 100644 --- a/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_Char.cs +++ b/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_Char.cs @@ -158,10 +158,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: jsObject.P1.charCodeAt(0) })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: initializer.P1.charCodeAt(0) })); } public get P1(): string { @@ -205,10 +205,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: jsObject.P1 ? jsObject.P1.charCodeAt(0) : null })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: initializer.P1 ? initializer.P1.charCodeAt(0) : null })); } public get P1(): string | null { @@ -252,10 +252,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: jsObject.P1.then(e => e.charCodeAt(0)) })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: initializer.P1.then(e => e.charCodeAt(0)) })); } public get P1(): Promise { @@ -299,10 +299,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: jsObject.P1 ? jsObject.P1.then(e => e.charCodeAt(0)) : null })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: initializer.P1 ? initializer.P1.then(e => e.charCodeAt(0)) : null })); } public get P1(): Promise | null { diff --git a/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_Delegates.cs b/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_Delegates.cs index 498b431b..d41e4cb9 100644 --- a/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_Delegates.cs +++ b/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_Delegates.cs @@ -1055,10 +1055,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: (arg0: number, arg1: ManagedObject) => jsObject.P1(String.fromCharCode(arg0), ProxyBase.fromHandle(UserClass, arg1)) })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: (arg0: number, arg1: ManagedObject) => initializer.P1(String.fromCharCode(arg0), ProxyBase.fromHandle(UserClass, arg1)) })); } public get P1(): (arg0: string, arg1: UserClass | UserClass.Initializer) => void { @@ -1112,10 +1112,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: (arg0: number) => { const retVal = jsObject.P1(String.fromCharCode(arg0)); return retVal instanceof UserClass ? retVal.instance : retVal } })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: (arg0: number) => { const retVal = initializer.P1(String.fromCharCode(arg0)); return retVal instanceof UserClass ? retVal.instance : retVal } })); } public get P1(): (arg0: string) => UserClass { @@ -1169,10 +1169,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: (arg0: number) => jsObject.P1(String.fromCharCode(arg0)) })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: (arg0: number) => initializer.P1(String.fromCharCode(arg0)) })); } public get P1(): (arg0: string) => void { @@ -1226,10 +1226,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: (arg0: ManagedObject) => jsObject.P1(ProxyBase.fromHandle(UserClass, arg0)) })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: (arg0: ManagedObject) => initializer.P1(ProxyBase.fromHandle(UserClass, arg0)) })); } public get P1(): (arg0: UserClass | UserClass.Initializer) => void { diff --git a/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_ParameterizedConstructors.cs b/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_ParameterizedConstructors.cs index a5e111d2..e8f68b0a 100644 --- a/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_ParameterizedConstructors.cs +++ b/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_ParameterizedConstructors.cs @@ -599,10 +599,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer })); } public get P1(): ManagedObject { @@ -645,10 +645,10 @@ public class C1(int i) AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(i: number, jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor(i, { ...jsObject })); + constructor(i: number, initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor(i, { ...initializer })); } public get P1(): ManagedObject { diff --git a/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_ParameterlessConstructors.cs b/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_ParameterlessConstructors.cs index d647f52c..d5650e53 100644 --- a/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_ParameterlessConstructors.cs +++ b/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_ParameterlessConstructors.cs @@ -39,10 +39,10 @@ public class C1() AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer })); } public get P1(): {{typeScriptType}} { @@ -99,10 +99,10 @@ public class C1() AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: jsObject.P1 instanceof UserClass ? jsObject.P1.instance : jsObject.P1 })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: initializer.P1 instanceof UserClass ? initializer.P1.instance : initializer.P1 })); } public get P1(): UserClass { diff --git a/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_Properties.cs b/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_Properties.cs index ce77b3a4..5114efd1 100644 --- a/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_Properties.cs +++ b/src/TypeShim.Generator.Tests/TypeScript/TypeScriptUserClassProxyRendererTests_Properties.cs @@ -41,10 +41,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer })); } public get P1(): {{typeScriptType}} { @@ -89,10 +89,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer })); } public get P1(): {{typeScriptType}} { @@ -133,10 +133,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer })); } public static get P1(): {{typeScriptType}} { @@ -254,10 +254,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: jsObject.P1 instanceof UserClass ? jsObject.P1.instance : jsObject.P1 })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: initializer.P1 instanceof UserClass ? initializer.P1.instance : initializer.P1 })); } public get P1(): UserClass { @@ -369,10 +369,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: jsObject.P1 ? jsObject.P1 instanceof UserClass ? jsObject.P1.instance : jsObject.P1 : null })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: initializer.P1 ? initializer.P1 instanceof UserClass ? initializer.P1.instance : initializer.P1 : null })); } public get P1(): UserClass | null { @@ -429,10 +429,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: jsObject.P1.then(e => e instanceof UserClass ? e.instance : e) })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: initializer.P1.then(e => e instanceof UserClass ? e.instance : e) })); } public get P1(): Promise { @@ -489,10 +489,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer })); } public get P1(): Promise { @@ -548,10 +548,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: jsObject.P1.map(e => e instanceof UserClass ? e.instance : e) })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: initializer.P1.map(e => e instanceof UserClass ? e.instance : e) })); } public get P1(): Array { @@ -608,10 +608,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: jsObject.P1.map(e => e ? e instanceof UserClass ? e.instance : e : null) })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: initializer.P1.map(e => e ? e instanceof UserClass ? e.instance : e : null) })); } public get P1(): Array { @@ -668,10 +668,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: jsObject.P1 ? jsObject.P1.map(e => e instanceof UserClass ? e.instance : e) : null })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: initializer.P1 ? initializer.P1.map(e => e instanceof UserClass ? e.instance : e) : null })); } public get P1(): Array | null { @@ -728,10 +728,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: jsObject.P1 ? jsObject.P1.map(e => e ? e instanceof UserClass ? e.instance : e : null) : null })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: initializer.P1 ? initializer.P1.map(e => e ? e instanceof UserClass ? e.instance : e : null) : null })); } public get P1(): Array | null { @@ -788,10 +788,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: jsObject.P1.then(e => e ? e instanceof UserClass ? e.instance : e : null) })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: initializer.P1.then(e => e ? e instanceof UserClass ? e.instance : e : null) })); } public get P1(): Promise { @@ -848,10 +848,10 @@ public class C1 AssertEx.EqualOrDiff(renderContext.ToString(), """ export class C1 extends ProxyBase { /** - * @param jsObject - Object with member-initializers + * @param initializer - Object with member-initializers */ - constructor(jsObject: C1.Initializer) { - super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...jsObject, P1: jsObject.P1 ? jsObject.P1.then(e => e ? e instanceof UserClass ? e.instance : e : null) : null })); + constructor(initializer: C1.Initializer) { + super(TypeShimConfig.exports.N1.C1Interop.ctor({ ...initializer, P1: initializer.P1 ? initializer.P1.then(e => e ? e instanceof UserClass ? e.instance : e : null) : null })); } public get P1(): Promise | null { diff --git a/src/TypeShim.Generator.Tests/TypeScript/TypescriptAssemblyExportsRendererTests.cs b/src/TypeShim.Generator.Tests/TypeScript/TypescriptAssemblyExportsRendererTests.cs index b883b4c7..5ca05a4d 100644 --- a/src/TypeShim.Generator.Tests/TypeScript/TypescriptAssemblyExportsRendererTests.cs +++ b/src/TypeShim.Generator.Tests/TypeScript/TypescriptAssemblyExportsRendererTests.cs @@ -58,7 +58,7 @@ export interface AssemblyExports{ }; N2: { UserClassInterop: { - ctor(jsObject: object): ManagedObject; + ctor(initializer: object): ManagedObject; get_Id(instance: ManagedObject): number; set_Id(instance: ManagedObject, value: number): void; }; @@ -177,7 +177,7 @@ export interface AssemblyExports{ }; N2: { UserClassInterop: { - ctor(jsObject: object): ManagedObject; + ctor(initializer: object): ManagedObject; get_Id(instance: ManagedObject): number; set_Id(instance: ManagedObject, value: number): void; }; @@ -238,7 +238,7 @@ export interface AssemblyExports{ }; N2: { UserClassInterop: { - ctor(nowYouCannotInitializeFromJS: number, jsObject: object): ManagedObject; + ctor(nowYouCannotInitializeFromJS: number, initializer: object): ManagedObject; get_Id(instance: ManagedObject): number; set_Id(instance: ManagedObject, value: number): void; }; @@ -339,7 +339,7 @@ export interface AssemblyExports{ DoStuff(instance: ManagedObject, u: Promise): void; }; UserClassInterop: { - ctor(jsObject: object): ManagedObject; + ctor(initializer: object): ManagedObject; get_Id(instance: ManagedObject): number; set_Id(instance: ManagedObject, value: number): void; }; @@ -397,7 +397,7 @@ export interface AssemblyExports{ DoStuff(instance: ManagedObject, u: Array): void; }; UserClassInterop: { - ctor(jsObject: object): ManagedObject; + ctor(initializer: object): ManagedObject; get_Id(instance: ManagedObject): number; set_Id(instance: ManagedObject, value: number): void; }; @@ -531,7 +531,7 @@ public class C1 export interface AssemblyExports{ N1: { C1Interop: { - ctor(jsObject: object): ManagedObject; + ctor(initializer: object): ManagedObject; get_P1(instance: ManagedObject): string; set_P1(instance: ManagedObject, value: string): void; }; @@ -572,7 +572,7 @@ public class C1(int i) export interface AssemblyExports{ N1: { C1Interop: { - ctor(i: number, jsObject: object): ManagedObject; + ctor(i: number, initializer: object): ManagedObject; get_P1(instance: ManagedObject): string; set_P1(instance: ManagedObject, value: string): void; }; @@ -672,7 +672,7 @@ export interface AssemblyExports{ M1(instance: ManagedObject, u: () => ManagedObject): void; }; UserClassInterop: { - ctor(jsObject: object): ManagedObject; + ctor(initializer: object): ManagedObject; get_Id(instance: ManagedObject): number; set_Id(instance: ManagedObject, value: number): void; }; @@ -730,7 +730,7 @@ export interface AssemblyExports{ M1(instance: ManagedObject, u: (arg0: ManagedObject) => ManagedObject): void; }; UserClassInterop: { - ctor(jsObject: object): ManagedObject; + ctor(initializer: object): ManagedObject; get_Id(instance: ManagedObject): number; set_Id(instance: ManagedObject, value: number): void; }; @@ -788,7 +788,7 @@ export interface AssemblyExports{ M1(instance: ManagedObject): () => ManagedObject; }; UserClassInterop: { - ctor(jsObject: object): ManagedObject; + ctor(initializer: object): ManagedObject; get_Id(instance: ManagedObject): number; set_Id(instance: ManagedObject, value: number): void; }; @@ -846,7 +846,7 @@ export interface AssemblyExports{ M1(instance: ManagedObject): (arg0: ManagedObject | object) => ManagedObject; }; UserClassInterop: { - ctor(jsObject: object): ManagedObject; + ctor(initializer: object): ManagedObject; get_Id(instance: ManagedObject): number; set_Id(instance: ManagedObject, value: number): void; }; diff --git a/src/TypeShim.Generator/CSharp/CSharpInteropClassRenderer.cs b/src/TypeShim.Generator/CSharp/CSharpInteropClassRenderer.cs index c17b8a22..2225eb35 100644 --- a/src/TypeShim.Generator/CSharp/CSharpInteropClassRenderer.cs +++ b/src/TypeShim.Generator/CSharp/CSharpInteropClassRenderer.cs @@ -65,9 +65,9 @@ internal string Render() _methodRenderer.RenderFromObjectMapper(); } - if (_classInfo.Constructor is { AcceptsInitializer: true, IsParameterless: true } constructorMethod) + if (_classInfo.Constructor is { AcceptsInitializer: true, IsParameterless: true, InitializerObject: { } initializerParameter } constructorMethod) { - _methodRenderer.RenderFromJSObjectMapper(constructorMethod); + _methodRenderer.RenderFromJSObjectMapper(constructorMethod, initializerParameter); } } diff --git a/src/TypeShim.Generator/CSharp/CSharpMethodRenderer.cs b/src/TypeShim.Generator/CSharp/CSharpMethodRenderer.cs index cb0e6195..fd5cb66c 100644 --- a/src/TypeShim.Generator/CSharp/CSharpMethodRenderer.cs +++ b/src/TypeShim.Generator/CSharp/CSharpMethodRenderer.cs @@ -214,14 +214,14 @@ internal void RenderFromObjectMapper() _ctx.AppendLine("}"); } - internal void RenderFromJSObjectMapper(ConstructorInfo constructorInfo) + internal void RenderFromJSObjectMapper(ConstructorInfo constructorInfo, MethodParameterInfo initializerParameter) { - _ctx.Append("public static ").Append(_ctx.Class.Type.CSharpTypeSyntax).Append(' ').Append(RenderConstants.FromJSObject).AppendLine("(JSObject jsObject)") + _ctx.Append("public static ").Append(_ctx.Class.Type.CSharpTypeSyntax).Append(' ').Append(RenderConstants.FromJSObject).Append("(JSObject ").Append(initializerParameter.Name).AppendLine(")") .AppendLine("{"); using (_ctx.Indent()) { - _ctx.AppendLine("using var _ = jsObject;"); + _ctx.Append("using var _ = ").Append(initializerParameter.Name).AppendLine(";"); RenderConstructorInvocation(constructorInfo); } _ctx.AppendLine("}"); @@ -230,7 +230,9 @@ internal void RenderFromJSObjectMapper(ConstructorInfo constructorInfo) private void RenderConstructorInvocation(ConstructorInfo constructorInfo) { PropertyInfo[] propertiesInMapper = [.. constructorInfo.MemberInitializers]; - Dictionary propertyToAccessorDict = RenderJSObjectPropertyRetrievalWithTypeConversions(propertiesInMapper); + Dictionary propertyToAccessorDict = constructorInfo.AcceptsInitializer && constructorInfo.InitializerObject is MethodParameterInfo initializerParameter + ? RenderJSObjectPropertyRetrievalWithTypeConversions(propertiesInMapper, initializerParameter) + : []; Debug.Assert(propertyToAccessorDict.Count == propertiesInMapper.Length, "Property count differs from renderer count"); _ctx.Append("return new ").Append(constructorInfo.Type.CSharpTypeSyntax).Append('('); @@ -261,13 +263,13 @@ private void RenderConstructorInvocation(ConstructorInfo constructorInfo) } _ctx.AppendLine("};"); - Dictionary RenderJSObjectPropertyRetrievalWithTypeConversions(PropertyInfo[] properties) + Dictionary RenderJSObjectPropertyRetrievalWithTypeConversions(PropertyInfo[] properties, MethodParameterInfo initializerParameter) { Dictionary convertedTaskExpressionDict = []; foreach (PropertyInfo propertyInfo in properties) { DeferredExpressionRenderer valueRetrievalExpressionRenderer = DeferredExpressionRenderer.FromUnary(() => { - _ctx.Append("jsObject.").Append(_methodResolver.ResolveJSObjectMethodName(propertyInfo.Type)) + _ctx.Append(initializerParameter.Name).Append(".").Append(_methodResolver.ResolveJSObjectMethodName(propertyInfo.Type)) .Append("(\"").Append(propertyInfo.Name).Append("\")"); }); @@ -278,7 +280,7 @@ Dictionary RenderJSObjectPropertyRetri nonNullableExpressionRenderer.Render(); _ctx.Append(" ?? throw new ArgumentException(\"Non-nullable property '") .Append(propertyInfo.Name) - .Append("' missing or of invalid type\", nameof(jsObject))"); + .Append("' missing or of invalid type\", nameof(").Append(initializerParameter.Name).Append("))"); }); } diff --git a/src/TypeShim.Generator/Parsing/ConstructorInfoBuilder.cs b/src/TypeShim.Generator/Parsing/ConstructorInfoBuilder.cs index 2fc557b9..ca97ad8f 100644 --- a/src/TypeShim.Generator/Parsing/ConstructorInfoBuilder.cs +++ b/src/TypeShim.Generator/Parsing/ConstructorInfoBuilder.cs @@ -13,7 +13,7 @@ internal sealed class ConstructorInfoBuilder(INamedTypeSymbol classSymbol, IMeth MethodParameterInfo? initializersObjectParameter = initializerProperties.Length == 0 ? null : new() { - Name = "jsObject", + Name = "initializer", IsInjectedInstanceParameter = false, Type = InteropTypeInfo.JSObjectTypeInfo };