js-generator: Don't produce newlines when the buffer is empty
MODIFY
packages/@romejs/js-generator/Buffer.ts
packages/@romejs/js-generator/Buffer.ts
@@ -255,6 +255,30 @@ export default class Buffer {
}
}
+ isEmpty(): boolean {
+ // Fast paths
+ if (this.buf.length === 0 && this._queue.length === 0) {
+ return true;
+ }
+ if (this.buf[0] !== '') {
+ return false;
+ }
+
+ for (const part of this.buf) {
+ if (part !== '') {
+ return false;
+ }
+ }
+
+ for (const [part] of this._queue) {
+ if (part !== '') {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
endsWith(suffix: string): boolean {
// Fast path to avoid iterating over this._queue.
if (suffix.length === 1) {

MODIFY
packages/@romejs/js-generator/Generator.ts
packages/@romejs/js-generator/Generator.ts
@@ -189,14 +189,18 @@ export default class Generator {
}
newline(): void {
- // never allow more than two lines
+ if (this.buf.isEmpty()) {
+ return;
+ }
+
+ // Never allow more than two lines
if (this.endsWith('\n\n')) {
- return undefined;
+ return;
}
//
if (this.endsWith('{\n') || this.endsWith(':\n')) {
- return undefined;
+ return;
}
this.append('\n', true /* queue */);

js-generator: Don't produce newlines when the buffer is empty