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

Add support for public properties defined in a private constructor #256

Merged
merged 2 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,3 +914,11 @@ export class DoNotOverridePrivates {
this.privateProperty = newValue;
}
}

/**
* Class that implements interface properties automatically, but using a private constructor
*/
export class ClassWithPrivateConstructorAndAutomaticProperties implements IInterfaceWithProperties {
private constructor(public readonly readOnlyString: string, public readWriteString: string) {
}
}
37 changes: 36 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,41 @@
}
]
},
"jsii-calc.ClassWithPrivateConstructorAndAutomaticProperties": {
"assembly": "jsii-calc",
"docs": {
"comment": "Class that implements interface properties automatically, but using a private constructor"
},
"fqn": "jsii-calc.ClassWithPrivateConstructorAndAutomaticProperties",
"interfaces": [
{
"fqn": "jsii-calc.IInterfaceWithProperties"
}
],
"kind": "class",
"name": "ClassWithPrivateConstructorAndAutomaticProperties",
"properties": [
{
"immutable": true,
"name": "readOnlyString",
"overrides": {
"fqn": "jsii-calc.IInterfaceWithProperties"
},
"type": {
"primitive": "string"
}
},
{
"name": "readWriteString",
"overrides": {
"fqn": "jsii-calc.IInterfaceWithProperties"
},
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.DefaultedConstructorArgument": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.DefaultedConstructorArgument",
Expand Down Expand Up @@ -3297,5 +3332,5 @@
}
},
"version": "0.7.6",
"fingerprint": "IrPnQp841TiCOiG/Z2z18s0K8pxTwuMglW1UJ2t1zsM="
"fingerprint": "eFasWxN7YC37iWdz+dDbEFTSQCzyangrqP5Nu02rzpw="
}
28 changes: 21 additions & 7 deletions packages/jsii-runtime/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions packages/jsii/lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,23 +385,30 @@ export class Assembler implements Emitter {
const constructor = type.symbol.members && type.symbol.members.get(ts.InternalSymbolName.Constructor);
const ctorDeclaration = constructor && (constructor.declarations[0] as ts.ConstructorDeclaration);
if (constructor && ctorDeclaration) {
const signature = this._typeChecker.getSignatureFromDeclaration(ctorDeclaration);

// tslint:disable-next-line:no-bitwise
if ((ts.getCombinedModifierFlags(ctorDeclaration) & ts.ModifierFlags.Private) === 0) {
const signature = this._typeChecker.getSignatureFromDeclaration(ctorDeclaration);
jsiiType.initializer = { initializer: true };
if (signature) {
for (const param of signature.getParameters()) {
jsiiType.initializer.parameters = jsiiType.initializer.parameters || [];
jsiiType.initializer.parameters.push(await this._toParameter(param));
if (ts.isParameterPropertyDeclaration(param.valueDeclaration)) {
await this._visitProperty(param, jsiiType);
}
jsiiType.initializer.variadic = jsiiType.initializer.parameters
&& jsiiType.initializer.parameters.find(p => !!p.variadic) != null;
}
}
this._visitDocumentation(constructor, jsiiType.initializer);
}

// Proces constructor-based property declarations even if constructor is private
if (signature) {
for (const param of signature.getParameters()) {
if (ts.isParameterPropertyDeclaration(param.valueDeclaration)) {
await this._visitProperty(param, jsiiType);
}
}
}
} else if (jsiiType.base) {
this._defer(() => {
const baseType = this._dereference(jsiiType.base!, type.symbol.valueDeclaration);
Expand Down Expand Up @@ -451,7 +458,7 @@ export class Assembler implements Emitter {
* @returns ``documentable``
*/
private _visitDocumentation<T extends spec.Documentable>(symbol: ts.Symbol | ts.Signature, documentable: T): T {
const comment = ts.displayPartsToString(symbol.getDocumentationComment(this._typeChecker));
const comment = ts.displayPartsToString(symbol.getDocumentationComment(this._typeChecker)).trim();
if (comment) {
if (LOG.isTraceEnabled()) {
LOG.trace(`Found documentation comment: ${colors.yellow(comment)}`);
Expand Down