1 | class RenderContext {
|
2 | constructor (features, type) {
|
3 | this.__indention = 0
|
4 | this.indentionSize = 2
|
5 | Object.assign(this, features)
|
6 | this.renderMethod = type === 'fire' ? 'toFSString' : 'toESString'
|
7 | this.renderStack = []
|
8 | this.buffer = []
|
9 | this.lines = 0
|
10 | this.columns = 0
|
11 | }
|
12 |
|
13 | indent (size, noReturn) {
|
14 | size = size || 0
|
15 | this.__indention += size
|
16 | return noReturn ? '\n' : '\n' + ' '.repeat(this.__indention * this.indentionSize)
|
17 | }
|
18 |
|
19 | join (arr, joiner) {
|
20 | return arr.map((item) => item[this.renderMethod](this)).join(joiner)
|
21 | }
|
22 |
|
23 | each (arr, fn, joiner) {
|
24 | return arr.map((item, index, arr) => fn(item[this.renderMethod](this), item, index, arr)).join(joiner)
|
25 | }
|
26 |
|
27 | registerItem (obj) {
|
28 | if (!obj.location) {
|
29 | return this
|
30 | }
|
31 |
|
32 | const item = {
|
33 | index: this.stackLength,
|
34 | len: 0,
|
35 | srcLocation: obj.location.join(':'),
|
36 | destLocation: `${this.lines}:${this.columns}`
|
37 | }
|
38 |
|
39 | this.renderStack.push(item)
|
40 |
|
41 | return this
|
42 | }
|
43 | }
|
44 |
|
45 | module.exports = RenderContext
|