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

feat(jsii): protect against prohibited member names #506

Merged
merged 1 commit into from
May 21, 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
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;
}
}