Skip to content

Commit b9621f1

Browse files
Hamza AssyadRomainMuller
authored andcommitted
feat(dotnet): drop the useless I prefix for non datatype interfaces (#728)
Drop the useless I prefix for non datatype interfaces as they are guaranteed by jsii to start with I. This removes a bunch of II* interfaces. Updated the tests, and tested with the CDK successfully. BREAKING CHANGE: names of .NET behavioral interfaces have changed (the duplicate prefix I was removed). Fixes #109
1 parent 68ef42a commit b9621f1

93 files changed

Lines changed: 221 additions & 216 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/jsii-dotnet-runtime-test/test/Amazon.JSII.Runtime.IntegrationTests/ComplianceTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ public override string TheProperty
495495
}
496496
}
497497

498-
class InterfaceWithProperties : DeputyBase, IIInterfaceWithProperties
498+
class InterfaceWithProperties : DeputyBase, IInterfaceWithProperties
499499
{
500500
string _x;
501501

@@ -630,10 +630,10 @@ public void SyncOverrides_CallsDoubleAsyncPropertySetterFails()
630630
[Fact(DisplayName = Prefix + nameof(TestInterfaces))]
631631
public void TestInterfaces()
632632
{
633-
IIFriendly friendly;
634-
IIFriendlier friendlier;
635-
IIRandomNumberGenerator randomNumberGenerator;
636-
IIFriendlyRandomGenerator friendlyRandomGenerator;
633+
IFriendly friendly;
634+
IFriendlier friendlier;
635+
IRandomNumberGenerator randomNumberGenerator;
636+
IFriendlyRandomGenerator friendlyRandomGenerator;
637637

638638
Add add = new Add(new Number(10), new Number(20));
639639
friendly = add;
@@ -700,10 +700,10 @@ public void TestNativeObjectsWithInterfaces()
700700
public void TestLiteralInterface()
701701
{
702702
JSObjectLiteralForInterface obj = new JSObjectLiteralForInterface();
703-
IIFriendly friendly = obj.GiveMeFriendly();
703+
IFriendly friendly = obj.GiveMeFriendly();
704704
Assert.Equal("I am literally friendly!", friendly.Hello());
705705

706-
IIFriendlyRandomGenerator gen = obj.GiveMeFriendlyGenerator();
706+
IFriendlyRandomGenerator gen = obj.GiveMeFriendlyGenerator();
707707
Assert.Equal("giveMeFriendlyGenerator", gen.Hello());
708708
Assert.Equal((double) 42, gen.Next());
709709
}
@@ -1014,7 +1014,7 @@ public override String ConsumePartiallyInitializedThis(ConstructorPassesThisOut
10141014
return "OK";
10151015
}
10161016
}
1017-
class NumberReturner : DeputyBase, IIReturnsNumber
1017+
class NumberReturner : DeputyBase, IReturnsNumber
10181018
{
10191019
public NumberReturner(double number)
10201020
{
@@ -1025,12 +1025,12 @@ public NumberReturner(double number)
10251025
public Number NumberProp { get; }
10261026

10271027
[JsiiMethod(name: "obtainNumber", returnsJson: "{\"type\":{\"fqn\":\"@scope/jsii-calc-lib.IDoublable\"}}", isOverride: true)]
1028-
public IIDoublable ObtainNumber()
1028+
public IDoublable ObtainNumber()
10291029
{
10301030
return new Doublable(this.NumberProp);
10311031
}
10321032

1033-
class Doublable : DeputyBase, IIDoublable
1033+
class Doublable : DeputyBase, IDoublable
10341034
{
10351035
public Doublable(Number number)
10361036
{
@@ -1130,7 +1130,7 @@ public override string TheProperty
11301130
public string AnotherTheProperty { get; set; }
11311131
}
11321132

1133-
class PureNativeFriendlyRandom : DeputyBase, IIFriendlyRandomGenerator
1133+
class PureNativeFriendlyRandom : DeputyBase, IFriendlyRandomGenerator
11341134
{
11351135
int _nextNumber = 1000;
11361136

@@ -1149,7 +1149,7 @@ public string Hello()
11491149
}
11501150
}
11511151

1152-
class SubclassNativeFriendlyRandom : Number, IIFriendly, IIRandomNumberGenerator
1152+
class SubclassNativeFriendlyRandom : Number, IFriendly, IRandomNumberGenerator
11531153
{
11541154
int _nextNumber;
11551155

packages/jsii-pacmak/lib/targets/dotnet/dotnetgenerator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export class DotNetGenerator extends Generator {
121121

122122
protected onBeginInterface(ifc: spec.InterfaceType) {
123123
const implementations = this.typeresolver.resolveImplementedInterfaces(ifc);
124-
const interfaceName = this.nameutils.convertInterfaceName(ifc.name);
124+
const interfaceName = this.nameutils.convertInterfaceName(ifc);
125125
const namespace = ifc.namespace ? `${this.assembly.targets!.dotnet!.namespace}.${ifc.namespace}` : this.assembly.targets!.dotnet!.namespace;
126126
this.openFileIfNeeded(interfaceName, namespace, this.isNested(ifc));
127127

@@ -137,7 +137,7 @@ export class DotNetGenerator extends Generator {
137137
}
138138

139139
protected onEndInterface(ifc: spec.InterfaceType) {
140-
const interfaceName = this.nameutils.convertInterfaceName(ifc.name);
140+
const interfaceName = this.nameutils.convertInterfaceName(ifc);
141141
this.code.closeBlock();
142142
this.closeFileIfNeeded(interfaceName, this.isNested(ifc));
143143

packages/jsii-pacmak/lib/targets/dotnet/dotnetruntimegenerator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class DotNetRuntimeGenerator {
2525
*/
2626
public emitAttributesForInterface(ifc: spec.InterfaceType) {
2727
const jsiiAttribute =
28-
`[JsiiInterface(nativeType: typeof(${this.nameutils.convertInterfaceName(ifc.name)}), fullyQualifiedName: "${ifc.fqn}")]`;
28+
`[JsiiInterface(nativeType: typeof(${this.nameutils.convertInterfaceName(ifc)}), fullyQualifiedName: "${ifc.fqn}")]`;
2929
this.code.line(jsiiAttribute);
3030
this.emitDeprecatedAttributeIfNecessary(ifc);
3131
}
@@ -104,7 +104,7 @@ export class DotNetRuntimeGenerator {
104104
* Ex: [JsiiTypeProxy(nativeType: typeof(IVeryBaseProps), fullyQualifiedName: "@scope/jsii-calc-base-of-base.VeryBaseProps")]
105105
*/
106106
public emitAttributesForInterfaceProxy(ifc: spec.ClassType | spec.InterfaceType): void {
107-
const name = ifc.kind === spec.TypeKind.Interface ? this.nameutils.convertInterfaceName(ifc.name)
107+
const name = ifc.kind === spec.TypeKind.Interface ? this.nameutils.convertInterfaceName(ifc)
108108
: this.typeresolver.toNativeFqn(ifc.fqn);
109109
this.code.line(`[JsiiTypeProxy(nativeType: typeof(${name}), fullyQualifiedName: \"${ifc.fqn}\")]`);
110110
this.emitDeprecatedAttributeIfNecessary(ifc);

packages/jsii-pacmak/lib/targets/dotnet/dotnettyperesolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class DotNetTypeResolver {
3131
let typeName: string = '';
3232
switch (type.kind) {
3333
case spec.TypeKind.Interface:
34-
typeName = this.nameutils.convertInterfaceName(type.name);
34+
typeName = this.nameutils.convertInterfaceName(type);
3535
break;
3636
case spec.TypeKind.Class:
3737
typeName = this.nameutils.convertClassName(type as spec.ClassType);

packages/jsii-pacmak/lib/targets/dotnet/nameutils.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ export class DotNetNameUtils {
3131
return this.capitalizeWord(original);
3232
}
3333

34-
public convertInterfaceName(original: string) {
35-
if (this.isInvalidName(original)) {
36-
throw new Error(`Invalid interface name: ${original}`);
34+
public convertInterfaceName(original: spec.InterfaceType) {
35+
if (this.isInvalidName(original.name)) {
36+
throw new Error(`Invalid interface name: ${original.name}`);
37+
}
38+
if (original.datatype) {
39+
// Datatype interfaces need to be prefixed by I so that they don't clash with the prop object implementation
40+
return 'I' + this.capitalizeWord(original.name);
41+
} else {
42+
// Non datatype interfaces are guaranteed by JSII to be prefixed by I already
43+
return this.capitalizeWord(original.name);
3744
}
38-
const capitalizedName = this.capitalizeWord(original);
39-
return 'I' + capitalizedName;
4045
}
4146

4247
public convertClassName(original: spec.ClassType) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Amazon.JSII.Runtime.Deputy;
2+
3+
namespace Amazon.JSII.Tests.CalculatorNamespace.BaseNamespace
4+
{
5+
[JsiiInterface(nativeType: typeof(IBaseInterface), fullyQualifiedName: "@scope/jsii-calc-base.IBaseInterface")]
6+
public interface IBaseInterface : Amazon.JSII.Tests.CalculatorNamespace.BaseOfBaseNamespace.IVeryBaseInterface
7+
{
8+
[JsiiMethod(name: "bar")]
9+
void Bar();
10+
}
11+
}

packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/Amazon/JSII/Tests/CalculatorNamespace/BaseNamespace/IBaseInterfaceProxy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Amazon.JSII.Tests.CalculatorNamespace.BaseNamespace
44
{
5-
[JsiiTypeProxy(nativeType: typeof(IIBaseInterface), fullyQualifiedName: "@scope/jsii-calc-base.IBaseInterface")]
6-
internal sealed class IBaseInterfaceProxy : DeputyBase, Amazon.JSII.Tests.CalculatorNamespace.BaseNamespace.IIBaseInterface
5+
[JsiiTypeProxy(nativeType: typeof(IBaseInterface), fullyQualifiedName: "@scope/jsii-calc-base.IBaseInterface")]
6+
internal sealed class IBaseInterfaceProxy : DeputyBase, Amazon.JSII.Tests.CalculatorNamespace.BaseNamespace.IBaseInterface
77
{
88
private IBaseInterfaceProxy(ByRefValue reference): base(reference)
99
{

packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/Amazon/JSII/Tests/CalculatorNamespace/BaseNamespace/IIBaseInterface.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/jsii-pacmak/test/expected.jsii-calc-lib/dotnet/Amazon.JSII.Tests.CalculatorPackageId.LibPackageId/Amazon/JSII/Tests/CalculatorNamespace/LibNamespace/IIDoublable.cs renamed to packages/jsii-pacmak/test/expected.jsii-calc-lib/dotnet/Amazon.JSII.Tests.CalculatorPackageId.LibPackageId/Amazon/JSII/Tests/CalculatorNamespace/LibNamespace/IDoublable.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace Amazon.JSII.Tests.CalculatorNamespace.LibNamespace
66
/// <remarks>
77
/// stability: Deprecated
88
/// </remarks>
9-
[JsiiInterface(nativeType: typeof(IIDoublable), fullyQualifiedName: "@scope/jsii-calc-lib.IDoublable")]
9+
[JsiiInterface(nativeType: typeof(IDoublable), fullyQualifiedName: "@scope/jsii-calc-lib.IDoublable")]
1010
[System.Obsolete()]
11-
public interface IIDoublable
11+
public interface IDoublable
1212
{
1313
/// <remarks>
1414
/// stability: Deprecated

packages/jsii-pacmak/test/expected.jsii-calc-lib/dotnet/Amazon.JSII.Tests.CalculatorPackageId.LibPackageId/Amazon/JSII/Tests/CalculatorNamespace/LibNamespace/IDoublableProxy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace Amazon.JSII.Tests.CalculatorNamespace.LibNamespace
66
/// <remarks>
77
/// stability: Deprecated
88
/// </remarks>
9-
[JsiiTypeProxy(nativeType: typeof(IIDoublable), fullyQualifiedName: "@scope/jsii-calc-lib.IDoublable")]
9+
[JsiiTypeProxy(nativeType: typeof(IDoublable), fullyQualifiedName: "@scope/jsii-calc-lib.IDoublable")]
1010
[System.Obsolete()]
11-
internal sealed class IDoublableProxy : DeputyBase, Amazon.JSII.Tests.CalculatorNamespace.LibNamespace.IIDoublable
11+
internal sealed class IDoublableProxy : DeputyBase, Amazon.JSII.Tests.CalculatorNamespace.LibNamespace.IDoublable
1212
{
1313
private IDoublableProxy(ByRefValue reference): base(reference)
1414
{

0 commit comments

Comments
 (0)