refactor(@angular-devkit/schematics): remove now unused internal UpdateBuffer class

The internal `UpdateBuffer` class is no longer used within the schematics package
and can be removed.
This commit is contained in:
Charles Lyding 2024-02-09 15:23:41 -05:00 committed by Alan Agius
parent 77f6a41a83
commit 860b93a25b
3 changed files with 7 additions and 305 deletions

View File

@ -6,11 +6,17 @@
* found in the LICENSE file at https://angular.io/license
*/
import { BaseException } from '@angular-devkit/core';
import MagicString from 'magic-string';
import { ContentHasMutatedException } from '../exception/exception';
import { IndexOutOfBoundException } from '../utility/update-buffer';
import { FileEntry, UpdateRecorder } from './interface';
export class IndexOutOfBoundException extends BaseException {
constructor(index: number, min: number, max = Infinity) {
super(`Index ${index} outside of range [${min}, ${max}].`);
}
}
export class UpdateRecorderBase implements UpdateRecorder {
protected _path: string;
protected content: MagicString;

View File

@ -1,98 +0,0 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { BaseException } from '@angular-devkit/core';
import MagicString from 'magic-string';
import { TextDecoder } from 'node:util';
export class IndexOutOfBoundException extends BaseException {
constructor(index: number, min: number, max = Infinity) {
super(`Index ${index} outside of range [${min}, ${max}].`);
}
}
/**
* Base class for an update buffer implementation that allows buffers to be inserted to the _right
* or _left, or deleted, while keeping indices to the original buffer.
*/
export abstract class UpdateBufferBase {
constructor(protected _originalContent: Buffer) {}
abstract get length(): number;
abstract get original(): Buffer;
abstract toString(encoding?: string): string;
abstract generate(): Buffer;
abstract insertLeft(index: number, content: Buffer, assert?: boolean): void;
abstract insertRight(index: number, content: Buffer, assert?: boolean): void;
abstract remove(index: number, length: number): void;
/**
* Creates an UpdateBufferBase instance.
*
* @param contentPath The path of the update buffer instance.
* @param originalContent The original content of the update buffer instance.
* @returns An UpdateBufferBase instance.
*/
static create(contentPath: string, originalContent: Buffer): UpdateBufferBase {
try {
// We only support utf8 encoding.
new TextDecoder('utf8', { fatal: true }).decode(originalContent);
return new UpdateBuffer(originalContent);
} catch (e) {
if (e instanceof TypeError) {
throw new Error(`Failed to decode "${contentPath}" as UTF-8 text.`);
}
throw e;
}
}
}
/**
* An utility class that allows buffers to be inserted to the _right or _left, or deleted, while
* keeping indices to the original buffer.
*/
export class UpdateBuffer extends UpdateBufferBase {
protected _mutatableContent: MagicString = new MagicString(this._originalContent.toString());
protected _assertIndex(index: number) {
if (index < 0 || index > this._originalContent.length) {
throw new IndexOutOfBoundException(index, 0, this._originalContent.length);
}
}
get length(): number {
return this._mutatableContent.length();
}
get original(): Buffer {
return this._originalContent;
}
toString(): string {
return this._mutatableContent.toString();
}
generate(): Buffer {
return Buffer.from(this.toString());
}
insertLeft(index: number, content: Buffer): void {
this._assertIndex(index);
this._mutatableContent.appendLeft(index, content.toString());
}
insertRight(index: number, content: Buffer): void {
this._assertIndex(index);
this._mutatableContent.appendRight(index, content.toString());
}
remove(index: number, length: number) {
this._assertIndex(index);
this._mutatableContent.remove(index, index + length);
}
}

View File

@ -1,206 +0,0 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { UpdateBuffer } from './update-buffer';
describe('UpdateBuffer', () => {
describe('inserts', () => {
it('works', () => {
const mb = new UpdateBuffer(Buffer.from('Hello World'));
mb.insertRight(6, Buffer.from('Beautiful '));
expect(mb.toString()).toBe('Hello Beautiful World');
mb.insertRight(6, Buffer.from('Great '));
expect(mb.toString()).toBe('Hello Beautiful Great World');
mb.insertRight(0, Buffer.from('1 '));
expect(mb.toString()).toBe('1 Hello Beautiful Great World');
mb.insertRight(5, Buffer.from('2 '));
expect(mb.toString()).toBe('1 Hello2 Beautiful Great World');
mb.insertRight(8, Buffer.from('3 '));
expect(mb.toString()).toBe('1 Hello2 Beautiful Great Wo3 rld');
mb.insertRight(0, Buffer.from('4 '));
expect(mb.toString()).toBe('1 4 Hello2 Beautiful Great Wo3 rld');
mb.insertRight(8, Buffer.from('5 '));
expect(mb.toString()).toBe('1 4 Hello2 Beautiful Great Wo3 5 rld');
mb.insertRight(1, Buffer.from('a '));
expect(mb.toString()).toBe('1 4 Ha ello2 Beautiful Great Wo3 5 rld');
mb.insertRight(2, Buffer.from('b '));
expect(mb.toString()).toBe('1 4 Ha eb llo2 Beautiful Great Wo3 5 rld');
mb.insertRight(7, Buffer.from('c '));
expect(mb.toString()).toBe('1 4 Ha eb llo2 Beautiful Great Wc o3 5 rld');
mb.insertRight(11, Buffer.from('d '));
expect(mb.toString()).toBe('1 4 Ha eb llo2 Beautiful Great Wc o3 5 rldd ');
});
it('works _left and _right', () => {
const mb = new UpdateBuffer(Buffer.from('Hello World'));
mb.insertRight(6, Buffer.from('Beautiful '));
expect(mb.toString()).toBe('Hello Beautiful World');
mb.insertLeft(6, Buffer.from('Great '));
expect(mb.toString()).toBe('Hello Great Beautiful World');
mb.insertLeft(6, Buffer.from('Awesome '));
expect(mb.toString()).toBe('Hello Great Awesome Beautiful World');
});
it('works with special characters', () => {
const mb = new UpdateBuffer(Buffer.from('Ülaut'));
mb.insertLeft(1, Buffer.from('m'));
expect(mb.toString()).toBe('Ümlaut');
mb.insertLeft(0, Buffer.from('Hello '));
expect(mb.toString()).toBe('Hello Ümlaut');
});
});
describe('delete', () => {
it('works for non-overlapping ranges', () => {
// 111111111122222222223333333333444444
// 0123456789012345678901234567890123456789012345
const mb = new UpdateBuffer(Buffer.from('1 4 Ha eb llo2 Beautiful Great Wc o3 5 rldd '));
mb.remove(43, 2);
expect(mb.toString()).toBe('1 4 Ha eb llo2 Beautiful Great Wc o3 5 rld');
mb.remove(33, 2);
expect(mb.toString()).toBe('1 4 Ha eb llo2 Beautiful Great Wo3 5 rld');
mb.remove(8, 2);
expect(mb.toString()).toBe('1 4 Ha ello2 Beautiful Great Wo3 5 rld');
mb.remove(5, 2);
expect(mb.toString()).toBe('1 4 Hello2 Beautiful Great Wo3 5 rld');
mb.remove(38, 2);
expect(mb.toString()).toBe('1 4 Hello2 Beautiful Great Wo3 rld');
mb.remove(2, 2);
expect(mb.toString()).toBe('1 Hello2 Beautiful Great Wo3 rld');
mb.remove(36, 2);
expect(mb.toString()).toBe('1 Hello2 Beautiful Great World');
mb.remove(13, 2);
expect(mb.toString()).toBe('1 Hello Beautiful Great World');
mb.remove(0, 2);
expect(mb.toString()).toBe('Hello Beautiful Great World');
mb.remove(26, 6);
expect(mb.toString()).toBe('Hello Beautiful World');
mb.remove(16, 10);
expect(mb.toString()).toBe('Hello World');
});
it('handles overlapping ranges', () => {
// 0123456789012
const mb = new UpdateBuffer(Buffer.from('ABCDEFGHIJKLM'));
// Overlapping.
mb.remove(2, 5);
expect(mb.toString()).toBe('ABHIJKLM');
mb.remove(3, 2);
expect(mb.toString()).toBe('ABHIJKLM');
mb.remove(3, 6);
expect(mb.toString()).toBe('ABJKLM');
mb.remove(3, 6);
expect(mb.toString()).toBe('ABJKLM');
mb.remove(10, 1);
expect(mb.toString()).toBe('ABJLM');
mb.remove(1, 11);
expect(mb.toString()).toBe('AM');
});
});
describe('inserts and deletes', () => {
it('works for non-overlapping indices', () => {
// 1
// 01234567890
const mb = new UpdateBuffer(Buffer.from('01234567890'));
mb.insertRight(6, Buffer.from('A'));
expect(mb.toString()).toBe('012345A67890');
mb.insertRight(2, Buffer.from('B'));
expect(mb.toString()).toBe('01B2345A67890');
mb.remove(3, 4);
expect(mb.toString()).toBe('01B27890');
mb.insertRight(4, Buffer.from('C'));
expect(mb.toString()).toBe('01B2C7890');
mb.remove(2, 6);
expect(mb.toString()).toBe('01890');
});
it('works for _left/_right inserts', () => {
// 0123456789
const mb = new UpdateBuffer(Buffer.from('0123456789'));
mb.insertLeft(5, Buffer.from('A'));
expect(mb.toString()).toBe('01234A56789');
mb.insertRight(5, Buffer.from('B'));
expect(mb.toString()).toBe('01234AB56789');
mb.insertRight(10, Buffer.from('C'));
expect(mb.toString()).toBe('01234AB56789C');
mb.remove(5, 5);
expect(mb.toString()).toBe('01234AC');
mb.remove(0, 5);
expect(mb.toString()).toBe('C');
});
it('works for content at start/end of buffer', () => {
const buffer = new UpdateBuffer(Buffer.from('012345'));
buffer.insertLeft(0, Buffer.from('ABC'));
buffer.insertRight(6, Buffer.from('DEF'));
buffer.remove(0, 6);
expect(buffer.toString()).toBe('ABCDEF');
});
it('is able to insert after a zero-length removal', () => {
const mb = new UpdateBuffer(Buffer.from('123'));
mb.remove(0, 0);
expect(mb.toString()).toBe('123');
mb.insertRight(0, Buffer.from('0'));
expect(mb.toString()).toBe('0123');
});
it('is able to insert after a negative-length removal', () => {
const mb = new UpdateBuffer(Buffer.from('123'));
mb.remove(0, -1);
expect(mb.toString()).toBe('3');
mb.insertRight(0, Buffer.from('0'));
expect(mb.toString()).toBe('03');
});
});
describe('generate', () => {
it('works', () => {
// 0123456789
const mb = new UpdateBuffer(Buffer.from('0123456789'));
mb.insertLeft(5, Buffer.from('A'));
expect(mb.toString()).toBe('01234A56789');
mb.remove(5, 5);
expect(mb.toString()).toBe('01234A');
mb.remove(0, 5);
expect(mb.toString()).toBe('');
const buffer = mb.generate();
expect(buffer.toString()).toBe('');
expect(buffer.length).toBe(0);
});
});
});