@promptctl/go-template-js 0.9.0 → 0.10.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 +43 -7
- package/dist/index.d.ts +164 -29
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +608 -221
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -40,6 +40,20 @@ pnpm add @promptctl/go-template-js
|
|
|
40
40
|
|
|
41
41
|
Requires Node **≥ 20.19.0** (per `engines.node`). The package is ESM-only.
|
|
42
42
|
|
|
43
|
+
### If the `@promptctl` scope is unreachable
|
|
44
|
+
|
|
45
|
+
Some internal npm proxies don't serve the `@promptctl` scope, and an ordinary install can't resolve the package. Every GitHub release from v0.9.0 on carries the packed tarball as an asset, byte-identical to the one npm serves for that version — depend on it directly:
|
|
46
|
+
|
|
47
|
+
```json
|
|
48
|
+
{
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@promptctl/go-template-js": "https://github.com/promptctl/go-template-js/releases/download/vX.Y.Z/promptctl-go-template-js-X.Y.Z.tgz"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Substitute a released version for `X.Y.Z` in both places; the [releases page](https://github.com/promptctl/go-template-js/releases) lists them, each with its asset. `@promptctl/rich-js` needs the same treatment if you depend on it — it sits in the same scope, so it won't resolve either: point it at its own release asset, and redirect the transitive `@promptctl/go-template-js` it pulls in to the URL above. The redirect field is `pnpm.overrides` for pnpm, `overrides` for npm, `resolutions` for yarn; pnpm ignores a top-level `overrides` without warning. Don't substitute `github:promptctl/go-template-js#semver:^X.Y.Z` — a git dependency installs the source tree, which carries no `dist/`, so the install exits 0 and the package fails later at import.
|
|
56
|
+
|
|
43
57
|
## Quick start
|
|
44
58
|
|
|
45
59
|
```ts
|
|
@@ -74,6 +88,7 @@ const engine = createEngine<Frag>({
|
|
|
74
88
|
red: {
|
|
75
89
|
fn: (s: unknown) => ({ color: "red", text: String(s) }),
|
|
76
90
|
argTypes: ["string"],
|
|
91
|
+
arity: { kind: "exact" },
|
|
77
92
|
returnType: "T",
|
|
78
93
|
},
|
|
79
94
|
},
|
|
@@ -88,7 +103,7 @@ tpl.evaluate({ label: "warn", value: "ALERT" });
|
|
|
88
103
|
// ]
|
|
89
104
|
```
|
|
90
105
|
|
|
91
|
-
Functions
|
|
106
|
+
Functions declare `argTypes` and `arity`, both required. The engine uses `argTypes` to enforce one architectural commitment: **a non-string `T` value will never silently flatten into a `string` parameter**. If you try to pipe a styled fragment into a function declared with `argTypes: ["string"]`, you get a `TypeMismatchError` naming the function, the argument index, and a suggestion to call your `unstyled` (or equivalent) flatten helper.
|
|
92
107
|
|
|
93
108
|
### Lifting and stringification: the boundary bridges
|
|
94
109
|
|
|
@@ -117,7 +132,8 @@ Every entry in `argTypes` is one of:
|
|
|
117
132
|
| Kind | Accepts at runtime | Used by (examples) |
|
|
118
133
|
| --- | --- | --- |
|
|
119
134
|
| `"string"` | JS `string` | `upper`, `lower`, `trim`, `printf` format string |
|
|
120
|
-
| `"int"` |
|
|
135
|
+
| `"int"` | safe-integer `number` or safe-integer-range `bigint` — a Go `int` parameter, so a fractional is refused as Go refuses `repeat 2.7 "x"`; gate normalizes to `number` | built-in `slice` indices, `repeat`, `substr`, `trunc`, `until`, `seq`, `chunk`, `splitn`, `round`'s precision |
|
|
136
|
+
| `"truncating-int"` | finite `number` or safe-integer-range `bigint` — the numeric half of sprig's `cast.ToInt64` read of an `interface{}` parameter (the cast's string/bool/nil conversions are refused); gate truncates toward zero (`max 1.5 2.5` → `2`, as in Go) | `add`, `add1`, `sub`, `mul`, `div`, `mod`, `max`, `min`, sprig's list `slice` |
|
|
121
137
|
| `"float"` | any `number` (including `NaN`/`±Infinity` — legitimate IEEE-754 floats) or `bigint` whose `Number()` is finite; gate normalizes to `number` via `Number(v)` | `addf`, `subf`, `mulf`, `divf`, `maxf`, `minf` |
|
|
122
138
|
| `"bool"` | `typeof === "boolean"` | (rare; most boolean slots use `"truthy"`) |
|
|
123
139
|
| `"T"` | Any non-primitive (consumer-defined fragment) | consumer-defined typed funcs returning `T` |
|
|
@@ -139,11 +155,27 @@ Every entry in `argTypes` is one of:
|
|
|
139
155
|
|
|
140
156
|
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.
|
|
141
157
|
|
|
142
|
-
|
|
158
|
+
A slot naming a kind that is not in this table — a stale `["number" as ArgType]`, or any registration from a JS caller with no compile-time guard — is rejected by `createEngine`, naming the func and the slot. That check is at construct time rather than at the gate on purpose: the kind a slot declares is a fact about the declaration, so it is knowable before a template is ever parsed, and reporting it there means a consumer sees it against the line that wrote it instead of mid-render.
|
|
159
|
+
|
|
160
|
+
### Arity
|
|
161
|
+
|
|
162
|
+
Every registration declares an `arity`, which says how many arguments the func takes and how `argTypes` covers them:
|
|
163
|
+
|
|
164
|
+
| `arity` | Meaning |
|
|
165
|
+
| --- | --- |
|
|
166
|
+
| `{ kind: "exact" }` | Exactly `argTypes.length` arguments; each slot is declared once. |
|
|
167
|
+
| `{ kind: "variadic" }` | At least `argTypes.length - 1` arguments. One slot per Go parameter, the repeating one last, so every excess argument is validated against it. |
|
|
168
|
+
| `{ kind: "alternating", minimum: n }` | At least `n` arguments, with `argTypes` read as a *cycle*: the slot for argument `i` is `argTypes[i % argTypes.length]`. |
|
|
169
|
+
|
|
170
|
+
The gate enforces those counts before the func body runs, and rejects a call it does not accept with an `ArgCountError` carrying the position and a source caret. The message mirrors Go's: `wrong number of args for upper: want 1 got 2` for a fixed-arity signature, `want at least 1 got 0` for a variadic one. The counts themselves are pinned against Go's real signatures by `go-arity.test.ts`, so the gate rejects exactly what Go rejects — note that `add` is `func(i ...interface{})` in sprig, so `{{ add 1 }}` and even `{{ add }}` are legal calls there and here.
|
|
171
|
+
|
|
172
|
+
A func body therefore never sees a wrong argument count, and never needs to check for one.
|
|
173
|
+
|
|
174
|
+
Neither `"exact"` nor `"variadic"` carries a count — it *is* `argTypes.length` and `argTypes.length - 1` respectively, and a second copy could only disagree. `minimum` survives on `"alternating"` alone, where `argTypes` is a cycle length rather than a parameter count: `merge` declares `["dict", "dict"]` and requires one, while `dict` declares a two-slot cycle and requires nothing.
|
|
143
175
|
|
|
144
|
-
|
|
176
|
+
Alternation is what lets `dict "k1" v1 "k2" v2 …` be checked at the gate: `dict` declares `argTypes: ["string", "value"]` with `{ kind: "alternating", minimum: 0 }`, and the gate enforces "string at even index, anything at odd index" without per-key revalidation in the body.
|
|
145
177
|
|
|
146
|
-
|
|
178
|
+
A declared minimum mirrors Go's *arity gate*, which is not always the same as the function body's own requirement. Go's `eq` is `eq(arg1, arg2 ...)`, so its gate wants at least one argument; the familiar "missing argument for comparison" comes from Go's `eq` body and is a different error.
|
|
147
179
|
|
|
148
180
|
## Syntax and built-ins
|
|
149
181
|
|
|
@@ -219,6 +251,8 @@ The engine matches Go's `text/template` semantics aggressively. The remaining di
|
|
|
219
251
|
- **Numeric typing.** Go has `int`, `float64`, and friends as distinct runtime types; JavaScript has one `number`. The numeric-verb type label reflects JS's view: integer JS numbers report as `int`, fractional as `float64`, `bigint` as `int64`. Non-numeric values into `%d`/`%f`/`%x` produce a Go-style `%!<verb>(<type>=<value>)` diagnostic — e.g. `printf "%d" "foo"` → `%!d(string=foo)`.
|
|
220
252
|
- **Regex semantics.** `sprigRegex()` uses ECMAScript regex, not Go's RE2. Lookbehind syntax, Unicode property escapes, and certain catastrophic-backtracking patterns differ.
|
|
221
253
|
- **String length.** Go strings are UTF-8 byte sequences; `len("é")` is `2` in Go. JS strings are UTF-16 code units; `len("é")` is `1` here. Choose grapheme libraries on the JS side if you need byte/grapheme counts.
|
|
254
|
+
- **String slicing.** `trunc`, `substr`, `abbrev`, and `abbrevboth` count code points where Go counts bytes, so the same `n` keeps a different amount of text in each engine: `substr 0 2 "éx"` → `é` in Go, `éx` here. The divergence starts at any two-byte character, not only astral ones, and Go's answer is frequently not valid UTF-8 at all: `trunc 1 "éx"` → the single byte `0xC3` in Go, `é` here; `trunc 1 "𐍈x"` → the single byte `0xF0` in Go, `𐍈` here. Each of those Go results is the first fragment of the character's UTF-8 encoding, and is not a well-formed string on its own. Counting code points is deliberate: it keeps them from splitting a character and returning a lone surrogate. Separately, `abbrevboth` disagrees with Go on plain ASCII whenever the offset is 4 or less — a defect rather than an intended residual, and one that will be fixed.
|
|
255
|
+
- **Width counting.** `wrap` and `wrapWith` measure width in UTF-16 code units where Go measures bytes: `wrap 5 "éé éé"` → `éé\néé` in Go, unwrapped `éé éé` here. `printf`'s width flags diverge on a different axis — Go pads to a rune count and this engine to a UTF-16 unit count, so the two agree on BMP text and differ only on astral: `printf "%-5s|" "𐍈𐍈"` → `𐍈𐍈 |` in Go, `𐍈𐍈 |` here. Neither is converted to code points: they never cut inside a character, so they cannot emit a lone surrogate, and counting runes in `wrap` would move it further from Go's bytes rather than closer.
|
|
222
256
|
- **Field access.** Walks JS objects, Maps, and arrays using JS property semantics, not Go reflection. Named-field access on an array is a missing field — silently `<no value>` under the default `missingKey: "default"` policy, or a `MissingFieldError` under `missingKey: "error"`. There is no `length` / `[N]` magic; use the `index` and `len` built-ins.
|
|
223
257
|
- **`missingkey=zero`.** Accepted as a config value (`missingKey: "zero"`) for API parity with Go's `Option("missingkey=zero")`, but JavaScript erases value-types at runtime — there is no `reflect.New(elemType).Elem()` analogue, so `"zero"` produces the same observable behavior as `"default"` (silent `<no value>`). To force a typed-zero substitute at the use site, use sprig: `{{ .missing | default "" }}`.
|
|
224
258
|
- **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`.
|
|
@@ -236,6 +270,7 @@ Places where users sometimes expect divergence and there isn't one:
|
|
|
236
270
|
- `{{ "key" | get .dict }}` composes correctly: last-arg piping puts the key in the trailing slot, which is `get(d, key)`'s second parameter. The form `{{ .dict | get "key" }}` does *not* work in either engine — the dict ends up in the key slot.
|
|
237
271
|
- `eq` / `ne` compare arrays, Maps, Sets, and plain objects by **structural deep-equal**, matching Go's `text/template` semantics. Reference-identity comparison is not a divergence — it was a bug fixed in epic `template-laws-3gt`.
|
|
238
272
|
- Sprig dict ops (`get`, `keys`, `values`, `pluck`, `hasKey`, …) accept **plain objects only**, not `Map`. Pass `Object.fromEntries(map)` if you have a `Map` to feed in. Maps remain valid for field-access (`.get(key)`) and for `range`.
|
|
273
|
+
- **Splitting by character** matches Go. `split ""`, `splitList ""`, and `trimAll`'s cutset all work in whole characters on both sides, astral input included — pinned by the conformance fixtures `sprig-split-astral-empty-sep`, `sprig-splitlist-astral-empty-sep`, and `sprig-trimall-astral-cutset`. It is the slicing and width-counting functions, noted above, that do diverge.
|
|
239
274
|
- `empty` on a non-empty object returns **false**, even when every value is the zero value of its type (e.g. `{tag: "", text: ""}`). Matches Go sprig: `text/template.isTrue` treats structs as always truthy, and sprig's `empty` follows by returning `false` for any non-empty map or struct. `reflect.IsZero` is *not* what Go sprig consults here. The conformance fixture `sprig-empty-object-parity` pins this behavior byte-for-byte.
|
|
240
275
|
|
|
241
276
|
## Errors
|
|
@@ -247,6 +282,7 @@ TemplateError
|
|
|
247
282
|
├── ParseError
|
|
248
283
|
└── EvalError
|
|
249
284
|
├── FuncNotFoundError (with did-you-mean suggestions)
|
|
285
|
+
├── ArgCountError (wrong number of args, Go's own message)
|
|
250
286
|
├── TypeMismatchError (no-silent-flatten violation)
|
|
251
287
|
└── MissingFieldError (named field path failure)
|
|
252
288
|
```
|
|
@@ -265,8 +301,8 @@ Every error carries `pos`, `source`, and a `kind` discriminator. `.toString()` p
|
|
|
265
301
|
|
|
266
302
|
The package exports exactly the following from `"@promptctl/go-template-js"` — anything else is internal and may change at any time:
|
|
267
303
|
|
|
268
|
-
- Engine: `createEngine`, `Engine`, `Template`, `EngineConfig`, `FuncMap`, `TemplateFunc`, `ArgType`, `MissingKeyOption`, `Delims`.
|
|
269
|
-
- Errors: `TemplateError`, `ParseError`, `EvalError`, `FuncNotFoundError`, `TypeMismatchError`, `MissingFieldError`, `FailError`, `ErrorKind`.
|
|
304
|
+
- Engine: `createEngine`, `Engine`, `Template`, `EngineConfig`, `FuncMap`, `TemplateFunc`, `ArgType`, `Arity`, `MissingKeyOption`, `Delims`.
|
|
305
|
+
- Errors: `TemplateError`, `ParseError`, `EvalError`, `FuncNotFoundError`, `ArgCountError`, `ArgCount`, `TypeMismatchError`, `MissingFieldError`, `FailError`, `ErrorKind`.
|
|
270
306
|
- Sprig categories: `sprigDefaults`, `sprigStrings`, `sprigMath`, `sprigLists`, `sprigDicts`, `sprigRegex`, `sprigTypes`, `sprigConversions`, `sprigSemver`, `sprigFlow`, `sprigRandom`, `sprigHash`, `sprigDatetime`.
|
|
271
307
|
|
|
272
308
|
Reaching into `dist/` subpaths or `src/` deep imports is unsupported.
|
package/dist/index.d.ts
CHANGED
|
@@ -16,9 +16,11 @@ interface Pos {
|
|
|
16
16
|
}
|
|
17
17
|
//#endregion
|
|
18
18
|
//#region src/errors.d.ts
|
|
19
|
-
type ErrorKind = "ParseError" | "EvalError" | "FuncNotFoundError" | "TypeMismatchError" | "MissingFieldError" | "FailError";
|
|
19
|
+
type ErrorKind = "ParseError" | "EvalError" | "FuncNotFoundError" | "ArgCountError" | "TypeMismatchError" | "MissingFieldError" | "FailError";
|
|
20
20
|
interface TemplateErrorContext {
|
|
21
21
|
readonly source?: string | undefined;
|
|
22
|
+
/** The error a function body threw, when this one reports it at the call site. */
|
|
23
|
+
readonly cause?: unknown;
|
|
22
24
|
}
|
|
23
25
|
declare class TemplateError extends Error {
|
|
24
26
|
readonly name: string;
|
|
@@ -59,6 +61,37 @@ declare class FuncNotFoundError extends EvalError {
|
|
|
59
61
|
available?: readonly string[];
|
|
60
62
|
});
|
|
61
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* The argument counts a func accepts — a closed interval, with
|
|
66
|
+
* `maximum: Infinity` for the variadic and alternating kinds.
|
|
67
|
+
*
|
|
68
|
+
* [LAW:one-source-of-truth] Nothing declares these numbers. They are
|
|
69
|
+
* derived from `TemplateFunc.arity` and `argTypes.length` by
|
|
70
|
+
* `acceptedArgCount` in the evaluator, which is the only producer; a
|
|
71
|
+
* registration that stored its own minimum would be a second copy of a
|
|
72
|
+
* number the declaration already carries.
|
|
73
|
+
*/
|
|
74
|
+
interface ArgCount {
|
|
75
|
+
readonly minimum: number;
|
|
76
|
+
readonly maximum: number;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Thrown when a func is called with a number of arguments its declared
|
|
80
|
+
* arity does not accept — `{{ upper "a" "b" }}`, `{{ min }}`.
|
|
81
|
+
*
|
|
82
|
+
* The message mirrors Go's `text/template`, which reports a fixed-arity
|
|
83
|
+
* signature as `want N` and a variadic one as `want at least N`. That
|
|
84
|
+
* distinction is read off `accepted`, not passed in, so the two phrasings
|
|
85
|
+
* cannot disagree about which one a given func gets.
|
|
86
|
+
*/
|
|
87
|
+
declare class ArgCountError extends EvalError {
|
|
88
|
+
readonly name = "ArgCountError";
|
|
89
|
+
readonly kind: ErrorKind;
|
|
90
|
+
readonly funcName: string;
|
|
91
|
+
readonly accepted: ArgCount;
|
|
92
|
+
readonly got: number;
|
|
93
|
+
constructor(funcName: string, accepted: ArgCount, got: number, pos: Pos, ctx?: TemplateErrorContext);
|
|
94
|
+
}
|
|
62
95
|
declare class TypeMismatchError extends EvalError {
|
|
63
96
|
readonly name = "TypeMismatchError";
|
|
64
97
|
readonly kind: ErrorKind;
|
|
@@ -69,6 +102,18 @@ declare class TypeMismatchError extends EvalError {
|
|
|
69
102
|
readonly receivedSummary: string;
|
|
70
103
|
constructor(funcName: string, argIndex: number, expected: string, receivedSummary: string, pos: Pos, ctx?: TemplateErrorContext);
|
|
71
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* An integer slot refused a number that is not a safe integer. Still a
|
|
107
|
+
* `TypeMismatchError` (the slot's type refused the value), but worded
|
|
108
|
+
* with the value and the reason: a fractional is Go's own refusal,
|
|
109
|
+
* `expected integer; found 2.7`, and an integer past 2^53 is
|
|
110
|
+
* `integer out of range: <n>`, since JS cannot carry it exactly. The
|
|
111
|
+
* generic wording would say a number was expected and a number arrived,
|
|
112
|
+
* and suggest flattening a styled value that was never there.
|
|
113
|
+
*/
|
|
114
|
+
declare class NotIntegerError extends TypeMismatchError {
|
|
115
|
+
constructor(funcName: string, argIndex: number, value: number | bigint, pos: Pos, ctx?: TemplateErrorContext);
|
|
116
|
+
}
|
|
72
117
|
declare class MissingFieldError extends EvalError {
|
|
73
118
|
readonly name = "MissingFieldError";
|
|
74
119
|
readonly kind: ErrorKind;
|
|
@@ -116,6 +161,21 @@ declare class Defines {
|
|
|
116
161
|
}
|
|
117
162
|
//#endregion
|
|
118
163
|
//#region src/evaluator/evaluator.d.ts
|
|
164
|
+
/**
|
|
165
|
+
* Every kind a slot may declare.
|
|
166
|
+
*
|
|
167
|
+
* [LAW:one-source-of-truth] The kinds are a *list* first and a type
|
|
168
|
+
* second. `validateArgSpecs` must reject a declaration naming a kind
|
|
169
|
+
* that does not exist, and a runtime check needs a runtime list; writing
|
|
170
|
+
* that list out beside a hand-maintained union would be the second
|
|
171
|
+
* clock. So the array is authoritative and `ArgType` is derived from it
|
|
172
|
+
* — one edit adds a kind to the type, to the construct-time check, and
|
|
173
|
+
* to the two exhaustive switches that must then grow an arm.
|
|
174
|
+
*
|
|
175
|
+
* The per-kind semantics live on `ArgType` below, where a consumer
|
|
176
|
+
* hovering the exported name finds them.
|
|
177
|
+
*/
|
|
178
|
+
declare const ARG_TYPES: readonly ["string", "int", "truncating-int", "float", "bool", "T", "ordered", "list", "dict", "sized", "comparable", "stringifiable", "liftable", "callable", "collection", "index-key", "sliceable", "truthy", "reflective", "value", "serializable"];
|
|
119
179
|
/**
|
|
120
180
|
* Declared parameter type for a registered template function. Used by
|
|
121
181
|
* the no-silent-flatten guard to detect unsafe T-into-string flows.
|
|
@@ -123,13 +183,20 @@ declare class Defines {
|
|
|
123
183
|
* - "string" — must be a JS string. Non-string values raise
|
|
124
184
|
* TypeMismatchError. This is the **architectural commitment**: T
|
|
125
185
|
* never silently flattens into a string parameter.
|
|
126
|
-
* - "int" — validate-AND-parse integer carrier
|
|
127
|
-
* `number` and any `bigint`
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
* `
|
|
132
|
-
* `
|
|
186
|
+
* - "int" — validate-AND-parse integer carrier: a Go `int`
|
|
187
|
+
* parameter. Accepts any safe-integer `number` and any `bigint`
|
|
188
|
+
* whose `Number()` is safe-integer-representable; rejects
|
|
189
|
+
* fractionals, `NaN`, `±Infinity`, and integers past 2^53, as
|
|
190
|
+
* Go refuses `repeat 2.7 "x"`. The gate normalizes `values[i]` to
|
|
191
|
+
* `Number(v)` so bodies see `number`. Used by every slot Go declares
|
|
192
|
+
* `int`: the built-in `slice`'s indices, `repeat`, `substr`, `trunc`,
|
|
193
|
+
* `until`, `seq`, `chunk`, `splitn`, `round`'s precision ….
|
|
194
|
+
* - "truncating-int" — the numeric half of sprig's `cast.ToInt64` read
|
|
195
|
+
* of an `interface{}` parameter. Accepts what "int" accepts plus any
|
|
196
|
+
* finite fractional `number`, which the gate truncates toward zero
|
|
197
|
+
* (`max 1.5 2.5` is `2` in Go); refuses the strings, bools and nil
|
|
198
|
+
* the cast would convert. Used by `add`, `add1`, `sub`, `mul`, `div`, `mod`,
|
|
199
|
+
* `max`, `min`, `biggest`, and sprig's list `slice`.
|
|
133
200
|
* - "float" — validate-AND-parse float carrier. Accepts any `number`
|
|
134
201
|
* (including `NaN`/`±Infinity` — legitimate IEEE-754 floats) and any
|
|
135
202
|
* `bigint` whose `Number()` is finite (overflow-to-Infinity rejected
|
|
@@ -187,7 +254,75 @@ declare class Defines {
|
|
|
187
254
|
* `"callable"`). The history for this decision lives in epic
|
|
188
255
|
* template-laws-3gt.
|
|
189
256
|
*/
|
|
190
|
-
type ArgType =
|
|
257
|
+
type ArgType = (typeof ARG_TYPES)[number];
|
|
258
|
+
/**
|
|
259
|
+
* How many arguments a func accepts, and how `argTypes` covers them.
|
|
260
|
+
*
|
|
261
|
+
* [LAW:types-are-the-program] Argument *count* used to live only in the
|
|
262
|
+
* JS signature, where the gate could not see it: `enforceArgTypes`
|
|
263
|
+
* iterated the supplied values, so a missing or surplus argument was
|
|
264
|
+
* not a state it could represent, let alone reject. Declaring arity
|
|
265
|
+
* alongside `argTypes` makes the count a fact the gate reads —
|
|
266
|
+
* `acceptedArgCount` projects this union into the counts a declaration
|
|
267
|
+
* admits and rejects the rest before the slot loop.
|
|
268
|
+
*
|
|
269
|
+
* [LAW:one-source-of-truth] No kind carries a count that `argTypes`
|
|
270
|
+
* already holds. `"exact"` means exactly `argTypes.length`, and
|
|
271
|
+
* `"variadic"` means at least `argTypes.length - 1` — which is Go's
|
|
272
|
+
* gate for a variadic signature, always, since its fixed parameters are
|
|
273
|
+
* every one but the last. A `minimum` field on either would be a second
|
|
274
|
+
* copy of a number the array carries, and the copy is what drifts:
|
|
275
|
+
* `minimum` survives here only on `"alternating"`, where `argTypes` is
|
|
276
|
+
* a cycle length rather than a parameter count and the two are
|
|
277
|
+
* genuinely unrelated.
|
|
278
|
+
*
|
|
279
|
+
* [LAW:one-type-per-behavior] The three kinds are exactly the three
|
|
280
|
+
* slot-lookup rules, so `makeSlotLookup` switches on this union and
|
|
281
|
+
* nothing else. Folding the former `argTypePattern` field in here is
|
|
282
|
+
* what makes "exact yet alternating" unrepresentable rather than merely
|
|
283
|
+
* unused.
|
|
284
|
+
*
|
|
285
|
+
* There is deliberately no `between`/optional-trailing kind: Go's own
|
|
286
|
+
* `text/template` and `sprig` have no notion of an optional parameter,
|
|
287
|
+
* so every signature is fixed-arity or variadic. That every declaration
|
|
288
|
+
* here matches the Go function it mirrors is not a claim — it is
|
|
289
|
+
* checked by `go-arity.test.ts` against `go-arity.fixture.json`, which
|
|
290
|
+
* `conformance/gen/arity` extracts from Go. Do not add a fourth kind
|
|
291
|
+
* without a Go signature that demands it.
|
|
292
|
+
*/
|
|
293
|
+
type Arity = /** Exactly `argTypes.length` arguments; each slot is declared once. */{
|
|
294
|
+
readonly kind: "exact";
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* At least `argTypes.length - 1` arguments — Go's own gate for a
|
|
298
|
+
* variadic signature, whose fixed parameters are every one but the
|
|
299
|
+
* last. The trailing `argTypes` entry is that last, repeating
|
|
300
|
+
* parameter, so every argument past the declared ones validates
|
|
301
|
+
* against `argTypes[argTypes.length - 1]`.
|
|
302
|
+
*
|
|
303
|
+
* Declare one slot per Go parameter, the repeating one last: `eq` is
|
|
304
|
+
* `func(reflect.Value, ...reflect.Value)`, so it declares two
|
|
305
|
+
* `"comparable"` slots and requires one. Collapsing those to a single
|
|
306
|
+
* slot would render the same verdicts today and quietly lose the
|
|
307
|
+
* minimum.
|
|
308
|
+
*/
|
|
309
|
+
| {
|
|
310
|
+
readonly kind: "variadic";
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* At least `minimum` arguments — the one kind that needs the number,
|
|
314
|
+
* because `argTypes` below is a cycle length, not a parameter count.
|
|
315
|
+
* With `argTypes` read as a *cycle*:
|
|
316
|
+
* the slot for argument `i` is `argTypes[i % argTypes.length]`. Used
|
|
317
|
+
* by `dict`'s `string, value, string, value, …` kv pairing — without
|
|
318
|
+
* it the gate cannot distinguish even-index keys (must be string)
|
|
319
|
+
* from odd-index values (anything), and the body would re-validate
|
|
320
|
+
* per key, splitting `[LAW:single-enforcer]` across two layers.
|
|
321
|
+
*/
|
|
322
|
+
| {
|
|
323
|
+
readonly kind: "alternating";
|
|
324
|
+
readonly minimum: number;
|
|
325
|
+
};
|
|
191
326
|
interface TemplateFunc {
|
|
192
327
|
/**
|
|
193
328
|
* The function body. Parameter types are *contravariant-bottom*
|
|
@@ -199,38 +334,37 @@ interface TemplateFunc {
|
|
|
199
334
|
* site (`enforceArgTypes`). The compile-time signature does not
|
|
200
335
|
* duplicate that gate — it stays out of the way so consumer
|
|
201
336
|
* implementations can declare the precise types they expect.
|
|
337
|
+
*
|
|
338
|
+
* The signature is *not* a source of arity: `fn.length` stops at the
|
|
339
|
+
* first rest parameter and lies about several registrations (`min`,
|
|
340
|
+
* `max`, `mul` and friends report 0 while Go requires 1). Arity comes
|
|
341
|
+
* from `arity`, never from here.
|
|
202
342
|
*/
|
|
203
343
|
readonly fn: (...args: never[]) => unknown;
|
|
204
344
|
/**
|
|
205
345
|
* Declared positional parameter types. Required.
|
|
206
346
|
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* `Variadic`.
|
|
347
|
+
* How the slots cover the supplied arguments is `arity`'s job: an
|
|
348
|
+
* `"exact"` func declares one entry per parameter, a `"variadic"` one
|
|
349
|
+
* declares the repeating slot last, and an `"alternating"` one
|
|
350
|
+
* declares the cycle.
|
|
212
351
|
*
|
|
213
352
|
* The pipe-fed last argument is appended to the positional list
|
|
214
|
-
* before validation, so it is checked against the
|
|
353
|
+
* before validation, so it is checked against the slot its final
|
|
354
|
+
* position selects.
|
|
215
355
|
*/
|
|
216
356
|
readonly argTypes: readonly ArgType[];
|
|
217
357
|
readonly returnType?: ArgType;
|
|
218
358
|
/**
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
* `"alternating"`: `argTypes` describes a *cycle*. The slot for arg
|
|
225
|
-
* index `i` is `argTypes[i % argTypes.length]`. Used by `dict`'s
|
|
226
|
-
* `string, value, string, value, …` kv pairing — without this the
|
|
227
|
-
* gate cannot distinguish even-index keys (must be string) from
|
|
228
|
-
* odd-index values (anything), and the body would re-validate per
|
|
229
|
-
* key, splitting `[LAW:single-enforcer]` across two layers.
|
|
359
|
+
* Argument count. Required — a func whose arity is undeclared is a
|
|
360
|
+
* func the gate cannot check, and defaulting to a permissive value
|
|
361
|
+
* would hide exactly the registrations that most need the
|
|
362
|
+
* declaration.
|
|
230
363
|
*
|
|
231
|
-
*
|
|
364
|
+
* Minimums mirror Go's *arity gate*, which is not always the same as
|
|
365
|
+
* the function body's own requirement; see `eq` and `dig`.
|
|
232
366
|
*/
|
|
233
|
-
readonly
|
|
367
|
+
readonly arity: Arity;
|
|
234
368
|
}
|
|
235
369
|
type FuncMap = Record<string, TemplateFunc>;
|
|
236
370
|
/**
|
|
@@ -467,6 +601,7 @@ declare class Engine<T> {
|
|
|
467
601
|
private evalPipe;
|
|
468
602
|
private evalPipeWithoutDecls;
|
|
469
603
|
private evalCommand;
|
|
604
|
+
private invoke;
|
|
470
605
|
private evalPrimary;
|
|
471
606
|
private resolveFieldChain;
|
|
472
607
|
private resolveVariable;
|
|
@@ -556,5 +691,5 @@ declare function sprigStrings(): FuncMap;
|
|
|
556
691
|
//#region src/sprig/types/index.d.ts
|
|
557
692
|
declare function sprigTypes(): FuncMap;
|
|
558
693
|
//#endregion
|
|
559
|
-
export { type ArgType, Defines, type Delims, Engine, type EngineConfig, type ErrorKind, EvalError, FailError, type FuncMap, FuncNotFoundError, MissingFieldError, type MissingKeyOption, ParseError, type ReferencedArg, type ReferencedCall, type ReferencedLiteral, Template, TemplateError, type TemplateFunc, TypeMismatchError, createEngine, sprigConversions, sprigDatetime, sprigDefaults, sprigDicts, sprigFlow, sprigHash, sprigLists, sprigMath, sprigRandom, sprigRegex, sprigSemver, sprigStrings, sprigTypes, staticDictEntries };
|
|
694
|
+
export { type ArgCount, ArgCountError, type ArgType, type Arity, Defines, type Delims, Engine, type EngineConfig, type ErrorKind, EvalError, FailError, type FuncMap, FuncNotFoundError, MissingFieldError, type MissingKeyOption, NotIntegerError, ParseError, type ReferencedArg, type ReferencedCall, type ReferencedLiteral, Template, TemplateError, type TemplateFunc, TypeMismatchError, createEngine, sprigConversions, sprigDatetime, sprigDefaults, sprigDicts, sprigFlow, sprigHash, sprigLists, sprigMath, sprigRandom, sprigRegex, sprigSemver, sprigStrings, sprigTypes, staticDictEntries };
|
|
560
695
|
//# 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/parser/parser.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/dict.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;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/parser/pos.ts","../src/errors.ts","../src/parser/lexer.ts","../src/parser/parser.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/dict.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;;;KCUC,SAAA;AAAA,UASK,oBAAA;EAAA,SACN,MAAA;EASgB;EAAA,SAPhB,KAAA;AAAA;AAAA,cAOE,aAA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