Skip to content

Commit

Permalink
feat(jsii): protect against prohibited member names (#506)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rix0rrr authored and RomainMuller committed May 21, 2019
1 parent 8d11900 commit 2848f76
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/jsii/lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,10 @@ export class Assembler implements Emitter {
this._diagnostic(declaration, ts.DiagnosticCategory.Error, `Unable to compute signature for ${type.fqn}#${symbol.name}`);
return;
}
if (isProhibitedMemberName(symbol.name)) {
this._diagnostic(declaration, ts.DiagnosticCategory.Error, `Prohibited member name: ${symbol.name}`);
return;
}
const parameters = await Promise.all(signature.getParameters().map(p => this._toParameter(p)));

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

const signature = symbol.valueDeclaration as (ts.PropertySignature
| ts.PropertyDeclaration
Expand Down Expand Up @@ -1604,4 +1612,19 @@ function noEmptyDict<T>(xs: {[key: string]: T}): {[key: string]: T} | undefined
*/
function isErrorType(t: ts.Type) {
return (t as any).intrinsicName === 'error';
}

/**
* These specifially cause trouble in C#, where we have to specificially annotate them as 'new' but our generator isn't doing that
*
* In C#, 'GetHashCode' is also problematic, but jsii already prevents you from naming a
* method that starts with 'get' so we don't need to do anything special for that.
*/
const PROHIBITED_MEMBER_NAMES = ['equals', 'hashcode'];

/**
* Whether the given name is prohibited
*/
function isProhibitedMemberName(name: string) {
return PROHIBITED_MEMBER_NAMES.includes(name.toLowerCase());
}
8 changes: 8 additions & 0 deletions packages/jsii/test/negatives/neg.prohibited-member-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// tslint:disable-next-line:comment-format
///!MATCH_ERROR:Prohibited member name: equals

export class SomeClass {
public equals(): boolean {
return true;
}
}

0 comments on commit 2848f76

Please sign in to comment.