Skip to content

Commit

Permalink
fix(jsii): detect double interface member declarations (#360)
Browse files Browse the repository at this point in the history
In C# it's prohibited to declare an interface member
that also exists in an inherited interface. Have jsii
detect this illegal pattern.

Strictly speaking, this only true if we don't overload,
but since we don't support overloading anyway we just
check on the member names, not the types.

Fixes #340.
  • Loading branch information
rix0rrr committed Feb 14, 2019
1 parent 999f597 commit b2b2c89
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
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#).
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();
}
}

0 comments on commit b2b2c89

Please sign in to comment.