@nocobase/plugin-workflow-json-query 1.9.0-beta.17

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.
Files changed (50) hide show
  1. package/LICENSE.txt +172 -0
  2. package/README.md +1 -0
  3. package/client.d.ts +2 -0
  4. package/client.js +1 -0
  5. package/dist/client/index.d.ts +23 -0
  6. package/dist/client/index.js +10 -0
  7. package/dist/client/instruction.d.ts +167 -0
  8. package/dist/externalVersion.js +19 -0
  9. package/dist/index.d.ts +17 -0
  10. package/dist/index.js +42 -0
  11. package/dist/locale/en-US.json +13 -0
  12. package/dist/locale/index.d.ts +10 -0
  13. package/dist/locale/index.js +42 -0
  14. package/dist/locale/zh-CN.json +14 -0
  15. package/dist/node_modules/jmespath/LICENSE +13 -0
  16. package/dist/node_modules/jmespath/artifacts/jmespath.min.js +2 -0
  17. package/dist/node_modules/jmespath/bower.json +24 -0
  18. package/dist/node_modules/jmespath/jmespath.js +1 -0
  19. package/dist/node_modules/jmespath/jp.js +23 -0
  20. package/dist/node_modules/jmespath/package.json +1 -0
  21. package/dist/node_modules/jsonata/LICENSE +19 -0
  22. package/dist/node_modules/jsonata/jsonata-es5.js +9875 -0
  23. package/dist/node_modules/jsonata/jsonata-es5.min.js +1 -0
  24. package/dist/node_modules/jsonata/jsonata.d.ts +72 -0
  25. package/dist/node_modules/jsonata/jsonata.js +1 -0
  26. package/dist/node_modules/jsonata/jsonata.min.js +1 -0
  27. package/dist/node_modules/jsonata/package.json +1 -0
  28. package/dist/node_modules/jsonpath-plus/LICENSE +22 -0
  29. package/dist/node_modules/jsonpath-plus/bin/jsonpath-cli.js +36 -0
  30. package/dist/node_modules/jsonpath-plus/dist/index-browser-esm.js +2158 -0
  31. package/dist/node_modules/jsonpath-plus/dist/index-browser-esm.min.js +2 -0
  32. package/dist/node_modules/jsonpath-plus/dist/index-browser-umd.cjs +2166 -0
  33. package/dist/node_modules/jsonpath-plus/dist/index-browser-umd.min.cjs +2 -0
  34. package/dist/node_modules/jsonpath-plus/dist/index-node-cjs.cjs +1 -0
  35. package/dist/node_modules/jsonpath-plus/dist/index-node-esm.js +2068 -0
  36. package/dist/node_modules/jsonpath-plus/package.json +1 -0
  37. package/dist/node_modules/jsonpath-plus/src/Safe-Script.js +200 -0
  38. package/dist/node_modules/jsonpath-plus/src/jsonpath-browser.js +102 -0
  39. package/dist/node_modules/jsonpath-plus/src/jsonpath-node.js +8 -0
  40. package/dist/node_modules/jsonpath-plus/src/jsonpath.d.ts +226 -0
  41. package/dist/node_modules/jsonpath-plus/src/jsonpath.js +784 -0
  42. package/dist/server/JSONQueryInstruction.d.ts +42 -0
  43. package/dist/server/JSONQueryInstruction.js +99 -0
  44. package/dist/server/Plugin.d.ts +24 -0
  45. package/dist/server/Plugin.js +62 -0
  46. package/dist/server/index.d.ts +17 -0
  47. package/dist/server/index.js +42 -0
  48. package/package.json +31 -0
  49. package/server.d.ts +2 -0
  50. package/server.js +1 -0
