Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jsii): detect double interface member declarations #360

Merged
merged 1 commit into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/jsii/lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,27 @@ export class Assembler implements Emitter {
}
});

// Check that no interface declares a member that's already declared
// in a base type (not allowed in C#).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this is the error I get:

@aws-cdk/aws-iam: IRoleProxy.cs(113,21): error CS0111: Type 'IRoleProxy' already defines a member called 'AddToPolicy' with the same parameter types [/tmp/jsii-pacmak-codec20iQw/Amazon.CDK.AWS.IAM/Amazon.CDK.AWS.IAM.csproj]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I guess 'new' will do it. I don't get the point of it though, and our C# generator doesn't do the necessary analysis to add the modifier, so I think disallowing in jsii is the easiest way forward.

const memberNames = interfaceMemberNames(jsiiType);
const checkNoIntersection = (...bases: spec.Type[]) => {
for (const base of bases) {
if (!spec.isInterfaceType(base)) { continue; }

const baseMembers = interfaceMemberNames(base);
for (const memberName of memberNames) {
if (baseMembers.includes(memberName)) {
this._diagnostic(type.symbol.declarations[0],
ts.DiagnosticCategory.Error,
`Interface declares same member as inherited interface: ${memberName}`);
}
}
// Recurse upwards
this._deferUntilTypesAvailable(fqn, base.interfaces || [], type.symbol.valueDeclaration, checkNoIntersection);
}
};
this._deferUntilTypesAvailable(fqn, jsiiType.interfaces || [], type.symbol.valueDeclaration, checkNoIntersection);

return _sortMembers(this._visitDocumentation(type.symbol, jsiiType));
}

Expand Down Expand Up @@ -1164,6 +1185,22 @@ function intersection<T>(xs: Set<T>, ys: Set<T>): Set<T> {
return ret;
}

/**
* Return all members names of a JSII interface type
*
* Returns empty string for a non-interface type.
*/
function interfaceMemberNames(jsiiType: spec.InterfaceType): string[] {
const ret = new Array<string>();
if (jsiiType.methods) {
ret.push(...jsiiType.methods.map(m => m.name).filter(x => x !== undefined) as string[]);
}
if (jsiiType.properties) {
ret.push(...jsiiType.properties.map(m => m.name));
}
return ret;
}

/**
* Whether or not the given name is conventionally an interface name
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
///!MATCH_ERROR: Interface declares same member as inherited interface: foo

export interface IA {
foo(): void;
}

export interface IB extends IA {
bar(): void;
}

export interface IC extends IB {
foo(): void;
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
///!MATCH_ERROR: Interface declares same member as inherited interface: foo

export interface IA {
foo(): void;
}
export interface IB extends IA {
foo(): void;
}

8 changes: 8 additions & 0 deletions packages/jsii/test/negatives/neg.double-interface-members.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
///!MATCH_ERROR: Interface declares same member as inherited interface: foo

export interface A {
foo: number;
}
export interface B extends A {
foo: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface ISomething {
returnSomething(): Superclass;
}

export interface ISomethingElse extends ISomething {
returnSomething(): Subclass;
export class ISomethingElse implements ISomething {
public returnSomething(): Subclass {
return new Subclass();
}
}