Skip to content

Commit 2848f76

Browse files
rix0rrrRomainMuller
authored andcommitted
feat(jsii): protect against prohibited member names (#506)
We are not currently rigged to handle some special methods that have meaning in runtimes that we target (specifically, 'equals', 'hashCode' and 'GetHashCode'). Just prohibit them for now.
1 parent 8d11900 commit 2848f76

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

packages/jsii/lib/assembler.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,10 @@ export class Assembler implements Emitter {
922922
this._diagnostic(declaration, ts.DiagnosticCategory.Error, `Unable to compute signature for ${type.fqn}#${symbol.name}`);
923923
return;
924924
}
925+
if (isProhibitedMemberName(symbol.name)) {
926+
this._diagnostic(declaration, ts.DiagnosticCategory.Error, `Prohibited member name: ${symbol.name}`);
927+
return;
928+
}
925929
const parameters = await Promise.all(signature.getParameters().map(p => this._toParameter(p)));
926930

927931
const returnType = signature.getReturnType();
@@ -987,6 +991,10 @@ export class Assembler implements Emitter {
987991
if (LOG.isTraceEnabled()) {
988992
LOG.trace(`Processing property: ${colors.green(type.fqn)}#${colors.cyan(symbol.name)}`);
989993
}
994+
if (isProhibitedMemberName(symbol.name)) {
995+
this._diagnostic(symbol.valueDeclaration, ts.DiagnosticCategory.Error, `Prohibited member name: ${symbol.name}`);
996+
return;
997+
}
990998

991999
const signature = symbol.valueDeclaration as (ts.PropertySignature
9921000
| ts.PropertyDeclaration
@@ -1604,4 +1612,19 @@ function noEmptyDict<T>(xs: {[key: string]: T}): {[key: string]: T} | undefined
16041612
*/
16051613
function isErrorType(t: ts.Type) {
16061614
return (t as any).intrinsicName === 'error';
1615+
}
1616+
1617+
/**
1618+
* These specifially cause trouble in C#, where we have to specificially annotate them as 'new' but our generator isn't doing that
1619+
*
1620+
* In C#, 'GetHashCode' is also problematic, but jsii already prevents you from naming a
1621+
* method that starts with 'get' so we don't need to do anything special for that.
1622+
*/
1623+
const PROHIBITED_MEMBER_NAMES = ['equals', 'hashcode'];
1624+
1625+
/**
1626+
* Whether the given name is prohibited
1627+
*/
1628+
function isProhibitedMemberName(name: string) {
1629+
return PROHIBITED_MEMBER_NAMES.includes(name.toLowerCase());
16071630
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// tslint:disable-next-line:comment-format
2+
///!MATCH_ERROR:Prohibited member name: equals
3+
4+
export class SomeClass {
5+
public equals(): boolean {
6+
return true;
7+
}
8+
}

0 commit comments

Comments
 (0)