change.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.dev/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.ReplaceChange = exports.RemoveChange = exports.InsertChange = exports.NoopChange = void 0;
  11. exports.applyToUpdateRecorder = applyToUpdateRecorder;
  12. /**
  13. * An operation that does nothing.
  14. */
  15. class NoopChange {
  16. description = 'No operation.';
  17. order = Infinity;
  18. path = null;
  19. apply() {
  20. return Promise.resolve();
  21. }
  22. }
  23. exports.NoopChange = NoopChange;
  24. /**
  25. * Will add text to the source code.
  26. */
  27. class InsertChange {
  28. path;
  29. pos;
  30. toAdd;
  31. order;
  32. description;
  33. constructor(path, pos, toAdd) {
  34. this.path = path;
  35. this.pos = pos;
  36. this.toAdd = toAdd;
  37. if (pos < 0) {
  38. throw new Error('Negative positions are invalid');
  39. }
  40. this.description = `Inserted ${toAdd} into position ${pos} of ${path}`;
  41. this.order = pos;
  42. }
  43. /**
  44. * This method does not insert spaces if there is none in the original string.
  45. */
  46. apply(host) {
  47. return host.read(this.path).then((content) => {
  48. const prefix = content.substring(0, this.pos);
  49. const suffix = content.substring(this.pos);
  50. return host.write(this.path, `${prefix}${this.toAdd}${suffix}`);
  51. });
  52. }
  53. }
  54. exports.InsertChange = InsertChange;
  55. /**
  56. * Will remove text from the source code.
  57. */
  58. class RemoveChange {
  59. path;
  60. pos;
  61. toRemove;
  62. order;
  63. description;
  64. constructor(path, pos, toRemove) {
  65. this.path = path;
  66. this.pos = pos;
  67. this.toRemove = toRemove;
  68. if (pos < 0) {
  69. throw new Error('Negative positions are invalid');
  70. }
  71. this.description = `Removed ${toRemove} into position ${pos} of ${path}`;
  72. this.order = pos;
  73. }
  74. apply(host) {
  75. return host.read(this.path).then((content) => {
  76. const prefix = content.substring(0, this.pos);
  77. const suffix = content.substring(this.pos + this.toRemove.length);
  78. // TODO: throw error if toRemove doesn't match removed string.
  79. return host.write(this.path, `${prefix}${suffix}`);
  80. });
  81. }
  82. }
  83. exports.RemoveChange = RemoveChange;
  84. /**
  85. * Will replace text from the source code.
  86. */
  87. class ReplaceChange {
  88. path;
  89. pos;
  90. oldText;
  91. newText;
  92. order;
  93. description;
  94. constructor(path, pos, oldText, newText) {
  95. this.path = path;
  96. this.pos = pos;
  97. this.oldText = oldText;
  98. this.newText = newText;
  99. if (pos < 0) {
  100. throw new Error('Negative positions are invalid');
  101. }
  102. this.description = `Replaced ${oldText} into position ${pos} of ${path} with ${newText}`;
  103. this.order = pos;
  104. }
  105. apply(host) {
  106. return host.read(this.path).then((content) => {
  107. const prefix = content.substring(0, this.pos);
  108. const suffix = content.substring(this.pos + this.oldText.length);
  109. const text = content.substring(this.pos, this.pos + this.oldText.length);
  110. if (text !== this.oldText) {
  111. return Promise.reject(new Error(`Invalid replace: "${text}" != "${this.oldText}".`));
  112. }
  113. // TODO: throw error if oldText doesn't match removed string.
  114. return host.write(this.path, `${prefix}${this.newText}${suffix}`);
  115. });
  116. }
  117. }
  118. exports.ReplaceChange = ReplaceChange;
  119. function applyToUpdateRecorder(recorder, changes) {
  120. for (const change of changes) {
  121. if (change instanceof InsertChange) {
  122. recorder.insertLeft(change.pos, change.toAdd);
  123. }
  124. else if (change instanceof RemoveChange) {
  125. recorder.remove(change.order, change.toRemove.length);
  126. }
  127. else if (change instanceof ReplaceChange) {
  128. recorder.remove(change.order, change.oldText.length);
  129. recorder.insertLeft(change.order, change.newText);
  130. }
  131. else if (!(change instanceof NoopChange)) {
  132. throw new Error('Unknown Change type encountered when updating a recorder.');
  133. }
  134. }
  135. }