all files / dist/writers/ PropertyWriter.js

100% Statements 78/78
100% Branches 38/38
100% Functions 15/15
100% Lines 78/78
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105  24× 24× 24× 24× 24× 24× 24×   32× 30× 30× 30×     38×   15× 15× 15× 15×                               15× 15×   19× 18× 18× 17× 18×   19×          
"use strict";
var definitions_1 = require("./../definitions");
var PropertyWriter = (function () {
    function PropertyWriter(writer, baseDefinitionWriter, documentationedWriter, decoratorsWriter, typeWriter, scopeWriter, typeWithDefaultExpressionWriter) {
        this.writer = writer;
        this.baseDefinitionWriter = baseDefinitionWriter;
        this.documentationedWriter = documentationedWriter;
        this.decoratorsWriter = decoratorsWriter;
        this.typeWriter = typeWriter;
        this.scopeWriter = scopeWriter;
        this.typeWithDefaultExpressionWriter = typeWithDefaultExpressionWriter;
    }
    PropertyWriter.prototype.willWriteAccessorBody = function (def) {
        if (!(def instanceof definitions_1.ClassPropertyDefinition))
            return false;
        var isWriteableGetAccessor = (def.kind & definitions_1.ClassPropertyKind.GetAccessor) !== 0 && def.onWriteGetBody != null;
        var isWriteableSetAccessor = (def.kind & definitions_1.ClassPropertyKind.SetAccessor) !== 0 && def.onWriteSetBody != null;
        return this.isAccessor(def) && (isWriteableGetAccessor || isWriteableSetAccessor);
    };
    PropertyWriter.prototype.isAbstractAccessor = function (def) {
        return this.isAccessor(def) && def.isAbstract;
    };
    PropertyWriter.prototype.isAccessor = function (def) {
        return (def.kind & definitions_1.ClassPropertyKind.GetSetAccessor) !== 0;
    };
    PropertyWriter.prototype.write = function (def, flags) {
        var _this = this;
        this.baseDefinitionWriter.writeWrap(def, function () {
            _this.writeSingleCommonHeader(def, flags);
            if (def instanceof definitions_1.ClassPropertyDefinition && (_this.willWriteAccessorBody(def) || _this.isAbstractAccessor(def)))
                _this.writeAccessor(def, flags);
            else
                _this.writeNormalProperty(def, flags);
        });
    };
    PropertyWriter.prototype.writeNormalProperty = function (def, flags) {
        this.writeCommonHeader(def, flags);
        this.writer.write(def.name);
        this.writer.conditionalWrite(def.isOptional, "?");
        if (def instanceof definitions_1.InterfacePropertyDefinition)
            this.typeWriter.writeWithColon(def.type, "any");
        else
            this.typeWithDefaultExpressionWriter.writeWithOptionalCheck(def, flags, "any");
        this.writer.write(";");
    };
    PropertyWriter.prototype.writeAccessor = function (def, flags) {
        if (def.kind & definitions_1.ClassPropertyKind.GetAccessor)
            this.writeGetAccessor(def, flags);
        if (def.kind & definitions_1.ClassPropertyKind.SetAccessor) {
            this.writer.conditionalNewLine(def.kind === definitions_1.ClassPropertyKind.GetSetAccessor);
            this.writeSetAccessor(def, flags);
        }
    };
    PropertyWriter.prototype.writeGetAccessor = function (def, flags) {
        var _this = this;
        this.writeCommonHeader(def, flags);
        this.writer.write("get ");
        this.writer.write(def.name);
        this.writer.write("()");
        this.typeWriter.writeWithColon(def.type, "any");
        if (def.isAbstract)
            this.writer.write(";");
        else {
            this.writer.block(function () {
                if (typeof def.onWriteGetBody === "function")
                    def.onWriteGetBody(_this.writer);
            });
        }
    };
    PropertyWriter.prototype.writeSetAccessor = function (def, flags) {
        var _this = this;
        this.writeCommonHeader(def, flags);
        this.writer.write("set ");
        this.writer.write(def.name);
        this.writer.write("(value"); // default to value for now
        this.typeWriter.writeWithColon(def.type, "any");
        this.writer.write(")");
        if (def.isAbstract)
            this.writer.write(";");
        else {
            this.writer.block(function () {
                if (typeof def.onWriteSetBody === "function")
                    def.onWriteSetBody(_this.writer);
            });
        }
    };
    PropertyWriter.prototype.writeSingleCommonHeader = function (def, flags) {
        this.documentationedWriter.write(def);
        this.decoratorsWriter.write(def, flags);
    };
    PropertyWriter.prototype.writeCommonHeader = function (def, flags) {
        if (def instanceof definitions_1.ClassPropertyDefinition || def instanceof definitions_1.ClassStaticPropertyDefinition) {
            this.scopeWriter.writeScope(def.scope);
            if (def instanceof definitions_1.ClassPropertyDefinition)
                this.writer.conditionalWrite(def.isAbstract, "abstract ");
            this.writer.conditionalWrite(def instanceof definitions_1.ClassStaticPropertyDefinition, "static ");
        }
        this.writer.conditionalWrite(!this.willWriteAccessorBody(def) && def.isReadonly, "readonly ");
    };
    return PropertyWriter;
}());
exports.PropertyWriter = PropertyWriter;
 
//# sourceMappingURL=PropertyWriter.js.map