sfmc-handlebars-parser 0.1.0 → 0.1.1
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 +3 -2
- package/package.json +3 -2
- package/src/expression.js +46 -4
- package/src/index.js +15 -7
package/README.md
CHANGED
|
@@ -46,8 +46,9 @@ It accepts every construct `@handlebars/parser@2.2.2` accepts:
|
|
|
46
46
|
- Comments: `{{! }}` and `{{!-- --}}`.
|
|
47
47
|
- Blocks: `{{#x}}…{{/x}}`, inverse `{{^x}}…{{/x}}`, `{{else}}`, `{{else if …}}` chaining,
|
|
48
48
|
block params `as |a b|`, and empty block bodies.
|
|
49
|
-
- Paths: `this`, `./`, `../` (parent depth),
|
|
50
|
-
|
|
49
|
+
- Paths: `this`, `.` (current context), `..` (parent), `./`, `../` (parent depth), a
|
|
50
|
+
`{{...}}` mustache (parent path plus a current-context param), `@data` variables,
|
|
51
|
+
segment literals `[…]`, the legacy `/` separator, and identifiers containing `$` / `_`.
|
|
51
52
|
- Literals: strings (`"…"` / `'…'` with escapes), numbers (including negative),
|
|
52
53
|
`true` / `false` / `null` / `undefined`.
|
|
53
54
|
- Subexpressions `( … )` nested arbitrarily, and hash pairs `key=value`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sfmc-handlebars-parser",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Zero-dependency Handlebars parser producing the @handlebars/parser AST contract for SFMC tooling",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./src/index.d.ts",
|
|
11
|
-
"import": "./src/index.js"
|
|
11
|
+
"import": "./src/index.js",
|
|
12
|
+
"default": "./src/index.js"
|
|
12
13
|
}
|
|
13
14
|
},
|
|
14
15
|
"files": [
|
package/src/expression.js
CHANGED
|
@@ -198,10 +198,13 @@ function readContiguousSegment(reader) {
|
|
|
198
198
|
* Parses a single `PathExpression` starting at the cursor (leading whitespace is skipped first).
|
|
199
199
|
*
|
|
200
200
|
* Grammar handled: an optional `@` data prefix; leading `../` parent hops (each `..` increments
|
|
201
|
-
* `depth`); a
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
201
|
+
* `depth`); a bare `..` parent-context path; a bare `.` current-context path; a `this` context
|
|
202
|
+
* head; `.`- and legacy `/`-separated segments; bare-word `ID` segments (including `$`/`_`/`-`
|
|
203
|
+
* ids); and `[segment literal]` parts whose inner text is kept verbatim. Adjacent extra `.`/`..`
|
|
204
|
+
* tokens that do not continue this path (e.g. the trailing `.` of `{{...}}`) are left unconsumed
|
|
205
|
+
* so the caller can parse them as the next expression. `original` is the raw path text with
|
|
206
|
+
* segment-literal brackets removed and every separator kept. See {@link pathExpression} for how
|
|
207
|
+
* `head`/`tail`/`this` are derived.
|
|
205
208
|
*
|
|
206
209
|
* @param {object} reader - The shared cursor from {@link createReader}.
|
|
207
210
|
* @returns {object} A `PathExpression` node.
|
|
@@ -220,11 +223,14 @@ export function parsePath(reader) {
|
|
|
220
223
|
const parts = [];
|
|
221
224
|
let isSawThisHead = false;
|
|
222
225
|
let segmentCount = 0;
|
|
226
|
+
let isStarted = false;
|
|
227
|
+
let isLastWasSlash = false;
|
|
223
228
|
|
|
224
229
|
// Optional `@` data prefix.
|
|
225
230
|
if (reader.peekType() === TokenType.DATA) {
|
|
226
231
|
isData = true;
|
|
227
232
|
endOffset = reader.next().end;
|
|
233
|
+
isStarted = true;
|
|
228
234
|
}
|
|
229
235
|
|
|
230
236
|
// Walk segments and separators until the path can no longer continue.
|
|
@@ -249,8 +255,42 @@ export function parsePath(reader) {
|
|
|
249
255
|
},
|
|
250
256
|
);
|
|
251
257
|
}
|
|
258
|
+
// A second adjacent `..` that is not a `../` hop (e.g. `{{....}}`) is a new
|
|
259
|
+
// path, not more depth on this one. Leave it for the next expression.
|
|
260
|
+
if (isStarted && !isLastWasSlash) {
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
252
263
|
depth++;
|
|
264
|
+
isLastWasSlash = false;
|
|
265
|
+
isStarted = true;
|
|
266
|
+
endOffset = token.end;
|
|
267
|
+
reader.next();
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (token.value === '.') {
|
|
271
|
+
const next = reader.peek(1);
|
|
272
|
+
const continues =
|
|
273
|
+
isLastWasSlash ||
|
|
274
|
+
(next && next.type === TokenType.SEP && next.value === '/') ||
|
|
275
|
+
(next && SEGMENT_TOKEN_TYPES.has(next.type) && isStarted);
|
|
276
|
+
if (!continues) {
|
|
277
|
+
// Standalone `.` is current-context (`{{.}}`). A trailing `.` after an
|
|
278
|
+
// already-started path (`{{...}}`, `{{foo.}}`) is a new path — leave it.
|
|
279
|
+
if (!isStarted) {
|
|
280
|
+
endOffset = token.end;
|
|
281
|
+
reader.next();
|
|
282
|
+
}
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
isLastWasSlash = false;
|
|
286
|
+
isStarted = true;
|
|
287
|
+
endOffset = token.end;
|
|
288
|
+
reader.next();
|
|
289
|
+
continue;
|
|
253
290
|
}
|
|
291
|
+
// `/` separator (`./`, `../`, `foo/bar`).
|
|
292
|
+
isLastWasSlash = true;
|
|
293
|
+
isStarted = true;
|
|
254
294
|
endOffset = token.end;
|
|
255
295
|
reader.next();
|
|
256
296
|
continue;
|
|
@@ -261,6 +301,8 @@ export function parsePath(reader) {
|
|
|
261
301
|
// contiguous run as one path segment (`parts: ["0x1"]`).
|
|
262
302
|
const segment = readContiguousSegment(reader);
|
|
263
303
|
endOffset = segment.endOffset;
|
|
304
|
+
isLastWasSlash = false;
|
|
305
|
+
isStarted = true;
|
|
264
306
|
if (segmentCount === 0 && token.type === TokenType.ID && segment.value === 'this') {
|
|
265
307
|
// A `this` head is not a part; it only sets the `this` flag (when a tail follows).
|
|
266
308
|
isSawThisHead = true;
|
package/src/index.js
CHANGED
|
@@ -298,8 +298,9 @@ function parseMustache(reader) {
|
|
|
298
298
|
/**
|
|
299
299
|
* Parses the CALL HEAD of a mustache: a literal (`{{"str"}}` / `{{true}}` / `{{5}}` place the
|
|
300
300
|
* literal node in the `path` slot) when the cursor is on a standalone literal, otherwise a
|
|
301
|
-
* `PathExpression`.
|
|
302
|
-
*
|
|
301
|
+
* `PathExpression`. Current-context `.` and parent-context `..` (including `{{...}}`, which is
|
|
302
|
+
* path `..` plus a `.` param) are valid heads. A leading `.` followed by a segment (`{{.foo}}`)
|
|
303
|
+
* is not: upstream throws a Jison parse error anchored at the OPEN token.
|
|
303
304
|
*
|
|
304
305
|
* @param {object} reader - The shared cursor from {@link createReader}.
|
|
305
306
|
* @param {object} open - The consumed OPEN token (for error anchoring).
|
|
@@ -312,13 +313,20 @@ function parseCallHead(reader, open) {
|
|
|
312
313
|
}
|
|
313
314
|
reader.skipWhitespace();
|
|
314
315
|
const token = reader.peek();
|
|
315
|
-
// A path may begin with
|
|
316
|
-
//
|
|
317
|
-
//
|
|
316
|
+
// A path may begin with:
|
|
317
|
+
// - relative/parent context (`./`, `../`) — a `.`/`..` SEP immediately followed by `/`
|
|
318
|
+
// - parent-context `..` (`{{..}}`, and the head of `{{...}}`)
|
|
319
|
+
// - current-context `.` (`{{.}}`) when it is NOT followed by a segment
|
|
320
|
+
// A leading `.` SEP followed by an ID (`{{.foo}}`) is still invalid: upstream throws a
|
|
321
|
+
// Jison "got 'SEP'" error anchored at the OPEN token.
|
|
318
322
|
if (token && token.type === TokenType.SEP) {
|
|
319
323
|
const next = reader.peek(1);
|
|
320
|
-
const
|
|
321
|
-
|
|
324
|
+
const isSlashContinue = next && next.type === TokenType.SEP && next.value === '/';
|
|
325
|
+
const isParentContext = token.value === '..';
|
|
326
|
+
const nextIsSegment =
|
|
327
|
+
next && [TokenType.ID, TokenType.NUMBER, TokenType.SEGMENT].includes(next.type);
|
|
328
|
+
const isCurrentContext = token.value === '.' && !nextIsSegment;
|
|
329
|
+
if (!isSlashContinue && !isParentContext && !isCurrentContext) {
|
|
322
330
|
throw jisonAt(reader.table, open.start, open.end);
|
|
323
331
|
}
|
|
324
332
|
}
|