@@ -0,0 +1,2166 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.JSONPath = {}));
5
+ })(this, (function (exports) { 'use strict';
6
+
7
+ /**
8
+ * @implements {IHooks}
9
+ */
10
+ class Hooks {
11
+ /**
12
+ * @callback HookCallback
13
+ * @this {*|Jsep} this
14
+ * @param {Jsep} env
15
+ * @returns: void
16
+ */
17
+ /**
18
+ * Adds the given callback to the list of callbacks for the given hook.
19
+ *
20
+ * The callback will be invoked when the hook it is registered for is run.
21
+ *
22
+ * One callback function can be registered to multiple hooks and the same hook multiple times.
23
+ *
24
+ * @param {string|object} name The name of the hook, or an object of callbacks keyed by name
25
+ * @param {HookCallback|boolean} callback The callback function which is given environment variables.
26
+ * @param {?boolean} [first=false] Will add the hook to the top of the list (defaults to the bottom)
27
+ * @public
28
+ */
29
+ add(name, callback, first) {
30
+ if (typeof arguments[0] != 'string') {
31
+ // Multiple hook callbacks, keyed by name
32
+ for (let name in arguments[0]) {
33
+ this.add(name, arguments[0][name], arguments[1]);
34
+ }
35
+ } else {
36
+ (Array.isArray(name) ? name : [name]).forEach(function (name) {
37
+ this[name] = this[name] || [];
38
+ if (callback) {
39
+ this[name][first ? 'unshift' : 'push'](callback);
40
+ }
41
+ }, this);
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Runs a hook invoking all registered callbacks with the given environment variables.
47
+ *
48
+ * Callbacks will be invoked synchronously and in the order in which they were registered.
49
+ *
50
+ * @param {string} name The name of the hook.
51
+ * @param {Object<string, any>} env The environment variables of the hook passed to all callbacks registered.
52
+ * @public
53
+ */
54
+ run(name, env) {
55
+ this[name] = this[name] || [];
56
+ this[name].forEach(function (callback) {
57
+ callback.call(env && env.context ? env.context : env, env);
58
+ });
59
+ }
60
+ }
61
+
62
+ /**
63
+ * @implements {IPlugins}
64
+ */
65
+ class Plugins {
66
+ constructor(jsep) {
67
+ this.jsep = jsep;
68
+ this.registered = {};
69
+ }
70
+
71
+ /**
72
+ * @callback PluginSetup
73
+ * @this {Jsep} jsep
74
+ * @returns: void
75
+ */
76
+ /**
77
+ * Adds the given plugin(s) to the registry
78
+ *
79
+ * @param {object} plugins
80
+ * @param {string} plugins.name The name of the plugin
81
+ * @param {PluginSetup} plugins.init The init function
82
+ * @public
83
+ */
84
+ register() {
85
+ for (var _len = arguments.length, plugins = new Array(_len), _key = 0; _key < _len; _key++) {
86
+ plugins[_key] = arguments[_key];
87
+ }
88
+ plugins.forEach(plugin => {
89
+ if (typeof plugin !== 'object' || !plugin.name || !plugin.init) {
90
+ throw new Error('Invalid JSEP plugin format');
91
+ }
92
+ if (this.registered[plugin.name]) {
93
+ // already registered. Ignore.
94
+ return;
95
+ }
96
+ plugin.init(this.jsep);
97
+ this.registered[plugin.name] = plugin;
98
+ });
99
+ }
100
+ }
101
+
102
+ // JavaScript Expression Parser (JSEP) 1.4.0
103
+
104
+ class Jsep {
105
+ /**
106
+ * @returns {string}
107
+ */
108
+ static get version() {
109
+ // To be filled in by the template
110
+ return '1.4.0';
111
+ }
112
+
113
+ /**
114
+ * @returns {string}
115
+ */
116
+ static toString() {
117
+ return 'JavaScript Expression Parser (JSEP) v' + Jsep.version;
118
+ }
119
+ // ==================== CONFIG ================================
120
+ /**
121
+ * @method addUnaryOp
122
+ * @param {string} op_name The name of the unary op to add
123
+ * @returns {Jsep}
124
+ */
125
+ static addUnaryOp(op_name) {
126
+ Jsep.max_unop_len = Math.max(op_name.length, Jsep.max_unop_len);
127
+ Jsep.unary_ops[op_name] = 1;
128
+ return Jsep;
129
+ }
130
+
131
+ /**
132
+ * @method jsep.addBinaryOp
133
+ * @param {string} op_name The name of the binary op to add
134
+ * @param {number} precedence The precedence of the binary op (can be a float). Higher number = higher precedence
135
+ * @param {boolean} [isRightAssociative=false] whether operator is right-associative
136
+ * @returns {Jsep}
137
+ */
138
+ static addBinaryOp(op_name, precedence, isRightAssociative) {
139
+ Jsep.max_binop_len = Math.max(op_name.length, Jsep.max_binop_len);
140
+ Jsep.binary_ops[op_name] = precedence;
141
+ if (isRightAssociative) {
142
+ Jsep.right_associative.add(op_name);
143
+ } else {
144
+ Jsep.right_associative.delete(op_name);
145
+ }
146
+ return Jsep;
147
+ }
148
+
149
+ /**
150
+ * @method addIdentifierChar
151
+ * @param {string} char The additional character to treat as a valid part of an identifier
152
+ * @returns {Jsep}
153
+ */
154
+ static addIdentifierChar(char) {
155
+ Jsep.additional_identifier_chars.add(char);
156
+ return Jsep;
157
+ }
158
+
159
+ /**
160
+ * @method addLiteral
161
+ * @param {string} literal_name The name of the literal to add
162
+ * @param {*} literal_value The value of the literal
163
+ * @returns {Jsep}
164
+ */
165
+ static addLiteral(literal_name, literal_value) {
166
+ Jsep.literals[literal_name] = literal_value;
167
+ return Jsep;
168
+ }
169
+
170
+ /**
171
+ * @method removeUnaryOp
172
+ * @param {string} op_name The name of the unary op to remove
173
+ * @returns {Jsep}
174
+ */
175
+ static removeUnaryOp(op_name) {
176
+ delete Jsep.unary_ops[op_name];
177
+ if (op_name.length === Jsep.max_unop_len) {
178
+ Jsep.max_unop_len = Jsep.getMaxKeyLen(Jsep.unary_ops);
179
+ }
180
+ return Jsep;
181
+ }
182
+
183
+ /**
184
+ * @method removeAllUnaryOps
185
+ * @returns {Jsep}
186
+ */
187
+ static removeAllUnaryOps() {
188
+ Jsep.unary_ops = {};
189
+ Jsep.max_unop_len = 0;
190
+ return Jsep;
191
+ }
192
+
193
+ /**
194
+ * @method removeIdentifierChar
195
+ * @param {string} char The additional character to stop treating as a valid part of an identifier
196
+ * @returns {Jsep}
197
+ */
198
+ static removeIdentifierChar(char) {
199
+ Jsep.additional_identifier_chars.delete(char);
200
+ return Jsep;
201
+ }
202
+
203
+ /**
204
+ * @method removeBinaryOp
205
+ * @param {string} op_name The name of the binary op to remove
206
+ * @returns {Jsep}
207
+ */
208
+ static removeBinaryOp(op_name) {
209
+ delete Jsep.binary_ops[op_name];
210
+ if (op_name.length === Jsep.max_binop_len) {
211
+ Jsep.max_binop_len = Jsep.getMaxKeyLen(Jsep.binary_ops);
212
+ }
213
+ Jsep.right_associative.delete(op_name);
214
+ return Jsep;
215
+ }
216
+
217
+ /**
218
+ * @method removeAllBinaryOps
219
+ * @returns {Jsep}
220
+ */
221
+ static removeAllBinaryOps() {
222
+ Jsep.binary_ops = {};
223
+ Jsep.max_binop_len = 0;
224
+ return Jsep;
225
+ }
226
+
227
+ /**
228
+ * @method removeLiteral
229
+ * @param {string} literal_name The name of the literal to remove
230
+ * @returns {Jsep}
231
+ */
232
+ static removeLiteral(literal_name) {
233
+ delete Jsep.literals[literal_name];
234
+ return Jsep;
235
+ }
236
+
237
+ /**
238
+ * @method removeAllLiterals
239
+ * @returns {Jsep}
240
+ */
241
+ static removeAllLiterals() {
242
+ Jsep.literals = {};
243
+ return Jsep;
244
+ }
245
+ // ==================== END CONFIG ============================
246
+
247
+ /**
248
+ * @returns {string}
249
+ */
250
+ get char() {
251
+ return this.expr.charAt(this.index);
252
+ }
253
+
254
+ /**
255
+ * @returns {number}
256
+ */
257
+ get code() {
258
+ return this.expr.charCodeAt(this.index);
259
+ }
260
+ /**
261
+ * @param {string} expr a string with the passed in express
262
+ * @returns Jsep
263
+ */
264
+ constructor(expr) {
265
+ // `index` stores the character number we are currently at
266
+ // All of the gobbles below will modify `index` as we move along
267
+ this.expr = expr;
268
+ this.index = 0;
269
+ }
270
+
271
+ /**
272
+ * static top-level parser
273
+ * @returns {jsep.Expression}
274
+ */
275
+ static parse(expr) {
276
+ return new Jsep(expr).parse();
277
+ }
278
+
279
+ /**
280
+ * Get the longest key length of any object
281
+ * @param {object} obj
282
+ * @returns {number}
283
+ */
284
+ static getMaxKeyLen(obj) {
285
+ return Math.max(0, ...Object.keys(obj).map(k => k.length));
286
+ }
287
+
288
+ /**
289
+ * `ch` is a character code in the next three functions
290
+ * @param {number} ch
291
+ * @returns {boolean}
292
+ */
293
+ static isDecimalDigit(ch) {
294
+ return ch >= 48 && ch <= 57; // 0...9
295
+ }
296
+
297
+ /**
298
+ * Returns the precedence of a binary operator or `0` if it isn't a binary operator. Can be float.
299
+ * @param {string} op_val
300
+ * @returns {number}
301
+ */
302
+ static binaryPrecedence(op_val) {
303
+ return Jsep.binary_ops[op_val] || 0;
304
+ }
305
+
306
+ /**
307
+ * Looks for start of identifier
308
+ * @param {number} ch
309
+ * @returns {boolean}
310
+ */
311
+ static isIdentifierStart(ch) {
312
+ return ch >= 65 && ch <= 90 ||
313
+ // A...Z
314
+ ch >= 97 && ch <= 122 ||
315
+ // a...z
316
+ ch >= 128 && !Jsep.binary_ops[String.fromCharCode(ch)] ||
317
+ // any non-ASCII that is not an operator
318
+ Jsep.additional_identifier_chars.has(String.fromCharCode(ch)); // additional characters
319
+ }
320
+
321
+ /**
322
+ * @param {number} ch
323
+ * @returns {boolean}
324
+ */
325
+ static isIdentifierPart(ch) {
326
+ return Jsep.isIdentifierStart(ch) || Jsep.isDecimalDigit(ch);
327
+ }
328
+
329
+ /**
330
+ * throw error at index of the expression
331
+ * @param {string} message
332
+ * @throws
333
+ */
334
+ throwError(message) {
335
+ const error = new Error(message + ' at character ' + this.index);
336
+ error.index = this.index;
337
+ error.description = message;
338
+ throw error;
339
+ }
340
+
341
+ /**
342
+ * Run a given hook
343
+ * @param {string} name
344
+ * @param {jsep.Expression|false} [node]
345
+ * @returns {?jsep.Expression}
346
+ */
347
+ runHook(name, node) {
348
+ if (Jsep.hooks[name]) {
349
+ const env = {
350
+ context: this,
351
+ node
352
+ };
353
+ Jsep.hooks.run(name, env);
354
+ return env.node;
355
+ }
356
+ return node;
357
+ }
358
+
359
+ /**
360
+ * Runs a given hook until one returns a node
361
+ * @param {string} name
362
+ * @returns {?jsep.Expression}
363
+ */
364
+ searchHook(name) {
365
+ if (Jsep.hooks[name]) {
366
+ const env = {
367
+ context: this
368
+ };
369
+ Jsep.hooks[name].find(function (callback) {
370
+ callback.call(env.context, env);
371
+ return env.node;
372
+ });
373
+ return env.node;
374
+ }
375
+ }
376
+
377
+ /**
378
+ * Push `index` up to the next non-space character
379
+ */
380
+ gobbleSpaces() {
381
+ let ch = this.code;
382
+ // Whitespace
383
+ while (ch === Jsep.SPACE_CODE || ch === Jsep.TAB_CODE || ch === Jsep.LF_CODE || ch === Jsep.CR_CODE) {
384
+ ch = this.expr.charCodeAt(++this.index);
385
+ }
386
+ this.runHook('gobble-spaces');
387
+ }
388
+
389
+ /**
390
+ * Top-level method to parse all expressions and returns compound or single node
391
+ * @returns {jsep.Expression}
392
+ */
393
+ parse() {
394
+ this.runHook('before-all');
395
+ const nodes = this.gobbleExpressions();
396
+
397
+ // If there's only one expression just try returning the expression
398
+ const node = nodes.length === 1 ? nodes[0] : {
399
+ type: Jsep.COMPOUND,
400
+ body: nodes
401
+ };
402
+ return this.runHook('after-all', node);
403
+ }
404
+
405
+ /**
406
+ * top-level parser (but can be reused within as well)
407
+ * @param {number} [untilICode]
408
+ * @returns {jsep.Expression[]}
409
+ */
410
+ gobbleExpressions(untilICode) {
411
+ let nodes = [],
412
+ ch_i,
413
+ node;
414
+ while (this.index < this.expr.length) {
415
+ ch_i = this.code;
416
+
417
+ // Expressions can be separated by semicolons, commas, or just inferred without any
418
+ // separators
419
+ if (ch_i === Jsep.SEMCOL_CODE || ch_i === Jsep.COMMA_CODE) {
420
+ this.index++; // ignore separators
421
+ } else {
422
+ // Try to gobble each expression individually
423
+ if (node = this.gobbleExpression()) {
424
+ nodes.push(node);
425
+ // If we weren't able to find a binary expression and are out of room, then
426
+ // the expression passed in probably has too much
427
+ } else if (this.index < this.expr.length) {
428
+ if (ch_i === untilICode) {
429
+ break;
430
+ }
431
+ this.throwError('Unexpected "' + this.char + '"');
432
+ }
433
+ }
434
+ }
435
+ return nodes;
436
+ }
437
+
438
+ /**
439
+ * The main parsing function.
440
+ * @returns {?jsep.Expression}
441
+ */
442
+ gobbleExpression() {
443
+ const node = this.searchHook('gobble-expression') || this.gobbleBinaryExpression();
444
+ this.gobbleSpaces();
445
+ return this.runHook('after-expression', node);
446
+ }
447
+
448
+ /**
449
+ * Search for the operation portion of the string (e.g. `+`, `===`)
450
+ * Start by taking the longest possible binary operations (3 characters: `===`, `!==`, `>>>`)
451
+ * and move down from 3 to 2 to 1 character until a matching binary operation is found
452
+ * then, return that binary operation
453
+ * @returns {string|boolean}
454
+ */
455
+ gobbleBinaryOp() {
456
+ this.gobbleSpaces();
457
+ let to_check = this.expr.substr(this.index, Jsep.max_binop_len);
458
+ let tc_len = to_check.length;
459
+ while (tc_len > 0) {
460
+ // Don't accept a binary op when it is an identifier.
461
+ // Binary ops that start with a identifier-valid character must be followed
462
+ // by a non identifier-part valid character
463
+ if (Jsep.binary_ops.hasOwnProperty(to_check) && (!Jsep.isIdentifierStart(this.code) || this.index + to_check.length < this.expr.length && !Jsep.isIdentifierPart(this.expr.charCodeAt(this.index + to_check.length)))) {
464
+ this.index += tc_len;
465
+ return to_check;
466
+ }
467
+ to_check = to_check.substr(0, --tc_len);
468
+ }
469
+ return false;
470
+ }
471
+
472
+ /**
473
+ * This function is responsible for gobbling an individual expression,
474
+ * e.g. `1`, `1+2`, `a+(b*2)-Math.sqrt(2)`
475
+ * @returns {?jsep.BinaryExpression}
476
+ */
477
+ gobbleBinaryExpression() {
478
+ let node, biop, prec, stack, biop_info, left, right, i, cur_biop;
479
+
480
+ // First, try to get the leftmost thing
481
+ // Then, check to see if there's a binary operator operating on that leftmost thing
482
+ // Don't gobbleBinaryOp without a left-hand-side
483
+ left = this.gobbleToken();
484
+ if (!left) {
485
+ return left;
486
+ }
487
+ biop = this.gobbleBinaryOp();
488
+
489
+ // If there wasn't a binary operator, just return the leftmost node
490
+ if (!biop) {
491
+ return left;
492
+ }
493
+
494
+ // Otherwise, we need to start a stack to properly place the binary operations in their
495
+ // precedence structure
496
+ biop_info = {
497
+ value: biop,
498
+ prec: Jsep.binaryPrecedence(biop),
499
+ right_a: Jsep.right_associative.has(biop)
500
+ };
501
+ right = this.gobbleToken();
502
+ if (!right) {
503
+ this.throwError("Expected expression after " + biop);
504
+ }
505
+ stack = [left, biop_info, right];
506
+
507
+ // Properly deal with precedence using [recursive descent](http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm)
508
+ while (biop = this.gobbleBinaryOp()) {
509
+ prec = Jsep.binaryPrecedence(biop);
510
+ if (prec === 0) {
511
+ this.index -= biop.length;
512
+ break;
513
+ }
514
+ biop_info = {
515
+ value: biop,
516
+ prec,
517
+ right_a: Jsep.right_associative.has(biop)
518
+ };
519
+ cur_biop = biop;
520
+
521
+ // Reduce: make a binary expression from the three topmost entries.
522
+ const comparePrev = prev => biop_info.right_a && prev.right_a ? prec > prev.prec : prec <= prev.prec;
523
+ while (stack.length > 2 && comparePrev(stack[stack.length - 2])) {
524
+ right = stack.pop();
525
+ biop = stack.pop().value;
526
+ left = stack.pop();
527
+ node = {
528
+ type: Jsep.BINARY_EXP,
529
+ operator: biop,
530
+ left,
531
+ right
532
+ };
533
+ stack.push(node);
534
+ }
535
+ node = this.gobbleToken();
536
+ if (!node) {
537
+ this.throwError("Expected expression after " + cur_biop);
538
+ }
539
+ stack.push(biop_info, node);
540
+ }
541
+ i = stack.length - 1;
542
+ node = stack[i];
543
+ while (i > 1) {
544
+ node = {
545
+ type: Jsep.BINARY_EXP,
546
+ operator: stack[i - 1].value,
547
+ left: stack[i - 2],
548
+ right: node
549
+ };
550
+ i -= 2;
551
+ }
552
+ return node;
553
+ }
554
+
555
+ /**
556
+ * An individual part of a binary expression:
557
+ * e.g. `foo.bar(baz)`, `1`, `"abc"`, `(a % 2)` (because it's in parenthesis)
558
+ * @returns {boolean|jsep.Expression}
559
+ */
560
+ gobbleToken() {
561
+ let ch, to_check, tc_len, node;
562
+ this.gobbleSpaces();
563
+ node = this.searchHook('gobble-token');
564
+ if (node) {
565
+ return this.runHook('after-token', node);
566
+ }
567
+ ch = this.code;
568
+ if (Jsep.isDecimalDigit(ch) || ch === Jsep.PERIOD_CODE) {
569
+ // Char code 46 is a dot `.` which can start off a numeric literal
570
+ return this.gobbleNumericLiteral();
571
+ }
572
+ if (ch === Jsep.SQUOTE_CODE || ch === Jsep.DQUOTE_CODE) {
573
+ // Single or double quotes
574
+ node = this.gobbleStringLiteral();
575
+ } else if (ch === Jsep.OBRACK_CODE) {
576
+ node = this.gobbleArray();
577
+ } else {
578
+ to_check = this.expr.substr(this.index, Jsep.max_unop_len);
579
+ tc_len = to_check.length;
580
+ while (tc_len > 0) {
581
+ // Don't accept an unary op when it is an identifier.
582
+ // Unary ops that start with a identifier-valid character must be followed
583
+ // by a non identifier-part valid character
584
+ if (Jsep.unary_ops.hasOwnProperty(to_check) && (!Jsep.isIdentifierStart(this.code) || this.index + to_check.length < this.expr.length && !Jsep.isIdentifierPart(this.expr.charCodeAt(this.index + to_check.length)))) {
585
+ this.index += tc_len;
586
+ const argument = this.gobbleToken();
587
+ if (!argument) {
588
+ this.throwError('missing unaryOp argument');
589
+ }
590
+ return this.runHook('after-token', {
591
+ type: Jsep.UNARY_EXP,
592
+ operator: to_check,
593
+ argument,
594
+ prefix: true
595
+ });
596
+ }
597
+ to_check = to_check.substr(0, --tc_len);
598
+ }
599
+ if (Jsep.isIdentifierStart(ch)) {
600
+ node = this.gobbleIdentifier();
601
+ if (Jsep.literals.hasOwnProperty(node.name)) {
602
+ node = {
603
+ type: Jsep.LITERAL,
604
+ value: Jsep.literals[node.name],
605
+ raw: node.name
606
+ };
607
+ } else if (node.name === Jsep.this_str) {
608
+ node = {
609
+ type: Jsep.THIS_EXP
610
+ };
611
+ }
612
+ } else if (ch === Jsep.OPAREN_CODE) {
613
+ // open parenthesis
614
+ node = this.gobbleGroup();
615
+ }
616
+ }
617
+ if (!node) {
618
+ return this.runHook('after-token', false);
619
+ }
620
+ node = this.gobbleTokenProperty(node);
621
+ return this.runHook('after-token', node);
622
+ }
623
+
624
+ /**
625
+ * Gobble properties of of identifiers/strings/arrays/groups.
626
+ * e.g. `foo`, `bar.baz`, `foo['bar'].baz`
627
+ * It also gobbles function calls:
628
+ * e.g. `Math.acos(obj.angle)`
629
+ * @param {jsep.Expression} node
630
+ * @returns {jsep.Expression}
631
+ */
632
+ gobbleTokenProperty(node) {
633
+ this.gobbleSpaces();
634
+ let ch = this.code;
635
+ while (ch === Jsep.PERIOD_CODE || ch === Jsep.OBRACK_CODE || ch === Jsep.OPAREN_CODE || ch === Jsep.QUMARK_CODE) {
636
+ let optional;
637
+ if (ch === Jsep.QUMARK_CODE) {
638
+ if (this.expr.charCodeAt(this.index + 1) !== Jsep.PERIOD_CODE) {
639
+ break;
640
+ }
641
+ optional = true;
642
+ this.index += 2;
643
+ this.gobbleSpaces();
644
+ ch = this.code;
645
+ }
646
+ this.index++;
647
+ if (ch === Jsep.OBRACK_CODE) {
648
+ node = {
649
+ type: Jsep.MEMBER_EXP,
650
+ computed: true,
651
+ object: node,
652
+ property: this.gobbleExpression()
653
+ };
654
+ if (!node.property) {
655
+ this.throwError('Unexpected "' + this.char + '"');
656
+ }
657
+ this.gobbleSpaces();
658
+ ch = this.code;
659
+ if (ch !== Jsep.CBRACK_CODE) {
660
+ this.throwError('Unclosed [');
661
+ }
662
+ this.index++;
663
+ } else if (ch === Jsep.OPAREN_CODE) {
664
+ // A function call is being made; gobble all the arguments
665
+ node = {
666
+ type: Jsep.CALL_EXP,
667
+ 'arguments': this.gobbleArguments(Jsep.CPAREN_CODE),
668
+ callee: node
669
+ };
670
+ } else if (ch === Jsep.PERIOD_CODE || optional) {
671
+ if (optional) {
672
+ this.index--;
673
+ }
674
+ this.gobbleSpaces();
675
+ node = {
676
+ type: Jsep.MEMBER_EXP,
677
+ computed: false,
678
+ object: node,
679
+ property: this.gobbleIdentifier()
680
+ };
681
+ }
682
+ if (optional) {
683
+ node.optional = true;
684
+ } // else leave undefined for compatibility with esprima
685
+
686
+ this.gobbleSpaces();
687
+ ch = this.code;
688
+ }
689
+ return node;
690
+ }
691
+
692
+ /**
693
+ * Parse simple numeric literals: `12`, `3.4`, `.5`. Do this by using a string to
694
+ * keep track of everything in the numeric literal and then calling `parseFloat` on that string
695
+ * @returns {jsep.Literal}
696
+ */
697
+ gobbleNumericLiteral() {
698
+ let number = '',
699
+ ch,
700
+ chCode;
701
+ while (Jsep.isDecimalDigit(this.code)) {
702
+ number += this.expr.charAt(this.index++);
703
+ }
704
+ if (this.code === Jsep.PERIOD_CODE) {
705
+ // can start with a decimal marker
706
+ number += this.expr.charAt(this.index++);
707
+ while (Jsep.isDecimalDigit(this.code)) {
708
+ number += this.expr.charAt(this.index++);
709
+ }
710
+ }
711
+ ch = this.char;
712
+ if (ch === 'e' || ch === 'E') {
713
+ // exponent marker
714
+ number += this.expr.charAt(this.index++);
715
+ ch = this.char;
716
+ if (ch === '+' || ch === '-') {
717
+ // exponent sign
718
+ number += this.expr.charAt(this.index++);
719
+ }
720
+ while (Jsep.isDecimalDigit(this.code)) {
721
+ // exponent itself
722
+ number += this.expr.charAt(this.index++);
723
+ }
724
+ if (!Jsep.isDecimalDigit(this.expr.charCodeAt(this.index - 1))) {
725
+ this.throwError('Expected exponent (' + number + this.char + ')');
726
+ }
727
+ }
728
+ chCode = this.code;
729
+
730
+ // Check to make sure this isn't a variable name that start with a number (123abc)
731
+ if (Jsep.isIdentifierStart(chCode)) {
732
+ this.throwError('Variable names cannot start with a number (' + number + this.char + ')');
733
+ } else if (chCode === Jsep.PERIOD_CODE || number.length === 1 && number.charCodeAt(0) === Jsep.PERIOD_CODE) {
734
+ this.throwError('Unexpected period');
735
+ }
736
+ return {
737
+ type: Jsep.LITERAL,
738
+ value: parseFloat(number),
739
+ raw: number
740
+ };
741
+ }
742
+
743
+ /**
744
+ * Parses a string literal, staring with single or double quotes with basic support for escape codes
745
+ * e.g. `"hello world"`, `'this is\nJSEP'`
746
+ * @returns {jsep.Literal}
747
+ */
748
+ gobbleStringLiteral() {
749
+ let str = '';
750
+ const startIndex = this.index;
751
+ const quote = this.expr.charAt(this.index++);
752
+ let closed = false;
753
+ while (this.index < this.expr.length) {
754
+ let ch = this.expr.charAt(this.index++);
755
+ if (ch === quote) {
756
+ closed = true;
757
+ break;
758
+ } else if (ch === '\\') {
759
+ // Check for all of the common escape codes
760
+ ch = this.expr.charAt(this.index++);
761
+ switch (ch) {
762
+ case 'n':
763
+ str += '\n';
764
+ break;
765
+ case 'r':
766
+ str += '\r';
767
+ break;
768
+ case 't':
769
+ str += '\t';
770
+ break;
771
+ case 'b':
772
+ str += '\b';
773
+ break;
774
+ case 'f':
775
+ str += '\f';
776
+ break;
777
+ case 'v':
778
+ str += '\x0B';
779
+ break;
780
+ default:
781
+ str += ch;
782
+ }
783
+ } else {
784
+ str += ch;
785
+ }
786
+ }
787
+ if (!closed) {
788
+ this.throwError('Unclosed quote after "' + str + '"');
789
+ }
790
+ return {
791
+ type: Jsep.LITERAL,
792
+ value: str,
793
+ raw: this.expr.substring(startIndex, this.index)
794
+ };
795
+ }
796
+
797
+ /**
798
+ * Gobbles only identifiers
799
+ * e.g.: `foo`, `_value`, `$x1`
800
+ * Also, this function checks if that identifier is a literal:
801
+ * (e.g. `true`, `false`, `null`) or `this`
802
+ * @returns {jsep.Identifier}
803
+ */
804
+ gobbleIdentifier() {
805
+ let ch = this.code,
806
+ start = this.index;
807
+ if (Jsep.isIdentifierStart(ch)) {
808
+ this.index++;
809
+ } else {
810
+ this.throwError('Unexpected ' + this.char);
811
+ }
812
+ while (this.index < this.expr.length) {
813
+ ch = this.code;
814
+ if (Jsep.isIdentifierPart(ch)) {
815
+ this.index++;
816
+ } else {
817
+ break;
818
+ }
819
+ }
820
+ return {
821
+ type: Jsep.IDENTIFIER,
822
+ name: this.expr.slice(start, this.index)
823
+ };
824
+ }
825
+
826
+ /**
827
+ * Gobbles a list of arguments within the context of a function call
828
+ * or array literal. This function also assumes that the opening character
829
+ * `(` or `[` has already been gobbled, and gobbles expressions and commas
830
+ * until the terminator character `)` or `]` is encountered.
831
+ * e.g. `foo(bar, baz)`, `my_func()`, or `[bar, baz]`
832
+ * @param {number} termination
833
+ * @returns {jsep.Expression[]}
834
+ */
835
+ gobbleArguments(termination) {
836
+ const args = [];
837
+ let closed = false;
838
+ let separator_count = 0;
839
+ while (this.index < this.expr.length) {
840
+ this.gobbleSpaces();
841
+ let ch_i = this.code;
842
+ if (ch_i === termination) {
843
+ // done parsing
844
+ closed = true;
845
+ this.index++;
846
+ if (termination === Jsep.CPAREN_CODE && separator_count && separator_count >= args.length) {
847
+ this.throwError('Unexpected token ' + String.fromCharCode(termination));
848
+ }
849
+ break;
850
+ } else if (ch_i === Jsep.COMMA_CODE) {
851
+ // between expressions
852
+ this.index++;
853
+ separator_count++;
854
+ if (separator_count !== args.length) {
855
+ // missing argument
856
+ if (termination === Jsep.CPAREN_CODE) {
857
+ this.throwError('Unexpected token ,');
858
+ } else if (termination === Jsep.CBRACK_CODE) {
859
+ for (let arg = args.length; arg < separator_count; arg++) {
860
+ args.push(null);
861
+ }
862
+ }
863
+ }
864
+ } else if (args.length !== separator_count && separator_count !== 0) {
865
+ // NOTE: `&& separator_count !== 0` allows for either all commas, or all spaces as arguments
866
+ this.throwError('Expected comma');
867
+ } else {
868
+ const node = this.gobbleExpression();
869
+ if (!node || node.type === Jsep.COMPOUND) {
870
+ this.throwError('Expected comma');
871
+ }
872
+ args.push(node);
873
+ }
874
+ }
875
+ if (!closed) {
876
+ this.throwError('Expected ' + String.fromCharCode(termination));
877
+ }
878
+ return args;
879
+ }
880
+
881
+ /**
882
+ * Responsible for parsing a group of things within parentheses `()`
883
+ * that have no identifier in front (so not a function call)
884
+ * This function assumes that it needs to gobble the opening parenthesis
885
+ * and then tries to gobble everything within that parenthesis, assuming
886
+ * that the next thing it should see is the close parenthesis. If not,
887
+ * then the expression probably doesn't have a `)`
888
+ * @returns {boolean|jsep.Expression}
889
+ */
890
+ gobbleGroup() {
891
+ this.index++;
892
+ let nodes = this.gobbleExpressions(Jsep.CPAREN_CODE);
893
+ if (this.code === Jsep.CPAREN_CODE) {
894
+ this.index++;
895
+ if (nodes.length === 1) {
896
+ return nodes[0];
897
+ } else if (!nodes.length) {
898
+ return false;
899
+ } else {
900
+ return {
901
+ type: Jsep.SEQUENCE_EXP,
902
+ expressions: nodes
903
+ };
904
+ }
905
+ } else {
906
+ this.throwError('Unclosed (');
907
+ }
908
+ }
909
+
910
+ /**
911
+ * Responsible for parsing Array literals `[1, 2, 3]`
912
+ * This function assumes that it needs to gobble the opening bracket
913
+ * and then tries to gobble the expressions as arguments.
914
+ * @returns {jsep.ArrayExpression}
915
+ */
916
+ gobbleArray() {
917
+ this.index++;
918
+ return {
919
+ type: Jsep.ARRAY_EXP,
920
+ elements: this.gobbleArguments(Jsep.CBRACK_CODE)
921
+ };
922
+ }
923
+ }
924
+
925
+ // Static fields:
926
+ const hooks = new Hooks();
927
+ Object.assign(Jsep, {
928
+ hooks,
929
+ plugins: new Plugins(Jsep),
930
+ // Node Types
931
+ // ----------
932
+ // This is the full set of types that any JSEP node can be.
933
+ // Store them here to save space when minified
934
+ COMPOUND: 'Compound',
935
+ SEQUENCE_EXP: 'SequenceExpression',
936
+ IDENTIFIER: 'Identifier',
937
+ MEMBER_EXP: 'MemberExpression',
938
+ LITERAL: 'Literal',
939
+ THIS_EXP: 'ThisExpression',
940
+ CALL_EXP: 'CallExpression',
941
+ UNARY_EXP: 'UnaryExpression',
942
+ BINARY_EXP: 'BinaryExpression',
943
+ ARRAY_EXP: 'ArrayExpression',
944
+ TAB_CODE: 9,
945
+ LF_CODE: 10,
946
+ CR_CODE: 13,
947
+ SPACE_CODE: 32,
948
+ PERIOD_CODE: 46,
949
+ // '.'
950
+ COMMA_CODE: 44,
951
+ // ','
952
+ SQUOTE_CODE: 39,
953
+ // single quote
954
+ DQUOTE_CODE: 34,
955
+ // double quotes
956
+ OPAREN_CODE: 40,
957
+ // (
958
+ CPAREN_CODE: 41,
959
+ // )
960
+ OBRACK_CODE: 91,
961
+ // [
962
+ CBRACK_CODE: 93,
963
+ // ]
964
+ QUMARK_CODE: 63,
965
+ // ?
966
+ SEMCOL_CODE: 59,
967
+ // ;
968
+ COLON_CODE: 58,
969
+ // :
970
+
971
+ // Operations
972
+ // ----------
973
+ // Use a quickly-accessible map to store all of the unary operators
974
+ // Values are set to `1` (it really doesn't matter)
975
+ unary_ops: {
976
+ '-': 1,
977
+ '!': 1,
978
+ '~': 1,
979
+ '+': 1
980
+ },
981
+ // Also use a map for the binary operations but set their values to their
982
+ // binary precedence for quick reference (higher number = higher precedence)
983
+ // see [Order of operations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence)
984
+ binary_ops: {
985
+ '||': 1,
986
+ '??': 1,
987
+ '&&': 2,
988
+ '|': 3,
989
+ '^': 4,
990
+ '&': 5,
991
+ '==': 6,
992
+ '!=': 6,
993
+ '===': 6,
994
+ '!==': 6,
995
+ '<': 7,
996
+ '>': 7,
997
+ '<=': 7,
998
+ '>=': 7,
999
+ '<<': 8,
1000
+ '>>': 8,
1001
+ '>>>': 8,
1002
+ '+': 9,
1003
+ '-': 9,
1004
+ '*': 10,
1005
+ '/': 10,
1006
+ '%': 10,
1007
+ '**': 11
1008
+ },
1009
+ // sets specific binary_ops as right-associative
1010
+ right_associative: new Set(['**']),
1011
+ // Additional valid identifier chars, apart from a-z, A-Z and 0-9 (except on the starting char)
1012
+ additional_identifier_chars: new Set(['$', '_']),
1013
+ // Literals
1014
+ // ----------
1015
+ // Store the values to return for the various literals we may encounter
1016
+ literals: {
1017
+ 'true': true,
1018
+ 'false': false,
1019
+ 'null': null
1020
+ },
1021
+ // Except for `this`, which is special. This could be changed to something like `'self'` as well
1022
+ this_str: 'this'
1023
+ });
1024
+ Jsep.max_unop_len = Jsep.getMaxKeyLen(Jsep.unary_ops);
1025
+ Jsep.max_binop_len = Jsep.getMaxKeyLen(Jsep.binary_ops);
1026
+
1027
+ // Backward Compatibility:
1028
+ const jsep = expr => new Jsep(expr).parse();
1029
+ const stdClassProps = Object.getOwnPropertyNames(class Test {});
1030
+ Object.getOwnPropertyNames(Jsep).filter(prop => !stdClassProps.includes(prop) && jsep[prop] === undefined).forEach(m => {
1031
+ jsep[m] = Jsep[m];
1032
+ });
1033
+ jsep.Jsep = Jsep; // allows for const { Jsep } = require('jsep');
1034
+
1035
+ const CONDITIONAL_EXP = 'ConditionalExpression';
1036
+ var ternary = {
1037
+ name: 'ternary',
1038
+ init(jsep) {
1039
+ // Ternary expression: test ? consequent : alternate
1040
+ jsep.hooks.add('after-expression', function gobbleTernary(env) {
1041
+ if (env.node && this.code === jsep.QUMARK_CODE) {
1042
+ this.index++;
1043
+ const test = env.node;
1044
+ const consequent = this.gobbleExpression();
1045
+ if (!consequent) {
1046
+ this.throwError('Expected expression');
1047
+ }
1048
+ this.gobbleSpaces();
1049
+ if (this.code === jsep.COLON_CODE) {
1050
+ this.index++;
1051
+ const alternate = this.gobbleExpression();
1052
+ if (!alternate) {
1053
+ this.throwError('Expected expression');
1054
+ }
1055
+ env.node = {
1056
+ type: CONDITIONAL_EXP,
1057
+ test,
1058
+ consequent,
1059
+ alternate
1060
+ };
1061
+
1062
+ // check for operators of higher priority than ternary (i.e. assignment)
1063
+ // jsep sets || at 1, and assignment at 0.9, and conditional should be between them
1064
+ if (test.operator && jsep.binary_ops[test.operator] <= 0.9) {
1065
+ let newTest = test;
1066
+ while (newTest.right.operator && jsep.binary_ops[newTest.right.operator] <= 0.9) {
1067
+ newTest = newTest.right;
1068
+ }
1069
+ env.node.test = newTest.right;
1070
+ newTest.right = env.node;
1071
+ env.node = test;
1072
+ }
1073
+ } else {
1074
+ this.throwError('Expected :');
1075
+ }
1076
+ }
1077
+ });
1078
+ }
1079
+ };
1080
+
1081
+ // Add default plugins:
1082
+
1083
+ jsep.plugins.register(ternary);
1084
+
1085
+ const FSLASH_CODE = 47; // '/'
1086
+ const BSLASH_CODE = 92; // '\\'
1087
+
1088
+ var index = {
1089
+ name: 'regex',
1090
+ init(jsep) {
1091
+ // Regex literal: /abc123/ig
1092
+ jsep.hooks.add('gobble-token', function gobbleRegexLiteral(env) {
1093
+ if (this.code === FSLASH_CODE) {
1094
+ const patternIndex = ++this.index;
1095
+ let inCharSet = false;
1096
+ while (this.index < this.expr.length) {
1097
+ if (this.code === FSLASH_CODE && !inCharSet) {
1098
+ const pattern = this.expr.slice(patternIndex, this.index);
1099
+ let flags = '';
1100
+ while (++this.index < this.expr.length) {
1101
+ const code = this.code;
1102
+ if (code >= 97 && code <= 122 // a...z
1103
+ || code >= 65 && code <= 90 // A...Z
1104
+ || code >= 48 && code <= 57) {
1105
+ // 0-9
1106
+ flags += this.char;
1107
+ } else {
1108
+ break;
1109
+ }
1110
+ }
1111
+ let value;
1112
+ try {
1113
+ value = new RegExp(pattern, flags);
1114
+ } catch (e) {
1115
+ this.throwError(e.message);
1116
+ }
1117
+ env.node = {
1118
+ type: jsep.LITERAL,
1119
+ value,
1120
+ raw: this.expr.slice(patternIndex - 1, this.index)
1121
+ };
1122
+
1123
+ // allow . [] and () after regex: /regex/.test(a)
1124
+ env.node = this.gobbleTokenProperty(env.node);
1125
+ return env.node;
1126
+ }
1127
+ if (this.code === jsep.OBRACK_CODE) {
1128
+ inCharSet = true;
1129
+ } else if (inCharSet && this.code === jsep.CBRACK_CODE) {
1130
+ inCharSet = false;
1131
+ }
1132
+ this.index += this.code === BSLASH_CODE ? 2 : 1;
1133
+ }
1134
+ this.throwError('Unclosed Regex');
1135
+ }
1136
+ });
1137
+ }
1138
+ };
1139
+
1140
+ const PLUS_CODE = 43; // +
1141
+ const MINUS_CODE = 45; // -
1142
+
1143
+ const plugin = {
1144
+ name: 'assignment',
1145
+ assignmentOperators: new Set(['=', '*=', '**=', '/=', '%=', '+=', '-=', '<<=', '>>=', '>>>=', '&=', '^=', '|=', '||=', '&&=', '??=']),
1146
+ updateOperators: [PLUS_CODE, MINUS_CODE],
1147
+ assignmentPrecedence: 0.9,
1148
+ init(jsep) {
1149
+ const updateNodeTypes = [jsep.IDENTIFIER, jsep.MEMBER_EXP];
1150
+ plugin.assignmentOperators.forEach(op => jsep.addBinaryOp(op, plugin.assignmentPrecedence, true));
1151
+ jsep.hooks.add('gobble-token', function gobbleUpdatePrefix(env) {
1152
+ const code = this.code;
1153
+ if (plugin.updateOperators.some(c => c === code && c === this.expr.charCodeAt(this.index + 1))) {
1154
+ this.index += 2;
1155
+ env.node = {
1156
+ type: 'UpdateExpression',
1157
+ operator: code === PLUS_CODE ? '++' : '--',
1158
+ argument: this.gobbleTokenProperty(this.gobbleIdentifier()),
1159
+ prefix: true
1160
+ };
1161
+ if (!env.node.argument || !updateNodeTypes.includes(env.node.argument.type)) {
1162
+ this.throwError(`Unexpected ${env.node.operator}`);
1163
+ }
1164
+ }
1165
+ });
1166
+ jsep.hooks.add('after-token', function gobbleUpdatePostfix(env) {
1167
+ if (env.node) {
1168
+ const code = this.code;
1169
+ if (plugin.updateOperators.some(c => c === code && c === this.expr.charCodeAt(this.index + 1))) {
1170
+ if (!updateNodeTypes.includes(env.node.type)) {
1171
+ this.throwError(`Unexpected ${env.node.operator}`);
1172
+ }
1173
+ this.index += 2;
1174
+ env.node = {
1175
+ type: 'UpdateExpression',
1176
+ operator: code === PLUS_CODE ? '++' : '--',
1177
+ argument: env.node,
1178
+ prefix: false
1179
+ };
1180
+ }
1181
+ }
1182
+ });
1183
+ jsep.hooks.add('after-expression', function gobbleAssignment(env) {
1184
+ if (env.node) {
1185
+ // Note: Binaries can be chained in a single expression to respect
1186
+ // operator precedence (i.e. a = b = 1 + 2 + 3)
1187
+ // Update all binary assignment nodes in the tree
1188
+ updateBinariesToAssignments(env.node);
1189
+ }
1190
+ });
1191
+ function updateBinariesToAssignments(node) {
1192
+ if (plugin.assignmentOperators.has(node.operator)) {
1193
+ node.type = 'AssignmentExpression';
1194
+ updateBinariesToAssignments(node.left);
1195
+ updateBinariesToAssignments(node.right);
1196
+ } else if (!node.operator) {
1197
+ Object.values(node).forEach(val => {
1198
+ if (val && typeof val === 'object') {
1199
+ updateBinariesToAssignments(val);
1200
+ }
1201
+ });
1202
+ }
1203
+ }
1204
+ }
1205
+ };
1206
+
1207
+ /* eslint-disable no-bitwise -- Convenient */
1208
+
1209
+ // register plugins
1210
+ jsep.plugins.register(index, plugin);
1211
+ jsep.addUnaryOp('typeof');
1212
+ jsep.addLiteral('null', null);
1213
+ jsep.addLiteral('undefined', undefined);
1214
+ const BLOCKED_PROTO_PROPERTIES = new Set(['constructor', '__proto__', '__defineGetter__', '__defineSetter__']);
1215
+ const SafeEval = {
1216
+ /**
1217
+ * @param {jsep.Expression} ast
1218
+ * @param {Record<string, any>} subs
1219
+ */
1220
+ evalAst(ast, subs) {
1221
+ switch (ast.type) {
1222
+ case 'BinaryExpression':
1223
+ case 'LogicalExpression':
1224
+ return SafeEval.evalBinaryExpression(ast, subs);
1225
+ case 'Compound':
1226
+ return SafeEval.evalCompound(ast, subs);
1227
+ case 'ConditionalExpression':
1228
+ return SafeEval.evalConditionalExpression(ast, subs);
1229
+ case 'Identifier':
1230
+ return SafeEval.evalIdentifier(ast, subs);
1231
+ case 'Literal':
1232
+ return SafeEval.evalLiteral(ast, subs);
1233
+ case 'MemberExpression':
1234
+ return SafeEval.evalMemberExpression(ast, subs);
1235
+ case 'UnaryExpression':
1236
+ return SafeEval.evalUnaryExpression(ast, subs);
1237
+ case 'ArrayExpression':
1238
+ return SafeEval.evalArrayExpression(ast, subs);
1239
+ case 'CallExpression':
1240
+ return SafeEval.evalCallExpression(ast, subs);
1241
+ case 'AssignmentExpression':
1242
+ return SafeEval.evalAssignmentExpression(ast, subs);
1243
+ default:
1244
+ throw SyntaxError('Unexpected expression', ast);
1245
+ }
1246
+ },
1247
+ evalBinaryExpression(ast, subs) {
1248
+ const result = {
1249
+ '||': (a, b) => a || b(),
1250
+ '&&': (a, b) => a && b(),
1251
+ '|': (a, b) => a | b(),
1252
+ '^': (a, b) => a ^ b(),
1253
+ '&': (a, b) => a & b(),
1254
+ // eslint-disable-next-line eqeqeq -- API
1255
+ '==': (a, b) => a == b(),
1256
+ // eslint-disable-next-line eqeqeq -- API
1257
+ '!=': (a, b) => a != b(),
1258
+ '===': (a, b) => a === b(),
1259
+ '!==': (a, b) => a !== b(),
1260
+ '<': (a, b) => a < b(),
1261
+ '>': (a, b) => a > b(),
1262
+ '<=': (a, b) => a <= b(),
1263
+ '>=': (a, b) => a >= b(),
1264
+ '<<': (a, b) => a << b(),
1265
+ '>>': (a, b) => a >> b(),
1266
+ '>>>': (a, b) => a >>> b(),
1267
+ '+': (a, b) => a + b(),
1268
+ '-': (a, b) => a - b(),
1269
+ '*': (a, b) => a * b(),
1270
+ '/': (a, b) => a / b(),
1271
+ '%': (a, b) => a % b()
1272
+ }[ast.operator](SafeEval.evalAst(ast.left, subs), () => SafeEval.evalAst(ast.right, subs));
1273
+ return result;
1274
+ },
1275
+ evalCompound(ast, subs) {
1276
+ let last;
1277
+ for (let i = 0; i < ast.body.length; i++) {
1278
+ if (ast.body[i].type === 'Identifier' && ['var', 'let', 'const'].includes(ast.body[i].name) && ast.body[i + 1] && ast.body[i + 1].type === 'AssignmentExpression') {
1279
+ // var x=2; is detected as
1280
+ // [{Identifier var}, {AssignmentExpression x=2}]
1281
+ // eslint-disable-next-line @stylistic/max-len -- Long
1282
+ // eslint-disable-next-line sonarjs/updated-loop-counter -- Convenient
1283
+ i += 1;
1284
+ }
1285
+ const expr = ast.body[i];
1286
+ last = SafeEval.evalAst(expr, subs);
1287
+ }
1288
+ return last;
1289
+ },
1290
+ evalConditionalExpression(ast, subs) {
1291
+ if (SafeEval.evalAst(ast.test, subs)) {
1292
+ return SafeEval.evalAst(ast.consequent, subs);
1293
+ }
1294
+ return SafeEval.evalAst(ast.alternate, subs);
1295
+ },
1296
+ evalIdentifier(ast, subs) {
1297
+ if (Object.hasOwn(subs, ast.name)) {
1298
+ return subs[ast.name];
1299
+ }
1300
+ throw ReferenceError(`${ast.name} is not defined`);
1301
+ },
1302
+ evalLiteral(ast) {
1303
+ return ast.value;
1304
+ },
1305
+ evalMemberExpression(ast, subs) {
1306
+ const prop = String(
1307
+ // NOTE: `String(value)` throws error when
1308
+ // value has overwritten the toString method to return non-string
1309
+ // i.e. `value = {toString: () => []}`
1310
+ ast.computed ? SafeEval.evalAst(ast.property) // `object[property]`
1311
+ : ast.property.name // `object.property` property is Identifier
1312
+ );
1313
+ const obj = SafeEval.evalAst(ast.object, subs);
1314
+ if (obj === undefined || obj === null) {
1315
+ throw TypeError(`Cannot read properties of ${obj} (reading '${prop}')`);
1316
+ }
1317
+ if (!Object.hasOwn(obj, prop) && BLOCKED_PROTO_PROPERTIES.has(prop)) {
1318
+ throw TypeError(`Cannot read properties of ${obj} (reading '${prop}')`);
1319
+ }
1320
+ const result = obj[prop];
1321
+ if (typeof result === 'function') {
1322
+ return result.bind(obj); // arrow functions aren't affected by bind.
1323
+ }
1324
+ return result;
1325
+ },
1326
+ evalUnaryExpression(ast, subs) {
1327
+ const result = {
1328
+ '-': a => -SafeEval.evalAst(a, subs),
1329
+ '!': a => !SafeEval.evalAst(a, subs),
1330
+ '~': a => ~SafeEval.evalAst(a, subs),
1331
+ // eslint-disable-next-line no-implicit-coercion -- API
1332
+ '+': a => +SafeEval.evalAst(a, subs),
1333
+ typeof: a => typeof SafeEval.evalAst(a, subs)
1334
+ }[ast.operator](ast.argument);
1335
+ return result;
1336
+ },
1337
+ evalArrayExpression(ast, subs) {
1338
+ return ast.elements.map(el => SafeEval.evalAst(el, subs));
1339
+ },
1340
+ evalCallExpression(ast, subs) {
1341
+ const args = ast.arguments.map(arg => SafeEval.evalAst(arg, subs));
1342
+ const func = SafeEval.evalAst(ast.callee, subs);
1343
+ // if (func === Function) {
1344
+ // throw new Error('Function constructor is disabled');
1345
+ // }
1346
+ return func(...args);
1347
+ },
1348
+ evalAssignmentExpression(ast, subs) {
1349
+ if (ast.left.type !== 'Identifier') {
1350
+ throw SyntaxError('Invalid left-hand side in assignment');
1351
+ }
1352
+ const id = ast.left.name;
1353
+ const value = SafeEval.evalAst(ast.right, subs);
1354
+ subs[id] = value;
1355
+ return subs[id];
1356
+ }
1357
+ };
1358
+
1359
+ /**
1360
+ * A replacement for NodeJS' VM.Script which is also {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP | Content Security Policy} friendly.
1361
+ */
1362
+ class SafeScript {
1363
+ /**
1364
+ * @param {string} expr Expression to evaluate
1365
+ */
1366
+ constructor(expr) {
1367
+ this.code = expr;
1368
+ this.ast = jsep(this.code);
1369
+ }
1370
+
1371
+ /**
1372
+ * @param {object} context Object whose items will be added
1373
+ * to evaluation
1374
+ * @returns {EvaluatedResult} Result of evaluated code
1375
+ */
1376
+ runInNewContext(context) {
1377
+ // `Object.create(null)` creates a prototypeless object
1378
+ const keyMap = Object.assign(Object.create(null), context);
1379
+ return SafeEval.evalAst(this.ast, keyMap);
1380
+ }
1381
+ }
1382
+
1383
+ /* eslint-disable camelcase -- Convenient for escaping */
1384
+
1385
+
1386
+ /**
1387
+ * @typedef {null|boolean|number|string|object|GenericArray} JSONObject
1388
+ */
1389
+
1390
+ /**
1391
+ * @typedef {any} AnyItem
1392
+ */
1393
+
1394
+ /**
1395
+ * @typedef {any} AnyResult
1396
+ */
1397
+
1398
+ /**
1399
+ * Copies array and then pushes item into it.
1400
+ * @param {GenericArray} arr Array to copy and into which to push
1401
+ * @param {AnyItem} item Array item to add (to end)
1402
+ * @returns {GenericArray} Copy of the original array
1403
+ */
1404
+ function push(arr, item) {
1405
+ arr = arr.slice();
1406
+ arr.push(item);
1407
+ return arr;
1408
+ }
1409
+ /**
1410
+ * Copies array and then unshifts item into it.
1411
+ * @param {AnyItem} item Array item to add (to beginning)
1412
+ * @param {GenericArray} arr Array to copy and into which to unshift
1413
+ * @returns {GenericArray} Copy of the original array
1414
+ */
1415
+ function unshift(item, arr) {
1416
+ arr = arr.slice();
1417
+ arr.unshift(item);
1418
+ return arr;
1419
+ }
1420
+
1421
+ /**
1422
+ * Caught when JSONPath is used without `new` but rethrown if with `new`
1423
+ * @extends Error
1424
+ */
1425
+ class NewError extends Error {
1426
+ /**
1427
+ * @param {AnyResult} value The evaluated scalar value
1428
+ */
1429
+ constructor(value) {
1430
+ super('JSONPath should not be called with "new" (it prevents return ' + 'of (unwrapped) scalar values)');
1431
+ this.avoidNew = true;
1432
+ this.value = value;
1433
+ this.name = 'NewError';
1434
+ }
1435
+ }
1436
+
1437
+ /**
1438
+ * @typedef {object} ReturnObject
1439
+ * @property {string} path
1440
+ * @property {JSONObject} value
1441
+ * @property {object|GenericArray} parent
1442
+ * @property {string} parentProperty
1443
+ */
1444
+
1445
+ /**
1446
+ * @callback JSONPathCallback
1447
+ * @param {string|object} preferredOutput
1448
+ * @param {"value"|"property"} type
1449
+ * @param {ReturnObject} fullRetObj
1450
+ * @returns {void}
1451
+ */
1452
+
1453
+ /**
1454
+ * @callback OtherTypeCallback
1455
+ * @param {JSONObject} val
1456
+ * @param {string} path
1457
+ * @param {object|GenericArray} parent
1458
+ * @param {string} parentPropName
1459
+ * @returns {boolean}
1460
+ */
1461
+
1462
+ /**
1463
+ * @typedef {any} ContextItem
1464
+ */
1465
+
1466
+ /**
1467
+ * @typedef {any} EvaluatedResult
1468
+ */
1469
+
1470
+ /**
1471
+ * @callback EvalCallback
1472
+ * @param {string} code
1473
+ * @param {ContextItem} context
1474
+ * @returns {EvaluatedResult}
1475
+ */
1476
+
1477
+ /**
1478
+ * @typedef {typeof SafeScript} EvalClass
1479
+ */
1480
+
1481
+ /**
1482
+ * @typedef {object} JSONPathOptions
1483
+ * @property {JSON} json
1484
+ * @property {string|string[]} path
1485
+ * @property {"value"|"path"|"pointer"|"parent"|"parentProperty"|
1486
+ * "all"} [resultType="value"]
1487
+ * @property {boolean} [flatten=false]
1488
+ * @property {boolean} [wrap=true]
1489
+ * @property {object} [sandbox={}]
1490
+ * @property {EvalCallback|EvalClass|'safe'|'native'|
1491
+ * boolean} [eval = 'safe']
1492
+ * @property {object|GenericArray|null} [parent=null]
1493
+ * @property {string|null} [parentProperty=null]
1494
+ * @property {JSONPathCallback} [callback]
1495
+ * @property {OtherTypeCallback} [otherTypeCallback] Defaults to
1496
+ * function which throws on encountering `@other`
1497
+ * @property {boolean} [autostart=true]
1498
+ */
1499
+
1500
+ /**
1501
+ * @param {string|JSONPathOptions} opts If a string, will be treated as `expr`
1502
+ * @param {string} [expr] JSON path to evaluate
1503
+ * @param {JSON} [obj] JSON object to evaluate against
1504
+ * @param {JSONPathCallback} [callback] Passed 3 arguments: 1) desired payload
1505
+ * per `resultType`, 2) `"value"|"property"`, 3) Full returned object with
1506
+ * all payloads
1507
+ * @param {OtherTypeCallback} [otherTypeCallback] If `@other()` is at the end
1508
+ * of one's query, this will be invoked with the value of the item, its
1509
+ * path, its parent, and its parent's property name, and it should return
1510
+ * a boolean indicating whether the supplied value belongs to the "other"
1511
+ * type or not (or it may handle transformations and return `false`).
1512
+ * @returns {JSONPath}
1513
+ * @class
1514
+ */
1515
+ function JSONPath(opts, expr, obj, callback, otherTypeCallback) {
1516
+ // eslint-disable-next-line no-restricted-syntax -- Allow for pseudo-class
1517
+ if (!(this instanceof JSONPath)) {
1518
+ try {
1519
+ return new JSONPath(opts, expr, obj, callback, otherTypeCallback);
1520
+ } catch (e) {
1521
+ if (!e.avoidNew) {
1522
+ throw e;
1523
+ }
1524
+ return e.value;
1525
+ }
1526
+ }
1527
+ if (typeof opts === 'string') {
1528
+ otherTypeCallback = callback;
1529
+ callback = obj;
1530
+ obj = expr;
1531
+ expr = opts;
1532
+ opts = null;
1533
+ }
1534
+ const optObj = opts && typeof opts === 'object';
1535
+ opts = opts || {};
1536
+ this.json = opts.json || obj;
1537
+ this.path = opts.path || expr;
1538
+ this.resultType = opts.resultType || 'value';
1539
+ this.flatten = opts.flatten || false;
1540
+ this.wrap = Object.hasOwn(opts, 'wrap') ? opts.wrap : true;
1541
+ this.sandbox = opts.sandbox || {};
1542
+ this.eval = opts.eval === undefined ? 'safe' : opts.eval;
1543
+ this.ignoreEvalErrors = typeof opts.ignoreEvalErrors === 'undefined' ? false : opts.ignoreEvalErrors;
1544
+ this.parent = opts.parent || null;
1545
+ this.parentProperty = opts.parentProperty || null;
1546
+ this.callback = opts.callback || callback || null;
1547
+ this.otherTypeCallback = opts.otherTypeCallback || otherTypeCallback || function () {
1548
+ throw new TypeError('You must supply an otherTypeCallback callback option ' + 'with the @other() operator.');
1549
+ };
1550
+ if (opts.autostart !== false) {
1551
+ const args = {
1552
+ path: optObj ? opts.path : expr
1553
+ };
1554
+ if (!optObj) {
1555
+ args.json = obj;
1556
+ } else if ('json' in opts) {
1557
+ args.json = opts.json;
1558
+ }
1559
+ const ret = this.evaluate(args);
1560
+ if (!ret || typeof ret !== 'object') {
1561
+ throw new NewError(ret);
1562
+ }
1563
+ return ret;
1564
+ }
1565
+ }
1566
+
1567
+ // PUBLIC METHODS
1568
+ JSONPath.prototype.evaluate = function (expr, json, callback, otherTypeCallback) {
1569
+ let currParent = this.parent,
1570
+ currParentProperty = this.parentProperty;
1571
+ let {
1572
+ flatten,
1573
+ wrap
1574
+ } = this;
1575
+ this.currResultType = this.resultType;
1576
+ this.currEval = this.eval;
1577
+ this.currSandbox = this.sandbox;
1578
+ callback = callback || this.callback;
1579
+ this.currOtherTypeCallback = otherTypeCallback || this.otherTypeCallback;
1580
+ json = json || this.json;
1581
+ expr = expr || this.path;
1582
+ if (expr && typeof expr === 'object' && !Array.isArray(expr)) {
1583
+ if (!expr.path && expr.path !== '') {
1584
+ throw new TypeError('You must supply a "path" property when providing an object ' + 'argument to JSONPath.evaluate().');
1585
+ }
1586
+ if (!Object.hasOwn(expr, 'json')) {
1587
+ throw new TypeError('You must supply a "json" property when providing an object ' + 'argument to JSONPath.evaluate().');
1588
+ }
1589
+ ({
1590
+ json
1591
+ } = expr);
1592
+ flatten = Object.hasOwn(expr, 'flatten') ? expr.flatten : flatten;
1593
+ this.currResultType = Object.hasOwn(expr, 'resultType') ? expr.resultType : this.currResultType;
1594
+ this.currSandbox = Object.hasOwn(expr, 'sandbox') ? expr.sandbox : this.currSandbox;
1595
+ wrap = Object.hasOwn(expr, 'wrap') ? expr.wrap : wrap;
1596
+ this.currEval = Object.hasOwn(expr, 'eval') ? expr.eval : this.currEval;
1597
+ callback = Object.hasOwn(expr, 'callback') ? expr.callback : callback;
1598
+ this.currOtherTypeCallback = Object.hasOwn(expr, 'otherTypeCallback') ? expr.otherTypeCallback : this.currOtherTypeCallback;
1599
+ currParent = Object.hasOwn(expr, 'parent') ? expr.parent : currParent;
1600
+ currParentProperty = Object.hasOwn(expr, 'parentProperty') ? expr.parentProperty : currParentProperty;
1601
+ expr = expr.path;
1602
+ }
1603
+ currParent = currParent || null;
1604
+ currParentProperty = currParentProperty || null;
1605
+ if (Array.isArray(expr)) {
1606
+ expr = JSONPath.toPathString(expr);
1607
+ }
1608
+ if (!expr && expr !== '' || !json) {
1609
+ return undefined;
1610
+ }
1611
+ const exprList = JSONPath.toPathArray(expr);
1612
+ if (exprList[0] === '$' && exprList.length > 1) {
1613
+ exprList.shift();
1614
+ }
1615
+ this._hasParentSelector = null;
1616
+ const result = this._trace(exprList, json, ['$'], currParent, currParentProperty, callback).filter(function (ea) {
1617
+ return ea && !ea.isParentSelector;
1618
+ });
1619
+ if (!result.length) {
1620
+ return wrap ? [] : undefined;
1621
+ }
1622
+ if (!wrap && result.length === 1 && !result[0].hasArrExpr) {
1623
+ return this._getPreferredOutput(result[0]);
1624
+ }
1625
+ return result.reduce((rslt, ea) => {
1626
+ const valOrPath = this._getPreferredOutput(ea);
1627
+ if (flatten && Array.isArray(valOrPath)) {
1628
+ rslt = rslt.concat(valOrPath);
1629
+ } else {
1630
+ rslt.push(valOrPath);
1631
+ }
1632
+ return rslt;
1633
+ }, []);
1634
+ };
1635
+
1636
+ // PRIVATE METHODS
1637
+
1638
+ JSONPath.prototype._getPreferredOutput = function (ea) {
1639
+ const resultType = this.currResultType;
1640
+ switch (resultType) {
1641
+ case 'all':
1642
+ {
1643
+ const path = Array.isArray(ea.path) ? ea.path : JSONPath.toPathArray(ea.path);
1644
+ ea.pointer = JSONPath.toPointer(path);
1645
+ ea.path = typeof ea.path === 'string' ? ea.path : JSONPath.toPathString(ea.path);
1646
+ return ea;
1647
+ }
1648
+ case 'value':
1649
+ case 'parent':
1650
+ case 'parentProperty':
1651
+ return ea[resultType];
1652
+ case 'path':
1653
+ return JSONPath.toPathString(ea[resultType]);
1654
+ case 'pointer':
1655
+ return JSONPath.toPointer(ea.path);
1656
+ default:
1657
+ throw new TypeError('Unknown result type');
1658
+ }
1659
+ };
1660
+ JSONPath.prototype._handleCallback = function (fullRetObj, callback, type) {
1661
+ if (callback) {
1662
+ const preferredOutput = this._getPreferredOutput(fullRetObj);
1663
+ fullRetObj.path = typeof fullRetObj.path === 'string' ? fullRetObj.path : JSONPath.toPathString(fullRetObj.path);
1664
+ // eslint-disable-next-line n/callback-return -- No need to return
1665
+ callback(preferredOutput, type, fullRetObj);
1666
+ }
1667
+ };
1668
+
1669
+ /**
1670
+ *
1671
+ * @param {string} expr
1672
+ * @param {JSONObject} val
1673
+ * @param {string} path
1674
+ * @param {object|GenericArray} parent
1675
+ * @param {string} parentPropName
1676
+ * @param {JSONPathCallback} callback
1677
+ * @param {boolean} hasArrExpr
1678
+ * @param {boolean} literalPriority
1679
+ * @returns {ReturnObject|ReturnObject[]}
1680
+ */
1681
+ JSONPath.prototype._trace = function (expr, val, path, parent, parentPropName, callback, hasArrExpr, literalPriority) {
1682
+ // No expr to follow? return path and value as the result of
1683
+ // this trace branch
1684
+ let retObj;
1685
+ if (!expr.length) {
1686
+ retObj = {
1687
+ path,
1688
+ value: val,
1689
+ parent,
1690
+ parentProperty: parentPropName,
1691
+ hasArrExpr
1692
+ };
1693
+ this._handleCallback(retObj, callback, 'value');
1694
+ return retObj;
1695
+ }
1696
+ const loc = expr[0],
1697
+ x = expr.slice(1);
1698
+
1699
+ // We need to gather the return value of recursive trace calls in order to
1700
+ // do the parent sel computation.
1701
+ const ret = [];
1702
+ /**
1703
+ *
1704
+ * @param {ReturnObject|ReturnObject[]} elems
1705
+ * @returns {void}
1706
+ */
1707
+ function addRet(elems) {
1708
+ if (Array.isArray(elems)) {
1709
+ // This was causing excessive stack size in Node (with or
1710
+ // without Babel) against our performance test:
1711
+ // `ret.push(...elems);`
1712
+ elems.forEach(t => {
1713
+ ret.push(t);
1714
+ });
1715
+ } else {
1716
+ ret.push(elems);
1717
+ }
1718
+ }
1719
+ if ((typeof loc !== 'string' || literalPriority) && val && Object.hasOwn(val, loc)) {
1720
+ // simple case--directly follow property
1721
+ addRet(this._trace(x, val[loc], push(path, loc), val, loc, callback, hasArrExpr));
1722
+ // eslint-disable-next-line unicorn/prefer-switch -- Part of larger `if`
1723
+ } else if (loc === '*') {
1724
+ // all child properties
1725
+ this._walk(val, m => {
1726
+ addRet(this._trace(x, val[m], push(path, m), val, m, callback, true, true));
1727
+ });
1728
+ } else if (loc === '..') {
1729
+ // all descendent parent properties
1730
+ // Check remaining expression with val's immediate children
1731
+ addRet(this._trace(x, val, path, parent, parentPropName, callback, hasArrExpr));
1732
+ this._walk(val, m => {
1733
+ // We don't join m and x here because we only want parents,
1734
+ // not scalar values
1735
+ if (typeof val[m] === 'object') {
1736
+ // Keep going with recursive descent on val's
1737
+ // object children
1738
+ addRet(this._trace(expr.slice(), val[m], push(path, m), val, m, callback, true));
1739
+ }
1740
+ });
1741
+ // The parent sel computation is handled in the frame above using the
1742
+ // ancestor object of val
1743
+ } else if (loc === '^') {
1744
+ // This is not a final endpoint, so we do not invoke the callback here
1745
+ this._hasParentSelector = true;
1746
+ return {
1747
+ path: path.slice(0, -1),
1748
+ expr: x,
1749
+ isParentSelector: true
1750
+ };
1751
+ } else if (loc === '~') {
1752
+ // property name
1753
+ retObj = {
1754
+ path: push(path, loc),
1755
+ value: parentPropName,
1756
+ parent,
1757
+ parentProperty: null
1758
+ };
1759
+ this._handleCallback(retObj, callback, 'property');
1760
+ return retObj;
1761
+ } else if (loc === '$') {
1762
+ // root only
1763
+ addRet(this._trace(x, val, path, null, null, callback, hasArrExpr));
1764
+ } else if (/^(-?\d*):(-?\d*):?(\d*)$/u.test(loc)) {
1765
+ // [start:end:step] Python slice syntax
1766
+ addRet(this._slice(loc, x, val, path, parent, parentPropName, callback));
1767
+ } else if (loc.indexOf('?(') === 0) {
1768
+ // [?(expr)] (filtering)
1769
+ if (this.currEval === false) {
1770
+ throw new Error('Eval [?(expr)] prevented in JSONPath expression.');
1771
+ }
1772
+ const safeLoc = loc.replace(/^\?\((.*?)\)$/u, '$1');
1773
+ // check for a nested filter expression
1774
+ const nested = /@.?([^?]*)[['](\??\(.*?\))(?!.\)\])[\]']/gu.exec(safeLoc);
1775
+ if (nested) {
1776
+ // find if there are matches in the nested expression
1777
+ // add them to the result set if there is at least one match
1778
+ this._walk(val, m => {
1779
+ const npath = [nested[2]];
1780
+ const nvalue = nested[1] ? val[m][nested[1]] : val[m];
1781
+ const filterResults = this._trace(npath, nvalue, path, parent, parentPropName, callback, true);
1782
+ if (filterResults.length > 0) {
1783
+ addRet(this._trace(x, val[m], push(path, m), val, m, callback, true));
1784
+ }
1785
+ });
1786
+ } else {
1787
+ this._walk(val, m => {
1788
+ if (this._eval(safeLoc, val[m], m, path, parent, parentPropName)) {
1789
+ addRet(this._trace(x, val[m], push(path, m), val, m, callback, true));
1790
+ }
1791
+ });
1792
+ }
1793
+ } else if (loc[0] === '(') {
1794
+ // [(expr)] (dynamic property/index)
1795
+ if (this.currEval === false) {
1796
+ throw new Error('Eval [(expr)] prevented in JSONPath expression.');
1797
+ }
1798
+ // As this will resolve to a property name (but we don't know it
1799
+ // yet), property and parent information is relative to the
1800
+ // parent of the property to which this expression will resolve
1801
+ addRet(this._trace(unshift(this._eval(loc, val, path.at(-1), path.slice(0, -1), parent, parentPropName), x), val, path, parent, parentPropName, callback, hasArrExpr));
1802
+ } else if (loc[0] === '@') {
1803
+ // value type: @boolean(), etc.
1804
+ let addType = false;
1805
+ const valueType = loc.slice(1, -2);
1806
+ switch (valueType) {
1807
+ case 'scalar':
1808
+ if (!val || !['object', 'function'].includes(typeof val)) {
1809
+ addType = true;
1810
+ }
1811
+ break;
1812
+ case 'boolean':
1813
+ case 'string':
1814
+ case 'undefined':
1815
+ case 'function':
1816
+ if (typeof val === valueType) {
1817
+ addType = true;
1818
+ }
1819
+ break;
1820
+ case 'integer':
1821
+ if (Number.isFinite(val) && !(val % 1)) {
1822
+ addType = true;
1823
+ }
1824
+ break;
1825
+ case 'number':
1826
+ if (Number.isFinite(val)) {
1827
+ addType = true;
1828
+ }
1829
+ break;
1830
+ case 'nonFinite':
1831
+ if (typeof val === 'number' && !Number.isFinite(val)) {
1832
+ addType = true;
1833
+ }
1834
+ break;
1835
+ case 'object':
1836
+ if (val && typeof val === valueType) {
1837
+ addType = true;
1838
+ }
1839
+ break;
1840
+ case 'array':
1841
+ if (Array.isArray(val)) {
1842
+ addType = true;
1843
+ }
1844
+ break;
1845
+ case 'other':
1846
+ addType = this.currOtherTypeCallback(val, path, parent, parentPropName);
1847
+ break;
1848
+ case 'null':
1849
+ if (val === null) {
1850
+ addType = true;
1851
+ }
1852
+ break;
1853
+ /* c8 ignore next 2 */
1854
+ default:
1855
+ throw new TypeError('Unknown value type ' + valueType);
1856
+ }
1857
+ if (addType) {
1858
+ retObj = {
1859
+ path,
1860
+ value: val,
1861
+ parent,
1862
+ parentProperty: parentPropName
1863
+ };
1864
+ this._handleCallback(retObj, callback, 'value');
1865
+ return retObj;
1866
+ }
1867
+ // `-escaped property
1868
+ } else if (loc[0] === '`' && val && Object.hasOwn(val, loc.slice(1))) {
1869
+ const locProp = loc.slice(1);
1870
+ addRet(this._trace(x, val[locProp], push(path, locProp), val, locProp, callback, hasArrExpr, true));
1871
+ } else if (loc.includes(',')) {
1872
+ // [name1,name2,...]
1873
+ const parts = loc.split(',');
1874
+ for (const part of parts) {
1875
+ addRet(this._trace(unshift(part, x), val, path, parent, parentPropName, callback, true));
1876
+ }
1877
+ // simple case--directly follow property
1878
+ } else if (!literalPriority && val && Object.hasOwn(val, loc)) {
1879
+ addRet(this._trace(x, val[loc], push(path, loc), val, loc, callback, hasArrExpr, true));
1880
+ }
1881
+
1882
+ // We check the resulting values for parent selections. For parent
1883
+ // selections we discard the value object and continue the trace with the
1884
+ // current val object
1885
+ if (this._hasParentSelector) {
1886
+ for (let t = 0; t < ret.length; t++) {
1887
+ const rett = ret[t];
1888
+ if (rett && rett.isParentSelector) {
1889
+ const tmp = this._trace(rett.expr, val, rett.path, parent, parentPropName, callback, hasArrExpr);
1890
+ if (Array.isArray(tmp)) {
1891
+ ret[t] = tmp[0];
1892
+ const tl = tmp.length;
1893
+ for (let tt = 1; tt < tl; tt++) {
1894
+ // eslint-disable-next-line @stylistic/max-len -- Long
1895
+ // eslint-disable-next-line sonarjs/updated-loop-counter -- Convenient
1896
+ t++;
1897
+ ret.splice(t, 0, tmp[tt]);
1898
+ }
1899
+ } else {
1900
+ ret[t] = tmp;
1901
+ }
1902
+ }
1903
+ }
1904
+ }
1905
+ return ret;
1906
+ };
1907
+ JSONPath.prototype._walk = function (val, f) {
1908
+ if (Array.isArray(val)) {
1909
+ const n = val.length;
1910
+ for (let i = 0; i < n; i++) {
1911
+ f(i);
1912
+ }
1913
+ } else if (val && typeof val === 'object') {
1914
+ Object.keys(val).forEach(m => {
1915
+ f(m);
1916
+ });
1917
+ }
1918
+ };
1919
+ JSONPath.prototype._slice = function (loc, expr, val, path, parent, parentPropName, callback) {
1920
+ if (!Array.isArray(val)) {
1921
+ return undefined;
1922
+ }
1923
+ const len = val.length,
1924
+ parts = loc.split(':'),
1925
+ step = parts[2] && Number.parseInt(parts[2]) || 1;
1926
+ let start = parts[0] && Number.parseInt(parts[0]) || 0,
1927
+ end = parts[1] && Number.parseInt(parts[1]) || len;
1928
+ start = start < 0 ? Math.max(0, start + len) : Math.min(len, start);
1929
+ end = end < 0 ? Math.max(0, end + len) : Math.min(len, end);
1930
+ const ret = [];
1931
+ for (let i = start; i < end; i += step) {
1932
+ const tmp = this._trace(unshift(i, expr), val, path, parent, parentPropName, callback, true);
1933
+ // Should only be possible to be an array here since first part of
1934
+ // ``unshift(i, expr)` passed in above would not be empty, nor `~`,
1935
+ // nor begin with `@` (as could return objects)
1936
+ // This was causing excessive stack size in Node (with or
1937
+ // without Babel) against our performance test: `ret.push(...tmp);`
1938
+ tmp.forEach(t => {
1939
+ ret.push(t);
1940
+ });
1941
+ }
1942
+ return ret;
1943
+ };
1944
+ JSONPath.prototype._eval = function (code, _v, _vname, path, parent, parentPropName) {
1945
+ this.currSandbox._$_parentProperty = parentPropName;
1946
+ this.currSandbox._$_parent = parent;
1947
+ this.currSandbox._$_property = _vname;
1948
+ this.currSandbox._$_root = this.json;
1949
+ this.currSandbox._$_v = _v;
1950
+ const containsPath = code.includes('@path');
1951
+ if (containsPath) {
1952
+ this.currSandbox._$_path = JSONPath.toPathString(path.concat([_vname]));
1953
+ }
1954
+ const scriptCacheKey = this.currEval + 'Script:' + code;
1955
+ if (!JSONPath.cache[scriptCacheKey]) {
1956
+ let script = code.replaceAll('@parentProperty', '_$_parentProperty').replaceAll('@parent', '_$_parent').replaceAll('@property', '_$_property').replaceAll('@root', '_$_root').replaceAll(/@([.\s)[])/gu, '_$_v$1');
1957
+ if (containsPath) {
1958
+ script = script.replaceAll('@path', '_$_path');
1959
+ }
1960
+ if (this.currEval === 'safe' || this.currEval === true || this.currEval === undefined) {
1961
+ JSONPath.cache[scriptCacheKey] = new this.safeVm.Script(script);
1962
+ } else if (this.currEval === 'native') {
1963
+ JSONPath.cache[scriptCacheKey] = new this.vm.Script(script);
1964
+ } else if (typeof this.currEval === 'function' && this.currEval.prototype && Object.hasOwn(this.currEval.prototype, 'runInNewContext')) {
1965
+ const CurrEval = this.currEval;
1966
+ JSONPath.cache[scriptCacheKey] = new CurrEval(script);
1967
+ } else if (typeof this.currEval === 'function') {
1968
+ JSONPath.cache[scriptCacheKey] = {
1969
+ runInNewContext: context => this.currEval(script, context)
1970
+ };
1971
+ } else {
1972
+ throw new TypeError(`Unknown "eval" property "${this.currEval}"`);
1973
+ }
1974
+ }
1975
+ try {
1976
+ return JSONPath.cache[scriptCacheKey].runInNewContext(this.currSandbox);
1977
+ } catch (e) {
1978
+ if (this.ignoreEvalErrors) {
1979
+ return false;
1980
+ }
1981
+ throw new Error('jsonPath: ' + e.message + ': ' + code);
1982
+ }
1983
+ };
1984
+
1985
+ // PUBLIC CLASS PROPERTIES AND METHODS
1986
+
1987
+ // Could store the cache object itself
1988
+ JSONPath.cache = {};
1989
+
1990
+ /**
1991
+ * @param {string[]} pathArr Array to convert
1992
+ * @returns {string} The path string
1993
+ */
1994
+ JSONPath.toPathString = function (pathArr) {
1995
+ const x = pathArr,
1996
+ n = x.length;
1997
+ let p = '$';
1998
+ for (let i = 1; i < n; i++) {
1999
+ if (!/^(~|\^|@.*?\(\))$/u.test(x[i])) {
2000
+ p += /^[0-9*]+$/u.test(x[i]) ? '[' + x[i] + ']' : "['" + x[i] + "']";
2001
+ }
2002
+ }
2003
+ return p;
2004
+ };
2005
+
2006
+ /**
2007
+ * @param {string} pointer JSON Path
2008
+ * @returns {string} JSON Pointer
2009
+ */
2010
+ JSONPath.toPointer = function (pointer) {
2011
+ const x = pointer,
2012
+ n = x.length;
2013
+ let p = '';
2014
+ for (let i = 1; i < n; i++) {
2015
+ if (!/^(~|\^|@.*?\(\))$/u.test(x[i])) {
2016
+ p += '/' + x[i].toString().replaceAll('~', '~0').replaceAll('/', '~1');
2017
+ }
2018
+ }
2019
+ return p;
2020
+ };
2021
+
2022
+ /**
2023
+ * @param {string} expr Expression to convert
2024
+ * @returns {string[]}
2025
+ */
2026
+ JSONPath.toPathArray = function (expr) {
2027
+ const {
2028
+ cache
2029
+ } = JSONPath;
2030
+ if (cache[expr]) {
2031
+ return cache[expr].concat();
2032
+ }
2033
+ const subx = [];
2034
+ const normalized = expr
2035
+ // Properties
2036
+ .replaceAll(/@(?:null|boolean|number|string|integer|undefined|nonFinite|scalar|array|object|function|other)\(\)/gu, ';$&;')
2037
+ // Parenthetical evaluations (filtering and otherwise), directly
2038
+ // within brackets or single quotes
2039
+ .replaceAll(/[['](\??\(.*?\))[\]'](?!.\])/gu, function ($0, $1) {
2040
+ return '[#' + (subx.push($1) - 1) + ']';
2041
+ })
2042
+ // Escape periods and tildes within properties
2043
+ .replaceAll(/\[['"]([^'\]]*)['"]\]/gu, function ($0, prop) {
2044
+ return "['" + prop.replaceAll('.', '%@%').replaceAll('~', '%%@@%%') + "']";
2045
+ })
2046
+ // Properties operator
2047
+ .replaceAll('~', ';~;')
2048
+ // Split by property boundaries
2049
+ .replaceAll(/['"]?\.['"]?(?![^[]*\])|\[['"]?/gu, ';')
2050
+ // Reinsert periods within properties
2051
+ .replaceAll('%@%', '.')
2052
+ // Reinsert tildes within properties
2053
+ .replaceAll('%%@@%%', '~')
2054
+ // Parent
2055
+ .replaceAll(/(?:;)?(\^+)(?:;)?/gu, function ($0, ups) {
2056
+ return ';' + ups.split('').join(';') + ';';
2057
+ })
2058
+ // Descendents
2059
+ .replaceAll(/;;;|;;/gu, ';..;')
2060
+ // Remove trailing
2061
+ .replaceAll(/;$|'?\]|'$/gu, '');
2062
+ const exprList = normalized.split(';').map(function (exp) {
2063
+ const match = exp.match(/#(\d+)/u);
2064
+ return !match || !match[1] ? exp : subx[match[1]];
2065
+ });
2066
+ cache[expr] = exprList;
2067
+ return cache[expr].concat();
2068
+ };
2069
+ JSONPath.prototype.safeVm = {
2070
+ Script: SafeScript
2071
+ };
2072
+
2073
+ /**
2074
+ * @typedef {any} ContextItem
2075
+ */
2076
+
2077
+ /**
2078
+ * @typedef {any} EvaluatedResult
2079
+ */
2080
+
2081
+ /**
2082
+ * @callback ConditionCallback
2083
+ * @param {ContextItem} item
2084
+ * @returns {boolean}
2085
+ */
2086
+
2087
+ /**
2088
+ * Copy items out of one array into another.
2089
+ * @param {GenericArray} source Array with items to copy
2090
+ * @param {GenericArray} target Array to which to copy
2091
+ * @param {ConditionCallback} conditionCb Callback passed the current item;
2092
+ * will move item if evaluates to `true`
2093
+ * @returns {void}
2094
+ */
2095
+ const moveToAnotherArray = function (source, target, conditionCb) {
2096
+ const il = source.length;
2097
+ for (let i = 0; i < il; i++) {
2098
+ const item = source[i];
2099
+ if (conditionCb(item)) {
2100
+ // eslint-disable-next-line @stylistic/max-len -- Long
2101
+ // eslint-disable-next-line sonarjs/updated-loop-counter -- Convenient
2102
+ target.push(source.splice(i--, 1)[0]);
2103
+ }
2104
+ }
2105
+ };
2106
+
2107
+ /**
2108
+ * In-browser replacement for NodeJS' VM.Script.
2109
+ */
2110
+ class Script {
2111
+ /**
2112
+ * @param {string} expr Expression to evaluate
2113
+ */
2114
+ constructor(expr) {
2115
+ this.code = expr;
2116
+ }
2117
+
2118
+ /**
2119
+ * @param {object} context Object whose items will be added
2120
+ * to evaluation
2121
+ * @returns {EvaluatedResult} Result of evaluated code
2122
+ */
2123
+ runInNewContext(context) {
2124
+ let expr = this.code;
2125
+ const keys = Object.keys(context);
2126
+ const funcs = [];
2127
+ moveToAnotherArray(keys, funcs, key => {
2128
+ return typeof context[key] === 'function';
2129
+ });
2130
+ const values = keys.map(vr => {
2131
+ return context[vr];
2132
+ });
2133
+ const funcString = funcs.reduce((s, func) => {
2134
+ let fString = context[func].toString();
2135
+ if (!/function/u.test(fString)) {
2136
+ fString = 'function ' + fString;
2137
+ }
2138
+ return 'var ' + func + '=' + fString + ';' + s;
2139
+ }, '');
2140
+ expr = funcString + expr;
2141
+
2142
+ // Mitigate http://perfectionkills.com/global-eval-what-are-the-options/#new_function
2143
+ if (!/(['"])use strict\1/u.test(expr) && !keys.includes('arguments')) {
2144
+ expr = 'var arguments = undefined;' + expr;
2145
+ }
2146
+
2147
+ // Remove last semi so `return` will be inserted before
2148
+ // the previous one instead, allowing for the return
2149
+ // of a bare ending expression
2150
+ expr = expr.replace(/;\s*$/u, '');
2151
+
2152
+ // Insert `return`
2153
+ const lastStatementEnd = expr.lastIndexOf(';');
2154
+ const code = lastStatementEnd !== -1 ? expr.slice(0, lastStatementEnd + 1) + ' return ' + expr.slice(lastStatementEnd + 1) : ' return ' + expr;
2155
+
2156
+ // eslint-disable-next-line no-new-func -- User's choice
2157
+ return new Function(...keys, code)(...values);
2158
+ }
2159
+ }
2160
+ JSONPath.prototype.vm = {
2161
+ Script
2162
+ };
2163
+
2164
+ exports.JSONPath = JSONPath;
2165
+
2166
+ }));