@promptctl/go-template-js 0.4.0 → 0.6.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 +50 -16
- package/dist/index.d.ts +46 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +131 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,18 +1,46 @@
|
|
|
1
|
-
# go-template-js
|
|
1
|
+
# @promptctl/go-template-js
|
|
2
2
|
|
|
3
3
|
Go template syntax + Sprig subset, generic over output type, in TypeScript.
|
|
4
4
|
|
|
5
|
-
-
|
|
6
|
-
-
|
|
7
|
-
-
|
|
5
|
+
[](https://www.npmjs.com/package/@promptctl/go-template-js)
|
|
6
|
+
[](https://github.com/promptctl/go-template-js/actions/workflows/ci.yml)
|
|
7
|
+
[](https://nodejs.org)
|
|
8
|
+
[](./LICENSE)
|
|
9
|
+
|
|
10
|
+
## Highlights
|
|
11
|
+
|
|
12
|
+
- **Same syntax as Go's `text/template`.** Templates that parse there parse here.
|
|
13
|
+
- **Generic over the output type `T`.** Render to strings, structured fragments, or anything else; the engine emits `T[]`.
|
|
14
|
+
- **No silent flattening.** Typed values never become strings without an explicit conversion — slots that should accept text declare so, and mismatches throw `TypeMismatchError`.
|
|
15
|
+
- **Pure ESM, side-effect-free, single runtime dep.** Tree-shakes cleanly; only [`@noble/hashes`](https://github.com/paulmillr/noble-hashes) at runtime (for `sprigHash()`).
|
|
16
|
+
|
|
17
|
+
## Table of contents
|
|
18
|
+
|
|
19
|
+
- [Highlights](#highlights)
|
|
20
|
+
- [Install](#install)
|
|
21
|
+
- [Quick start](#quick-start)
|
|
22
|
+
- [Custom output types](#custom-output-types)
|
|
23
|
+
- [`ArgType` reference](#argtype-reference)
|
|
24
|
+
- [Syntax and built-ins](#syntax-and-built-ins)
|
|
25
|
+
- [Sprig subset](#sprig-subset)
|
|
26
|
+
- [Composing funcs from multiple sources](#composing-funcs-from-multiple-sources)
|
|
27
|
+
- [Divergences from Go](#divergences-from-go)
|
|
28
|
+
- [Errors](#errors)
|
|
29
|
+
- [Versioning policy](#versioning-policy)
|
|
30
|
+
- [Development](#development)
|
|
31
|
+
- [License](#license)
|
|
8
32
|
|
|
9
33
|
## Install
|
|
10
34
|
|
|
11
35
|
```bash
|
|
12
36
|
pnpm add @promptctl/go-template-js
|
|
37
|
+
# or: npm install @promptctl/go-template-js
|
|
38
|
+
# or: yarn add @promptctl/go-template-js
|
|
13
39
|
```
|
|
14
40
|
|
|
15
|
-
|
|
41
|
+
Requires Node **≥ 20.19.0** (per `engines.node`). The package is ESM-only.
|
|
42
|
+
|
|
43
|
+
## Quick start
|
|
16
44
|
|
|
17
45
|
```ts
|
|
18
46
|
import { createEngine } from "@promptctl/go-template-js";
|
|
@@ -31,7 +59,7 @@ const greet = engine.compile("hi {{ .name }}");
|
|
|
31
59
|
greet({ name: "ada" }).join(""); // "hi ada"
|
|
32
60
|
```
|
|
33
61
|
|
|
34
|
-
##
|
|
62
|
+
## Custom output types
|
|
35
63
|
|
|
36
64
|
The engine is parameterised over its output fragment type `T`. Consumers pick a fragment shape (e.g. styled text, AST nodes, layout primitives) and the engine emits a `T[]` that integrates into their downstream pipeline:
|
|
37
65
|
|
|
@@ -82,7 +110,7 @@ const engine = createEngine<Frag>({
|
|
|
82
110
|
|
|
83
111
|
Use `"liftable"` for any consumer-defined function whose slot should accept either a `T` value or a string literal. The body, by contract, only ever receives `T` — the gate owns the lift, so the body has no string-vs-`T` discrimination to do.
|
|
84
112
|
|
|
85
|
-
|
|
113
|
+
## `ArgType` reference
|
|
86
114
|
|
|
87
115
|
Every entry in `argTypes` is one of:
|
|
88
116
|
|
|
@@ -111,13 +139,13 @@ Every entry in `argTypes` is one of:
|
|
|
111
139
|
|
|
112
140
|
The legacy `"any"` kind was removed in epic `template-laws-3gt`. Slots that genuinely accept anything now declare their *intent* (`"truthy"` / `"reflective"` / `"value"` / `"serializable"`); slots that did not are pinned to their precise kind so the gate enforces the constraint instead of leaking it into func bodies.
|
|
113
141
|
|
|
114
|
-
|
|
142
|
+
### Variadic patterns
|
|
115
143
|
|
|
116
144
|
For variadic funcs, declare the trailing slot's type once — every excess argument is validated against it.
|
|
117
145
|
|
|
118
146
|
For funcs whose variadic tail *cycles* (e.g. `dict "k1" v1 "k2" v2 …`), set `argTypePattern: "alternating"` on the registration. The slot for arg index `i` becomes `argTypes[i % argTypes.length]`, so `dict` declares `argTypes: ["string", "value"]` and the gate enforces "string at even index, anything at odd index" without per-key revalidation in the body.
|
|
119
147
|
|
|
120
|
-
## Syntax
|
|
148
|
+
## Syntax and built-ins
|
|
121
149
|
|
|
122
150
|
Template syntax is Go template syntax. Read the canonical spec:
|
|
123
151
|
https://pkg.go.dev/text/template
|
|
@@ -141,7 +169,7 @@ All of Go template's runtime built-ins:
|
|
|
141
169
|
|
|
142
170
|
`and`/`or` short-circuit (the engine passes thunks for those). `printf` supports `%s`, `%d`, `%v`, `%q`, `%f` (precision-aware), `%t`, `%x`, plus width and `-` left-align flags. `html` flattens its arguments like `print` (via the engine's `toString`) and then escapes the six HTML-significant characters (`\0` → `�`, `"` → `"`, `'` → `'`, `&` → `&`, `<` → `<`, `>` → `>`) — matching Go's `text/template.HTMLEscaper` byte-for-byte. `js` shares the same flatten pipeline and emits JavaScript-literal-safe output: `\` → `\\`, `'` → `\'`, `"` → `\"`, `<` → `\u003C`, `>` → `\u003E`, `&` → `\u0026`, `=` → `\u003D`, ASCII control chars (< 0x20) and non-printable runes (incl. U+2028/U+2029) → `\u%04X` uppercase — matching Go's `text/template.JSEscaper`. `urlquery` shares the same flatten pipeline and percent-encodes the UTF-8 byte stream per Go's `net/url.QueryEscape`: the unreserved set `[A-Za-z0-9-._~]` passes through, space becomes `+`, every other byte becomes `%XX` uppercase hex (so a literal `+` round-trips as `%2B`, and `α` encodes to `%CE%B1`).
|
|
143
171
|
|
|
144
|
-
|
|
172
|
+
## Sprig subset
|
|
145
173
|
|
|
146
174
|
Imported from `@promptctl/go-template-js` as category-scoped `FuncMap` factories:
|
|
147
175
|
|
|
@@ -194,7 +222,9 @@ The engine matches Go's `text/template` semantics aggressively. The remaining di
|
|
|
194
222
|
- **Type mismatches at function boundaries are runtime errors, not compile errors.** By design: the engine does not know your `T` shape statically, so it surfaces the architectural commitment when the bad flow happens. See `TypeMismatchError`.
|
|
195
223
|
- **printf verbs** beyond `%s %d %v %q %f %t %x` are rendered as `%!<verb>(<type>=<value>)`. If you need more, register your own formatter via the `funcs` registry.
|
|
196
224
|
|
|
197
|
-
Notable parity statements
|
|
225
|
+
### Notable parity statements
|
|
226
|
+
|
|
227
|
+
Places where users sometimes expect divergence and there isn't one:
|
|
198
228
|
|
|
199
229
|
- A nil/undefined pipeline emits the literal `<no value>` string, matching Go's `text/template`.
|
|
200
230
|
- **Missing field/map-key access** follows Go's `Option("missingkey=...")`. Defaults to `"default"` (silent `<no value>`); set `missingKey: "error"` in `EngineConfig` to throw `MissingFieldError` instead. The `"zero"` value is accepted; see the divergence note above.
|
|
@@ -243,10 +273,14 @@ Reaching into `dist/` subpaths or `src/` deep imports is unsupported.
|
|
|
243
273
|
|
|
244
274
|
```bash
|
|
245
275
|
pnpm install
|
|
246
|
-
pnpm build
|
|
247
|
-
pnpm test
|
|
248
|
-
pnpm lint
|
|
249
|
-
pnpm typecheck
|
|
276
|
+
pnpm build # tsdown → dist/
|
|
277
|
+
pnpm test # vitest
|
|
278
|
+
pnpm lint # biome check
|
|
279
|
+
pnpm typecheck # tsc --noEmit
|
|
250
280
|
```
|
|
251
281
|
|
|
252
|
-
The roadmap and active work live in the project's `lit` issue tracker (run `lit ready` to see the queue).
|
|
282
|
+
Bug reports and feature requests go to [GitHub Issues](https://github.com/promptctl/go-template-js/issues). The roadmap and active work live in the project's `lit` issue tracker (run `lit ready` to see the queue).
|
|
283
|
+
|
|
284
|
+
## License
|
|
285
|
+
|
|
286
|
+
[MIT](./LICENSE) © Brandon Fryslie
|
package/dist/index.d.ts
CHANGED
|
@@ -309,11 +309,56 @@ interface EngineConfig<T> {
|
|
|
309
309
|
/** Optional registry of named functions usable in pipelines. */
|
|
310
310
|
readonly funcs?: FuncMap;
|
|
311
311
|
}
|
|
312
|
+
interface ReferencedCall {
|
|
313
|
+
/** The function name at the head of the command. */
|
|
314
|
+
readonly name: string;
|
|
315
|
+
/**
|
|
316
|
+
* The positional arguments after the head. A literal string argument is its
|
|
317
|
+
* decoded value; any non-string-literal argument is `null` (its value is only
|
|
318
|
+
* known at evaluation time). Indices match the call site, so `args[0]` is the
|
|
319
|
+
* first argument regardless of the kinds in between.
|
|
320
|
+
*/
|
|
321
|
+
readonly args: readonly (string | null)[];
|
|
322
|
+
}
|
|
312
323
|
declare class Template<T> {
|
|
313
324
|
readonly source: string;
|
|
314
325
|
private readonly _evaluate;
|
|
326
|
+
private readonly _referencedFunctions;
|
|
327
|
+
private readonly _referencedCalls;
|
|
315
328
|
private constructor();
|
|
316
329
|
evaluate(scope: unknown): T[];
|
|
330
|
+
/**
|
|
331
|
+
* The FuncMap function names this template references — every identifier that
|
|
332
|
+
* names a function anywhere in the template body or its `{{ define }}` blocks.
|
|
333
|
+
*
|
|
334
|
+
* This is a STATIC fact derived from the parsed AST, not an execution trace:
|
|
335
|
+
* a name in the set is a function the template *can* invoke; a name absent
|
|
336
|
+
* from the set is one it provably never invokes. Use it to ask "does this
|
|
337
|
+
* template use helper X?" without evaluating it (e.g. to discover which
|
|
338
|
+
* templates depend on a feature func) — robust where a source-text scan is
|
|
339
|
+
* not, because it sees through whitespace, pipelines, and field/string
|
|
340
|
+
* lookalikes.
|
|
341
|
+
*
|
|
342
|
+
* Built-in operators registered for every engine (`and`, `eq`, `index`,
|
|
343
|
+
* `printf`, …) are reported the same as consumer funcs — a referenced name is
|
|
344
|
+
* a referenced name regardless of who registered it.
|
|
345
|
+
*/
|
|
346
|
+
referencedFunctions(): ReadonlySet<string>;
|
|
347
|
+
/**
|
|
348
|
+
* Every command-head call in the template body and its `{{ define }}` blocks,
|
|
349
|
+
* in preorder, each paired with its positional arguments projected to literal
|
|
350
|
+
* strings (a non-string-literal argument is reported as `null`, preserving
|
|
351
|
+
* argument positions).
|
|
352
|
+
*
|
|
353
|
+
* Like {@link referencedFunctions} this is a STATIC fact from the parsed AST,
|
|
354
|
+
* not an execution trace. Use it when "is X called?" is not enough and you
|
|
355
|
+
* need the literal arguments a call was written with — e.g. to discover that a
|
|
356
|
+
* template contains `{{ menu "applyTheme" "themePage" }}` and read its first
|
|
357
|
+
* argument without evaluating the template. Calls whose head is not a bare
|
|
358
|
+
* function identifier (e.g. a field invoked via `call`) are not reported here;
|
|
359
|
+
* their names still appear in {@link referencedFunctions}.
|
|
360
|
+
*/
|
|
361
|
+
referencedCalls(): readonly ReferencedCall[];
|
|
317
362
|
}
|
|
318
363
|
declare class Engine<T> {
|
|
319
364
|
private readonly fromString;
|
|
@@ -426,5 +471,5 @@ declare function sprigStrings(): FuncMap;
|
|
|
426
471
|
//#region src/sprig/types/index.d.ts
|
|
427
472
|
declare function sprigTypes(): FuncMap;
|
|
428
473
|
//#endregion
|
|
429
|
-
export { type ArgType, type Delims, Engine, type EngineConfig, type ErrorKind, EvalError, FailError, type FuncMap, FuncNotFoundError, MissingFieldError, type MissingKeyOption, ParseError, Template, TemplateError, type TemplateFunc, TypeMismatchError, createEngine, sprigConversions, sprigDatetime, sprigDefaults, sprigDicts, sprigFlow, sprigHash, sprigLists, sprigMath, sprigRandom, sprigRegex, sprigSemver, sprigStrings, sprigTypes };
|
|
474
|
+
export { type ArgType, type Delims, Engine, type EngineConfig, type ErrorKind, EvalError, FailError, type FuncMap, FuncNotFoundError, MissingFieldError, type MissingKeyOption, ParseError, type ReferencedCall, Template, TemplateError, type TemplateFunc, TypeMismatchError, createEngine, sprigConversions, sprigDatetime, sprigDefaults, sprigDicts, sprigFlow, sprigHash, sprigLists, sprigMath, sprigRandom, sprigRegex, sprigSemver, sprigStrings, sprigTypes };
|
|
430
475
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/parser/pos.ts","../src/errors.ts","../src/parser/lexer.ts","../src/evaluator/evaluator.ts","../src/sprig/conversions/index.ts","../src/sprig/datetime/_clock.ts","../src/sprig/datetime/index.ts","../src/sprig/defaults/index.ts","../src/sprig/dicts/index.ts","../src/sprig/flow/index.ts","../src/sprig/hash/index.ts","../src/sprig/lists/index.ts","../src/sprig/math/index.ts","../src/sprig/random/index.ts","../src/sprig/regex/index.ts","../src/sprig/semver/index.ts","../src/sprig/strings/index.ts","../src/sprig/types/index.ts"],"mappings":";;AAUA;;;;;;;;;UAAiB,GAAA;EAAA,SACN,IAAA;EAAA,SACA,MAAA;EAAA,SACA,MAAA;AAAA;;;KCSC,SAAA;AAAA,UAQK,oBAAA;EAAA,SACN,MAAA;AAAA;AAAA,cAOE,aAAA,SAAsB,KAAA;EAAA,SACf,IAAA;EAAA,SACT,IAAA,EAAM,SAAA;EAAA,SACN,GAAA,EAAK,GAAA;EAAA,SACL,MAAA;cAEG,OAAA,UAAiB,GAAA,EAAK,GAAA,EAAK,GAAA,GAAK,oBAAA;EANX;EAAA,IAa7B,aAAA,CAAA;EAIK,QAAA,CAAA;AAAA;AAAA,UAWM,iBAAA,SAA0B,oBAAA;EAAA,SAChC,QAAA;EAAA,SACA,KAAA;AAAA;AAAA,cAGE,UAAA,SAAmB,aAAA;EAAA,SACZ,IAAA;EAAA,SACA,IAAA,EAAM,SAAA;EAAA,SACf,QAAA;EAAA,SACA,KAAA;cAEG,OAAA,UAAiB,GAAA,EAAK,GAAA,EAAK,GAAA,GAAK,iBAAA;AAAA;AAAA,cAWjC,SAAA,SAAkB,aAAA;EAAA,SACX,IAAA;EAAA,SACA,IAAA,EAAM,SAAA;AAAA;;AAxB1B;;;;cAgCa,iBAAA,SAA0B,SAAA;EAAA,SACnB,IAAA;EAAA,SACA,IAAA,EAAM,SAAA;EAAA,SACf,QAAA;EAAA,SACA,WAAA;cAGP,QAAA,UACA,GAAA,EAAK,GAAA,EACL,GAAA,GAAK,oBAAA;IAAyB,SAAA;EAAA;AAAA;AAAA,cAUrB,iBAAA,SAA0B,SAAA;EAAA,SACnB,IAAA;EAAA,SACA,IAAA,EAAM,SAAA;EAAA,SACf,QAAA;EAjDkC;EAAA,SAmDlC,QAAA;EAAA,SACA,QAAA;EAAA,SACA,eAAA;cAGP,QAAA,UACA,QAAA,UACA,QAAA,UACA,eAAA,UACA,GAAA,EAAK,GAAA,EACL,GAAA,GAAK,oBAAA;AAAA;AAAA,cAgBI,iBAAA,SAA0B,SAAA;EAAA,SACnB,IAAA;EAAA,SACA,IAAA,EAAM,SAAA;EAAA,SACf,IAAA;cAEG,IAAA,qBAAyB,GAAA,EAAK,GAAA,EAAK,GAAA,GAAK,oBAAA;AAAA;;;;AAjEtD;;;;;;cAoFa,SAAA,SAAkB,SAAA;EAAA,SACX,IAAA;EAAA,SACA,IAAA,EAAM,SAAA;cAGtB,OAAA,UACA,GAAA,GAAK,GAAA,EACL,GAAA,GAAK,oBAAA;AAAA;;;;;;;;;;;;;;UCnFQ,MAAA;EAAA,SACN,IAAA;EAAA,SACA,KAAA;AAAA;;;;;;;AD5DX;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA;;;;;;;;;AAKA;;;;;;;;;;;;;;;;;;;;;;;AAiBA;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/parser/pos.ts","../src/errors.ts","../src/parser/lexer.ts","../src/evaluator/evaluator.ts","../src/sprig/conversions/index.ts","../src/sprig/datetime/_clock.ts","../src/sprig/datetime/index.ts","../src/sprig/defaults/index.ts","../src/sprig/dicts/index.ts","../src/sprig/flow/index.ts","../src/sprig/hash/index.ts","../src/sprig/lists/index.ts","../src/sprig/math/index.ts","../src/sprig/random/index.ts","../src/sprig/regex/index.ts","../src/sprig/semver/index.ts","../src/sprig/strings/index.ts","../src/spr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,GAAiB,OAAA;;;iBCDjB,UAAA,CAAA,GAAc,OAAA;;;iBCPd,SAAA,CAAA,GAAa,OAAA;;;;iBCeb,SAAA,CAAA,GAAa,OAAA;;;iBCqBb,UAAA,CAAA,GAAc,OAAA;;;iBCEd,SAAA,CAAA,GAAa,OAAA;;;;;AXnB7B;;;;iBYLgB,WAAA,CAAY,MAAA,kBAAqC,OAAA;;;iBCNjD,UAAA,CAAA,GAAc,OAAA;;;iBCPd,WAAA,CAAA,GAAe,OAAA;;;iBCyEf,YAAA,CAAA,GAAgB,OAAA;;;iBCxEhB,UAAA,CAAA,GAAc,OAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1161,6 +1161,66 @@ function decodeRuneLiteral(raw, pos, source) {
|
|
|
1161
1161
|
return decoded.codePointAt(0) ?? 0;
|
|
1162
1162
|
}
|
|
1163
1163
|
//#endregion
|
|
1164
|
+
//#region src/parser/walk.ts
|
|
1165
|
+
/**
|
|
1166
|
+
* AST traversal helpers.
|
|
1167
|
+
*
|
|
1168
|
+
* [LAW:dataflow-not-control-flow] `children()` is one exhaustive switch on
|
|
1169
|
+
* the node's `type` discriminator that always returns a (possibly empty)
|
|
1170
|
+
* array of children. Walkers do not skip operations — they always call
|
|
1171
|
+
* `visit(n)` on every reachable node, then recurse into whatever children
|
|
1172
|
+
* the data-driven `children()` exposes.
|
|
1173
|
+
*
|
|
1174
|
+
* [LAW:single-enforcer] All AST traversal goes through `walk` /
|
|
1175
|
+
* `children`. Any caller who reinvents traversal will diverge — fix the
|
|
1176
|
+
* helper, not the callsite.
|
|
1177
|
+
*/
|
|
1178
|
+
/**
|
|
1179
|
+
* Walk an AST in preorder. The visitor sees a node before its children.
|
|
1180
|
+
*/
|
|
1181
|
+
function walk(node, visit) {
|
|
1182
|
+
if (visit(node) === false) return;
|
|
1183
|
+
for (const child of children(node)) walk(child, visit);
|
|
1184
|
+
}
|
|
1185
|
+
/**
|
|
1186
|
+
* The direct children of a node, in source order. Empty for leaves.
|
|
1187
|
+
*
|
|
1188
|
+
* Returning a fresh array each call keeps the contract simple — callers
|
|
1189
|
+
* may iterate or splat without aliasing surprises. The cost is trivial
|
|
1190
|
+
* for AST traversal.
|
|
1191
|
+
*/
|
|
1192
|
+
function children(node) {
|
|
1193
|
+
switch (node.type) {
|
|
1194
|
+
case "Text":
|
|
1195
|
+
case "Comment":
|
|
1196
|
+
case "Dot":
|
|
1197
|
+
case "Nil":
|
|
1198
|
+
case "Bool":
|
|
1199
|
+
case "Number":
|
|
1200
|
+
case "String":
|
|
1201
|
+
case "Identifier":
|
|
1202
|
+
case "Field":
|
|
1203
|
+
case "Variable":
|
|
1204
|
+
case "Break":
|
|
1205
|
+
case "Continue": return [];
|
|
1206
|
+
case "List": return node.nodes;
|
|
1207
|
+
case "Chain": return [node.node];
|
|
1208
|
+
case "Command": return node.args;
|
|
1209
|
+
case "Pipe": return [...node.decls, ...node.cmds];
|
|
1210
|
+
case "Action": return [node.pipe];
|
|
1211
|
+
case "If":
|
|
1212
|
+
case "Range":
|
|
1213
|
+
case "With": return node.elseList ? [
|
|
1214
|
+
node.pipe,
|
|
1215
|
+
node.list,
|
|
1216
|
+
node.elseList
|
|
1217
|
+
] : [node.pipe, node.list];
|
|
1218
|
+
case "Template": return node.pipe ? [node.pipe] : [];
|
|
1219
|
+
case "Block": return node.pipe ? [node.pipe, node.list] : [node.list];
|
|
1220
|
+
default: return assertNever(node);
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
//#endregion
|
|
1164
1224
|
//#region src/evaluator/access.ts
|
|
1165
1225
|
/**
|
|
1166
1226
|
* Field / index access for evaluator primaries.
|
|
@@ -1715,17 +1775,85 @@ let internalCreateTemplate;
|
|
|
1715
1775
|
var Template = class Template {
|
|
1716
1776
|
source;
|
|
1717
1777
|
_evaluate;
|
|
1718
|
-
|
|
1778
|
+
_referencedFunctions;
|
|
1779
|
+
_referencedCalls;
|
|
1780
|
+
constructor(source, evaluate, referencedFunctions, referencedCalls) {
|
|
1719
1781
|
this.source = source;
|
|
1720
1782
|
this._evaluate = evaluate;
|
|
1783
|
+
this._referencedFunctions = referencedFunctions;
|
|
1784
|
+
this._referencedCalls = referencedCalls;
|
|
1721
1785
|
}
|
|
1722
1786
|
static {
|
|
1723
|
-
internalCreateTemplate = (source, evaluate) => new Template(source, evaluate);
|
|
1787
|
+
internalCreateTemplate = (source, evaluate, referencedFunctions, referencedCalls) => new Template(source, evaluate, referencedFunctions, referencedCalls);
|
|
1724
1788
|
}
|
|
1725
1789
|
evaluate(scope) {
|
|
1726
1790
|
return this._evaluate(scope);
|
|
1727
1791
|
}
|
|
1792
|
+
/**
|
|
1793
|
+
* The FuncMap function names this template references — every identifier that
|
|
1794
|
+
* names a function anywhere in the template body or its `{{ define }}` blocks.
|
|
1795
|
+
*
|
|
1796
|
+
* This is a STATIC fact derived from the parsed AST, not an execution trace:
|
|
1797
|
+
* a name in the set is a function the template *can* invoke; a name absent
|
|
1798
|
+
* from the set is one it provably never invokes. Use it to ask "does this
|
|
1799
|
+
* template use helper X?" without evaluating it (e.g. to discover which
|
|
1800
|
+
* templates depend on a feature func) — robust where a source-text scan is
|
|
1801
|
+
* not, because it sees through whitespace, pipelines, and field/string
|
|
1802
|
+
* lookalikes.
|
|
1803
|
+
*
|
|
1804
|
+
* Built-in operators registered for every engine (`and`, `eq`, `index`,
|
|
1805
|
+
* `printf`, …) are reported the same as consumer funcs — a referenced name is
|
|
1806
|
+
* a referenced name regardless of who registered it.
|
|
1807
|
+
*/
|
|
1808
|
+
referencedFunctions() {
|
|
1809
|
+
return this._referencedFunctions;
|
|
1810
|
+
}
|
|
1811
|
+
/**
|
|
1812
|
+
* Every command-head call in the template body and its `{{ define }}` blocks,
|
|
1813
|
+
* in preorder, each paired with its positional arguments projected to literal
|
|
1814
|
+
* strings (a non-string-literal argument is reported as `null`, preserving
|
|
1815
|
+
* argument positions).
|
|
1816
|
+
*
|
|
1817
|
+
* Like {@link referencedFunctions} this is a STATIC fact from the parsed AST,
|
|
1818
|
+
* not an execution trace. Use it when "is X called?" is not enough and you
|
|
1819
|
+
* need the literal arguments a call was written with — e.g. to discover that a
|
|
1820
|
+
* template contains `{{ menu "applyTheme" "themePage" }}` and read its first
|
|
1821
|
+
* argument without evaluating the template. Calls whose head is not a bare
|
|
1822
|
+
* function identifier (e.g. a field invoked via `call`) are not reported here;
|
|
1823
|
+
* their names still appear in {@link referencedFunctions}.
|
|
1824
|
+
*/
|
|
1825
|
+
referencedCalls() {
|
|
1826
|
+
return this._referencedCalls;
|
|
1827
|
+
}
|
|
1728
1828
|
};
|
|
1829
|
+
function collectReferencedFunctions(parsed) {
|
|
1830
|
+
const names = /* @__PURE__ */ new Set();
|
|
1831
|
+
const collect = (root) => {
|
|
1832
|
+
walk(root, (node) => {
|
|
1833
|
+
if (node.type === "Identifier") names.add(node.ident);
|
|
1834
|
+
});
|
|
1835
|
+
};
|
|
1836
|
+
collect(parsed.root);
|
|
1837
|
+
for (const body of parsed.defines.values()) collect(body);
|
|
1838
|
+
return names;
|
|
1839
|
+
}
|
|
1840
|
+
function collectReferencedCalls(parsed) {
|
|
1841
|
+
const calls = [];
|
|
1842
|
+
const collect = (root) => {
|
|
1843
|
+
walk(root, (node) => {
|
|
1844
|
+
if (node.type !== "Command") return;
|
|
1845
|
+
const head = node.args[0];
|
|
1846
|
+
if (head === void 0 || head.type !== "Identifier") return;
|
|
1847
|
+
calls.push({
|
|
1848
|
+
name: head.ident,
|
|
1849
|
+
args: node.args.slice(1).map((arg) => arg.type === "String" ? arg.value : null)
|
|
1850
|
+
});
|
|
1851
|
+
});
|
|
1852
|
+
};
|
|
1853
|
+
collect(parsed.root);
|
|
1854
|
+
for (const body of parsed.defines.values()) collect(body);
|
|
1855
|
+
return calls;
|
|
1856
|
+
}
|
|
1729
1857
|
var Engine = class {
|
|
1730
1858
|
fromString;
|
|
1731
1859
|
toString;
|
|
@@ -1752,7 +1880,7 @@ var Engine = class {
|
|
|
1752
1880
|
*/
|
|
1753
1881
|
parse(source) {
|
|
1754
1882
|
const parsed = parse(source, this.delims);
|
|
1755
|
-
return internalCreateTemplate(parsed.source, (scope) => this.evalParsed(parsed, scope));
|
|
1883
|
+
return internalCreateTemplate(parsed.source, (scope) => this.evalParsed(parsed, scope), collectReferencedFunctions(parsed), collectReferencedCalls(parsed));
|
|
1756
1884
|
}
|
|
1757
1885
|
/**
|
|
1758
1886
|
* Convenience sugar for `parse(src).evaluate.bind(template)`.
|