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/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# sfmc-handlebars-parser
|
|
2
|
+
|
|
3
|
+
A zero-dependency, clean-room Handlebars parser that produces an
|
|
4
|
+
`@handlebars/parser`-compatible AST for Salesforce Marketing Cloud (SFMC) tooling.
|
|
5
|
+
|
|
6
|
+
It parses Handlebars templates (including Handlebars embedded in HTML) into the same
|
|
7
|
+
node shapes the reference `@handlebars/parser` emits — 1-based line / 0-based column
|
|
8
|
+
locations (in UTF-16 code units), `params` always an array, `hash` present as a key,
|
|
9
|
+
and `program` / `inverse` present as keys — so it can drop into tools that were built
|
|
10
|
+
against that AST.
|
|
11
|
+
|
|
12
|
+
**Clean-room note:** this parser is written from the observed public AST behaviour of
|
|
13
|
+
`@handlebars/parser` (MIT) and the published Handlebars language specification. No source
|
|
14
|
+
was copied from handlebars.js or `@handlebars/parser`. The upstream package is kept only
|
|
15
|
+
as a development dependency, used by the capture tooling to freeze the AST/error goldens
|
|
16
|
+
this parser is verified against.
|
|
17
|
+
|
|
18
|
+
This package is used internally by:
|
|
19
|
+
|
|
20
|
+
- [eslint-plugin-sfmc](https://www.npmjs.com/package/eslint-plugin-sfmc) — Handlebars linting for Marketing Cloud Next
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
npm install sfmc-handlebars-parser
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import { parse } from 'sfmc-handlebars-parser';
|
|
32
|
+
|
|
33
|
+
const ast = parse('{{#each items as |item|}}{{item.name}}{{/each}}');
|
|
34
|
+
|
|
35
|
+
console.log(ast.type); // 'Program'
|
|
36
|
+
console.log(ast.body[0].type); // 'BlockStatement'
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### `parse(input)`
|
|
40
|
+
|
|
41
|
+
Parses a Handlebars (or HTML-with-Handlebars) source string into the root `Program` node.
|
|
42
|
+
|
|
43
|
+
It accepts every construct `@handlebars/parser@2.2.2` accepts:
|
|
44
|
+
|
|
45
|
+
- Mustaches: `{{ }}`, `{{{ }}}` (unescaped), `{{& }}`; `~` whitespace control on any tag.
|
|
46
|
+
- Comments: `{{! }}` and `{{!-- --}}`.
|
|
47
|
+
- Blocks: `{{#x}}…{{/x}}`, inverse `{{^x}}…{{/x}}`, `{{else}}`, `{{else if …}}` chaining,
|
|
48
|
+
block params `as |a b|`, and empty block bodies.
|
|
49
|
+
- Paths: `this`, `./`, `../` (parent depth), `@data` variables, segment literals `[…]`,
|
|
50
|
+
the legacy `/` separator, and identifiers containing `$` / `_`.
|
|
51
|
+
- Literals: strings (`"…"` / `'…'` with escapes), numbers (including negative),
|
|
52
|
+
`true` / `false` / `null` / `undefined`.
|
|
53
|
+
- Subexpressions `( … )` nested arbitrarily, and hash pairs `key=value`.
|
|
54
|
+
- Partials: `{{> p}}`, `{{> (dyn)}}`, `{{> p ctx k=v}}`, `{{#> pb}}…{{/pb}}`.
|
|
55
|
+
- Decorators: `{{* d}}`, `{{#*inline "n"}}…{{/inline}}`.
|
|
56
|
+
- Raw blocks: `{{{{raw}}}}…{{{{/raw}}}}` (inner `{{ }}` are not parsed).
|
|
57
|
+
|
|
58
|
+
On a malformed template, `parse` throws. Parse errors mirror the two upstream error
|
|
59
|
+
classes: a Jison-class error carries `hash.loc` (`{first_line, first_column, last_line,
|
|
60
|
+
last_column}`); an exception-class error carries `lineNumber` / `column`.
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
import { parse } from 'sfmc-handlebars-parser';
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
parse('{{#alpha}}x'); // unclosed block
|
|
67
|
+
} catch (ex) {
|
|
68
|
+
console.log(ex.hash.loc); // { first_line, first_column, last_line, last_column }
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## TypeScript
|
|
73
|
+
|
|
74
|
+
The package ships type declarations. The `parse` signature and the full `AST` node
|
|
75
|
+
namespace are exported, so consumers can import both the value and the types:
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { parse, type AST } from 'sfmc-handlebars-parser';
|
|
79
|
+
|
|
80
|
+
const program: AST.Program = parse('{{greeting}}');
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
MIT © Joern Berkefeld
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sfmc-handlebars-parser",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Zero-dependency Handlebars parser producing the @handlebars/parser AST contract for SFMC tooling",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.d.ts",
|
|
11
|
+
"import": "./src/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"src"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/JoernBerkefeld/sfmc-handlebars-parser.git"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/JoernBerkefeld/sfmc-handlebars-parser/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/JoernBerkefeld/sfmc-handlebars-parser#readme",
|
|
25
|
+
"author": "Joern Berkefeld",
|
|
26
|
+
"keywords": [
|
|
27
|
+
"handlebars",
|
|
28
|
+
"sfmc",
|
|
29
|
+
"salesforce",
|
|
30
|
+
"marketing-cloud",
|
|
31
|
+
"parser",
|
|
32
|
+
"ast"
|
|
33
|
+
],
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"scripts": {
|
|
36
|
+
"test": "npm run test:types && node --test tests/*.test.mjs",
|
|
37
|
+
"test:types": "tsc --noEmit -p tsconfig.types.json",
|
|
38
|
+
"lint": "eslint .",
|
|
39
|
+
"lint:fix": "eslint --fix .",
|
|
40
|
+
"prepare": "husky || true",
|
|
41
|
+
"format": "prettier --write .",
|
|
42
|
+
"format:check": "prettier --check ."
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=22.0.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@eslint/js": "^10.0.1",
|
|
49
|
+
"@handlebars/parser": "^2.2.2",
|
|
50
|
+
"eslint": "^10.8.0",
|
|
51
|
+
"eslint-config-prettier": "^10.1.8",
|
|
52
|
+
"eslint-plugin-jsdoc": "^63.3.3",
|
|
53
|
+
"eslint-plugin-prettier": "^5.5.6",
|
|
54
|
+
"eslint-plugin-unicorn": "^73.0.0",
|
|
55
|
+
"globals": "^17.9.0",
|
|
56
|
+
"husky": "^9.1.7",
|
|
57
|
+
"lint-staged": "^17.3.0",
|
|
58
|
+
"prettier": "^3.9.6",
|
|
59
|
+
"typescript": "^6.0.3"
|
|
60
|
+
},
|
|
61
|
+
"lint-staged": {
|
|
62
|
+
"*.{js,mjs,cjs}": [
|
|
63
|
+
"eslint --fix"
|
|
64
|
+
]
|
|
65
|
+
}
|
|
66
|
+
}
|
package/src/errors.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error classes for sfmc-handlebars-parser.
|
|
3
|
+
*
|
|
4
|
+
* Reproduces the TWO distinct error shapes that `@handlebars/parser@2.2.2` throws, so the
|
|
5
|
+
* `eslint-plugin-sfmc` wrapper (`eslint-plugin-sfmc/src/handlebars-parser.js`) and the LSP
|
|
6
|
+
* derive the same 1:1-vs-real-position diagnostics they do today:
|
|
7
|
+
*
|
|
8
|
+
* - {@link HandlebarsParseError} — the **Jison-class** parse/lex error. Carries
|
|
9
|
+
* `hash.loc = { first_line, first_column, last_line, last_column }` (1-based line, 0-based
|
|
10
|
+
* column) and NO `lineNumber`/`column`. The wrapper reads `hash.loc.first_line` /
|
|
11
|
+
* `first_column` and reports the real position.
|
|
12
|
+
* - {@link HandlebarsException} — the **exception-class** logic error (block-name mismatch,
|
|
13
|
+
* `Invalid path:`, lexical "Unrecognized text"). Carries NO `hash`; instead it MAY set
|
|
14
|
+
* `lineNumber` / `column` (1-based line, 1-based column, matching upstream's `Exception`).
|
|
15
|
+
* When it carries neither (the lexical case), the wrapper falls back to reporting `1:1`.
|
|
16
|
+
*
|
|
17
|
+
* The golden error projection (`tests/golden/errors.baseline.json`) classifies an error as
|
|
18
|
+
* `jison` iff `hash.loc` is present, else `exception` — so the CLASS is what these two subclasses
|
|
19
|
+
* encode. Message wording is not part of the parity gate (only class + position), but each
|
|
20
|
+
* builder emits a descriptive message for the plugin's single-line diagnostic.
|
|
21
|
+
*
|
|
22
|
+
* Clean-room note: written from the observed public error behaviour of `@handlebars/parser`
|
|
23
|
+
* (MIT) and the Handlebars language spec; no upstream source was copied.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A Jison-class parse/lex error. Carries a `hash.loc` span (Jison's `SourceLocation` shape) and
|
|
28
|
+
* deliberately NO `lineNumber`/`column`, so the golden projection classifies it as `jison`.
|
|
29
|
+
*/
|
|
30
|
+
export class HandlebarsParseError extends Error {
|
|
31
|
+
/**
|
|
32
|
+
* @param {string} message - The diagnostic message (wording is not parity-gated).
|
|
33
|
+
* @param {object} loc - The Jison `hash.loc` span.
|
|
34
|
+
* @param {number} loc.first_line - 1-based start line.
|
|
35
|
+
* @param {number} loc.first_column - 0-based start column.
|
|
36
|
+
* @param {number} loc.last_line - 1-based end line.
|
|
37
|
+
* @param {number} loc.last_column - 0-based end column.
|
|
38
|
+
*/
|
|
39
|
+
constructor(message, loc) {
|
|
40
|
+
super(message);
|
|
41
|
+
this.name = 'HandlebarsParseError';
|
|
42
|
+
this.hash = { loc };
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* An exception-class logic/lexical error. Carries NO `hash`; the golden projection classifies it
|
|
48
|
+
* as `exception`. `lineNumber` / `column` are set for the logic errors (block mismatch, invalid
|
|
49
|
+
* path) and left unset for the lexical "Unrecognized text" error (matching upstream, whose
|
|
50
|
+
* lexical exception exposes neither — the wrapper then reports `1:1`).
|
|
51
|
+
*/
|
|
52
|
+
export class HandlebarsException extends Error {
|
|
53
|
+
/**
|
|
54
|
+
* @param {string} message - The diagnostic message (the ` - line:column` suffix, if any, is
|
|
55
|
+
* expected to already be appended by the caller for the logic-error cases).
|
|
56
|
+
* @param {object} [position] - Optional 1-based line / 1-based column of the error.
|
|
57
|
+
* @param {number} [position.lineNumber] - 1-based line number.
|
|
58
|
+
* @param {number} [position.column] - 1-based column number.
|
|
59
|
+
*/
|
|
60
|
+
constructor(message, position) {
|
|
61
|
+
super(message);
|
|
62
|
+
this.name = 'HandlebarsException';
|
|
63
|
+
if (position && position.lineNumber !== undefined) {
|
|
64
|
+
this.lineNumber = position.lineNumber;
|
|
65
|
+
}
|
|
66
|
+
if (position && position.column !== undefined) {
|
|
67
|
+
this.column = position.column;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|