all files / dist/writers/ ModuledWriter.js

92.73% Statements 51/55
86.49% Branches 32/37
100% Functions 7/7
92.73% Lines 51/55
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            15×       15×   15×   15×   15× 15× 30×     30×       30×     15× 28× 16×   28×   24×   19×   14×                          
"use strict";
var definitions = require("./../definitions");
var utils_1 = require("./../utils");
var WriteFlags_1 = require("./../WriteFlags");
// todo: tests
var ModuledWriter = (function () {
    function ModuledWriter(writer, interfaceWriter, classWriter, enumWriter, functionWriter, variableWriter, typeAliasWriter) {
        this.writer = writer;
        this.interfaceWriter = interfaceWriter;
        this.classWriter = classWriter;
        this.enumWriter = enumWriter;
        this.functionWriter = functionWriter;
        this.variableWriter = variableWriter;
        this.typeAliasWriter = typeAliasWriter;
    }
    ModuledWriter.prototype.initialize = function (namespaceWriter) {
        // this was the only way I could think of to handle the circular dependency caused by the
        // circular nature of the problem... maybe there's a better way
        this.namespaceWriter = namespaceWriter;
    };
    ModuledWriter.prototype.write = function (def, flags) {
        Iif (def instanceof definitions.FileDefinition && utils_1.DefinitionUtils.isDefinitionFile(def) || def instanceof definitions.NamespaceDefinition && def.isAmbient) {
            flags = flags | WriteFlags_1.WriteFlags.IsInAmbientContext;
        }
        else {
            flags = flags & ~WriteFlags_1.WriteFlags.IsInAmbientContext;
        }
        this.writeChildren(def, flags);
    };
    ModuledWriter.prototype.writeChildren = function (def, flags) {
        var _this = this;
        // order these by what should be written if order is null
        var allDefinitions = def.typeAliases.concat(def.interfaces, def.enums, def.classes, def.namespaces, def.variables, def.functions);
        allDefinitions.sort(function (a, b) {
            Iif (b.order == null) {
                return -1;
            }
            else Iif (a.order == null) {
                return 1;
            }
            else {
                return a.order - b.order;
            }
        });
        allDefinitions.forEach(function (d, i) {
            if (i > 0 && !(d instanceof definitions.TypeAliasDefinition) && !(d instanceof definitions.VariableDefinition)) {
                _this.writer.newLine();
            }
            if (d instanceof definitions.ClassDefinition) {
                _this.classWriter.write(d, flags);
            }
            else if (d instanceof definitions.InterfaceDefinition) {
                _this.interfaceWriter.write(d, flags);
            }
            else if (d instanceof definitions.FunctionDefinition) {
                _this.functionWriter.write(d, flags);
                _this.writer.newLineIfLastNotNewLine();
            }
            else if (d instanceof definitions.NamespaceDefinition) {
                _this.namespaceWriter.write(d, flags);
            }
            else if (d instanceof definitions.VariableDefinition) {
                if (i > 0 && !(allDefinitions[i - 1] instanceof definitions.VariableDefinition)) {
                    _this.writer.newLine();
                }
                _this.variableWriter.write(d, flags);
                _this.writer.newLine();
            }
            else if (d instanceof definitions.EnumDefinition) {
                _this.enumWriter.write(d, flags);
                _this.writer.newLine();
            }
            else Eif (d instanceof definitions.TypeAliasDefinition) {
                Iif (i > 0 && !(allDefinitions[i - 1] instanceof definitions.TypeAliasDefinition)) {
                    _this.writer.newLine();
                }
                _this.typeAliasWriter.write(d, flags);
                _this.writer.newLine();
            }
        });
    };
    return ModuledWriter;
}());
exports.ModuledWriter = ModuledWriter;
 
//# sourceMappingURL=ModuledWriter.js.map