@zvenigora/ng-eval-core 0.11.1 → 0.12.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/CHANGELOG.md +52 -0
- package/README.md +45 -0
- package/fesm2022/zvenigora-ng-eval-core.mjs +390 -15
- package/fesm2022/zvenigora-ng-eval-core.mjs.map +1 -1
- package/package.json +1 -1
- package/types/zvenigora-ng-eval-core.d.ts +54 -7
package/CHANGELOG.md
CHANGED
|
@@ -14,6 +14,58 @@ Versions 0.1.104–0.1.107 are on npm without entries here.
|
|
|
14
14
|
|
|
15
15
|
---
|
|
16
16
|
|
|
17
|
+
## [0.12.0] - 2026-10-13
|
|
18
|
+
|
|
19
|
+
**Colon-joined names, opt-in — Phase 9 ([plan](../../docs/colon-identifiers/phase-9-plan.md)) — and
|
|
20
|
+
a parse cache that honours the options it is asked with.** A minor: two additions, the
|
|
21
|
+
compiled-string key that goes with them, and the [A26](../../docs/backlog-retired.md#a26) fix. No
|
|
22
|
+
exported symbol changes shape: the `.d.ts` differs from 0.11.1 by `ParserOptions.colonIdentifiers`,
|
|
23
|
+
`provideColonIdentifiers` and documentation comments, beside a private member each on
|
|
24
|
+
`ParserService` and `CompilerService`. The peer ranges are unchanged, `acorn ^8.11.2` included: the
|
|
25
|
+
grammar's spec and corpus pass at 8.11.2 (the plan's § 8 q3). `eval-signals` 0.5.1 and `eval-forms` 0.5.0 admit it;
|
|
26
|
+
`eval-signals` 0.4.0–0.5.0 and `eval-forms` 0.4.x declare `>=0.11.0 <0.12.0` and do not.
|
|
27
|
+
|
|
28
|
+
### Added
|
|
29
|
+
- **`ParserOptions.colonIdentifiers`.** With it on, a colon-joined name such as `Q1:1` or `VP14:0`
|
|
30
|
+
parses as one `Identifier` whose `name` is the whole key, so it reads `context['Q1:1']`. It is
|
|
31
|
+
off by default, and with it off a parse is acorn's own, as before. Where JavaScript already gives
|
|
32
|
+
the colon a meaning, the name gives way or is refused and is never read as something else: in a
|
|
33
|
+
conditional's consequent it is written in parentheses, `c ? (Q1:1) : 0`, and as an object key it
|
|
34
|
+
is a `SyntaxError` — quote it. So under the option a compact object literal, `({a:1})`, needs a
|
|
35
|
+
space after the key's colon. Hooks, dependency tracking and every prototype-pollution refusal see
|
|
36
|
+
an ordinary identifier. The README's "Colon-joined names" gives the rule.
|
|
37
|
+
- **`provideColonIdentifiers()`**, which turns `colonIdentifiers` on for an application.
|
|
38
|
+
`ParserService` reads it when it is constructed, so every parse through `EvalService`,
|
|
39
|
+
`CompilerService` and `DiscoveryService` follows it, and a per-call `colonIdentifiers` overrides
|
|
40
|
+
it. It belongs in the root providers: in a child environment injector, such as a lazy route's, it
|
|
41
|
+
throws when that injector is created. `new ParserService()` outside an injection context keeps
|
|
42
|
+
the option off.
|
|
43
|
+
- **`CompilerService` keys a compiled string on the options its parse uses**, as `ParserService`
|
|
44
|
+
keys an AST, so a text compiled before `ParserService.parserOptions` was replaced through its
|
|
45
|
+
setter — with `colonIdentifiers` turned on, for one — is compiled again rather than served the
|
|
46
|
+
old function. A write in place into that object is not seen, as it is not for an AST
|
|
47
|
+
([A34](../../docs/backlog.md#a34)). Two narrower
|
|
48
|
+
changes: a subclass that sets `CompilerService`'s protected per-call options is served a function
|
|
49
|
+
for its current ones, compiled on every call when they carry an acorn callback or `program`, and
|
|
50
|
+
a value JSON cannot hold, set in `ParserService.parserOptions` under a key `defaultParserOptions`
|
|
51
|
+
does not carry, now compiles uncached.
|
|
52
|
+
|
|
53
|
+
### Fixed
|
|
54
|
+
- **`ParserService` serves a cached AST only to a parse with the options that produced it —
|
|
55
|
+
[A26](../../docs/backlog-retired.md#a26).** The parse cache was keyed on the expression alone, so
|
|
56
|
+
the first parse of a text decided what every later caller got for it, whatever options they
|
|
57
|
+
passed: after one `ParserService.parse('await p', { ecmaVersion: 2022, allowAwaitOutsideFunction:
|
|
58
|
+
true })`, `EvalService.simpleEval('await p')` returned a promise instead of throwing its
|
|
59
|
+
`SyntaxError`, and a direct `ParserService.parse` caller and the services, which ask for
|
|
60
|
+
differently shaped results, were each served the other's. The key now holds every option that
|
|
61
|
+
changes the parse, which is all but `cacheSize`, acorn's four callbacks and `program`. A parse
|
|
62
|
+
carrying a callback or a `program` neither reads nor writes the cache, so the callback runs on
|
|
63
|
+
every parse. A change made through the `parserOptions` setter is honoured for text already
|
|
64
|
+
cached; a write in place into an options object, the service's or one passed again, is not
|
|
65
|
+
([A34](../../docs/backlog.md#a34)). Every published version to 0.11.1 is affected.
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
17
69
|
## [0.11.1] - 2026-10-10
|
|
18
70
|
|
|
19
71
|
**Two cache fixes: `CompilerService` no longer returns one AST's compiled function for another, and
|
package/README.md
CHANGED
|
@@ -78,6 +78,51 @@ const result = service.simpleEval('cat.action(args, cat.num, "times")', evalCont
|
|
|
78
78
|
|
|
79
79
|
Note that an `EvalContext` may back any number of evaluations, and that `caseInsensitive` set on the context alone does not reach the walk — pass it in the evaluation options too.
|
|
80
80
|
|
|
81
|
+
### Colon-joined names — `provideColonIdentifiers`
|
|
82
|
+
|
|
83
|
+
Some expression languages name their values `Q1:1` or `VP14:0`. With `colonIdentifiers` on, such a name parses as one identifier and reads the context key of the same spelling, `context['Q1:1']`. It is off by default, and with it off every expression parses as it always has.
|
|
84
|
+
|
|
85
|
+
`provideColonIdentifiers()` turns it on for an application: `ParserService` reads it when it is constructed, so every parse through `EvalService`, `CompilerService` and `DiscoveryService` follows it. Put it in the application's **root** providers. `ParserService` is provided in the root, so in a lazy route's providers, or any other child injector's, the provider would reach no parse; there it throws when that injector is created instead.
|
|
86
|
+
|
|
87
|
+
A per-call `colonIdentifiers` in the options of `ParserService.parse`, or of the exported `parse`, overrides it — and the exported `parse` sees only the options it is given. Setting `ParserService.parserOptions` to a new object is the imperative alternative: every later parse or compile follows it, of a text seen before as of a new one, while a function already compiled keeps the reading it was compiled with.
|
|
88
|
+
|
|
89
|
+
```javascript
|
|
90
|
+
import { EvalService, ParserService,
|
|
91
|
+
provideColonIdentifiers } from '@zvenigora/ng-eval-core';
|
|
92
|
+
|
|
93
|
+
// app.config.ts: the application's root providers, not a lazy route's
|
|
94
|
+
export const appConfig = { providers: [provideColonIdentifiers()] };
|
|
95
|
+
|
|
96
|
+
const parser = inject(ParserService);
|
|
97
|
+
parser.parserOptions.colonIdentifiers; // true - the effective setting
|
|
98
|
+
|
|
99
|
+
const service = inject(EvalService);
|
|
100
|
+
service.simpleEval('Q1:1 + 1', { 'Q1:1': 2 }); // 3
|
|
101
|
+
service.simpleEval('Q1:1 + 1', { Q1: 100 }); // NaN - 'Q1:1' is a key of its own
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
**The rule.** A colon-joined name is a first segment — an ASCII letter, `_` or `$`, then ASCII letters, digits, `_` or `$` — followed by one or more segments of the same characters, each after a `:`, which may start with a digit: `Q1:1`, `VP14:0`, `Q1:1:3`, `Q1:_x`. Nothing may sit inside it, no space, comment or line break, and the longest match wins, so `Q1:1:3` is one name. The name is an opaque key: `Q1:01` and `Q1:1` are different keys, and nothing splits one into segments. It joins wherever an identifier can stand — in an expression, after `.` and `?.` (`obj.Q1:1` reads `obj['Q1:1']`, and `sheet:A1.value` is `(sheet:A1).value`), as a binding (`let Q1:1 = 5`, `(Q1:1) => …`) and inside a template's `${…}` — and text stays text: `"Q1:1"`, a template's own text, a regex, a comment.
|
|
105
|
+
|
|
106
|
+
Some spellings keep the meaning they always had. A keyword does not start a name, so `true:1`, `this:1` and `typeof:1` are syntax errors as before, while a word acorn reads as a name, such as `let` or `async`, does start one. A first segment holding a non-ASCII character or a `\u` escape does not join, and neither does a colon followed by a non-ASCII character, so `Вопрос:1` and `Q1:Ответ` are labels; so is a colon with a space beside it: `Q1: 1` is the label `Q1`, under the option too. A non-ASCII character later in a segment ends the name there, and what follows it is a `SyntaxError`: `Q1:1é`.
|
|
107
|
+
|
|
108
|
+
**Where JavaScript already gives the colon a meaning**, the name gives way or is refused, and never reads silently as something else:
|
|
109
|
+
|
|
110
|
+
- **A conditional's consequent.** Between a `?` and the `:` that closes it, a name does not join at the conditional's own level, so `a?b:c` keeps its meaning. Put a colon-joined name there in parentheses, `c ? (Q1:1) : 0`; inside a call, an array or a template the consequent opens it joins as anywhere else, as it does in the test and the alternate: `Q1:1 ? f(Q2:2) : Q3:3`. Without the parentheses the leftover colon is a `SyntaxError` at its own position, whose message names the fix — except when the `:` that closes the conditional starts a line of its own, where it is acorn's plain `Unexpected token`.
|
|
111
|
+
- **An object key.** `{Q1:1}` would be `{Q1: 1}` to JavaScript and the shorthand `{'Q1:1': Q1:1}` here, so a colon-joined key is a `SyntaxError` naming both fixes. Quote a colon-named key, `{'Q1:1': v}`. A value, a computed key and a spread join as usual: `({a: Q1:1})`, `({[Q1:1]: 2})`, `({...o, k: Q1:1})`. So under the option a compact literal or pattern — `({a:1})`, `let {a:b} = o` — needs a space after the key's colon.
|
|
112
|
+
|
|
113
|
+
```javascript
|
|
114
|
+
const ctx = { c: true, 'Q1:1': 5 };
|
|
115
|
+
|
|
116
|
+
service.simpleEval('c ? (Q1:1) : 0', ctx); // 5
|
|
117
|
+
service.simpleEval('c ? Q1:1 : 0', ctx); // throws SyntaxError: Unexpected token: a colon-joined
|
|
118
|
+
// name in the consequent of a conditional needs
|
|
119
|
+
// parentheses: (Q1:1) (1:9)
|
|
120
|
+
service.simpleEval("({ 'Q1:1': 2 })['Q1:1']"); // 2 - a colon-named key is quoted
|
|
121
|
+
service.simpleEval('({a:1}).a'); // throws SyntaxError - write ({a: 1})
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
**Read events and dependencies stay dotted.** A colon-joined name is one segment of a `path`: reading `obj.Q2:3` reports the `key` `Q2:3` and the `path` `obj.Q2:3`, and `createDependencyTracker()` records `obj.Q2:3`. Split a path on `.`, never on `:`.
|
|
125
|
+
|
|
81
126
|
## Options
|
|
82
127
|
|
|
83
128
|
### Per-node timing
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import {
|
|
2
|
+
import { InjectionToken, inject, makeEnvironmentProviders, provideEnvironmentInitializer, Injectable } from '@angular/core';
|
|
3
3
|
import { sha256 } from 'js-sha256';
|
|
4
|
-
import { parse as parse$1, defaultOptions, version } from 'acorn';
|
|
4
|
+
import { tokTypes, Parser, parse as parse$1, defaultOptions, version } from 'acorn';
|
|
5
5
|
import * as walk from 'acorn-walk';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -3006,16 +3006,211 @@ const updateExpressionVisitor = (node, st, callback) => {
|
|
|
3006
3006
|
afterVisitor(node, st);
|
|
3007
3007
|
};
|
|
3008
3008
|
|
|
3009
|
+
/**
|
|
3010
|
+
* @description
|
|
3011
|
+
* The colon-joined name grammar of `docs/colon-identifiers/phase-9-plan.md`
|
|
3012
|
+
* § 3.4, as an acorn plugin (§ 3.5). `parse` uses the parser built here when
|
|
3013
|
+
* `colonIdentifiers` is set, and acorn's own `parse` otherwise. No source text
|
|
3014
|
+
* is rewritten, so every position acorn reports is a position in what the
|
|
3015
|
+
* author wrote. Not re-exported from any barrel.
|
|
3016
|
+
*
|
|
3017
|
+
* Of acorn's documented API this module uses `Parser.extend`, `tokTypes` and
|
|
3018
|
+
* the `input` member. Everything else it touches is an acorn internal,
|
|
3019
|
+
* undeclared in acorn's `.d.ts`, and this is the complete list — what § 8 q3
|
|
3020
|
+
* checks at the `acorn` peer floor:
|
|
3021
|
+
*
|
|
3022
|
+
* - members read: `pos`, `start`, `type`, `value`, `keywords`;
|
|
3023
|
+
* - members written: `pos`, `containsEsc`;
|
|
3024
|
+
* - methods overridden, each calling acorn's own through `super`: `readWord`,
|
|
3025
|
+
* `finishToken`, `parsePropertyName`, `parseStatement`, `unexpected`;
|
|
3026
|
+
* - methods called: `finishToken`, `raise`;
|
|
3027
|
+
* - what the overrides assume of acorn's tokenizer: `readToken` is the only
|
|
3028
|
+
* caller of `readWord`, with `pos` at the token's first character; every
|
|
3029
|
+
* token is finished through `finishToken`, in source order, before the next
|
|
3030
|
+
* is read — except that a `/` read as `slash` is finished again as
|
|
3031
|
+
* `regexp`, and neither type is counted here; `?.`, `??` and `??=` are never
|
|
3032
|
+
* `question` tokens; the `}` that closes a `${` is finished as `braceR`; the
|
|
3033
|
+
* constructor reads no token;
|
|
3034
|
+
* - what they assume of acorn's parser: every key of an object literal, an
|
|
3035
|
+
* object pattern or a class body is read through `parsePropertyName`, with
|
|
3036
|
+
* the key as the current token; every statement through `parseStatement`,
|
|
3037
|
+
* with its first token as the current token; a colon the grammar cannot
|
|
3038
|
+
* place reaches `unexpected`, from `expect` or `semicolon`, with `type` and
|
|
3039
|
+
* `start` still at that colon.
|
|
3040
|
+
*
|
|
3041
|
+
* The token types it compares by identity, from `tokTypes`: `name`, `colon`,
|
|
3042
|
+
* `question`, `comma`, `semi`, `parenL`, `parenR`, `bracketL`, `bracketR`,
|
|
3043
|
+
* `braceL`, `braceR`, `dollarBraceL`.
|
|
3044
|
+
*/
|
|
3045
|
+
const COLON = 58;
|
|
3046
|
+
/** `[A-Za-z_$]`: the first character of a name that may join. */
|
|
3047
|
+
const isFirstChar = (code) => (code >= 65 && code <= 90) || (code >= 97 && code <= 122) || code === 95 || code === 36;
|
|
3048
|
+
/** `[A-Za-z0-9_$]`: any later character, and a segment's first. */
|
|
3049
|
+
const isSegmentChar = (code) => isFirstChar(code) || (code >= 48 && code <= 57);
|
|
3050
|
+
/** Whether a `:segment` starts at `pos`: a colon with a segment character after it. */
|
|
3051
|
+
const joinsAt = (input, pos) => input.charCodeAt(pos) === COLON && isSegmentChar(input.charCodeAt(pos + 1));
|
|
3052
|
+
const colonIdentifiers = (Base) => {
|
|
3053
|
+
class ColonParser extends Base {
|
|
3054
|
+
constructor() {
|
|
3055
|
+
super(...arguments);
|
|
3056
|
+
/** Never empty: the top level is never closed. */
|
|
3057
|
+
this.levels = [{ pending: 0 }];
|
|
3058
|
+
/**
|
|
3059
|
+
* The level the current token was read at: for a `(`, `[`, `{` or `${`,
|
|
3060
|
+
* the one around the level it opened.
|
|
3061
|
+
*/
|
|
3062
|
+
this.tokenLevel = this.levels[0];
|
|
3063
|
+
}
|
|
3064
|
+
get level() {
|
|
3065
|
+
return this.levels[this.levels.length - 1];
|
|
3066
|
+
}
|
|
3067
|
+
/**
|
|
3068
|
+
* Reads a colon-joined name as one `name` token when one starts at `pos`
|
|
3069
|
+
* and no `?` at this level is waiting for its `:` (§ 3.4, 1). In every
|
|
3070
|
+
* other case acorn's own `readWord`, so a word that does not join is read
|
|
3071
|
+
* exactly as acorn reads it.
|
|
3072
|
+
*/
|
|
3073
|
+
readWord() {
|
|
3074
|
+
const end = this.colonNameEnd();
|
|
3075
|
+
if (end === undefined) {
|
|
3076
|
+
return super.readWord();
|
|
3077
|
+
}
|
|
3078
|
+
const name = this.input.slice(this.pos, end);
|
|
3079
|
+
if (this.level.pending > 0) {
|
|
3080
|
+
this.level.suppressed = name;
|
|
3081
|
+
return super.readWord();
|
|
3082
|
+
}
|
|
3083
|
+
this.pos = end;
|
|
3084
|
+
this.containsEsc = false;
|
|
3085
|
+
this.finishToken(tokTypes.name, name);
|
|
3086
|
+
}
|
|
3087
|
+
/**
|
|
3088
|
+
* Where the colon name that starts at `pos` ends — its longest match — or
|
|
3089
|
+
* `undefined` when none does: the first segment is not ASCII, is a keyword
|
|
3090
|
+
* of the parse's `ecmaVersion`, or has no `:segment` directly after it.
|
|
3091
|
+
*/
|
|
3092
|
+
colonNameEnd() {
|
|
3093
|
+
const input = this.input;
|
|
3094
|
+
let end = this.pos;
|
|
3095
|
+
if (!isFirstChar(input.charCodeAt(end))) {
|
|
3096
|
+
return undefined;
|
|
3097
|
+
}
|
|
3098
|
+
do {
|
|
3099
|
+
end++;
|
|
3100
|
+
} while (isSegmentChar(input.charCodeAt(end)));
|
|
3101
|
+
if (!joinsAt(input, end) || this.keywords.test(input.slice(this.pos, end))) {
|
|
3102
|
+
return undefined;
|
|
3103
|
+
}
|
|
3104
|
+
do {
|
|
3105
|
+
end += 2;
|
|
3106
|
+
while (isSegmentChar(input.charCodeAt(end))) {
|
|
3107
|
+
end++;
|
|
3108
|
+
}
|
|
3109
|
+
} while (joinsAt(input, end));
|
|
3110
|
+
return end;
|
|
3111
|
+
}
|
|
3112
|
+
/**
|
|
3113
|
+
* Keeps the levels after acorn has finished the token, recording the one
|
|
3114
|
+
* the token was read at: `(`, `[`, `{` and `${` open a level and `)`, `]`
|
|
3115
|
+
* and `}` close it; a `?` is counted at its level and a `:` there
|
|
3116
|
+
* uncounts it. `?.` and `??` are other token types, and count for nothing.
|
|
3117
|
+
*/
|
|
3118
|
+
finishToken(type, value) {
|
|
3119
|
+
super.finishToken(type, value);
|
|
3120
|
+
const level = this.level;
|
|
3121
|
+
this.tokenLevel = level;
|
|
3122
|
+
switch (type) {
|
|
3123
|
+
case tokTypes.parenL:
|
|
3124
|
+
case tokTypes.bracketL:
|
|
3125
|
+
case tokTypes.braceL:
|
|
3126
|
+
case tokTypes.dollarBraceL:
|
|
3127
|
+
this.levels.push({ pending: 0 });
|
|
3128
|
+
break;
|
|
3129
|
+
case tokTypes.parenR:
|
|
3130
|
+
case tokTypes.bracketR:
|
|
3131
|
+
case tokTypes.braceR:
|
|
3132
|
+
if (this.levels.length > 1) {
|
|
3133
|
+
this.levels.pop();
|
|
3134
|
+
}
|
|
3135
|
+
break;
|
|
3136
|
+
case tokTypes.question:
|
|
3137
|
+
level.pending++;
|
|
3138
|
+
break;
|
|
3139
|
+
case tokTypes.colon:
|
|
3140
|
+
if (level.pending > 0) {
|
|
3141
|
+
level.pending--;
|
|
3142
|
+
}
|
|
3143
|
+
break;
|
|
3144
|
+
case tokTypes.comma:
|
|
3145
|
+
case tokTypes.semi:
|
|
3146
|
+
level.suppressed = undefined;
|
|
3147
|
+
break;
|
|
3148
|
+
}
|
|
3149
|
+
}
|
|
3150
|
+
/**
|
|
3151
|
+
* Refuses a colon name as an object key, in a literal or a pattern alike
|
|
3152
|
+
* (§ 3.4, 2): `{Q1:1}` is `{Q1: 1}` to JavaScript and a shorthand to the
|
|
3153
|
+
* grammar, and keeping either would answer some author wrongly. Only a
|
|
3154
|
+
* joined token is a `name` holding a colon.
|
|
3155
|
+
*/
|
|
3156
|
+
parsePropertyName(prop) {
|
|
3157
|
+
const name = this.value;
|
|
3158
|
+
if (this.type === tokTypes.name && typeof name === 'string' && name.includes(':')) {
|
|
3159
|
+
const first = name.slice(0, name.indexOf(':'));
|
|
3160
|
+
this.raise(this.start, `Colon-joined name '${name}' cannot be an object key: ` +
|
|
3161
|
+
`write '${first}: ...' with a space for a property, or quote the key`);
|
|
3162
|
+
}
|
|
3163
|
+
return super.parsePropertyName(prop);
|
|
3164
|
+
}
|
|
3165
|
+
/**
|
|
3166
|
+
* Forgets the name not joined at the level a statement starts at: the
|
|
3167
|
+
* statement boundary ended the conditional whose consequent lost it. A
|
|
3168
|
+
* semicolon acorn inserts at a line break is such a boundary, and no `;`
|
|
3169
|
+
* token marks it.
|
|
3170
|
+
*/
|
|
3171
|
+
parseStatement(...args) {
|
|
3172
|
+
this.tokenLevel.suppressed = undefined;
|
|
3173
|
+
return super.parseStatement(...args);
|
|
3174
|
+
}
|
|
3175
|
+
/**
|
|
3176
|
+
* acorn's own error, with the hint of § 3.4, 1 when the unexpected token
|
|
3177
|
+
* is a `:` at a level that did not join a name since its last `,`, `;` or
|
|
3178
|
+
* statement start: the colon the name lost to the conditional. Any other
|
|
3179
|
+
* error is left as acorn words it.
|
|
3180
|
+
*/
|
|
3181
|
+
unexpected(pos) {
|
|
3182
|
+
const suppressed = this.tokenLevel.suppressed;
|
|
3183
|
+
if (suppressed !== undefined && this.type === tokTypes.colon &&
|
|
3184
|
+
(pos === undefined || pos === this.start)) {
|
|
3185
|
+
return this.raise(this.start, 'Unexpected token: a colon-joined name in the ' +
|
|
3186
|
+
`consequent of a conditional needs parentheses: (${suppressed})`);
|
|
3187
|
+
}
|
|
3188
|
+
return super.unexpected(pos);
|
|
3189
|
+
}
|
|
3190
|
+
}
|
|
3191
|
+
return ColonParser;
|
|
3192
|
+
};
|
|
3193
|
+
/**
|
|
3194
|
+
* acorn's `Parser` with the grammar, built once, at module load. `extend`
|
|
3195
|
+
* returns a subclass, so acorn's own `Parser`, which `acorn.parse` uses, is
|
|
3196
|
+
* left as it was.
|
|
3197
|
+
*/
|
|
3198
|
+
const ColonIdentifiersParser = Parser.extend(colonIdentifiers);
|
|
3199
|
+
|
|
3009
3200
|
/**
|
|
3010
3201
|
* @description
|
|
3011
3202
|
* Parses expression and returns ES6/ES2020 AST.
|
|
3203
|
+
* With `colonIdentifiers` set the parser is the colon-joined name grammar's;
|
|
3204
|
+
* otherwise it is acorn's own `parse`.
|
|
3012
3205
|
* @param expr expression to parse
|
|
3013
3206
|
* @param options parser options
|
|
3014
3207
|
*/
|
|
3015
3208
|
const parse = (expr, options) => {
|
|
3016
3209
|
options = { ecmaVersion: 2020, ...options };
|
|
3017
3210
|
const acornOptions = { ecmaVersion: 2020, ...options };
|
|
3018
|
-
const program =
|
|
3211
|
+
const program = options.colonIdentifiers
|
|
3212
|
+
? ColonIdentifiersParser.parse(expr, acornOptions)
|
|
3213
|
+
: parse$1(expr, acornOptions);
|
|
3019
3214
|
if (!options.extractExpressions) {
|
|
3020
3215
|
return program;
|
|
3021
3216
|
}
|
|
@@ -5682,6 +5877,148 @@ class BaseEval {
|
|
|
5682
5877
|
}
|
|
5683
5878
|
}
|
|
5684
5879
|
|
|
5880
|
+
/**
|
|
5881
|
+
* The options a cached AST cannot honour: acorn calls each callback as it
|
|
5882
|
+
* parses, and appends to a caller's `program` node. A parse carrying any of
|
|
5883
|
+
* them with a value acorn acts on bypasses the cache, reading and writing
|
|
5884
|
+
* (`docs/colon-identifiers/phase-9-plan.md` § 3.2). The test is on the value,
|
|
5885
|
+
* never the key: `defaultParserOptions`, which every service passes, carries
|
|
5886
|
+
* all five as `null`.
|
|
5887
|
+
*/
|
|
5888
|
+
const UNCACHEABLE_OPTIONS = [
|
|
5889
|
+
'onToken', 'onComment', 'onInsertedSemicolon', 'onTrailingComma', 'program'
|
|
5890
|
+
];
|
|
5891
|
+
/**
|
|
5892
|
+
* Left out of the fingerprint: the five above, and `cacheSize`, which sizes
|
|
5893
|
+
* the cache and changes no parse.
|
|
5894
|
+
*/
|
|
5895
|
+
const UNKEYED_OPTIONS = new Set([...UNCACHEABLE_OPTIONS, 'cacheSize']);
|
|
5896
|
+
/** Stands for a call that passes no options object of its own. */
|
|
5897
|
+
const NO_CALL_OPTIONS = Object.freeze({});
|
|
5898
|
+
/**
|
|
5899
|
+
* Builds the fingerprint of a parse with the given options — the service's and
|
|
5900
|
+
* the call's, merged as `ParserService.parse` merges them — or `null` when the
|
|
5901
|
+
* parse must bypass the cache.
|
|
5902
|
+
*
|
|
5903
|
+
* Every other option is in it under its own name, so an option this file does
|
|
5904
|
+
* not list keys the cache with no change here. Sorted `[key, value]` pairs as
|
|
5905
|
+
* JSON: canonical, so equal options give one fingerprint whatever their key
|
|
5906
|
+
* order or identity. A key whose value is `undefined` is written `[key]`
|
|
5907
|
+
* rather than dropped, since acorn reads it differently from a missing key:
|
|
5908
|
+
* `checkPrivateFields` defaults to `true`. A value JSON cannot hold, a cycle
|
|
5909
|
+
* or a `BigInt`, gives no fingerprint, so that parse bypasses the cache rather
|
|
5910
|
+
* than throw where it used to succeed.
|
|
5911
|
+
*/
|
|
5912
|
+
const buildParseFingerprint = (options) => {
|
|
5913
|
+
for (const key of UNCACHEABLE_OPTIONS) {
|
|
5914
|
+
if (options[key]) {
|
|
5915
|
+
return null;
|
|
5916
|
+
}
|
|
5917
|
+
}
|
|
5918
|
+
const values = options;
|
|
5919
|
+
const pairs = Object.keys(values)
|
|
5920
|
+
.filter(key => !UNKEYED_OPTIONS.has(key))
|
|
5921
|
+
.sort()
|
|
5922
|
+
.map(key => values[key] === undefined ? [key] : [key, values[key]]);
|
|
5923
|
+
try {
|
|
5924
|
+
return JSON.stringify(pairs);
|
|
5925
|
+
}
|
|
5926
|
+
catch {
|
|
5927
|
+
return null;
|
|
5928
|
+
}
|
|
5929
|
+
};
|
|
5930
|
+
/**
|
|
5931
|
+
* Returns the cache namespace for a parse with the service's options and the
|
|
5932
|
+
* call's — the sha256 of their fingerprint, in hex — or `null` when the parse
|
|
5933
|
+
* must bypass the cache. Built once per pair of objects and remembered by
|
|
5934
|
+
* identity in the service's `memo`, so `EvalService`, which passes the same
|
|
5935
|
+
* `defaultParserOptions` on every call, pays two lookups per parse rather than
|
|
5936
|
+
* a serialization and a digest. The digest keeps `getHashKey`'s input short:
|
|
5937
|
+
* it joins the namespace and the text with `:`, and 64 hex characters, none a
|
|
5938
|
+
* colon, cannot make two (options, text) pairs one key when the text holds
|
|
5939
|
+
* colons.
|
|
5940
|
+
*
|
|
5941
|
+
* Remembered by identity, it does not see an object changed in place: the
|
|
5942
|
+
* object the `parserOptions` getter returns, an options object a caller passes
|
|
5943
|
+
* again after writing into it, or the exported `defaultParserOptions`. A change
|
|
5944
|
+
* through the `parserOptions` setter installs a new object, and is seen
|
|
5945
|
+
* (`docs/backlog.md` A34).
|
|
5946
|
+
*/
|
|
5947
|
+
const parseFingerprint = (memo, serviceOptions, callOptions) => {
|
|
5948
|
+
let byCall = memo.get(serviceOptions);
|
|
5949
|
+
if (!byCall) {
|
|
5950
|
+
byCall = new WeakMap();
|
|
5951
|
+
memo.set(serviceOptions, byCall);
|
|
5952
|
+
}
|
|
5953
|
+
// A JavaScript caller can pass something other than an object, such as the
|
|
5954
|
+
// `false` of `condition && options`. It cannot be a `WeakMap` key, and acorn
|
|
5955
|
+
// reads none of its options from it, so it is fingerprinted as none.
|
|
5956
|
+
const call = typeof callOptions === 'object' && callOptions !== null
|
|
5957
|
+
? callOptions : undefined;
|
|
5958
|
+
const callKey = call ?? NO_CALL_OPTIONS;
|
|
5959
|
+
let digest = byCall.get(callKey);
|
|
5960
|
+
if (digest === undefined) {
|
|
5961
|
+
const fingerprint = buildParseFingerprint({ ...serviceOptions, ...call });
|
|
5962
|
+
digest = fingerprint === null ? null : sha256(fingerprint);
|
|
5963
|
+
byCall.set(callKey, digest);
|
|
5964
|
+
}
|
|
5965
|
+
return digest;
|
|
5966
|
+
};
|
|
5967
|
+
|
|
5968
|
+
/**
|
|
5969
|
+
* Set by {@link provideColonIdentifiers}, and by nothing else. Exported from no
|
|
5970
|
+
* barrel: the provider is the only way to set it, and
|
|
5971
|
+
* `inject(ParserService).parserOptions` the published way to read the
|
|
5972
|
+
* effective value (`docs/colon-identifiers/phase-9-plan.md` § 3.3).
|
|
5973
|
+
*/
|
|
5974
|
+
const COLON_IDENTIFIERS = new InjectionToken('COLON_IDENTIFIERS');
|
|
5975
|
+
/** Angular's code for `inject()` called outside an injection context, NG0203. */
|
|
5976
|
+
const MISSING_INJECTION_CONTEXT = -203;
|
|
5977
|
+
/**
|
|
5978
|
+
* Whether the injector constructing a `ParserService` holds
|
|
5979
|
+
* {@link provideColonIdentifiers}. `false` outside an injection context:
|
|
5980
|
+
* `new ParserService()` constructed a working service before the provider
|
|
5981
|
+
* existed, and still does, with the option off. Any other error from the read
|
|
5982
|
+
* is rethrown (plan § 3.1).
|
|
5983
|
+
*/
|
|
5984
|
+
const injectColonIdentifiers = () => {
|
|
5985
|
+
try {
|
|
5986
|
+
return inject(COLON_IDENTIFIERS, { optional: true }) === true;
|
|
5987
|
+
}
|
|
5988
|
+
catch (error) {
|
|
5989
|
+
if (error?.code === MISSING_INJECTION_CONTEXT) {
|
|
5990
|
+
return false;
|
|
5991
|
+
}
|
|
5992
|
+
throw error;
|
|
5993
|
+
}
|
|
5994
|
+
};
|
|
5995
|
+
/**
|
|
5996
|
+
* Turns `colonIdentifiers` on for every parse through `ParserService`, and so
|
|
5997
|
+
* through `EvalService`, `CompilerService` and `DiscoveryService`: a
|
|
5998
|
+
* colon-joined name such as `Q1:1:3` parses as one identifier and reads the
|
|
5999
|
+
* context key of that spelling. A per-call `colonIdentifiers` overrides it.
|
|
6000
|
+
*
|
|
6001
|
+
* Belongs in the application's root providers. `ParserService` is provided in
|
|
6002
|
+
* the root, so in a child environment injector - a lazy route's `providers` -
|
|
6003
|
+
* it would reach no parse; there it throws when that injector is created.
|
|
6004
|
+
*
|
|
6005
|
+
* @returns The providers, for the application's root providers.
|
|
6006
|
+
*/
|
|
6007
|
+
function provideColonIdentifiers() {
|
|
6008
|
+
return makeEnvironmentProviders([
|
|
6009
|
+
{ provide: COLON_IDENTIFIERS, useValue: true },
|
|
6010
|
+
provideEnvironmentInitializer(() => {
|
|
6011
|
+
if (inject(ParserService).parserOptions?.colonIdentifiers !== true) {
|
|
6012
|
+
throw new Error('provideColonIdentifiers() reaches no parse here: ' +
|
|
6013
|
+
'ParserService is provided in the root, and the root ParserService ' +
|
|
6014
|
+
'does not have colonIdentifiers on. Put provideColonIdentifiers() in ' +
|
|
6015
|
+
'the application\'s root providers, not a lazy route\'s or another ' +
|
|
6016
|
+
'child injector\'s.');
|
|
6017
|
+
}
|
|
6018
|
+
}),
|
|
6019
|
+
]);
|
|
6020
|
+
}
|
|
6021
|
+
|
|
5685
6022
|
/**
|
|
5686
6023
|
* @description
|
|
5687
6024
|
* Service for parsing expressions and returning ES6/ES2020 AST.
|
|
@@ -5702,13 +6039,17 @@ class ParserService {
|
|
|
5702
6039
|
this._parserOptions = value;
|
|
5703
6040
|
}
|
|
5704
6041
|
/**
|
|
5705
|
-
* Constructs a new instance of the ParserService class.
|
|
6042
|
+
* Constructs a new instance of the ParserService class. Its options carry
|
|
6043
|
+
* `colonIdentifiers: true` when the injector constructing it holds
|
|
6044
|
+
* `provideColonIdentifiers()`, and no such key otherwise.
|
|
5706
6045
|
*/
|
|
5707
6046
|
constructor() {
|
|
6047
|
+
this._fingerprints = new WeakMap();
|
|
5708
6048
|
this._isDestroyed = false;
|
|
5709
6049
|
this.parserOptions = {
|
|
5710
6050
|
...defaultParserOptions,
|
|
5711
|
-
extractExpressions: true
|
|
6051
|
+
extractExpressions: true,
|
|
6052
|
+
...(injectColonIdentifiers() ? { colonIdentifiers: true } : {})
|
|
5712
6053
|
};
|
|
5713
6054
|
if (this.parserOptions.cacheSize) {
|
|
5714
6055
|
this._cache = new Cache(this.parserOptions.cacheSize);
|
|
@@ -5777,19 +6118,30 @@ class ParserService {
|
|
|
5777
6118
|
const parserOptions = { ...this.parserOptions, ...options };
|
|
5778
6119
|
const isCache = this.parserOptions.cacheSize && this._cache;
|
|
5779
6120
|
const ast = isCache
|
|
5780
|
-
? this.fromCacheOrParse(expr, parserOptions)
|
|
6121
|
+
? this.fromCacheOrParse(expr, parserOptions, options)
|
|
5781
6122
|
: parse(expr, parserOptions);
|
|
5782
6123
|
return ast;
|
|
5783
6124
|
}
|
|
5784
6125
|
return undefined;
|
|
5785
6126
|
}
|
|
5786
6127
|
//#region Private methods
|
|
5787
|
-
|
|
6128
|
+
/**
|
|
6129
|
+
* Serves a cached AST only to a parse whose parse-relevant options match the
|
|
6130
|
+
* parse that cached it: the key is the text under a digest of the
|
|
6131
|
+
* fingerprint of the service's options and the call's, not the text alone
|
|
6132
|
+
* (`docs/backlog-retired.md` A26). A parse carrying a callback or a
|
|
6133
|
+
* `program` neither reads nor writes the cache.
|
|
6134
|
+
*/
|
|
6135
|
+
fromCacheOrParse(expr, parserOptions, callOptions) {
|
|
5788
6136
|
if (this._cache) {
|
|
5789
|
-
const
|
|
6137
|
+
const namespace = parseFingerprint(this._fingerprints, this.parserOptions, callOptions);
|
|
6138
|
+
if (namespace === null) {
|
|
6139
|
+
return parse(expr, parserOptions);
|
|
6140
|
+
}
|
|
6141
|
+
const hashKey = this._cache.getHashKey(namespace, expr);
|
|
5790
6142
|
let ast = this._cache.get(hashKey);
|
|
5791
6143
|
if (!ast) {
|
|
5792
|
-
ast = parse(expr,
|
|
6144
|
+
ast = parse(expr, parserOptions);
|
|
5793
6145
|
this._cache.set(hashKey, ast);
|
|
5794
6146
|
}
|
|
5795
6147
|
return ast;
|
|
@@ -5928,6 +6280,8 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.1", ngImpor
|
|
|
5928
6280
|
}]
|
|
5929
6281
|
}], ctorParameters: () => [] });
|
|
5930
6282
|
|
|
6283
|
+
/** Stands for a `ParserService` that has no options object of its own. */
|
|
6284
|
+
const NO_SERVICE_OPTIONS = Object.freeze({});
|
|
5931
6285
|
/**
|
|
5932
6286
|
* CompilerService class that extends BaseEval.
|
|
5933
6287
|
* Provides methods for compiling and evaluating expressions with performance caching.
|
|
@@ -5941,6 +6295,7 @@ class CompilerService extends BaseEval {
|
|
|
5941
6295
|
this._cacheTTL = 10 * 60 * 1000; // 10 minutes
|
|
5942
6296
|
this._accessOrder = new Set();
|
|
5943
6297
|
this._isDestroyed = false;
|
|
6298
|
+
this._fingerprints = new WeakMap();
|
|
5944
6299
|
this.parserService = inject(ParserService);
|
|
5945
6300
|
this.evalService = inject(EvalService);
|
|
5946
6301
|
this.parserOptions = defaultParserOptions;
|
|
@@ -6000,10 +6355,23 @@ class CompilerService extends BaseEval {
|
|
|
6000
6355
|
}
|
|
6001
6356
|
/**
|
|
6002
6357
|
* Generate cache key for expression. Only a string is cached, so only a
|
|
6003
|
-
* string has a key
|
|
6358
|
+
* string has a key: the text under the digest of the options its parse
|
|
6359
|
+
* uses, `ParserService`'s merged with this service's, so a change through
|
|
6360
|
+
* the `ParserService.parserOptions` setter reaches a string compiled before
|
|
6361
|
+
* it (`docs/colon-identifiers/phase-9-plan.md` § 3.2). The memo is
|
|
6362
|
+
* `ParserService`'s kind, and a write in place goes unseen here as there
|
|
6363
|
+
* (`docs/backlog.md` A34). `null` when that parse bypasses `ParserService`'s
|
|
6364
|
+
* cache - a callback or a `program` among its options, or a value JSON
|
|
6365
|
+
* cannot hold - and this cache then bypasses too, since a cached function
|
|
6366
|
+
* runs no callback.
|
|
6004
6367
|
*/
|
|
6005
6368
|
generateCacheKey(expression) {
|
|
6006
|
-
|
|
6369
|
+
// A test double for `ParserService` need not carry `parserOptions`, and a
|
|
6370
|
+
// missing one cannot key the memo; it is fingerprinted as none.
|
|
6371
|
+
const serviceOptions = this.parserService.parserOptions;
|
|
6372
|
+
const digest = parseFingerprint(this._fingerprints, typeof serviceOptions === 'object' && serviceOptions !== null
|
|
6373
|
+
? serviceOptions : NO_SERVICE_OPTIONS, this.parserOptions);
|
|
6374
|
+
return digest === null ? null : `str:${digest}:${expression}`;
|
|
6007
6375
|
}
|
|
6008
6376
|
/**
|
|
6009
6377
|
* Update access order for LRU cache
|
|
@@ -6014,7 +6382,8 @@ class CompilerService extends BaseEval {
|
|
|
6014
6382
|
}
|
|
6015
6383
|
/**
|
|
6016
6384
|
* Compiles the given expression into a state callback function. A string is
|
|
6017
|
-
* compiled once and cached
|
|
6385
|
+
* compiled once and cached, unless its parse bypasses `ParserService`'s
|
|
6386
|
+
* cache; an AST is compiled on every call.
|
|
6018
6387
|
* @param expression - The expression to compile.
|
|
6019
6388
|
* @returns The compiled state callback function.
|
|
6020
6389
|
* @throws Error if there is an error during compilation.
|
|
@@ -6033,6 +6402,9 @@ class CompilerService extends BaseEval {
|
|
|
6033
6402
|
return compile(this.parse(expression));
|
|
6034
6403
|
}
|
|
6035
6404
|
const cacheKey = this.generateCacheKey(expression);
|
|
6405
|
+
if (cacheKey === null) {
|
|
6406
|
+
return compile(this.parse(expression));
|
|
6407
|
+
}
|
|
6036
6408
|
// Check cache first
|
|
6037
6409
|
const cached = this._compilationCache.get(cacheKey);
|
|
6038
6410
|
if (cached?.sync) {
|
|
@@ -6099,8 +6471,8 @@ class CompilerService extends BaseEval {
|
|
|
6099
6471
|
}
|
|
6100
6472
|
/**
|
|
6101
6473
|
* Compiles the given expression into an asynchronous state callback
|
|
6102
|
-
* function. A string is compiled once and cached
|
|
6103
|
-
* every call.
|
|
6474
|
+
* function. A string is compiled once and cached, unless its parse bypasses
|
|
6475
|
+
* `ParserService`'s cache; an AST is compiled on every call.
|
|
6104
6476
|
* @param expression - The expression to compile.
|
|
6105
6477
|
* @returns The compiled asynchronous state callback function.
|
|
6106
6478
|
* @throws Error if there is an error during compilation.
|
|
@@ -6111,6 +6483,9 @@ class CompilerService extends BaseEval {
|
|
|
6111
6483
|
return compileAsync(this.parse(expression));
|
|
6112
6484
|
}
|
|
6113
6485
|
const cacheKey = this.generateCacheKey(expression);
|
|
6486
|
+
if (cacheKey === null) {
|
|
6487
|
+
return compileAsync(this.parse(expression));
|
|
6488
|
+
}
|
|
6114
6489
|
// Check cache first
|
|
6115
6490
|
const cached = this._compilationCache.get(cacheKey);
|
|
6116
6491
|
if (cached?.async) {
|
|
@@ -6231,5 +6606,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.1", ngImpor
|
|
|
6231
6606
|
* Generated bundle index. Do not edit.
|
|
6232
6607
|
*/
|
|
6233
6608
|
|
|
6234
|
-
export { ASYNC_HOOK_MESSAGE, BaseRegistry, Cache, CaseInsensitiveRegistry, CompilerService, DiscoveryService, EMPTY_COMPLETION, EvalContext, EvalHooks, EvalResult, EvalScope, EvalService, EvalState, EvalTrace, ParserService, Registry, Stack, call, callAsync, compile, compileAsync, createDependencyTracker, createTimingHook, defaultParserOptions, evaluate, evaluateAsync, extract, extractExpression, fromContext, getContextValue, parse };
|
|
6609
|
+
export { ASYNC_HOOK_MESSAGE, BaseRegistry, Cache, CaseInsensitiveRegistry, CompilerService, DiscoveryService, EMPTY_COMPLETION, EvalContext, EvalHooks, EvalResult, EvalScope, EvalService, EvalState, EvalTrace, ParserService, Registry, Stack, call, callAsync, compile, compileAsync, createDependencyTracker, createTimingHook, defaultParserOptions, evaluate, evaluateAsync, extract, extractExpression, fromContext, getContextValue, parse, provideColonIdentifiers };
|
|
6235
6610
|
//# sourceMappingURL=zvenigora-ng-eval-core.mjs.map
|