sfmc-handlebars-parser 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -0
- package/package.json +66 -0
- package/src/errors.js +70 -0
- package/src/expression.js +488 -0
- package/src/index.d.ts +300 -0
- package/src/index.js +996 -0
- package/src/loc.js +109 -0
- package/src/nodes.js +492 -0
- package/src/tokenizer.js +757 -0
package/src/loc.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Source-location utilities for sfmc-handlebars-parser.
|
|
3
|
+
*
|
|
4
|
+
* Converts absolute string offsets (UTF-16 code-unit indices into the source) into
|
|
5
|
+
* `{ line, column }` positions and builds `SourceLocation { start, end }` spans that the
|
|
6
|
+
* parser (W2) attaches to AST nodes.
|
|
7
|
+
*
|
|
8
|
+
* Position contract (matches the upstream `@handlebars/parser` AST and the ESLint/LSP
|
|
9
|
+
* wrappers): `line` is 1-based, `column` is 0-based, and both are counted in UTF-16 code
|
|
10
|
+
* units (i.e. plain JavaScript string indices). A `\r\n` sequence counts as a SINGLE line
|
|
11
|
+
* break; the column resets to 0 on the code unit after the `\n`. A lone `\r` and a lone
|
|
12
|
+
* `\n` each also count as a line break.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Precomputed line-start table for a single source string. Enables O(log n) offset→position
|
|
17
|
+
* lookups without rescanning the source for every token.
|
|
18
|
+
*
|
|
19
|
+
* @typedef {object} LineTable
|
|
20
|
+
* @property {string} source - The source string this table was built from.
|
|
21
|
+
* @property {number[]} lineStarts - Absolute offset (UTF-16 code unit) at which each line begins.
|
|
22
|
+
* `lineStarts[0]` is always `0`; entry `i` is the offset of the first character of line `i + 1`.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A position in the source. `line` is 1-based, `column` is 0-based, both in UTF-16 code units.
|
|
27
|
+
*
|
|
28
|
+
* @typedef {object} Position
|
|
29
|
+
* @property {number} line - 1-based line number.
|
|
30
|
+
* @property {number} column - 0-based column (UTF-16 code units from the line start).
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A source span with inclusive start and exclusive end positions.
|
|
35
|
+
*
|
|
36
|
+
* @typedef {object} SourceLocation
|
|
37
|
+
* @property {Position} start - Start position (offset of the first code unit of the span).
|
|
38
|
+
* @property {Position} end - End position (offset just past the last code unit of the span).
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Builds a {@link LineTable} for the given source. A `\r\n` pair is treated as one line
|
|
43
|
+
* break: the next line begins at the offset following the `\n`. Lone `\r` and lone `\n`
|
|
44
|
+
* each start a new line as well.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} source - The full source string.
|
|
47
|
+
* @returns {LineTable} A table mapping offsets to positions for `source`.
|
|
48
|
+
*/
|
|
49
|
+
export function buildLineTable(source) {
|
|
50
|
+
const lineStarts = [0];
|
|
51
|
+
const length = source.length;
|
|
52
|
+
for (let index = 0; index < length; index++) {
|
|
53
|
+
const code = source.codePointAt(index);
|
|
54
|
+
// \n (LF)
|
|
55
|
+
if (code === 10) {
|
|
56
|
+
lineStarts.push(index + 1);
|
|
57
|
+
} else if (code === 13) {
|
|
58
|
+
// \r (CR); if followed by \n, consume the pair as a single line break.
|
|
59
|
+
if (index + 1 < length && source.codePointAt(index + 1) === 10) {
|
|
60
|
+
index++;
|
|
61
|
+
}
|
|
62
|
+
lineStarts.push(index + 1);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return { source, lineStarts };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Converts an absolute offset into a {@link Position} using a precomputed {@link LineTable}.
|
|
70
|
+
*
|
|
71
|
+
* The offset is clamped into `[0, source.length]`, so passing `source.length` (one past the
|
|
72
|
+
* end) yields a valid end position on the final line.
|
|
73
|
+
*
|
|
74
|
+
* @param {LineTable} table - A table produced by {@link buildLineTable}.
|
|
75
|
+
* @param {number} offset - Absolute offset (UTF-16 code unit) into the source.
|
|
76
|
+
* @returns {Position} The 1-based-line / 0-based-column position for `offset`.
|
|
77
|
+
*/
|
|
78
|
+
export function offsetToPosition(table, offset) {
|
|
79
|
+
const { lineStarts, source } = table;
|
|
80
|
+
const clamped = offset < 0 ? 0 : Math.min(offset, source.length);
|
|
81
|
+
// Binary search for the greatest lineStart that is <= clamped.
|
|
82
|
+
let low = 0;
|
|
83
|
+
let high = lineStarts.length - 1;
|
|
84
|
+
while (low < high) {
|
|
85
|
+
const mid = (low + high + 1) >> 1;
|
|
86
|
+
if (lineStarts[mid] <= clamped) {
|
|
87
|
+
low = mid;
|
|
88
|
+
} else {
|
|
89
|
+
high = mid - 1;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return { line: low + 1, column: clamped - lineStarts[low] };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Builds a {@link SourceLocation} spanning `[startOffset, endOffset)` using a
|
|
97
|
+
* precomputed {@link LineTable}.
|
|
98
|
+
*
|
|
99
|
+
* @param {LineTable} table - A table produced by {@link buildLineTable}.
|
|
100
|
+
* @param {number} startOffset - Absolute offset of the first code unit of the span.
|
|
101
|
+
* @param {number} endOffset - Absolute offset just past the last code unit of the span.
|
|
102
|
+
* @returns {SourceLocation} A `{ start, end }` location.
|
|
103
|
+
*/
|
|
104
|
+
export function locFromOffsets(table, startOffset, endOffset) {
|
|
105
|
+
return {
|
|
106
|
+
start: offsetToPosition(table, startOffset),
|
|
107
|
+
end: offsetToPosition(table, endOffset),
|
|
108
|
+
};
|
|
109
|
+
}
|
package/src/nodes.js
ADDED
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AST node factory for sfmc-handlebars-parser.
|
|
3
|
+
*
|
|
4
|
+
* One builder per upstream `@handlebars/parser@2.2.2` AST node type. Each builder stamps the
|
|
5
|
+
* node's `type`, its type-specific fields, and a `loc` {@link import('./loc.js').SourceLocation}
|
|
6
|
+
* built via {@link locFromOffsets}. The shapes reproduce the frozen golden
|
|
7
|
+
* (`tests/golden/ast.baseline.json`) EXACTLY — field names, `undefined`-valued-key vs omitted
|
|
8
|
+
* rules, and literal `value`/`original` typing — so the parity gate (W3a) diffs to zero.
|
|
9
|
+
*
|
|
10
|
+
* Clean-room note: written from the observed public AST behaviour of `@handlebars/parser`
|
|
11
|
+
* (MIT) and the Handlebars language spec; no upstream source was copied.
|
|
12
|
+
*
|
|
13
|
+
* ── loc contract ─────────────────────────────────────────────────────────────────────────
|
|
14
|
+
* Every node's `loc` is `{ start, end, source }` where `start`/`end` are
|
|
15
|
+
* {@link import('./loc.js').Position} objects and `source` is `undefined` (the key is PRESENT
|
|
16
|
+
* with value `undefined`, matching upstream's `SourceLocation` when `parse` is called without a
|
|
17
|
+
* `srcName`; the golden captures it as `"source": {"__undefined__": true}`). All builders that
|
|
18
|
+
* take offsets go through {@link makeLoc}, so this is uniform.
|
|
19
|
+
*
|
|
20
|
+
* ── undefined-valued keys ────────────────────────────────────────────────────────────────
|
|
21
|
+
* The golden preserves keys whose value is `undefined` (via the capture serialiser's
|
|
22
|
+
* `{ __undefined__: true }` marker). Two expression-level cases matter for W2a:
|
|
23
|
+
* - `hash` on {@link mustacheStatement}/{@link subExpression}/block/partial/decorator is the
|
|
24
|
+
* key `hash` present with value `undefined` when there is no `k=v` pair; a real
|
|
25
|
+
* {@link hash} node is emitted only when >= 1 pair exists. W2a's
|
|
26
|
+
* {@link import('./expression.js').parseParametersAndHash} returns `hash: undefined` in that
|
|
27
|
+
* case, and the caller passes it straight through — assigning `undefined` keeps the key.
|
|
28
|
+
* - Literal `original`/`value` for {@link undefinedLiteral} are genuinely `undefined`.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { locFromOffsets } from './loc.js';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Builds a node `loc` matching the upstream `SourceLocation` shape: `{ start, end, source }`
|
|
35
|
+
* with `source` present and `undefined`. Delegates start/end to {@link locFromOffsets}.
|
|
36
|
+
*
|
|
37
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source being parsed.
|
|
38
|
+
* @param {number} startOffset - Absolute offset (UTF-16 code unit) of the node's first code unit.
|
|
39
|
+
* @param {number} endOffset - Absolute offset just past the node's last code unit.
|
|
40
|
+
* @returns {import('./loc.js').SourceLocation} The `{ start, end, source: undefined }` location.
|
|
41
|
+
*/
|
|
42
|
+
export function makeLoc(table, startOffset, endOffset) {
|
|
43
|
+
const loc = locFromOffsets(table, startOffset, endOffset);
|
|
44
|
+
loc.source = undefined;
|
|
45
|
+
return loc;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Builds a `PathExpression` node.
|
|
50
|
+
*
|
|
51
|
+
* Upstream splits a dotted/slashed path into `parts` (segment strings), derives `head` =
|
|
52
|
+
* `parts[0]` (or `undefined` for a bare `this`, whose `parts` is empty) and `tail` =
|
|
53
|
+
* `parts.slice(1)`, and records `original` = the raw path text with segment-literal brackets
|
|
54
|
+
* removed but every separator kept verbatim (e.g. `@root.[0].x` -> `@root.0.x`, `group/member`
|
|
55
|
+
* -> `group/member`). `data` is `true` for an `@`-prefixed path; `depth` counts leading `../`;
|
|
56
|
+
* `this` is `true` only when the path began with a `this` head that carried further segments
|
|
57
|
+
* (`this.label` -> `this:true`, bare `this` -> `this:false`).
|
|
58
|
+
*
|
|
59
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
60
|
+
* @param {number} startOffset - Offset of the path's first code unit.
|
|
61
|
+
* @param {number} endOffset - Offset just past the path's last code unit.
|
|
62
|
+
* @param {object} fields - The precomputed path fields.
|
|
63
|
+
* @param {boolean} fields.data - Whether the path is `@`-data-prefixed.
|
|
64
|
+
* @param {number} fields.depth - Number of leading `../` parent hops.
|
|
65
|
+
* @param {string[]} fields.parts - The resolved segment strings (brackets removed).
|
|
66
|
+
* @param {string} fields.original - The raw path text with brackets removed, separators kept.
|
|
67
|
+
* @param {boolean} fields.this - Whether the path had an explicit `this` head with a tail.
|
|
68
|
+
* @returns {object} A `PathExpression` node.
|
|
69
|
+
*/
|
|
70
|
+
export function pathExpression(table, startOffset, endOffset, fields) {
|
|
71
|
+
const parts = fields.parts;
|
|
72
|
+
return {
|
|
73
|
+
type: 'PathExpression',
|
|
74
|
+
data: fields.data,
|
|
75
|
+
depth: fields.depth,
|
|
76
|
+
parts,
|
|
77
|
+
head: parts[0],
|
|
78
|
+
tail: parts.slice(1),
|
|
79
|
+
this: fields.this,
|
|
80
|
+
original: fields.original,
|
|
81
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Builds a `StringLiteral` node. `value` is the decoded string; `original` mirrors it (upstream
|
|
87
|
+
* stores the decoded value in both for string literals).
|
|
88
|
+
*
|
|
89
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
90
|
+
* @param {number} startOffset - Offset of the opening quote.
|
|
91
|
+
* @param {number} endOffset - Offset just past the closing quote.
|
|
92
|
+
* @param {string} value - The decoded string value.
|
|
93
|
+
* @returns {object} A `StringLiteral` node.
|
|
94
|
+
*/
|
|
95
|
+
export function stringLiteral(table, startOffset, endOffset, value) {
|
|
96
|
+
return {
|
|
97
|
+
type: 'StringLiteral',
|
|
98
|
+
value,
|
|
99
|
+
original: value,
|
|
100
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Builds a `NumberLiteral` node. Both `value` and `original` are the NUMERIC value (upstream
|
|
106
|
+
* stores a JS number in both, not the raw lexeme).
|
|
107
|
+
*
|
|
108
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
109
|
+
* @param {number} startOffset - Offset of the number's first code unit.
|
|
110
|
+
* @param {number} endOffset - Offset just past the number's last code unit.
|
|
111
|
+
* @param {number} value - The parsed numeric value.
|
|
112
|
+
* @returns {object} A `NumberLiteral` node.
|
|
113
|
+
*/
|
|
114
|
+
export function numberLiteral(table, startOffset, endOffset, value) {
|
|
115
|
+
return {
|
|
116
|
+
type: 'NumberLiteral',
|
|
117
|
+
value,
|
|
118
|
+
original: value,
|
|
119
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Builds a `BooleanLiteral` node. Both `value` and `original` are the boolean.
|
|
125
|
+
*
|
|
126
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
127
|
+
* @param {number} startOffset - Offset of the literal's first code unit.
|
|
128
|
+
* @param {number} endOffset - Offset just past the literal's last code unit.
|
|
129
|
+
* @param {boolean} value - The boolean value.
|
|
130
|
+
* @returns {object} A `BooleanLiteral` node.
|
|
131
|
+
*/
|
|
132
|
+
export function booleanLiteral(table, startOffset, endOffset, value) {
|
|
133
|
+
return {
|
|
134
|
+
type: 'BooleanLiteral',
|
|
135
|
+
value,
|
|
136
|
+
original: value,
|
|
137
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Builds a `NullLiteral` node. Both `value` and `original` are `null`.
|
|
143
|
+
*
|
|
144
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
145
|
+
* @param {number} startOffset - Offset of the literal's first code unit.
|
|
146
|
+
* @param {number} endOffset - Offset just past the literal's last code unit.
|
|
147
|
+
* @returns {object} A `NullLiteral` node.
|
|
148
|
+
*/
|
|
149
|
+
export function nullLiteral(table, startOffset, endOffset) {
|
|
150
|
+
return {
|
|
151
|
+
type: 'NullLiteral',
|
|
152
|
+
value: null,
|
|
153
|
+
original: null,
|
|
154
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Builds an `UndefinedLiteral` node. Both `value` and `original` are `undefined` (present keys).
|
|
160
|
+
*
|
|
161
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
162
|
+
* @param {number} startOffset - Offset of the literal's first code unit.
|
|
163
|
+
* @param {number} endOffset - Offset just past the literal's last code unit.
|
|
164
|
+
* @returns {object} An `UndefinedLiteral` node.
|
|
165
|
+
*/
|
|
166
|
+
export function undefinedLiteral(table, startOffset, endOffset) {
|
|
167
|
+
return {
|
|
168
|
+
type: 'UndefinedLiteral',
|
|
169
|
+
value: undefined,
|
|
170
|
+
original: undefined,
|
|
171
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Builds a `HashPair` node. `key` is the pair name (a plain string); `value` is a literal, path,
|
|
177
|
+
* or subexpression node. The pair `loc` spans from the key's first code unit to the value's last.
|
|
178
|
+
*
|
|
179
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
180
|
+
* @param {number} startOffset - Offset of the key's first code unit.
|
|
181
|
+
* @param {number} endOffset - Offset just past the value's last code unit.
|
|
182
|
+
* @param {string} key - The hash-pair key.
|
|
183
|
+
* @param {object} value - The value node (literal / path / subexpression).
|
|
184
|
+
* @returns {object} A `HashPair` node.
|
|
185
|
+
*/
|
|
186
|
+
export function hashPair(table, startOffset, endOffset, key, value) {
|
|
187
|
+
return {
|
|
188
|
+
type: 'HashPair',
|
|
189
|
+
key,
|
|
190
|
+
value,
|
|
191
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Builds a `Hash` node wrapping >= 1 {@link hashPair}. The `Hash` `loc` spans from the first
|
|
197
|
+
* pair's start to the last pair's end. A `Hash` node is emitted ONLY when at least one pair
|
|
198
|
+
* exists; callers pass `hash: undefined` (a present key) otherwise — see the module header.
|
|
199
|
+
*
|
|
200
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
201
|
+
* @param {number} startOffset - Offset of the first pair's first code unit.
|
|
202
|
+
* @param {number} endOffset - Offset just past the last pair's last code unit.
|
|
203
|
+
* @param {object[]} pairs - The `HashPair` nodes (>= 1).
|
|
204
|
+
* @returns {object} A `Hash` node.
|
|
205
|
+
*/
|
|
206
|
+
export function hash(table, startOffset, endOffset, pairs) {
|
|
207
|
+
return {
|
|
208
|
+
type: 'Hash',
|
|
209
|
+
pairs,
|
|
210
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Builds a `SubExpression` node: `( path params hash )`. `hash` is a real {@link hash} node when
|
|
216
|
+
* >= 1 pair, else the key present with value `undefined`. The `loc` spans the parens inclusive.
|
|
217
|
+
*
|
|
218
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
219
|
+
* @param {number} startOffset - Offset of the opening `(`.
|
|
220
|
+
* @param {number} endOffset - Offset just past the closing `)`.
|
|
221
|
+
* @param {object} path - The call-path node (a `PathExpression`).
|
|
222
|
+
* @param {object[]} parameters - The positional parameter nodes (possibly empty).
|
|
223
|
+
* @param {object|undefined} hashNode - A `Hash` node, or `undefined` when no `k=v` pair.
|
|
224
|
+
* @returns {object} A `SubExpression` node.
|
|
225
|
+
*/
|
|
226
|
+
export function subExpression(table, startOffset, endOffset, path, parameters, hashNode) {
|
|
227
|
+
return {
|
|
228
|
+
type: 'SubExpression',
|
|
229
|
+
path,
|
|
230
|
+
params: parameters,
|
|
231
|
+
hash: hashNode,
|
|
232
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Builds a `ContentStatement` node — a run of literal template text between mustaches. `value`
|
|
238
|
+
* is the rendered text and `original` the raw source slice; upstream's whitespace-control pass
|
|
239
|
+
* can later mutate `value`, so both are stored (the golden denylists `value`, keeping `original`).
|
|
240
|
+
*
|
|
241
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
242
|
+
* @param {number} startOffset - Offset of the content's first code unit.
|
|
243
|
+
* @param {number} endOffset - Offset just past the content's last code unit.
|
|
244
|
+
* @param {string} value - The rendered text value.
|
|
245
|
+
* @param {string} original - The raw source text of the run.
|
|
246
|
+
* @returns {object} A `ContentStatement` node.
|
|
247
|
+
*/
|
|
248
|
+
export function contentStatement(table, startOffset, endOffset, value, original) {
|
|
249
|
+
return {
|
|
250
|
+
type: 'ContentStatement',
|
|
251
|
+
value,
|
|
252
|
+
original,
|
|
253
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Builds a `MustacheStatement` node — `{{ path params hash }}` (or `{{{ … }}}` / `{{& … }}`).
|
|
259
|
+
* `escaped` is `true` for the HTML-escaping `{{` form and `false` for the unescaped
|
|
260
|
+
* `{{{`/`{{&` forms.
|
|
261
|
+
*
|
|
262
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
263
|
+
* @param {number} startOffset - Offset of the opening delimiter.
|
|
264
|
+
* @param {number} endOffset - Offset just past the closing delimiter.
|
|
265
|
+
* @param {object} path - The call head (a `PathExpression` or literal node).
|
|
266
|
+
* @param {object[]} parameters - The positional parameter nodes (possibly empty).
|
|
267
|
+
* @param {object|undefined} hashNode - A `Hash` node, or `undefined` when no `k=v` pair.
|
|
268
|
+
* @param {boolean} escaped - Whether this is the HTML-escaping `{{` form.
|
|
269
|
+
* @param {{open: boolean, close: boolean}} strip - Whitespace-control flags (denylisted in the
|
|
270
|
+
* golden but kept for the plugin).
|
|
271
|
+
* @returns {object} A `MustacheStatement` node.
|
|
272
|
+
*/
|
|
273
|
+
export function mustacheStatement(
|
|
274
|
+
table,
|
|
275
|
+
startOffset,
|
|
276
|
+
endOffset,
|
|
277
|
+
path,
|
|
278
|
+
parameters,
|
|
279
|
+
hashNode,
|
|
280
|
+
escaped,
|
|
281
|
+
strip,
|
|
282
|
+
) {
|
|
283
|
+
return {
|
|
284
|
+
type: 'MustacheStatement',
|
|
285
|
+
path,
|
|
286
|
+
params: parameters,
|
|
287
|
+
hash: hashNode,
|
|
288
|
+
escaped,
|
|
289
|
+
strip,
|
|
290
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Builds a `CommentStatement` node from a `{{! … }}` or `{{!-- … --}}` comment.
|
|
296
|
+
*
|
|
297
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
298
|
+
* @param {number} startOffset - Offset of the opening `{{`.
|
|
299
|
+
* @param {number} endOffset - Offset just past the closing delimiter.
|
|
300
|
+
* @param {string} value - The raw inner comment text.
|
|
301
|
+
* @param {{open: boolean, close: boolean}} strip - Whitespace-control flags (denylisted).
|
|
302
|
+
* @returns {object} A `CommentStatement` node.
|
|
303
|
+
*/
|
|
304
|
+
export function commentStatement(table, startOffset, endOffset, value, strip) {
|
|
305
|
+
return {
|
|
306
|
+
type: 'CommentStatement',
|
|
307
|
+
value,
|
|
308
|
+
strip,
|
|
309
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Builds a `Program` node — a body of statements. The `loc` key is always PRESENT; its value is
|
|
315
|
+
* `undefined` for an empty body (matching upstream, which omits the span for empty programs) and
|
|
316
|
+
* a real span otherwise. The `blockParams` and `chained` keys are included ONLY when the caller
|
|
317
|
+
* passes the corresponding option, reproducing the golden's key-presence rules:
|
|
318
|
+
* - a block's primary program carries `blockParams` (array or `undefined`);
|
|
319
|
+
* - an `{{else}}`/`{{else if}}` inverse program does NOT carry `blockParams`;
|
|
320
|
+
* - a chained `{{else if}}` inverse program carries `chained: true`;
|
|
321
|
+
* - raw-block and partial-block programs carry neither.
|
|
322
|
+
*
|
|
323
|
+
* @param {object[]} body - The statement nodes.
|
|
324
|
+
* @param {import('./loc.js').SourceLocation|undefined} loc - The program span, or `undefined`.
|
|
325
|
+
* @param {object} [options] - Optional key-presence controls.
|
|
326
|
+
* @param {boolean} [options.hasBlockParams] - Include a `blockParams` key.
|
|
327
|
+
* @param {string[]|undefined} [options.blockParams] - The block-param names (or `undefined`).
|
|
328
|
+
* @param {boolean} [options.chained] - Include `chained: true`.
|
|
329
|
+
* @returns {object} A `Program` node.
|
|
330
|
+
*/
|
|
331
|
+
export function program(body, loc, options = {}) {
|
|
332
|
+
const node = { type: 'Program', body };
|
|
333
|
+
if (options.hasBlockParams) {
|
|
334
|
+
node.blockParams = options.blockParams;
|
|
335
|
+
}
|
|
336
|
+
if (options.chained) {
|
|
337
|
+
node.chained = true;
|
|
338
|
+
}
|
|
339
|
+
node.loc = loc;
|
|
340
|
+
return node;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Builds a `BlockStatement` node — `{{#x}} … {{/x}}` (with optional `{{else}}`/`{{^}}` inverse).
|
|
345
|
+
* The `inverse` key is included when `hasInverse` is set (its value may be `undefined` for a
|
|
346
|
+
* plain block that has no inverse but is not a raw block); raw blocks omit the key entirely.
|
|
347
|
+
*
|
|
348
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
349
|
+
* @param {number} startOffset - Offset of the opening `{{#`/`{{^`.
|
|
350
|
+
* @param {number} endOffset - Offset just past the closing `{{/x}}`.
|
|
351
|
+
* @param {object} fields - The block's child nodes and options.
|
|
352
|
+
* @param {object} fields.path - The block-helper path.
|
|
353
|
+
* @param {object[]} fields.params - The positional parameters.
|
|
354
|
+
* @param {object|undefined} fields.hash - A `Hash` node or `undefined`.
|
|
355
|
+
* @param {object|undefined} fields.programNode - The primary `Program` (or `undefined`).
|
|
356
|
+
* @param {boolean} fields.hasInverse - Whether to include the `inverse` key.
|
|
357
|
+
* @param {object|undefined} [fields.inverse] - The inverse `Program` (or `undefined`).
|
|
358
|
+
* @param {{open: boolean, close: boolean}} fields.openStrip - Open-delimiter strip flags.
|
|
359
|
+
* @param {{open: boolean, close: boolean}} fields.inverseStrip - Inverse-delimiter strip flags.
|
|
360
|
+
* @param {{open: boolean, close: boolean}} fields.closeStrip - Close-delimiter strip flags.
|
|
361
|
+
* @returns {object} A `BlockStatement` node.
|
|
362
|
+
*/
|
|
363
|
+
export function blockStatement(table, startOffset, endOffset, fields) {
|
|
364
|
+
const node = {
|
|
365
|
+
type: 'BlockStatement',
|
|
366
|
+
path: fields.path,
|
|
367
|
+
params: fields.params,
|
|
368
|
+
hash: fields.hash,
|
|
369
|
+
program: fields.programNode,
|
|
370
|
+
};
|
|
371
|
+
if (fields.hasInverse) {
|
|
372
|
+
node.inverse = fields.inverse;
|
|
373
|
+
}
|
|
374
|
+
node.openStrip = fields.openStrip;
|
|
375
|
+
node.inverseStrip = fields.inverseStrip;
|
|
376
|
+
node.closeStrip = fields.closeStrip;
|
|
377
|
+
node.loc = makeLoc(table, startOffset, endOffset);
|
|
378
|
+
return node;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Builds a `PartialStatement` node — `{{> name params hash }}`. `name` is a `PathExpression` or
|
|
383
|
+
* a `SubExpression`. `indent` is the leading-whitespace indent captured for standalone partials
|
|
384
|
+
* (empty string for inline ones).
|
|
385
|
+
*
|
|
386
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
387
|
+
* @param {number} startOffset - Offset of the opening `{{>`.
|
|
388
|
+
* @param {number} endOffset - Offset just past the closing delimiter.
|
|
389
|
+
* @param {object} fields - The partial's child nodes and options.
|
|
390
|
+
* @param {object} fields.name - The partial name (`PathExpression`/`SubExpression`).
|
|
391
|
+
* @param {object[]} fields.params - The positional parameters.
|
|
392
|
+
* @param {object|undefined} fields.hash - A `Hash` node or `undefined`.
|
|
393
|
+
* @param {string} fields.indent - The captured indent string.
|
|
394
|
+
* @param {{open: boolean, close: boolean}} fields.strip - Strip flags (denylisted).
|
|
395
|
+
* @returns {object} A `PartialStatement` node.
|
|
396
|
+
*/
|
|
397
|
+
export function partialStatement(table, startOffset, endOffset, fields) {
|
|
398
|
+
return {
|
|
399
|
+
type: 'PartialStatement',
|
|
400
|
+
name: fields.name,
|
|
401
|
+
params: fields.params,
|
|
402
|
+
hash: fields.hash,
|
|
403
|
+
indent: fields.indent,
|
|
404
|
+
strip: fields.strip,
|
|
405
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Builds a `PartialBlockStatement` node — `{{#> name}} … {{/name}}`. Carries a `program` body
|
|
411
|
+
* and no `inverse` key.
|
|
412
|
+
*
|
|
413
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
414
|
+
* @param {number} startOffset - Offset of the opening `{{#>`.
|
|
415
|
+
* @param {number} endOffset - Offset just past the closing `{{/name}}`.
|
|
416
|
+
* @param {object} fields - The partial-block's child nodes and options.
|
|
417
|
+
* @param {object} fields.name - The partial name (`PathExpression`/`SubExpression`).
|
|
418
|
+
* @param {object[]} fields.params - The positional parameters.
|
|
419
|
+
* @param {object|undefined} fields.hash - A `Hash` node or `undefined`.
|
|
420
|
+
* @param {object|undefined} fields.programNode - The `Program` body (or `undefined`).
|
|
421
|
+
* @param {{open: boolean, close: boolean}} fields.openStrip - Open-delimiter strip flags.
|
|
422
|
+
* @param {{open: boolean, close: boolean}} fields.closeStrip - Close-delimiter strip flags.
|
|
423
|
+
* @returns {object} A `PartialBlockStatement` node.
|
|
424
|
+
*/
|
|
425
|
+
export function partialBlockStatement(table, startOffset, endOffset, fields) {
|
|
426
|
+
return {
|
|
427
|
+
type: 'PartialBlockStatement',
|
|
428
|
+
name: fields.name,
|
|
429
|
+
params: fields.params,
|
|
430
|
+
hash: fields.hash,
|
|
431
|
+
program: fields.programNode,
|
|
432
|
+
openStrip: fields.openStrip,
|
|
433
|
+
closeStrip: fields.closeStrip,
|
|
434
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Builds a `Decorator` node — `{{* name params hash }}`. Mirrors {@link mustacheStatement} but
|
|
440
|
+
* with a `Decorator` type; `escaped` is always `true` in upstream's output.
|
|
441
|
+
*
|
|
442
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
443
|
+
* @param {number} startOffset - Offset of the opening `{{*`.
|
|
444
|
+
* @param {number} endOffset - Offset just past the closing delimiter.
|
|
445
|
+
* @param {object} path - The decorator path.
|
|
446
|
+
* @param {object[]} parameters - The positional parameters.
|
|
447
|
+
* @param {object|undefined} hashNode - A `Hash` node or `undefined`.
|
|
448
|
+
* @param {{open: boolean, close: boolean}} strip - Strip flags (denylisted).
|
|
449
|
+
* @returns {object} A `Decorator` node.
|
|
450
|
+
*/
|
|
451
|
+
export function decorator(table, startOffset, endOffset, path, parameters, hashNode, strip) {
|
|
452
|
+
return {
|
|
453
|
+
type: 'Decorator',
|
|
454
|
+
path,
|
|
455
|
+
params: parameters,
|
|
456
|
+
hash: hashNode,
|
|
457
|
+
escaped: true,
|
|
458
|
+
strip,
|
|
459
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Builds a `DecoratorBlock` node — `{{#* name}} … {{/name}}`. Carries a `program` body and an
|
|
465
|
+
* `inverse` key (`undefined` in practice).
|
|
466
|
+
*
|
|
467
|
+
* @param {import('./loc.js').LineTable} table - Line table for the source.
|
|
468
|
+
* @param {number} startOffset - Offset of the opening `{{#*`.
|
|
469
|
+
* @param {number} endOffset - Offset just past the closing `{{/name}}`.
|
|
470
|
+
* @param {object} fields - The decorator-block's child nodes and options.
|
|
471
|
+
* @param {object} fields.path - The decorator path.
|
|
472
|
+
* @param {object[]} fields.params - The positional parameters.
|
|
473
|
+
* @param {object|undefined} fields.hash - A `Hash` node or `undefined`.
|
|
474
|
+
* @param {object|undefined} fields.programNode - The `Program` body (or `undefined`).
|
|
475
|
+
* @param {object|undefined} fields.inverse - The inverse (`undefined`).
|
|
476
|
+
* @param {{open: boolean, close: boolean}} fields.openStrip - Open-delimiter strip flags.
|
|
477
|
+
* @param {{open: boolean, close: boolean}} fields.closeStrip - Close-delimiter strip flags.
|
|
478
|
+
* @returns {object} A `DecoratorBlock` node.
|
|
479
|
+
*/
|
|
480
|
+
export function decoratorBlock(table, startOffset, endOffset, fields) {
|
|
481
|
+
return {
|
|
482
|
+
type: 'DecoratorBlock',
|
|
483
|
+
path: fields.path,
|
|
484
|
+
params: fields.params,
|
|
485
|
+
hash: fields.hash,
|
|
486
|
+
program: fields.programNode,
|
|
487
|
+
inverse: fields.inverse,
|
|
488
|
+
openStrip: fields.openStrip,
|
|
489
|
+
closeStrip: fields.closeStrip,
|
|
490
|
+
loc: makeLoc(table, startOffset, endOffset),
|
|
491
|
+
};
|
|
492
|
+
}
|