ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
55 lines (54 loc) • 2.18 kB
JavaScript
;
var __read = (this && this.__read)/* istanbul ignore next */ || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread)/* istanbul ignore next */ || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
var __values = (this && this.__values)/* istanbul ignore next */ || function (o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m) return m.call(o);
return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
};
Object.defineProperty(exports, "__esModule", { value: true });
function getTextFromFormattingEdits(sourceFile, formattingEdits) {
// reverse the order
formattingEdits = __spread(formattingEdits).sort(function (a, b) { return b.getSpan().getStart() - a.getSpan().getStart(); });
var text = sourceFile.getFullText();
try {
for (var formattingEdits_1 = __values(formattingEdits), formattingEdits_1_1 = formattingEdits_1.next(); !formattingEdits_1_1.done; formattingEdits_1_1 = formattingEdits_1.next()) {
var textChange = formattingEdits_1_1.value;
var span = textChange.getSpan();
text = text.slice(0, span.getStart()) + textChange.getNewText() + text.slice(span.getEnd());
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (formattingEdits_1_1 && !formattingEdits_1_1.done && (_a = formattingEdits_1.return)) _a.call(formattingEdits_1);
}
finally { if (e_1) throw e_1.error; }
}
return text;
var e_1, _a;
}
exports.getTextFromFormattingEdits = getTextFromFormattingEdits;