Skip to content

Commit a9801ac

Browse files
committed
feat: allow usage of nested file buffers
1 parent c33cd60 commit a9801ac

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

src/file_buffer.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class FileBuffer {
2828
/**
2929
* Collected lines
3030
*/
31-
#buffer: string[] = []
31+
#buffer: (string | FileBuffer)[] = []
3232

3333
/**
3434
* Current indentation size. Each call to indent will increment
@@ -66,8 +66,13 @@ export class FileBuffer {
6666
* @param text - The text to write as a new line
6767
* @returns This FileBuffer instance for method chaining
6868
*/
69-
writeLine(text: string): this {
70-
this.#buffer.push(`${' '.repeat(this.#identationSize)}${text}\n`)
69+
writeLine(text: string | FileBuffer): this {
70+
if (typeof text === 'string') {
71+
this.#buffer.push(`${' '.repeat(this.#identationSize)}${text}\n`)
72+
} else {
73+
this.#buffer.push(text)
74+
this.#buffer.push('')
75+
}
7176
return this
7277
}
7378

@@ -77,8 +82,12 @@ export class FileBuffer {
7782
* @param text - The text to write without a newline
7883
* @returns This FileBuffer instance for method chaining
7984
*/
80-
write(text: string): this {
81-
this.#buffer.push(`${' '.repeat(this.#identationSize)}${text}`)
85+
write(text: string | FileBuffer): this {
86+
if (typeof text === 'string') {
87+
this.#buffer.push(`${' '.repeat(this.#identationSize)}${text}`)
88+
} else {
89+
this.#buffer.push(text)
90+
}
8291
return this
8392
}
8493

@@ -106,6 +115,10 @@ export class FileBuffer {
106115
return this
107116
}
108117

118+
toString() {
119+
return this.flush()
120+
}
121+
109122
/**
110123
* Return template as a string, joining all buffer lines
111124
*

tests/file_buffer.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) AdonisJS
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { test } from '@japa/runner'
11+
import { FileBuffer } from '../src/file_buffer.ts'
12+
13+
test.group('File buffer', () => {
14+
test('use nested file buffers', ({ assert }) => {
15+
const buffer = new FileBuffer()
16+
const importsBuffer = buffer.create()
17+
18+
buffer.write(importsBuffer)
19+
buffer.write('export namespace {').indent()
20+
buffer.dedent().write('}')
21+
22+
importsBuffer.write(`import foo from './foo.ts'`)
23+
importsBuffer.write(`import bar from './bar.ts'`)
24+
25+
assert.snapshot(buffer.flush()).matchInline(`
26+
"import foo from './foo.ts'
27+
import bar from './bar.ts'
28+
export namespace {
29+
}"
30+
`)
31+
})
32+
33+
test('use nested file buffers with a line break', ({ assert }) => {
34+
const buffer = new FileBuffer()
35+
const importsBuffer = buffer.create()
36+
37+
buffer.writeLine(importsBuffer)
38+
buffer.write('export namespace {').indent()
39+
buffer.dedent().write('}')
40+
41+
importsBuffer.write(`import foo from './foo.ts'`)
42+
importsBuffer.write(`import bar from './bar.ts'`)
43+
44+
assert.snapshot(buffer.flush()).matchInline(`
45+
"import foo from './foo.ts'
46+
import bar from './bar.ts'
47+
48+
export namespace {
49+
}"
50+
`)
51+
})
52+
})

0 commit comments

Comments
 (0)