@promptctl/go-template-js 0.4.0 → 0.5.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 +18 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +95 -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
|
@@ -312,8 +312,26 @@ interface EngineConfig<T> {
|
|
|
312
312
|
declare class Template<T> {
|
|
313
313
|
readonly source: string;
|
|
314
314
|
private readonly _evaluate;
|
|
315
|
+
private readonly _referencedFunctions;
|
|
315
316
|
private constructor();
|
|
316
317
|
evaluate(scope: unknown): T[];
|
|
318
|
+
/**
|
|
319
|
+
* The FuncMap function names this template references — every identifier that
|
|
320
|
+
* names a function anywhere in the template body or its `{{ define }}` blocks.
|
|
321
|
+
*
|
|
322
|
+
* This is a STATIC fact derived from the parsed AST, not an execution trace:
|
|
323
|
+
* a name in the set is a function the template *can* invoke; a name absent
|
|
324
|
+
* from the set is one it provably never invokes. Use it to ask "does this
|
|
325
|
+
* template use helper X?" without evaluating it (e.g. to discover which
|
|
326
|
+
* templates depend on a feature func) — robust where a source-text scan is
|
|
327
|
+
* not, because it sees through whitespace, pipelines, and field/string
|
|
328
|
+
* lookalikes.
|
|
329
|
+
*
|
|
330
|
+
* Built-in operators registered for every engine (`and`, `eq`, `index`,
|
|
331
|
+
* `printf`, …) are reported the same as consumer funcs — a referenced name is
|
|
332
|
+
* a referenced name regardless of who registered it.
|
|
333
|
+
*/
|
|
334
|
+
referencedFunctions(): ReadonlySet<string>;
|
|
317
335
|
}
|
|
318
336
|
declare class Engine<T> {
|
|
319
337
|
private readonly fromString;
|
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.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AA,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/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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**
|
|
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,49 @@ let internalCreateTemplate;
|
|
|
1715
1775
|
var Template = class Template {
|
|
1716
1776
|
source;
|
|
1717
1777
|
_evaluate;
|
|
1718
|
-
|
|
1778
|
+
_referencedFunctions;
|
|
1779
|
+
constructor(source, evaluate, referencedFunctions) {
|
|
1719
1780
|
this.source = source;
|
|
1720
1781
|
this._evaluate = evaluate;
|
|
1782
|
+
this._referencedFunctions = referencedFunctions;
|
|
1721
1783
|
}
|
|
1722
1784
|
static {
|
|
1723
|
-
internalCreateTemplate = (source, evaluate) => new Template(source, evaluate);
|
|
1785
|
+
internalCreateTemplate = (source, evaluate, referencedFunctions) => new Template(source, evaluate, referencedFunctions);
|
|
1724
1786
|
}
|
|
1725
1787
|
evaluate(scope) {
|
|
1726
1788
|
return this._evaluate(scope);
|
|
1727
1789
|
}
|
|
1790
|
+
/**
|
|
1791
|
+
* The FuncMap function names this template references — every identifier that
|
|
1792
|
+
* names a function anywhere in the template body or its `{{ define }}` blocks.
|
|
1793
|
+
*
|
|
1794
|
+
* This is a STATIC fact derived from the parsed AST, not an execution trace:
|
|
1795
|
+
* a name in the set is a function the template *can* invoke; a name absent
|
|
1796
|
+
* from the set is one it provably never invokes. Use it to ask "does this
|
|
1797
|
+
* template use helper X?" without evaluating it (e.g. to discover which
|
|
1798
|
+
* templates depend on a feature func) — robust where a source-text scan is
|
|
1799
|
+
* not, because it sees through whitespace, pipelines, and field/string
|
|
1800
|
+
* lookalikes.
|
|
1801
|
+
*
|
|
1802
|
+
* Built-in operators registered for every engine (`and`, `eq`, `index`,
|
|
1803
|
+
* `printf`, …) are reported the same as consumer funcs — a referenced name is
|
|
1804
|
+
* a referenced name regardless of who registered it.
|
|
1805
|
+
*/
|
|
1806
|
+
referencedFunctions() {
|
|
1807
|
+
return this._referencedFunctions;
|
|
1808
|
+
}
|
|
1728
1809
|
};
|
|
1810
|
+
function collectReferencedFunctions(parsed) {
|
|
1811
|
+
const names = /* @__PURE__ */ new Set();
|
|
1812
|
+
const collect = (root) => {
|
|
1813
|
+
walk(root, (node) => {
|
|
1814
|
+
if (node.type === "Identifier") names.add(node.ident);
|
|
1815
|
+
});
|
|
1816
|
+
};
|
|
1817
|
+
collect(parsed.root);
|
|
1818
|
+
for (const body of parsed.defines.values()) collect(body);
|
|
1819
|
+
return names;
|
|
1820
|
+
}
|
|
1729
1821
|
var Engine = class {
|
|
1730
1822
|
fromString;
|
|
1731
1823
|
toString;
|
|
@@ -1752,7 +1844,7 @@ var Engine = class {
|
|
|
1752
1844
|
*/
|
|
1753
1845
|
parse(source) {
|
|
1754
1846
|
const parsed = parse(source, this.delims);
|
|
1755
|
-
return internalCreateTemplate(parsed.source, (scope) => this.evalParsed(parsed, scope));
|
|
1847
|
+
return internalCreateTemplate(parsed.source, (scope) => this.evalParsed(parsed, scope), collectReferencedFunctions(parsed));
|
|
1756
1848
|
}
|
|
1757
1849
|
/**
|
|
1758
1850
|
* Convenience sugar for `parse(src).evaluate.bind(template)`.
|