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/src/index.d.ts ADDED
@@ -0,0 +1,300 @@
1
+ // Type declarations for sfmc-handlebars-parser.
2
+ //
3
+ // Clean-room note: these declarations are written from the OBSERVED runtime AST
4
+ // shapes this parser emits (frozen in `tests/golden/ast.baseline.json`, verified
5
+ // node-for-node against `@handlebars/parser@2.2.2`) and the published Handlebars
6
+ // language spec. No `.d.ts` text was copied from handlebars.js / `@handlebars/parser`.
7
+ // In particular `ContentStatement.original` is typed as `string` here — upstream's
8
+ // own declarations mistype it as `StripFlags`; that mistake is deliberately not copied.
9
+ //
10
+ // The `AST` namespace is intentionally exported in VALUE space (`export { AST }`, NOT
11
+ // `export type { AST }`) so BOTH of these resolve in downstream consumers:
12
+ // import { parse, type AST } from 'sfmc-handlebars-parser';
13
+ // import type { AST } from 'sfmc-handlebars-parser';
14
+ // (the sfmc-language-lsp uses each style in different files).
15
+
16
+ /**
17
+ * Parses a Handlebars template string into an upstream-compatible AST.
18
+ *
19
+ * The returned tree matches the `@handlebars/parser@2.2.2` node shapes: 1-based
20
+ * line / 0-based column locations (UTF-16 code units), `params` always an array,
21
+ * `hash` present as a key (its value `undefined` when there is no `k=v` pair),
22
+ * and `program` / `inverse` present as keys (their value `undefined` when absent).
23
+ *
24
+ * @param input - The Handlebars (or HTML-with-Handlebars) source to parse.
25
+ * @returns The root `Program` node.
26
+ * @throws On a malformed template — a Jison-class parse error (carrying
27
+ * `hash.loc`) or an exception-class error (carrying `lineNumber` / `column`).
28
+ */
29
+ export declare function parse(input: string): AST.Program;
30
+
31
+ declare namespace AST {
32
+ /**
33
+ * A source position. `line` is 1-based; `column` is 0-based; both count
34
+ * UTF-16 code units (matching upstream and the ESLint/LSP conventions).
35
+ */
36
+ interface Position {
37
+ line: number;
38
+ column: number;
39
+ }
40
+
41
+ /**
42
+ * The span of a node in the source. `source` is present with value
43
+ * `undefined` when `parse` is called without a source name.
44
+ */
45
+ interface SourceLocation {
46
+ source?: string;
47
+ start: Position;
48
+ end: Position;
49
+ }
50
+
51
+ /**
52
+ * The whitespace-control (`~`) flags for one delimiter pair. Both flags are
53
+ * always present as booleans (they are not read by SFMC tooling but are
54
+ * emitted for upstream parity).
55
+ */
56
+ interface StripFlags {
57
+ open: boolean;
58
+ close: boolean;
59
+ }
60
+
61
+ /**
62
+ * Common shape shared by every AST node: a discriminating `type` and a
63
+ * `loc`. On a `Program` with an empty `body`, `loc` may be `undefined`.
64
+ */
65
+ interface Node {
66
+ type: string;
67
+ loc: SourceLocation;
68
+ }
69
+
70
+ /**
71
+ * A body of statements. The root program, a block body, an inverse branch,
72
+ * or a partial-/decorator-/raw-block body.
73
+ *
74
+ * `blockParams` is present only when an `as |a b|` fence was written on the
75
+ * owning block (its value is the declared names). `chained` is `true` only on
76
+ * the inverse `Program` that holds an `{{else if}}` chain. `loc` is `undefined`
77
+ * for an empty body (`body: []`).
78
+ */
79
+ interface Program extends Omit<Node, 'loc'> {
80
+ body: Statement[];
81
+ blockParams?: string[];
82
+ chained?: boolean;
83
+ // An empty-body Program carries `loc` as a present key whose value is
84
+ // `undefined` (see the doc comment above and the frozen golden). Overriding
85
+ // the base `Node.loc` here forces consumers to guard with `program?.loc` —
86
+ // reading `program.loc.start` unguarded must NOT typecheck as always-safe.
87
+ // (`Node.loc` stays required for every other node type, which always have a
88
+ // real span; `Omit` is needed because TS forbids widening an inherited
89
+ // required property in an `extends` clause.)
90
+ loc: SourceLocation | undefined;
91
+ }
92
+
93
+ /** A `{{ }}` / `{{{ }}}` / `{{& }}` interpolation. */
94
+ interface MustacheStatement extends Node {
95
+ type: 'MustacheStatement';
96
+ path: Expression;
97
+ params: Expression[];
98
+ hash?: Hash;
99
+ escaped: boolean;
100
+ strip: StripFlags;
101
+ }
102
+
103
+ /**
104
+ * A `{{#name ...}} ... {{else}} ... {{/name}}` block, or the caret shorthand
105
+ * `{{^name}} ... {{/name}}`. `program` and `inverse` are present as keys with
106
+ * value `undefined` when the corresponding branch is absent.
107
+ */
108
+ interface BlockStatement extends Node {
109
+ type: 'BlockStatement';
110
+ path: PathExpression;
111
+ params: Expression[];
112
+ hash?: Hash;
113
+ program?: Program;
114
+ inverse?: Program;
115
+ openStrip: StripFlags;
116
+ inverseStrip: StripFlags;
117
+ closeStrip: StripFlags;
118
+ }
119
+
120
+ /** A run of literal template text between mustaches. */
121
+ interface ContentStatement extends Node {
122
+ type: 'ContentStatement';
123
+ value: string;
124
+ // Upstream mistypes this as `StripFlags`; it is a string. Do not copy that.
125
+ original: string;
126
+ }
127
+
128
+ /** A `{{! ... }}` or `{{!-- ... --}}` comment. */
129
+ interface CommentStatement extends Node {
130
+ type: 'CommentStatement';
131
+ value: string;
132
+ strip: StripFlags;
133
+ }
134
+
135
+ /** A `{{> name ...}}` partial reference. */
136
+ interface PartialStatement extends Node {
137
+ type: 'PartialStatement';
138
+ name: PathExpression | SubExpression | Literal;
139
+ params: Expression[];
140
+ hash?: Hash;
141
+ indent: string;
142
+ strip: StripFlags;
143
+ }
144
+
145
+ /** A `{{#> name}} ... {{/name}}` partial block. */
146
+ interface PartialBlockStatement extends Node {
147
+ type: 'PartialBlockStatement';
148
+ name: PathExpression | SubExpression | Literal;
149
+ params: Expression[];
150
+ hash?: Hash;
151
+ program?: Program;
152
+ openStrip: StripFlags;
153
+ closeStrip: StripFlags;
154
+ }
155
+
156
+ /** A `{{* name ...}}` inline decorator. */
157
+ interface Decorator extends Node {
158
+ type: 'Decorator';
159
+ path: Expression;
160
+ params: Expression[];
161
+ hash?: Hash;
162
+ escaped: boolean;
163
+ strip: StripFlags;
164
+ }
165
+
166
+ /** A `{{#* name}} ... {{/name}}` decorator block. */
167
+ interface DecoratorBlock extends Node {
168
+ type: 'DecoratorBlock';
169
+ path: PathExpression;
170
+ params: Expression[];
171
+ hash?: Hash;
172
+ program?: Program;
173
+ inverse?: Program;
174
+ openStrip: StripFlags;
175
+ closeStrip: StripFlags;
176
+ }
177
+
178
+ /** A parenthesised `( name ... )` helper call used as an argument. */
179
+ interface SubExpression extends Node {
180
+ type: 'SubExpression';
181
+ path: PathExpression;
182
+ params: Expression[];
183
+ hash?: Hash;
184
+ }
185
+
186
+ /**
187
+ * A dotted / bracketed / relative path such as `foo.bar`, `../x`, `@root.y`,
188
+ * `this.[a b]`, or a bare `this`.
189
+ *
190
+ * `parts` are the resolved segment strings (segment-literal brackets removed,
191
+ * inner spaces kept: `foo.[bar baz]` -> `["foo","bar baz"]`). `head` is
192
+ * `parts[0]` (or `undefined` for a bare `this`); `tail` is `parts.slice(1)`.
193
+ * `data` is `true` for an `@`-prefixed path; `depth` counts leading `../`;
194
+ * `this` is `true` when the path began with an explicit `this` head that
195
+ * carried further segments. `original` is the raw path text with brackets
196
+ * removed and every separator kept verbatim.
197
+ *
198
+ * The `parts` element type includes `SubExpression` so a scope walker's
199
+ * `typeof part === 'string'` guard type-checks; the parser emits string parts.
200
+ */
201
+ interface PathExpression extends Node {
202
+ type: 'PathExpression';
203
+ data: boolean;
204
+ depth: number;
205
+ parts: Array<string | SubExpression>;
206
+ head?: string;
207
+ tail: string[];
208
+ this: boolean;
209
+ original: string;
210
+ }
211
+
212
+ /** The hash (named-argument) map of a call, emitted only when >= 1 pair. */
213
+ interface Hash extends Node {
214
+ type: 'Hash';
215
+ pairs: HashPair[];
216
+ }
217
+
218
+ /** One `key=value` named argument. */
219
+ interface HashPair extends Node {
220
+ type: 'HashPair';
221
+ key: string;
222
+ value: Expression;
223
+ }
224
+
225
+ /** A `"..."` / `'...'` string literal (its `value`/`original` are the decoded string). */
226
+ interface StringLiteral extends Node {
227
+ type: 'StringLiteral';
228
+ value: string;
229
+ original: string;
230
+ }
231
+
232
+ /** A numeric literal (its `value`/`original` are the parsed JS number). */
233
+ interface NumberLiteral extends Node {
234
+ type: 'NumberLiteral';
235
+ value: number;
236
+ original: number;
237
+ }
238
+
239
+ /** A `true` / `false` literal. */
240
+ interface BooleanLiteral extends Node {
241
+ type: 'BooleanLiteral';
242
+ value: boolean;
243
+ original: boolean;
244
+ }
245
+
246
+ /** A `null` literal (`value`/`original` are `null`). */
247
+ interface NullLiteral extends Node {
248
+ type: 'NullLiteral';
249
+ value: null;
250
+ original: null;
251
+ }
252
+
253
+ /** An `undefined` literal (`value`/`original` are the key present with value `undefined`). */
254
+ interface UndefinedLiteral extends Node {
255
+ type: 'UndefinedLiteral';
256
+ value: undefined;
257
+ original: undefined;
258
+ }
259
+
260
+ /** Any of the five value literals. */
261
+ type Literal =
262
+ | StringLiteral
263
+ | NumberLiteral
264
+ | BooleanLiteral
265
+ | NullLiteral
266
+ | UndefinedLiteral;
267
+
268
+ /**
269
+ * Anything that can appear in a `path` slot, a positional `param`, or a
270
+ * `HashPair` value: a path, a subexpression, or a literal.
271
+ */
272
+ type Expression = PathExpression | SubExpression | Literal;
273
+
274
+ /** Anything that can appear directly in a `Program.body`. */
275
+ type Statement =
276
+ | MustacheStatement
277
+ | BlockStatement
278
+ | ContentStatement
279
+ | CommentStatement
280
+ | PartialStatement
281
+ | PartialBlockStatement
282
+ | Decorator
283
+ | DecoratorBlock;
284
+
285
+ /**
286
+ * The discriminated union of every node the parser emits. Covers the
287
+ * name-bearing nodes (`PartialStatement` / `PartialBlockStatement` /
288
+ * `Decorator` / `DecoratorBlock`) so a generic AST walker that descends
289
+ * `node.name` type-checks.
290
+ */
291
+ type Nodes =
292
+ | Program
293
+ | Statement
294
+ | SubExpression
295
+ | Expression
296
+ | Hash
297
+ | HashPair;
298
+ }
299
+
300
+ export { AST };