@promptctl/go-template-js 0.6.0 → 0.8.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/dist/index.d.ts +78 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +183 -40
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/dist/index.d.ts
CHANGED
|
@@ -107,6 +107,14 @@ interface Delims {
|
|
|
107
107
|
readonly right: string;
|
|
108
108
|
}
|
|
109
109
|
//#endregion
|
|
110
|
+
//#region src/parser/parser.d.ts
|
|
111
|
+
declare class Defines {
|
|
112
|
+
#private;
|
|
113
|
+
static readonly EMPTY: Defines;
|
|
114
|
+
private constructor();
|
|
115
|
+
has(name: string): boolean;
|
|
116
|
+
}
|
|
117
|
+
//#endregion
|
|
110
118
|
//#region src/evaluator/evaluator.d.ts
|
|
111
119
|
/**
|
|
112
120
|
* Declared parameter type for a registered template function. Used by
|
|
@@ -317,19 +325,59 @@ interface ReferencedCall {
|
|
|
317
325
|
* decoded value; any non-string-literal argument is `null` (its value is only
|
|
318
326
|
* known at evaluation time). Indices match the call site, so `args[0]` is the
|
|
319
327
|
* first argument regardless of the kinds in between.
|
|
328
|
+
*
|
|
329
|
+
* [LAW:one-source-of-truth] A derived view of {@link argExprs} — the
|
|
330
|
+
* string-literal projection kept for consumers that only read literal
|
|
331
|
+
* string slots. `args[i]` is `argExprs[i].value` when that is a literal
|
|
332
|
+
* string, `null` otherwise.
|
|
320
333
|
*/
|
|
321
334
|
readonly args: readonly (string | null)[];
|
|
335
|
+
/**
|
|
336
|
+
* The positional arguments after the head, each statically projected to a
|
|
337
|
+
* {@link ReferencedArg}. Indices match the call site (`argExprs[0]` is the
|
|
338
|
+
* first argument), and `argExprs.length === args.length`.
|
|
339
|
+
*/
|
|
340
|
+
readonly argExprs: readonly ReferencedArg[];
|
|
322
341
|
}
|
|
342
|
+
/**
|
|
343
|
+
* A scalar value statically decodable from a literal argument node. Numbers
|
|
344
|
+
* carry the same JS value evaluation would produce (safe ints as `number`,
|
|
345
|
+
* larger ints as `bigint`); `null` is the projection of the `nil` literal.
|
|
346
|
+
*/
|
|
347
|
+
type ReferencedLiteral = string | number | bigint | boolean | null;
|
|
348
|
+
type ReferencedArg = {
|
|
349
|
+
readonly kind: "literal";
|
|
350
|
+
readonly value: ReferencedLiteral;
|
|
351
|
+
} | {
|
|
352
|
+
readonly kind: "call";
|
|
353
|
+
readonly name: string;
|
|
354
|
+
readonly args: readonly ReferencedArg[];
|
|
355
|
+
} | {
|
|
356
|
+
readonly kind: "dynamic";
|
|
357
|
+
};
|
|
323
358
|
declare class Template<T> {
|
|
324
359
|
readonly source: string;
|
|
325
360
|
private readonly _evaluate;
|
|
361
|
+
private readonly _defines;
|
|
326
362
|
private readonly _referencedFunctions;
|
|
327
363
|
private readonly _referencedCalls;
|
|
328
364
|
private constructor();
|
|
365
|
+
/**
|
|
366
|
+
* The named sub-templates this template can invoke — its own `{{define}}`s
|
|
367
|
+
* chained onto whatever it inherited at parse. Pass it to `engine.parse` as
|
|
368
|
+
* `inherit` to let another template invoke the same set without re-parsing
|
|
369
|
+
* (and re-allocating) it: the idiom for a shared helper preamble is
|
|
370
|
+
* `const helpers = engine.parse(preamble).defines()` once, then
|
|
371
|
+
* `engine.parse(src, helpers)` per template. Opaque: the AST inside is not
|
|
372
|
+
* part of the public surface.
|
|
373
|
+
*/
|
|
374
|
+
defines(): Defines;
|
|
329
375
|
evaluate(scope: unknown): T[];
|
|
330
376
|
/**
|
|
331
377
|
* The FuncMap function names this template references — every identifier that
|
|
332
|
-
* names a function
|
|
378
|
+
* names a function in this parse: the template body and its own `{{ define }}`
|
|
379
|
+
* blocks. A set inherited at parse (see {@link Template.defines}) is described
|
|
380
|
+
* by the template that parsed it, not here.
|
|
333
381
|
*
|
|
334
382
|
* This is a STATIC fact derived from the parsed AST, not an execution trace:
|
|
335
383
|
* a name in the set is a function the template *can* invoke; a name absent
|
|
@@ -345,8 +393,8 @@ declare class Template<T> {
|
|
|
345
393
|
*/
|
|
346
394
|
referencedFunctions(): ReadonlySet<string>;
|
|
347
395
|
/**
|
|
348
|
-
* Every command-head call in the template body and its
|
|
349
|
-
* in preorder, each paired with its positional arguments projected to literal
|
|
396
|
+
* Every command-head call in this parse — the template body and its own
|
|
397
|
+
* `{{ define }}` blocks, never an inherited set — in preorder, each paired with its positional arguments projected to literal
|
|
350
398
|
* strings (a non-string-literal argument is reported as `null`, preserving
|
|
351
399
|
* argument positions).
|
|
352
400
|
*
|
|
@@ -354,9 +402,12 @@ declare class Template<T> {
|
|
|
354
402
|
* not an execution trace. Use it when "is X called?" is not enough and you
|
|
355
403
|
* need the literal arguments a call was written with — e.g. to discover that a
|
|
356
404
|
* template contains `{{ menu "applyTheme" "themePage" }}` and read its first
|
|
357
|
-
* argument without evaluating the template.
|
|
358
|
-
*
|
|
359
|
-
*
|
|
405
|
+
* argument without evaluating the template. Each call's `argExprs` carries the
|
|
406
|
+
* full {@link ReferencedArg} projection (scalar literals, nested literal calls
|
|
407
|
+
* like `(dict "k" "v")`, `dynamic` for eval-time values); `args` is its
|
|
408
|
+
* string-literal-only view. Calls whose head is not a bare function
|
|
409
|
+
* identifier (e.g. a field invoked via `call`) are not reported here; their
|
|
410
|
+
* names still appear in {@link referencedFunctions}.
|
|
360
411
|
*/
|
|
361
412
|
referencedCalls(): readonly ReferencedCall[];
|
|
362
413
|
}
|
|
@@ -374,7 +425,7 @@ declare class Engine<T> {
|
|
|
374
425
|
* it any number of times with different scopes is safe and avoids
|
|
375
426
|
* re-parsing.
|
|
376
427
|
*/
|
|
377
|
-
parse(source: string): Template<T>;
|
|
428
|
+
parse(source: string, inherit?: Defines): Template<T>;
|
|
378
429
|
/**
|
|
379
430
|
* Convenience sugar for `parse(src).evaluate.bind(template)`.
|
|
380
431
|
*
|
|
@@ -434,6 +485,25 @@ declare function sprigDatetime(clock?: Clock): FuncMap;
|
|
|
434
485
|
//#region src/sprig/defaults/index.d.ts
|
|
435
486
|
declare function sprigDefaults(): FuncMap;
|
|
436
487
|
//#endregion
|
|
488
|
+
//#region src/sprig/dicts/dict.d.ts
|
|
489
|
+
/**
|
|
490
|
+
* Statically read a `(dict k1 v1 …)` call argument's key/value pairs from a
|
|
491
|
+
* {@link ReferencedArg} projection (see `Template.referencedCalls`).
|
|
492
|
+
*
|
|
493
|
+
* Returns the entries only when the argument is provably a literal dict: a
|
|
494
|
+
* nested call named `dict` whose args are ALL literals, string-typed at every
|
|
495
|
+
* key position, in complete pairs. Anything else — a non-`dict` call, a
|
|
496
|
+
* dynamic entry (`(dict "k" .x)`), a non-string key (which would fail the
|
|
497
|
+
* runtime arg gate anyway), an odd-length pair list — returns `null`: the
|
|
498
|
+
* value is not statically readable, and a value is never guessed
|
|
499
|
+
* [LAW:no-silent-failure].
|
|
500
|
+
*
|
|
501
|
+
* [LAW:one-source-of-truth] Lives beside — and folds through — the runtime
|
|
502
|
+
* `dict` above, so the static reading and the evaluated value share one
|
|
503
|
+
* pairing semantics (including later-wins on duplicate keys).
|
|
504
|
+
*/
|
|
505
|
+
declare function staticDictEntries(arg: ReferencedArg): Record<string, ReferencedLiteral> | null;
|
|
506
|
+
//#endregion
|
|
437
507
|
//#region src/sprig/dicts/index.d.ts
|
|
438
508
|
declare function sprigDicts(): FuncMap;
|
|
439
509
|
//#endregion
|
|
@@ -471,5 +541,5 @@ declare function sprigStrings(): FuncMap;
|
|
|
471
541
|
//#region src/sprig/types/index.d.ts
|
|
472
542
|
declare function sprigTypes(): FuncMap;
|
|
473
543
|
//#endregion
|
|
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 };
|
|
544
|
+
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 };
|
|
475
545
|
//# 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
|
|
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;;;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,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BCxEhB,UAAA,CAAA,GAAc,OAAA"}
|
package/dist/index.js
CHANGED
|
@@ -558,17 +558,59 @@ function isIdentPart(c) {
|
|
|
558
558
|
}
|
|
559
559
|
//#endregion
|
|
560
560
|
//#region src/parser/parser.ts
|
|
561
|
-
|
|
562
|
-
|
|
561
|
+
/**
|
|
562
|
+
* The named sub-templates a template can invoke: the ones its own source
|
|
563
|
+
* declared plus, transitively, the ones it inherited at parse. Opaque: only
|
|
564
|
+
* `has` and the `EMPTY` identity are public, so a consumer can neither build
|
|
565
|
+
* one by hand (the parser is the single enforcer of the redefinition invariant
|
|
566
|
+
* below) nor reach the AST inside — the entry lookup is `lookupDefine`, a
|
|
567
|
+
* module export index.ts never re-exports.
|
|
568
|
+
*
|
|
569
|
+
* [LAW:one-source-of-truth] Inheritance is a parent LINK, never a copy. N
|
|
570
|
+
* templates parsed against one shared preamble hold one preamble AST among
|
|
571
|
+
* them — the alternative (prepending the preamble's source to each parse)
|
|
572
|
+
* multiplies the preamble's AST by N, which is how a 2 KB helper block became
|
|
573
|
+
* hundreds of megabytes in a long-lived process.
|
|
574
|
+
*
|
|
575
|
+
* Lookup is nearest-first, but the parser forbids a template from declaring a
|
|
576
|
+
* name its inherited set already holds (the same `redefinition` error as two
|
|
577
|
+
* declarations in one source), so nearest-first never actually shadows.
|
|
578
|
+
*/
|
|
579
|
+
let chainDefines;
|
|
580
|
+
let readDefine;
|
|
581
|
+
/** Nearest-first entry lookup through the inheritance chain. Package-internal. */
|
|
582
|
+
function lookupDefine(defines, name) {
|
|
583
|
+
return readDefine(defines, name);
|
|
584
|
+
}
|
|
585
|
+
var Defines = class Defines {
|
|
586
|
+
static EMPTY = new Defines(/* @__PURE__ */ new Map(), void 0);
|
|
587
|
+
#own;
|
|
588
|
+
#parent;
|
|
589
|
+
constructor(own, parent) {
|
|
590
|
+
this.#own = own;
|
|
591
|
+
this.#parent = parent;
|
|
592
|
+
}
|
|
593
|
+
static {
|
|
594
|
+
chainDefines = (own, parent) => new Defines(own, parent);
|
|
595
|
+
readDefine = (d, name) => d.#own.get(name) ?? (d.#parent && readDefine(d.#parent, name));
|
|
596
|
+
}
|
|
597
|
+
has(name) {
|
|
598
|
+
return readDefine(this, name) !== void 0;
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
function parse(source, delims, inherit = Defines.EMPTY) {
|
|
602
|
+
return new Parser(source, delims, inherit).parseTemplate();
|
|
563
603
|
}
|
|
564
604
|
var Parser = class {
|
|
565
605
|
lex;
|
|
566
606
|
buf = [];
|
|
567
607
|
defines = /* @__PURE__ */ new Map();
|
|
608
|
+
inherit;
|
|
568
609
|
source;
|
|
569
610
|
rangeDepth = 0;
|
|
570
|
-
constructor(source, delims) {
|
|
611
|
+
constructor(source, delims, inherit) {
|
|
571
612
|
this.source = source;
|
|
613
|
+
this.inherit = inherit;
|
|
572
614
|
this.lex = delims ? new Lexer(source, delims) : new Lexer(source);
|
|
573
615
|
}
|
|
574
616
|
peek(n = 0) {
|
|
@@ -606,7 +648,8 @@ var Parser = class {
|
|
|
606
648
|
}
|
|
607
649
|
return {
|
|
608
650
|
root,
|
|
609
|
-
defines: this.defines,
|
|
651
|
+
defines: chainDefines(this.defines, this.inherit),
|
|
652
|
+
ownDefines: this.defines,
|
|
610
653
|
source: this.source
|
|
611
654
|
};
|
|
612
655
|
}
|
|
@@ -822,7 +865,10 @@ var Parser = class {
|
|
|
822
865
|
trimLeft,
|
|
823
866
|
trimRight: this.consumeEnd()
|
|
824
867
|
};
|
|
825
|
-
if (!this.defines.has(name)) this.defines.set(name,
|
|
868
|
+
if (!this.defines.has(name) && !this.inherit.has(name)) this.defines.set(name, {
|
|
869
|
+
list,
|
|
870
|
+
source: this.source
|
|
871
|
+
});
|
|
826
872
|
return pipe ? {
|
|
827
873
|
type: "Block",
|
|
828
874
|
pos: startPos,
|
|
@@ -846,8 +892,11 @@ var Parser = class {
|
|
|
846
892
|
const list = this.parseList();
|
|
847
893
|
this.rangeDepth = savedDepth;
|
|
848
894
|
this.consumeEnd();
|
|
849
|
-
if (this.defines.has(name)) throw new ParseError(`redefinition of template ${JSON.stringify(name)}`, list.pos, { source: this.source });
|
|
850
|
-
this.defines.set(name,
|
|
895
|
+
if (this.defines.has(name) || this.inherit.has(name)) throw new ParseError(`redefinition of template ${JSON.stringify(name)}`, list.pos, { source: this.source });
|
|
896
|
+
this.defines.set(name, {
|
|
897
|
+
list,
|
|
898
|
+
source: this.source
|
|
899
|
+
});
|
|
851
900
|
}
|
|
852
901
|
parseQuotedName() {
|
|
853
902
|
const t = this.next();
|
|
@@ -1775,23 +1824,39 @@ let internalCreateTemplate;
|
|
|
1775
1824
|
var Template = class Template {
|
|
1776
1825
|
source;
|
|
1777
1826
|
_evaluate;
|
|
1827
|
+
_defines;
|
|
1778
1828
|
_referencedFunctions;
|
|
1779
1829
|
_referencedCalls;
|
|
1780
|
-
constructor(source, evaluate, referencedFunctions, referencedCalls) {
|
|
1830
|
+
constructor(source, evaluate, defines, referencedFunctions, referencedCalls) {
|
|
1781
1831
|
this.source = source;
|
|
1782
1832
|
this._evaluate = evaluate;
|
|
1833
|
+
this._defines = defines;
|
|
1783
1834
|
this._referencedFunctions = referencedFunctions;
|
|
1784
1835
|
this._referencedCalls = referencedCalls;
|
|
1785
1836
|
}
|
|
1786
1837
|
static {
|
|
1787
|
-
internalCreateTemplate = (source, evaluate, referencedFunctions, referencedCalls) => new Template(source, evaluate, referencedFunctions, referencedCalls);
|
|
1838
|
+
internalCreateTemplate = (source, evaluate, defines, referencedFunctions, referencedCalls) => new Template(source, evaluate, defines, referencedFunctions, referencedCalls);
|
|
1839
|
+
}
|
|
1840
|
+
/**
|
|
1841
|
+
* The named sub-templates this template can invoke — its own `{{define}}`s
|
|
1842
|
+
* chained onto whatever it inherited at parse. Pass it to `engine.parse` as
|
|
1843
|
+
* `inherit` to let another template invoke the same set without re-parsing
|
|
1844
|
+
* (and re-allocating) it: the idiom for a shared helper preamble is
|
|
1845
|
+
* `const helpers = engine.parse(preamble).defines()` once, then
|
|
1846
|
+
* `engine.parse(src, helpers)` per template. Opaque: the AST inside is not
|
|
1847
|
+
* part of the public surface.
|
|
1848
|
+
*/
|
|
1849
|
+
defines() {
|
|
1850
|
+
return this._defines;
|
|
1788
1851
|
}
|
|
1789
1852
|
evaluate(scope) {
|
|
1790
1853
|
return this._evaluate(scope);
|
|
1791
1854
|
}
|
|
1792
1855
|
/**
|
|
1793
1856
|
* The FuncMap function names this template references — every identifier that
|
|
1794
|
-
* names a function
|
|
1857
|
+
* names a function in this parse: the template body and its own `{{ define }}`
|
|
1858
|
+
* blocks. A set inherited at parse (see {@link Template.defines}) is described
|
|
1859
|
+
* by the template that parsed it, not here.
|
|
1795
1860
|
*
|
|
1796
1861
|
* This is a STATIC fact derived from the parsed AST, not an execution trace:
|
|
1797
1862
|
* a name in the set is a function the template *can* invoke; a name absent
|
|
@@ -1809,8 +1874,8 @@ var Template = class Template {
|
|
|
1809
1874
|
return this._referencedFunctions;
|
|
1810
1875
|
}
|
|
1811
1876
|
/**
|
|
1812
|
-
* Every command-head call in the template body and its
|
|
1813
|
-
* in preorder, each paired with its positional arguments projected to literal
|
|
1877
|
+
* Every command-head call in this parse — the template body and its own
|
|
1878
|
+
* `{{ define }}` blocks, never an inherited set — in preorder, each paired with its positional arguments projected to literal
|
|
1814
1879
|
* strings (a non-string-literal argument is reported as `null`, preserving
|
|
1815
1880
|
* argument positions).
|
|
1816
1881
|
*
|
|
@@ -1818,9 +1883,12 @@ var Template = class Template {
|
|
|
1818
1883
|
* not an execution trace. Use it when "is X called?" is not enough and you
|
|
1819
1884
|
* need the literal arguments a call was written with — e.g. to discover that a
|
|
1820
1885
|
* template contains `{{ menu "applyTheme" "themePage" }}` and read its first
|
|
1821
|
-
* argument without evaluating the template.
|
|
1822
|
-
*
|
|
1823
|
-
*
|
|
1886
|
+
* argument without evaluating the template. Each call's `argExprs` carries the
|
|
1887
|
+
* full {@link ReferencedArg} projection (scalar literals, nested literal calls
|
|
1888
|
+
* like `(dict "k" "v")`, `dynamic` for eval-time values); `args` is its
|
|
1889
|
+
* string-literal-only view. Calls whose head is not a bare function
|
|
1890
|
+
* identifier (e.g. a field invoked via `call`) are not reported here; their
|
|
1891
|
+
* names still appear in {@link referencedFunctions}.
|
|
1824
1892
|
*/
|
|
1825
1893
|
referencedCalls() {
|
|
1826
1894
|
return this._referencedCalls;
|
|
@@ -1834,9 +1902,46 @@ function collectReferencedFunctions(parsed) {
|
|
|
1834
1902
|
});
|
|
1835
1903
|
};
|
|
1836
1904
|
collect(parsed.root);
|
|
1837
|
-
for (const
|
|
1905
|
+
for (const entry of parsed.ownDefines.values()) collect(entry.list);
|
|
1838
1906
|
return names;
|
|
1839
1907
|
}
|
|
1908
|
+
const DYNAMIC_ARG = { kind: "dynamic" };
|
|
1909
|
+
function projectArg(node) {
|
|
1910
|
+
switch (node.type) {
|
|
1911
|
+
case "String": return {
|
|
1912
|
+
kind: "literal",
|
|
1913
|
+
value: node.value
|
|
1914
|
+
};
|
|
1915
|
+
case "Bool": return {
|
|
1916
|
+
kind: "literal",
|
|
1917
|
+
value: node.value
|
|
1918
|
+
};
|
|
1919
|
+
case "Nil": return {
|
|
1920
|
+
kind: "literal",
|
|
1921
|
+
value: null
|
|
1922
|
+
};
|
|
1923
|
+
case "Number": {
|
|
1924
|
+
const value = numberValue(node);
|
|
1925
|
+
return typeof value === "number" || typeof value === "bigint" ? {
|
|
1926
|
+
kind: "literal",
|
|
1927
|
+
value
|
|
1928
|
+
} : DYNAMIC_ARG;
|
|
1929
|
+
}
|
|
1930
|
+
case "Pipe": {
|
|
1931
|
+
if (node.decls.length !== 0 || node.cmds.length !== 1) return DYNAMIC_ARG;
|
|
1932
|
+
const cmd = node.cmds[0];
|
|
1933
|
+
if (cmd === void 0) return DYNAMIC_ARG;
|
|
1934
|
+
const head = cmd.args[0];
|
|
1935
|
+
if (head === void 0 || head.type !== "Identifier") return DYNAMIC_ARG;
|
|
1936
|
+
return {
|
|
1937
|
+
kind: "call",
|
|
1938
|
+
name: head.ident,
|
|
1939
|
+
args: cmd.args.slice(1).map(projectArg)
|
|
1940
|
+
};
|
|
1941
|
+
}
|
|
1942
|
+
default: return DYNAMIC_ARG;
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1840
1945
|
function collectReferencedCalls(parsed) {
|
|
1841
1946
|
const calls = [];
|
|
1842
1947
|
const collect = (root) => {
|
|
@@ -1844,14 +1949,16 @@ function collectReferencedCalls(parsed) {
|
|
|
1844
1949
|
if (node.type !== "Command") return;
|
|
1845
1950
|
const head = node.args[0];
|
|
1846
1951
|
if (head === void 0 || head.type !== "Identifier") return;
|
|
1952
|
+
const argExprs = node.args.slice(1).map(projectArg);
|
|
1847
1953
|
calls.push({
|
|
1848
1954
|
name: head.ident,
|
|
1849
|
-
args:
|
|
1955
|
+
args: argExprs.map((arg) => arg.kind === "literal" && typeof arg.value === "string" ? arg.value : null),
|
|
1956
|
+
argExprs
|
|
1850
1957
|
});
|
|
1851
1958
|
});
|
|
1852
1959
|
};
|
|
1853
1960
|
collect(parsed.root);
|
|
1854
|
-
for (const
|
|
1961
|
+
for (const entry of parsed.ownDefines.values()) collect(entry.list);
|
|
1855
1962
|
return calls;
|
|
1856
1963
|
}
|
|
1857
1964
|
var Engine = class {
|
|
@@ -1878,9 +1985,9 @@ var Engine = class {
|
|
|
1878
1985
|
* it any number of times with different scopes is safe and avoids
|
|
1879
1986
|
* re-parsing.
|
|
1880
1987
|
*/
|
|
1881
|
-
parse(source) {
|
|
1882
|
-
const parsed = parse(source, this.delims);
|
|
1883
|
-
return internalCreateTemplate(parsed.source, (scope) => this.evalParsed(parsed, scope), collectReferencedFunctions(parsed), collectReferencedCalls(parsed));
|
|
1988
|
+
parse(source, inherit) {
|
|
1989
|
+
const parsed = parse(source, this.delims, inherit);
|
|
1990
|
+
return internalCreateTemplate(parsed.source, (scope) => this.evalParsed(parsed, scope), parsed.defines, collectReferencedFunctions(parsed), collectReferencedCalls(parsed));
|
|
1884
1991
|
}
|
|
1885
1992
|
/**
|
|
1886
1993
|
* Convenience sugar for `parse(src).evaluate.bind(template)`.
|
|
@@ -2002,15 +2109,26 @@ var Engine = class {
|
|
|
2002
2109
|
}
|
|
2003
2110
|
}
|
|
2004
2111
|
evalTemplateInvoke(node, scope, ctx) {
|
|
2005
|
-
const
|
|
2006
|
-
if (!
|
|
2112
|
+
const entry = lookupDefine(ctx.defines, node.name);
|
|
2113
|
+
if (!entry) throw new EvalError(`template ${JSON.stringify(node.name)} is not defined`, node.pos, { source: ctx.source });
|
|
2007
2114
|
const child = pushScope(scope, node.pipe ? this.evalPipe(node.pipe, scope, ctx) : scope.dot);
|
|
2008
|
-
this.evalList(
|
|
2115
|
+
this.evalList(entry.list, child, {
|
|
2116
|
+
out: ctx.out,
|
|
2117
|
+
defines: ctx.defines,
|
|
2118
|
+
source: entry.source
|
|
2119
|
+
});
|
|
2009
2120
|
}
|
|
2010
2121
|
evalBlock(node, scope, ctx) {
|
|
2011
2122
|
const child = pushScope(scope, node.pipe ? this.evalPipe(node.pipe, scope, ctx) : scope.dot);
|
|
2012
|
-
const
|
|
2013
|
-
|
|
2123
|
+
const entry = lookupDefine(ctx.defines, node.name) ?? {
|
|
2124
|
+
list: node.list,
|
|
2125
|
+
source: ctx.source
|
|
2126
|
+
};
|
|
2127
|
+
this.evalList(entry.list, child, {
|
|
2128
|
+
out: ctx.out,
|
|
2129
|
+
defines: ctx.defines,
|
|
2130
|
+
source: entry.source
|
|
2131
|
+
});
|
|
2014
2132
|
}
|
|
2015
2133
|
evalAction(node, scope, ctx) {
|
|
2016
2134
|
return this.evalPipe(node.pipe, scope, ctx);
|
|
@@ -3645,22 +3763,37 @@ function sprigDefaults() {
|
|
|
3645
3763
|
}
|
|
3646
3764
|
//#endregion
|
|
3647
3765
|
//#region src/sprig/dicts/dict.ts
|
|
3648
|
-
/**
|
|
3649
|
-
* `dict k1 v1 k2 v2 …` — builds a record from alternating key/value pairs.
|
|
3650
|
-
*
|
|
3651
|
-
* [LAW:single-enforcer] The boundary gate (`enforceArgTypes`) validates
|
|
3652
|
-
* the kv cycle via `argTypePattern: "alternating"` against
|
|
3653
|
-
* `argTypes: ["string", "value"]`, so every key position is a string by
|
|
3654
|
-
* the time the body runs. The body trusts the gate; no per-key probe.
|
|
3655
|
-
*
|
|
3656
|
-
* Odd-length call (`dict "a"`) leaves the last value `undefined` —
|
|
3657
|
-
* matches Go sprig.
|
|
3658
|
-
*/
|
|
3659
3766
|
function dict(...kvs) {
|
|
3660
3767
|
const out = {};
|
|
3661
3768
|
for (let i = 0; i < kvs.length; i += 2) out[kvs[i]] = kvs[i + 1];
|
|
3662
3769
|
return out;
|
|
3663
3770
|
}
|
|
3771
|
+
/**
|
|
3772
|
+
* Statically read a `(dict k1 v1 …)` call argument's key/value pairs from a
|
|
3773
|
+
* {@link ReferencedArg} projection (see `Template.referencedCalls`).
|
|
3774
|
+
*
|
|
3775
|
+
* Returns the entries only when the argument is provably a literal dict: a
|
|
3776
|
+
* nested call named `dict` whose args are ALL literals, string-typed at every
|
|
3777
|
+
* key position, in complete pairs. Anything else — a non-`dict` call, a
|
|
3778
|
+
* dynamic entry (`(dict "k" .x)`), a non-string key (which would fail the
|
|
3779
|
+
* runtime arg gate anyway), an odd-length pair list — returns `null`: the
|
|
3780
|
+
* value is not statically readable, and a value is never guessed
|
|
3781
|
+
* [LAW:no-silent-failure].
|
|
3782
|
+
*
|
|
3783
|
+
* [LAW:one-source-of-truth] Lives beside — and folds through — the runtime
|
|
3784
|
+
* `dict` above, so the static reading and the evaluated value share one
|
|
3785
|
+
* pairing semantics (including later-wins on duplicate keys).
|
|
3786
|
+
*/
|
|
3787
|
+
function staticDictEntries(arg) {
|
|
3788
|
+
if (arg.kind !== "call" || arg.name !== "dict" || arg.args.length % 2 !== 0) return null;
|
|
3789
|
+
const values = [];
|
|
3790
|
+
for (const [i, entry] of arg.args.entries()) {
|
|
3791
|
+
if (entry.kind !== "literal") return null;
|
|
3792
|
+
if (i % 2 === 0 && typeof entry.value !== "string") return null;
|
|
3793
|
+
values.push(entry.value);
|
|
3794
|
+
}
|
|
3795
|
+
return dict(...values);
|
|
3796
|
+
}
|
|
3664
3797
|
//#endregion
|
|
3665
3798
|
//#region src/sprig/dicts/get.ts
|
|
3666
3799
|
/**
|
|
@@ -5448,9 +5581,19 @@ function indent(n, s) {
|
|
|
5448
5581
|
}
|
|
5449
5582
|
//#endregion
|
|
5450
5583
|
//#region src/sprig/strings/initials.ts
|
|
5451
|
-
/**
|
|
5584
|
+
/**
|
|
5585
|
+
* `initials s` — port of `goutils.Initials` (what Go sprig's `initials`
|
|
5586
|
+
* delegates to): the first character of each whitespace-separated word,
|
|
5587
|
+
* case preserved — `"Ada Lovelace"` -> `"AL"`, `"foo bar baz"` -> `"fbb"`.
|
|
5588
|
+
*
|
|
5589
|
+
* Byte-faithful: goutils indexes the word's first BYTE, so a multi-byte
|
|
5590
|
+
* leading character yields that byte as a code point — `"Ölga"` -> `"Ã"`,
|
|
5591
|
+
* `"𐍈foo"` -> `"ð"` (both verified against the Go conformance generator),
|
|
5592
|
+
* never the full glyph.
|
|
5593
|
+
*/
|
|
5452
5594
|
function initials(s) {
|
|
5453
|
-
|
|
5595
|
+
const enc = new TextEncoder();
|
|
5596
|
+
return s.split(/\s+/).filter((w) => w.length > 0).map((w) => String.fromCharCode(...enc.encode(w).slice(0, 1))).join("");
|
|
5454
5597
|
}
|
|
5455
5598
|
//#endregion
|
|
5456
5599
|
//#region src/sprig/strings/join.ts
|
|
@@ -6032,6 +6175,6 @@ function sprigTypes() {
|
|
|
6032
6175
|
};
|
|
6033
6176
|
}
|
|
6034
6177
|
//#endregion
|
|
6035
|
-
export { Engine, EvalError, FailError, FuncNotFoundError, MissingFieldError, ParseError, Template, TemplateError, TypeMismatchError, createEngine, sprigConversions, sprigDatetime, sprigDefaults, sprigDicts, sprigFlow, sprigHash, sprigLists, sprigMath, sprigRandom, sprigRegex, sprigSemver, sprigStrings, sprigTypes };
|
|
6178
|
+
export { Defines, Engine, EvalError, FailError, FuncNotFoundError, MissingFieldError, ParseError, Template, TemplateError, TypeMismatchError, createEngine, sprigConversions, sprigDatetime, sprigDefaults, sprigDicts, sprigFlow, sprigHash, sprigLists, sprigMath, sprigRandom, sprigRegex, sprigSemver, sprigStrings, sprigTypes, staticDictEntries };
|
|
6036
6179
|
|
|
6037
6180
|
//# sourceMappingURL=index.js.map
|