Skip to content

Commit b776d8d

Browse files
crisbetoalan-agius4
authored andcommitted
fix(@angular-devkit/schematics): handle zero or negative length removals in update buffer
This came up while fixing an issue in a Framework migration (https://github.com/angular/angular/pull/43776/files#diff-365738677fa9dfb9fb2c4fac7c75addb58e5a35182603e722e736cc3f24bbf5bR55). The `UpdateBuffer` stops recording changes if a removal with a zero length occurs. This doesn't appear to be an issue with `UpdateBuffer2`, but considering that the former is still being used and that the fix is trivial, I decided to proceed with it. I've also aligned the behavior between `UpdateBuffer` and `UpdateBuffer2` when dealing with negative removals.
1 parent 6e6b0d8 commit b776d8d

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

packages/angular_devkit/schematics/src/utility/update-buffer.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,15 @@ export class UpdateBuffer extends UpdateBufferBase {
238238
}
239239

240240
protected _slice(start: number): [Chunk, Chunk] {
241-
// If start is longer than the content, use start, otherwise determine exact position in string.
242-
const index = start >= this._originalContent.length ? start : this._getTextPosition(start);
241+
let index: number;
242+
243+
if (start >= this._originalContent.length) {
244+
index = start;
245+
} else if (start < 0) {
246+
index = this._originalContent.length + start;
247+
} else {
248+
index = this._getTextPosition(start);
249+
}
243250

244251
this._assertIndex(index);
245252

@@ -294,8 +301,11 @@ export class UpdateBuffer extends UpdateBufferBase {
294301
}
295302

296303
remove(index: number, length: number) {
297-
const end = index + length;
304+
if (length === 0) {
305+
return;
306+
}
298307

308+
const end = index + length;
299309
const first = this._slice(index)[1];
300310
const last = this._slice(end)[1];
301311

packages/angular_devkit/schematics/src/utility/update-buffer_spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,26 @@ describe('UpdateBuffer', () => {
178178
buffer.remove(0, 6);
179179
expect(buffer.toString()).toBe('ABC');
180180
});
181+
182+
it('is able to insert after a zero-length removal', () => {
183+
const mb = new UpdateBuffer(Buffer.from('123'));
184+
185+
mb.remove(0, 0);
186+
expect(mb.toString()).toBe('123');
187+
188+
mb.insertRight(0, Buffer.from('0'));
189+
expect(mb.toString()).toBe('0123');
190+
});
191+
192+
it('is able to insert after a negative-length removal', () => {
193+
const mb = new UpdateBuffer(Buffer.from('123'));
194+
195+
mb.remove(0, -1);
196+
expect(mb.toString()).toBe('3');
197+
198+
mb.insertRight(0, Buffer.from('0'));
199+
expect(mb.toString()).toBe('03');
200+
});
181201
});
182202

183203
describe('generate', () => {
@@ -355,6 +375,26 @@ describe('UpdateBuffer2', () => {
355375
buffer.remove(0, 6);
356376
expect(buffer.toString()).toBe('ABCDEF');
357377
});
378+
379+
it('is able to insert after a zero-length removal', () => {
380+
const mb = new UpdateBuffer2(Buffer.from('123'));
381+
382+
mb.remove(0, 0);
383+
expect(mb.toString()).toBe('123');
384+
385+
mb.insertRight(0, Buffer.from('0'));
386+
expect(mb.toString()).toBe('0123');
387+
});
388+
389+
it('is able to insert after a negative-length removal', () => {
390+
const mb = new UpdateBuffer2(Buffer.from('123'));
391+
392+
mb.remove(0, -1);
393+
expect(mb.toString()).toBe('3');
394+
395+
mb.insertRight(0, Buffer.from('0'));
396+
expect(mb.toString()).toBe('03');
397+
});
358398
});
359399

360400
describe('generate', () => {

0 commit comments

Comments
 (0)