UNPKG

@microsoft.azure/autorest.incubator

Version:
197 lines 7.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const initializer_1 = require("../../common/initializer"); const text_manipulation_1 = require("../../common/text-manipulation"); const access_modifier_1 = require("../../csharp/code-dom/access-modifier"); const attribute_1 = require("../../csharp/code-dom/attribute"); const doc_comments_1 = require("../../csharp/code-dom/doc-comments"); const expression_1 = require("../../csharp/code-dom/expression"); const statement_1 = require("../../csharp/code-dom/statements/statement"); class Property extends initializer_1.Initializer { constructor(name, type, objectInitializer) { super(); this.name = name; this.type = type; this['new'] = access_modifier_1.Modifier.None; this.getAccess = access_modifier_1.Access.Public; this.setAccess = access_modifier_1.Access.Public; this['static'] = access_modifier_1.Modifier.None; this.virtual = access_modifier_1.Modifier.None; this.sealed = access_modifier_1.Modifier.None; this.override = access_modifier_1.Modifier.None; this.abstract = access_modifier_1.Modifier.None; this.extern = access_modifier_1.Modifier.None; this.attributes = new Array(); this.metadata = {}; this.description = ''; this.apply(objectInitializer); } get visibility() { return access_modifier_1.highestAccess(this.getAccess, this.setAccess); } add(item) { if (item instanceof attribute_1.Attribute) { this.attributes.push(item); return item; } throw Error(`FATAL - UNABLE TO ADD UNKNOWN TYPE for '${JSON.stringify(item)}'`); } get attributeDeclaration() { return this.attributes.length > 0 ? `${this.attributes.joinWith(each => `${expression_1.valueOf(each)}`, text_manipulation_1.EOL)}${text_manipulation_1.EOL}` : ''; } get getterDeclaration() { return this.getAccess === this.visibility ? 'get' : `${this.getAccess} get`; } get setterDeclaration() { return this.setAccess === this.visibility ? 'set' : `${this.setAccess} set`; } get getter() { return `${this.getterDeclaration};`; } get setter() { return `${this.setterDeclaration};`; } get declaration() { return ` ${text_manipulation_1.docComment(doc_comments_1.summary(this.description) + doc_comments_1.remarks(this.externalDocs))} ${this.attributeDeclaration}${this.new}${this.visibility} ${this.static} ${this.virtual} ${this.sealed} ${this.override} ${this.abstract} ${this.extern} ${this.type.declaration} ${this.name} {${this.getter}${this.setter}} `.slim(); } get value() { return `${this.name}`; } get valuePrivate() { return `${this.name}`; } toString() { return this.value; } assign(expression) { return `${this.name} = ${expression_1.valueOf(expression)};`; } assignPrivate(expression) { return this.assign(expression); } get declarationExpression() { return this; } get declarationStatement() { throw new Error(`Property can not be a declaration statement`); } invokeMethod(methodName, ...parameters) { const e = `${this.value}.${methodName}(${parameters.joinWith(expression_1.valueOf)})`; return { implementation: `${e};`, value: e, }; } } exports.Property = Property; class ImplementedProperty extends Property { constructor(name, type, objectInitializer) { super(name, type); this.name = name; this.type = type; this.apply(objectInitializer); } get declaration() { return (` ${text_manipulation_1.docComment(doc_comments_1.summary(this.description) + doc_comments_1.remarks(this.externalDocs))} ${this.attributeDeclaration}${this.new}${this.visibility} ${this.static} ${this.virtual} ${this.sealed} ${this.override} ${this.abstract} ${this.extern} ${this.type.declaration} ${this.name}`.slim() + ` { ${this.getter} ${this.setter} } `).trim(); } get getter() { if (!this.getterStatements) { return ''; } return `${this.getterDeclaration} { ${text_manipulation_1.indent(this.getterStatements.implementation, 2)} }`.trim(); } get setter() { if (!this.setterStatements) { return ''; } return `${this.setterDeclaration} { ${text_manipulation_1.indent(this.setterStatements.implementation, 2)} }`.trim(); } } exports.ImplementedProperty = ImplementedProperty; class LambdaProperty extends Property { constructor(name, type, expression, objectInitializer) { super(name, type); this.name = name; this.type = type; this.expression = expression; this.apply(objectInitializer); } get declaration() { return ` ${text_manipulation_1.docComment(doc_comments_1.summary(this.description) + doc_comments_1.remarks(this.externalDocs))} ${this.attributeDeclaration}${this.new}${this.visibility} ${this.static} ${this.virtual} ${this.sealed} ${this.override} ${this.abstract} ${this.extern} ${this.type.declaration} ${this.name} => ${expression_1.valueOf(this.expression)}; `.slim(); } } exports.LambdaProperty = LambdaProperty; class LazyProperty extends Property { constructor(name, type, expression, objectInitializer) { super(name, type); this.name = name; this.type = type; this.expression = expression; this.instanceAccess = 'this'; this.backingName = `_${this.name.uncapitalize()}`; this.apply(objectInitializer); } get declaration() { return ` ${text_manipulation_1.docComment(doc_comments_1.summary(`Backing field for ${this.name} property`))} private ${this.static} ${this.type.declaration} ${this.backingName}; EOL ${text_manipulation_1.docComment(doc_comments_1.summary(this.description) + doc_comments_1.remarks(this.externalDocs))} ${this.attributeDeclaration}${this.new}${this.visibility} ${this.static} ${this.virtual} ${this.sealed} ${this.override} ${this.abstract} ${this.extern} ${this.type.declaration} ${this.name} => ${this.instanceAccess}.${this.backingName}?? (${this.instanceAccess}.${this.backingName} = ${this.expression.value}); `.slim(); } } exports.LazyProperty = LazyProperty; class BackedProperty extends ImplementedProperty { constructor(name, type, objectInitializer) { const backingName = `_${name.uncapitalize()}`; super(name, type, { getterStatements: new statement_1.Statements(`return this.${backingName};`), setterStatements: new statement_1.Statements(`this.${backingName} = value;`) }); this.backingName = backingName; this.apply(objectInitializer); } get declaration() { return ` ${text_manipulation_1.docComment(doc_comments_1.summary(`Backing field for ${this.name} property`))} private ${this.type.declaration} ${this.backingName}${this.initializer ? `= ${expression_1.valueOf(this.initializer)}` : ''}; EOL ${super.declaration} `.trim(); // ${docComment(summary(this.description))} // ${ this.attributeDeclaration } ${ this.new } ${ this.visibility } ${ this.static } ${ this.virtual } ${ this.sealed } ${ this.override } ${ this.abstract } ${ this.extern } ${ this.type.declaration } ${ this.name } { ${ this.getterDeclaration } { return this.${ this.backingName }; } ${ this.setterDeclaration } { this.${ this.backingName } = value; } } // EOL } get value() { return `${this.name}`; } get valuePrivate() { return `this.${this.backingName}`; } assignPrivate(expression) { return `${this.backingName} = ${expression_1.valueOf(expression)};`; } } exports.BackedProperty = BackedProperty; //# sourceMappingURL=property.js.map