UNPKG

2.03 kBJavaScriptView Raw
1import { __extends, __values } from "tslib";
2import determineIndent from '../utils/determineIndent';
3import NodePatcher from './NodePatcher';
4var ProgramPatcher = /** @class */ (function (_super) {
5 __extends(ProgramPatcher, _super);
6 function ProgramPatcher(patcherContext, body) {
7 var _this = _super.call(this, patcherContext) || this;
8 _this.helpers = new Map();
9 _this._indentString = null;
10 _this.body = body;
11 return _this;
12 }
13 ProgramPatcher.prototype.shouldTrimContentRange = function () {
14 return true;
15 };
16 /**
17 * Register a helper to be reused in several places.
18 *
19 * FIXME: Pick a different name than what is in scope.
20 */
21 ProgramPatcher.prototype.registerHelper = function (name, code) {
22 code = code.trim();
23 if (this.helpers.has(name)) {
24 if (this.helpers.get(name) !== code) {
25 throw new Error("BUG: cannot override helper '" + name + "'");
26 }
27 }
28 else {
29 this.helpers.set(name, code);
30 }
31 return name;
32 };
33 ProgramPatcher.prototype.patchHelpers = function () {
34 var e_1, _a;
35 try {
36 for (var _b = __values(this.helpers.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
37 var helper = _c.value;
38 this.editor.append("\n" + helper);
39 }
40 }
41 catch (e_1_1) { e_1 = { error: e_1_1 }; }
42 finally {
43 try {
44 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
45 }
46 finally { if (e_1) throw e_1.error; }
47 }
48 };
49 /**
50 * Gets the indent string used for each indent in this program.
51 */
52 ProgramPatcher.prototype.getProgramIndentString = function () {
53 if (!this._indentString) {
54 this._indentString = determineIndent(this.context.source);
55 }
56 return this._indentString;
57 };
58 return ProgramPatcher;
59}(NodePatcher));
60export default ProgramPatcher;