text: "use strict";
  var ts = require("typescript");
  var utils = require("./utils");
  // from https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#pretty-printer-using-the-ls-formatter
  // Note: this uses ts.formatting which is part of the typescript 1.4 package but is not currently
  //       exposed in the public typescript.d.ts. The typings should be exposed in the next release.
  function format(text, options) {
      "use strict";
      if (options === void 0) { options = utils.createDefaultFormatCodeOptions(); }
      // Parse the source text
      var sourceFile = ts.createSourceFile("file.ts", text, 2 /* Latest */, "0");
      fixupParentReferences(sourceFile);
      // Get the formatting edits on the input sources
      var edits = ts.formatting.formatDocument(sourceFile, getRuleProvider(options), options);
      // Apply the edits on the input code
      return applyEdits(text, edits);
      function getRuleProvider(options) {
          // Share this between multiple formatters using the same options.
          // This represents the bulk of the space the formatter uses.
          var ruleProvider = new ts.formatting.RulesProvider();
          ruleProvider.ensureUpToDate(options);
          return ruleProvider;
      }
      function applyEdits(text, edits) {
          // Apply edits in reverse on the existing text
          var result = text;
          for (var i = edits.length - 1; i >= 0; i--) {
              var change = edits[i];
              var head;
              if (typeof change.span.start === "number") {
                  head = result.slice(0, change.span.start);
              }
              else {
                  // backward compat for typescript-1.4.1
                  head = result.slice(0, change.span.start());
              }
              var tail;
              if (typeof change.span.start === "number") {
                  tail = result.slice(change.span.start + change.span.length);
              }
              else {
                  // backward compat for typescript-1.4.1
                  tail = result.slice(change.span.start() + change.span.length());
              }
              result = head + change.newText + tail;
          }
          return result;
      }
  }
  function fixupParentReferences(sourceFile) {
      "use strict";
      var parent = sourceFile;
      function walk(n) {
          n.parent = parent;
          var saveParent = parent;
          parent = n;
          ts.forEachChild(n, walk);
          parent = saveParent;
      }
      ts.forEachChild(sourceFile, walk);
  }
  module.exports = format;
  