@libreflare/worker-runtime 0.5.0 → 0.5.2

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 (36) hide show
  1. package/README.md +7 -0
  2. package/dist/rule-evaluator/analysis.d.ts +4 -0
  3. package/dist/rule-evaluator/analysis.d.ts.map +1 -1
  4. package/dist/rule-evaluator/analysis.js +19 -16
  5. package/dist/rule-evaluator/analysis.js.map +1 -1
  6. package/dist/rule-evaluator/diagnostics.d.ts +15 -1
  7. package/dist/rule-evaluator/diagnostics.d.ts.map +1 -1
  8. package/dist/rule-evaluator/diagnostics.js +103 -2
  9. package/dist/rule-evaluator/diagnostics.js.map +1 -1
  10. package/dist/rule-evaluator/function-signature-validation.d.ts +3 -0
  11. package/dist/rule-evaluator/function-signature-validation.d.ts.map +1 -1
  12. package/dist/rule-evaluator/function-signature-validation.js +215 -85
  13. package/dist/rule-evaluator/function-signature-validation.js.map +1 -1
  14. package/dist/rule-evaluator/minifier.d.ts +4 -0
  15. package/dist/rule-evaluator/minifier.d.ts.map +1 -1
  16. package/dist/rule-evaluator/minifier.js.map +1 -1
  17. package/dist/rule-evaluator/parser.d.ts +2 -0
  18. package/dist/rule-evaluator/parser.d.ts.map +1 -1
  19. package/dist/rule-evaluator/parser.js +60 -24
  20. package/dist/rule-evaluator/parser.js.map +1 -1
  21. package/dist/rule-evaluator/quantifier.d.ts +9 -3
  22. package/dist/rule-evaluator/quantifier.d.ts.map +1 -1
  23. package/dist/rule-evaluator/quantifier.js +15 -4
  24. package/dist/rule-evaluator/quantifier.js.map +1 -1
  25. package/dist/rule-evaluator/tokenizer.d.ts.map +1 -1
  26. package/dist/rule-evaluator/tokenizer.js +7 -7
  27. package/dist/rule-evaluator/tokenizer.js.map +1 -1
  28. package/dist/rule-evaluator/validation.d.ts +7 -0
  29. package/dist/rule-evaluator/validation.d.ts.map +1 -1
  30. package/dist/rule-evaluator/validation.js +194 -57
  31. package/dist/rule-evaluator/validation.js.map +1 -1
  32. package/dist/rule-evaluator.d.ts +2 -1
  33. package/dist/rule-evaluator.d.ts.map +1 -1
  34. package/dist/rule-evaluator.js +1 -0
  35. package/dist/rule-evaluator.js.map +1 -1
  36. package/package.json +1 -1
@@ -8,7 +8,7 @@ import { isKnownRuleExpressionFieldName, isRuleExpressionFieldAvailable, resolve
8
8
  import { DEPRECATED_FIELD_REPLACEMENTS, KNOWN_DEPRECATED_FIELDS, } from './fields.js';
9
9
  import { isRuleExpressionIpListItem, isRuleExpressionListName, } from './custom-lists.js';
10
10
  import { baseFunctionName, classifyInlineSetValue, isExpressionNode, isFieldNode, isFuncNode, inferRuleExpressionArgument, isIpAddressValueType, ruleExpressionComparisonTypeFamily, ruleExpressionValueTypes, } from './rule-language-types.js';
11
- import { extractQuantifierPredicate } from './quantifier.js';
11
+ import { extractQuantifierComparison, extractQuantifierPredicate } from './quantifier.js';
12
12
  export const MAX_CLOUDFLARE_RULE_EXPRESSION_LENGTH = 4096;
13
13
  export const RULE_EXPRESSION_VALIDATION_PRESETS = ['url-rewrite-filter', 'url-rewrite-target'];
14
14
  const URL_REWRITE_TARGET_SINGLE_USE_FUNCTIONS = new Set(['concat', 'regex_replace', 'wildcard_replace']);
@@ -34,6 +34,7 @@ export function validateRuleExpressionSyntax(source, config = {}) {
34
34
  const message = 'rule expression is required';
35
35
  errors.push(message);
36
36
  pushDiagnostic(diagnostics, {
37
+ id: 'rule_expression.required',
37
38
  severity: 'error',
38
39
  message,
39
40
  pos: null,
@@ -61,6 +62,13 @@ export function validateRuleExpressionSyntax(source, config = {}) {
61
62
  const message = `${expressionLabel} is ${lengthCheckedExpression.length} characters; ${limitLabel} is ${maxLength}`;
62
63
  errors.push(message);
63
64
  pushDiagnostic(diagnostics, {
65
+ id: 'rule_expression.too_long',
66
+ params: {
67
+ actualLength: lengthCheckedExpression.length,
68
+ maxLength,
69
+ expressionKind: minify ? 'minified' : 'source',
70
+ limitKind: maxLength === MAX_CLOUDFLARE_RULE_EXPRESSION_LENGTH ? 'cloudflare' : 'configured',
71
+ },
64
72
  severity: 'error',
65
73
  message,
66
74
  pos: null,
@@ -88,10 +96,16 @@ export function validateRuleExpressionSyntax(source, config = {}) {
88
96
  function fieldAvailabilityRules(preset, config, errors, diagnostics) {
89
97
  return resolveRuleExpressionFieldAvailability(preset, config, {
90
98
  onUnknownField: (configPath, field) => {
91
- pushValidationConfigError(`${configPath} contains unknown field '${field}'`, errors, diagnostics);
99
+ pushValidationConfigError(`${configPath} contains unknown field '${field}'`, errors, diagnostics, {
100
+ id: 'config.field_availability.unknown_field',
101
+ params: { configPath, field },
102
+ });
92
103
  },
93
104
  onUnknownPrefix: (configPath, prefix) => {
94
- pushValidationConfigError(`${configPath} contains unknown field prefix '${prefix}'`, errors, diagnostics);
105
+ pushValidationConfigError(`${configPath} contains unknown field prefix '${prefix}'`, errors, diagnostics, {
106
+ id: 'config.field_availability.unknown_prefix',
107
+ params: { configPath, prefix },
108
+ });
95
109
  },
96
110
  });
97
111
  }
@@ -99,7 +113,10 @@ function ruleExpressionCustomLists(value, errors, diagnostics) {
99
113
  if (value === undefined || value === null)
100
114
  return undefined;
101
115
  if (!isRecord(value)) {
102
- pushValidationConfigError('customLists must be an object', errors, diagnostics);
116
+ pushValidationConfigError('customLists must be an object', errors, diagnostics, {
117
+ id: 'config.custom_lists.invalid_type',
118
+ params: { configPath: 'customLists' },
119
+ });
103
120
  return undefined;
104
121
  }
105
122
  const lists = {};
@@ -107,27 +124,47 @@ function ruleExpressionCustomLists(value, errors, diagnostics) {
107
124
  const path = `customLists.${name}`;
108
125
  let ok = true;
109
126
  if (!isRuleExpressionListName(name)) {
110
- pushValidationConfigError(`${path} has invalid list name`, errors, diagnostics);
127
+ pushValidationConfigError(`${path} has invalid list name`, errors, diagnostics, {
128
+ id: 'config.custom_lists.invalid_name',
129
+ params: { configPath: path, listName: name },
130
+ });
111
131
  ok = false;
112
132
  }
113
133
  if (!isRecord(list)) {
114
- pushValidationConfigError(`${path} must be an object`, errors, diagnostics);
134
+ pushValidationConfigError(`${path} must be an object`, errors, diagnostics, {
135
+ id: 'config.custom_lists.invalid_list_type',
136
+ params: { configPath: path, listName: name },
137
+ });
115
138
  continue;
116
139
  }
117
140
  if (list.kind !== 'ip') {
118
- pushValidationConfigError(`${path}.kind must be 'ip'`, errors, diagnostics);
141
+ pushValidationConfigError(`${path}.kind must be 'ip'`, errors, diagnostics, {
142
+ id: 'config.custom_lists.unsupported_kind',
143
+ params: {
144
+ configPath: `${path}.kind`,
145
+ listName: name,
146
+ expectedKind: 'ip',
147
+ actualKind: typeof list.kind === 'string' ? list.kind : typeof list.kind,
148
+ },
149
+ });
119
150
  ok = false;
120
151
  }
121
152
  let items = null;
122
153
  if (!Array.isArray(list.items)) {
123
- pushValidationConfigError(`${path}.items must be an array`, errors, diagnostics);
154
+ pushValidationConfigError(`${path}.items must be an array`, errors, diagnostics, {
155
+ id: 'config.custom_lists.invalid_items_type',
156
+ params: { configPath: `${path}.items`, listName: name },
157
+ });
124
158
  ok = false;
125
159
  }
126
160
  else {
127
161
  items = [];
128
162
  for (const [index, item] of list.items.entries()) {
129
163
  if (typeof item !== 'string' || !isRuleExpressionIpListItem(item)) {
130
- pushValidationConfigError(`${path}.items[${index}] must be an IPv4, IPv4 CIDR, IPv6, or IPv6 CIDR string`, errors, diagnostics);
164
+ pushValidationConfigError(`${path}.items[${index}] must be an IPv4, IPv4 CIDR, IPv6, or IPv6 CIDR string`, errors, diagnostics, {
165
+ id: 'config.custom_lists.invalid_ip_item',
166
+ params: { configPath: `${path}.items[${index}]`, listName: name, index },
167
+ });
131
168
  ok = false;
132
169
  }
133
170
  else {
@@ -143,9 +180,10 @@ function ruleExpressionCustomLists(value, errors, diagnostics) {
143
180
  function isKnownFieldName(field) {
144
181
  return isKnownRuleExpressionFieldName(field);
145
182
  }
146
- function pushValidationConfigError(message, errors, diagnostics) {
183
+ function pushValidationConfigError(message, errors, diagnostics, metadata) {
147
184
  errors.push(message);
148
185
  pushDiagnostic(diagnostics, {
186
+ ...metadata,
149
187
  severity: 'error',
150
188
  message,
151
189
  pos: null,
@@ -153,29 +191,40 @@ function pushValidationConfigError(message, errors, diagnostics) {
153
191
  source: 'expression',
154
192
  });
155
193
  }
156
- function isRuleSyntaxWarning(warning) {
157
- return warning.includes('unexpected') ||
158
- warning.includes('missing argument') ||
159
- warning.includes('missing closing') ||
160
- warning.includes('missing comma');
161
- }
162
194
  function collectValidationDiagnostics(errors, warnings, diagnostics, messages, messageDiagnostics, source) {
195
+ const detailedMessages = new Set();
196
+ for (const diagnostic of messageDiagnostics) {
197
+ detailedMessages.add(diagnostic.message);
198
+ pushValidationMessage(errors, warnings, diagnostic.message, diagnostic.severity);
199
+ pushDiagnostic(diagnostics, {
200
+ ...(diagnostic.id ? { id: diagnostic.id } : {}),
201
+ ...(diagnostic.params ? { params: diagnostic.params } : {}),
202
+ severity: diagnostic.severity,
203
+ message: diagnostic.message,
204
+ pos: diagnostic.pos ?? null,
205
+ length: diagnostic.length,
206
+ source,
207
+ });
208
+ }
163
209
  for (const message of messages) {
164
- const severity = isRuleSyntaxWarning(message) ? 'error' : 'warning';
165
- if (severity === 'error')
166
- errors.push(message);
167
- else
168
- warnings.push(message);
169
- const diagnostic = messageDiagnostics.find(detail => detail.message === message);
210
+ if (detailedMessages.has(message))
211
+ continue;
212
+ pushValidationMessage(errors, warnings, message, 'warning');
170
213
  pushDiagnostic(diagnostics, {
171
- severity,
214
+ severity: 'warning',
172
215
  message,
173
- pos: diagnostic?.pos ?? null,
174
- length: diagnostic?.length ?? 0,
216
+ pos: null,
217
+ length: 0,
175
218
  source,
176
219
  });
177
220
  }
178
221
  }
222
+ function pushValidationMessage(errors, warnings, message, severity) {
223
+ if (severity === 'error')
224
+ errors.push(message);
225
+ else
226
+ warnings.push(message);
227
+ }
179
228
  function collectSemanticValidationDiagnostics(errors, warnings, diagnostics, node, expression, context) {
180
229
  validateSemanticNode(node, new Set(), errors, warnings, diagnostics, expression, context);
181
230
  if (context.preset === 'url-rewrite-target')
@@ -221,13 +270,15 @@ function validateSemanticNode(node, guardedHeaders, errors, warnings, diagnostic
221
270
  function validateComparisonRequirements(node, errors, diagnostics, context) {
222
271
  let invalidArrayComparison = false;
223
272
  if (node.left.kind === 'ArrayField') {
224
- pushComparisonError(node.left, '[*] cannot be used directly with comparison operators; wrap the comparison in any() or all()', errors, diagnostics);
273
+ pushComparisonError(node.left, '[*] cannot be used directly with comparison operators; wrap the comparison in any() or all()', errors, diagnostics, { id: 'array_expansion.direct_comparison' });
225
274
  invalidArrayComparison = true;
226
275
  }
227
276
  if (node.left.kind === 'Func' && node.left.name.endsWith('_array')) {
228
- pushFuncError(node.left, '[*] cannot be used directly with comparison operators; wrap the comparison in any() or all()', errors, diagnostics);
277
+ pushFuncError(node.left, '[*] cannot be used directly with comparison operators; wrap the comparison in any() or all()', errors, diagnostics, { id: 'array_expansion.direct_comparison' });
229
278
  invalidArrayComparison = true;
230
279
  }
280
+ if (!node.right)
281
+ return;
231
282
  if (node.right?.kind === 'ListRef') {
232
283
  validateListRef(node.right, node.left, node.op, errors, diagnostics, context);
233
284
  }
@@ -245,7 +296,7 @@ function validateComparisonTypeCompatibility(node, errors, diagnostics) {
245
296
  if (node.right?.kind === 'ListRef')
246
297
  return;
247
298
  if (node.right?.kind !== 'SetLiteral') {
248
- pushComparisonTypeError(node.right ?? node.left, "operator 'in' requires an inline list or list reference", errors, diagnostics);
299
+ pushComparisonTypeError(node.right ?? node.left, "operator 'in' requires an inline list or list reference", errors, diagnostics, { id: 'operator.in_requires_list', params: { operator: 'in' } });
249
300
  return;
250
301
  }
251
302
  }
@@ -257,20 +308,30 @@ function validateComparisonTypeCompatibility(node, errors, diagnostics) {
257
308
  return;
258
309
  const leftLabel = comparisonFamilyLabel(leftFamily);
259
310
  const rightLabel = comparisonFamilyLabel(rightFamily);
260
- const rightValueLabel = node.right.kind === 'SetLiteral' ? `inline list of ${rightLabel}` : rightLabel;
311
+ const valueIsInlineSet = node.right.kind === 'SetLiteral';
312
+ const rightValueLabel = valueIsInlineSet ? `inline list of ${rightLabel}` : rightLabel;
261
313
  switch (node.op) {
262
314
  case 'eq':
263
315
  case 'ne':
264
316
  if (!isEqualityFamily(leftFamily)) {
265
- pushComparisonTypeError(node.left, `operator '${node.op}' does not support ${leftLabel} values`, errors, diagnostics);
317
+ pushComparisonTypeError(node.left, `operator '${node.op}' does not support ${leftLabel} values`, errors, diagnostics, {
318
+ id: 'operator.unsupported_type',
319
+ params: { operator: node.op, side: 'left', valueType: leftLabel },
320
+ });
266
321
  return;
267
322
  }
268
323
  if (!isEqualityFamily(rightFamily)) {
269
- pushComparisonTypeError(node.right, `operator '${node.op}' does not support ${rightValueLabel}`, errors, diagnostics);
324
+ pushComparisonTypeError(node.right, `operator '${node.op}' does not support ${rightValueLabel}`, errors, diagnostics, {
325
+ id: 'operator.unsupported_type',
326
+ params: { operator: node.op, side: 'right', valueType: rightLabel, valueIsInlineSet },
327
+ });
270
328
  return;
271
329
  }
272
330
  if (leftFamily !== rightFamily) {
273
- pushComparisonTypeError(node.right, `operator '${node.op}' cannot compare ${leftLabel} with ${rightValueLabel}`, errors, diagnostics);
331
+ pushComparisonTypeError(node.right, `operator '${node.op}' cannot compare ${leftLabel} with ${rightValueLabel}`, errors, diagnostics, {
332
+ id: 'operator.type_mismatch',
333
+ params: { operator: node.op, leftType: leftLabel, rightType: rightLabel, valueIsInlineSet },
334
+ });
274
335
  }
275
336
  return;
276
337
  case 'lt':
@@ -278,11 +339,17 @@ function validateComparisonTypeCompatibility(node, errors, diagnostics) {
278
339
  case 'gt':
279
340
  case 'ge':
280
341
  if (!isOrderingFamily(leftFamily)) {
281
- pushComparisonTypeError(node.left, `operator '${node.op}' requires String or Number values; left side is ${leftLabel}`, errors, diagnostics);
342
+ pushComparisonTypeError(node.left, `operator '${node.op}' requires String or Number values; left side is ${leftLabel}`, errors, diagnostics, {
343
+ id: 'operator.requires_type',
344
+ params: { operator: node.op, side: 'left', requiredTypes: ['String', 'Number'], actualType: leftLabel },
345
+ });
282
346
  return;
283
347
  }
284
348
  if (!isOrderingFamily(rightFamily) || leftFamily !== rightFamily) {
285
- pushComparisonTypeError(node.right, `operator '${node.op}' cannot compare ${leftLabel} with ${rightValueLabel}`, errors, diagnostics);
349
+ pushComparisonTypeError(node.right, `operator '${node.op}' cannot compare ${leftLabel} with ${rightValueLabel}`, errors, diagnostics, {
350
+ id: 'operator.type_mismatch',
351
+ params: { operator: node.op, leftType: leftLabel, rightType: rightLabel, valueIsInlineSet },
352
+ });
286
353
  }
287
354
  return;
288
355
  case 'contains':
@@ -290,20 +357,32 @@ function validateComparisonTypeCompatibility(node, errors, diagnostics) {
290
357
  case 'wildcard':
291
358
  case 'strict_wildcard':
292
359
  if (leftFamily !== 'string') {
293
- pushComparisonTypeError(node.left, `operator '${node.op}' requires String values; left side is ${leftLabel}`, errors, diagnostics);
360
+ pushComparisonTypeError(node.left, `operator '${node.op}' requires String values; left side is ${leftLabel}`, errors, diagnostics, {
361
+ id: 'operator.requires_type',
362
+ params: { operator: node.op, side: 'left', requiredTypes: ['String'], actualType: leftLabel },
363
+ });
294
364
  return;
295
365
  }
296
366
  if (rightFamily !== 'string') {
297
- pushComparisonTypeError(node.right, `operator '${node.op}' requires String values; right side is ${rightValueLabel}`, errors, diagnostics);
367
+ pushComparisonTypeError(node.right, `operator '${node.op}' requires String values; right side is ${rightValueLabel}`, errors, diagnostics, {
368
+ id: 'operator.requires_type',
369
+ params: { operator: node.op, side: 'right', requiredTypes: ['String'], actualType: rightLabel, valueIsInlineSet },
370
+ });
298
371
  }
299
372
  return;
300
373
  case 'in':
301
374
  if (!isEqualityFamily(leftFamily)) {
302
- pushComparisonTypeError(node.left, `operator 'in' does not support ${leftLabel} values`, errors, diagnostics);
375
+ pushComparisonTypeError(node.left, `operator 'in' does not support ${leftLabel} values`, errors, diagnostics, {
376
+ id: 'operator.unsupported_type',
377
+ params: { operator: 'in', side: 'left', valueType: leftLabel },
378
+ });
303
379
  return;
304
380
  }
305
381
  if (!isEqualityFamily(rightFamily) || leftFamily !== rightFamily) {
306
- pushComparisonTypeError(node.right, `operator 'in' cannot compare ${leftLabel} with ${rightValueLabel}`, errors, diagnostics);
382
+ pushComparisonTypeError(node.right, `operator 'in' cannot compare ${leftLabel} with ${rightValueLabel}`, errors, diagnostics, {
383
+ id: 'operator.type_mismatch',
384
+ params: { operator: 'in', leftType: leftLabel, rightType: rightLabel, valueIsInlineSet },
385
+ });
307
386
  }
308
387
  return;
309
388
  }
@@ -328,8 +407,8 @@ function comparisonFamilyLabel(family) {
328
407
  case 'boolean': return 'Boolean';
329
408
  }
330
409
  }
331
- function pushComparisonTypeError(node, message, errors, diagnostics) {
332
- pushValueError(node, message, errors, diagnostics);
410
+ function pushComparisonTypeError(node, message, errors, diagnostics, metadata) {
411
+ pushValueError(node, message, errors, diagnostics, metadata);
333
412
  }
334
413
  function validateRewriteTargetFunctionUsage(node, errors, diagnostics) {
335
414
  const functions = new Map();
@@ -337,7 +416,10 @@ function validateRewriteTargetFunctionUsage(node, errors, diagnostics) {
337
416
  for (const functionName of URL_REWRITE_TARGET_SINGLE_USE_FUNCTIONS) {
338
417
  const nodes = functions.get(functionName) ?? [];
339
418
  for (const duplicate of nodes.slice(1)) {
340
- pushFuncError(duplicate, `function '${functionName}' can appear only once in url-rewrite-target expressions`, errors, diagnostics);
419
+ pushFuncError(duplicate, `function '${functionName}' can appear only once in url-rewrite-target expressions`, errors, diagnostics, {
420
+ id: 'function.rewrite_target.single_use',
421
+ params: { functionName, preset: 'url-rewrite-target' },
422
+ });
341
423
  }
342
424
  }
343
425
  }
@@ -350,7 +432,10 @@ function collectRewriteTargetFunctions(node, replacementFunctionStack, functions
350
432
  }
351
433
  const parentReplacementFunction = replacementFunctionStack.at(-1);
352
434
  if (parentReplacementFunction && URL_REWRITE_TARGET_NON_NESTABLE_REPLACEMENT_FUNCTIONS.has(functionName)) {
353
- pushFuncError(node, `function '${functionName}' cannot be nested inside '${parentReplacementFunction}' in url-rewrite-target expressions`, errors, diagnostics);
435
+ pushFuncError(node, `function '${functionName}' cannot be nested inside '${parentReplacementFunction}' in url-rewrite-target expressions`, errors, diagnostics, {
436
+ id: 'function.rewrite_target.nested_replacement',
437
+ params: { functionName, parentFunctionName: parentReplacementFunction, preset: 'url-rewrite-target' },
438
+ });
354
439
  }
355
440
  const nextReplacementFunctionStack = URL_REWRITE_TARGET_NON_NESTABLE_REPLACEMENT_FUNCTIONS.has(functionName)
356
441
  ? [...replacementFunctionStack, functionName]
@@ -388,13 +473,22 @@ function collectRewriteTargetFunctions(node, replacementFunctionStack, functions
388
473
  function validateListRef(node, left, op, errors, diagnostics, context) {
389
474
  const validName = validateListRefName(node, errors, diagnostics);
390
475
  if (context.rejectListReferences) {
391
- pushValueError(node, `list reference '$${node.name}' is not allowed`, errors, diagnostics);
476
+ pushValueError(node, `list reference '$${node.name}' is not allowed`, errors, diagnostics, {
477
+ id: 'list.reference_not_allowed',
478
+ params: { listName: node.name },
479
+ });
392
480
  }
393
481
  if (context.customLists !== undefined && validName && !Object.hasOwn(context.customLists, node.name)) {
394
- pushValueError(node, `list reference '$${node.name}' is not configured`, errors, diagnostics);
482
+ pushValueError(node, `list reference '$${node.name}' is not configured`, errors, diagnostics, {
483
+ id: 'list.reference_not_configured',
484
+ params: { listName: node.name },
485
+ });
395
486
  }
396
487
  if (op !== 'in') {
397
- pushValueError(node, `list '$${node.name}' can only be used with the in operator`, errors, diagnostics);
488
+ pushValueError(node, `list '$${node.name}' can only be used with the in operator`, errors, diagnostics, {
489
+ id: 'list.reference_requires_in_operator',
490
+ params: { listName: node.name, operator: op },
491
+ });
398
492
  return;
399
493
  }
400
494
  validateKnownListRefValueType(node, left, errors, diagnostics, context);
@@ -402,10 +496,16 @@ function validateListRef(node, left, op, errors, diagnostics, context) {
402
496
  function validateFunctionListRefArg(node, arg, errors, diagnostics, context) {
403
497
  const validName = validateListRefName(arg, errors, diagnostics);
404
498
  if (context.rejectListReferences) {
405
- pushValueError(arg, `list reference '$${arg.name}' is not allowed`, errors, diagnostics);
499
+ pushValueError(arg, `list reference '$${arg.name}' is not allowed`, errors, diagnostics, {
500
+ id: 'list.reference_not_allowed',
501
+ params: { listName: arg.name },
502
+ });
406
503
  }
407
504
  if (context.customLists !== undefined && validName && !Object.hasOwn(context.customLists, arg.name)) {
408
- pushValueError(arg, `list reference '$${arg.name}' is not configured`, errors, diagnostics);
505
+ pushValueError(arg, `list reference '$${arg.name}' is not configured`, errors, diagnostics, {
506
+ id: 'list.reference_not_configured',
507
+ params: { listName: arg.name },
508
+ });
409
509
  }
410
510
  const quantifier = extractQuantifierPredicate(node);
411
511
  if (quantifier?.value === arg) {
@@ -413,7 +513,10 @@ function validateFunctionListRefArg(node, arg, errors, diagnostics, context) {
413
513
  validateKnownListRefValueType(arg, quantifier.source, errors, diagnostics, context);
414
514
  return;
415
515
  }
416
- pushValueError(arg, `list '$${arg.name}' can only be used with the in operator`, errors, diagnostics);
516
+ pushValueError(arg, `list '$${arg.name}' can only be used with the in operator`, errors, diagnostics, {
517
+ id: 'list.reference_requires_in_operator',
518
+ params: { listName: arg.name, operator: quantifier.op },
519
+ });
417
520
  }
418
521
  }
419
522
  function validateKnownListRefValueType(node, left, errors, diagnostics, context) {
@@ -421,7 +524,11 @@ function validateKnownListRefValueType(node, left, errors, diagnostics, context)
421
524
  const valueTypes = ruleExpressionValueTypes(left);
422
525
  if (valueTypes.length === 0 || valueTypes.some(valueType => listKindAcceptsValueType(listKind, valueType)))
423
526
  return;
424
- pushValueError(node, listKindMismatchMessage(listKind, node.name, valueTypes), errors, diagnostics);
527
+ const diagnostic = listKindMismatchDiagnostic(listKind, node.name, valueTypes);
528
+ pushValueError(node, diagnostic.message, errors, diagnostics, {
529
+ id: 'list.reference_type_mismatch',
530
+ params: diagnostic.params,
531
+ });
425
532
  }
426
533
  function listKindAcceptsValueType(kind, valueType) {
427
534
  switch (kind) {
@@ -429,15 +536,18 @@ function listKindAcceptsValueType(kind, valueType) {
429
536
  return isIpAddressValueType(valueType);
430
537
  }
431
538
  }
432
- function listKindMismatchMessage(kind, listName, actualTypes) {
539
+ function listKindMismatchDiagnostic(kind, listName, actualTypes) {
433
540
  switch (kind) {
434
541
  case 'ip':
435
- return `IP list '$${listName}' can only be used with IP address values; left side is ${actualTypes.join(' | ')}`;
542
+ return {
543
+ message: `IP list '$${listName}' can only be used with IP address values; left side is ${actualTypes.join(' | ')}`,
544
+ params: { listKind: 'ip', listName, actualTypes },
545
+ };
436
546
  }
437
547
  }
438
548
  function validateListRefName(node, errors, diagnostics) {
439
549
  if (!isRuleExpressionListName(node.name)) {
440
- pushValueError(node, `list name '${node.name}' must contain only lowercase letters, numbers, and underscores`, errors, diagnostics);
550
+ pushValueError(node, `list name '${node.name}' must contain only lowercase letters, numbers, and underscores`, errors, diagnostics, { id: 'list.invalid_name', params: { listName: node.name } });
441
551
  return false;
442
552
  }
443
553
  return true;
@@ -449,12 +559,17 @@ function validateInlineSetLiteral(node, errors, diagnostics) {
449
559
  const valueTypes = new Set(node.values.map(classifyInlineSetValue));
450
560
  if (valueTypes.size <= 1)
451
561
  return;
452
- pushValueError(node, 'inline list elements must all have the same data type', errors, diagnostics);
562
+ pushValueError(node, 'inline list elements must all have the same data type', errors, diagnostics, {
563
+ id: 'inline_list.mixed_element_types',
564
+ params: { elementTypes: [...valueTypes] },
565
+ });
453
566
  }
454
567
  function validateWildcardPattern(node, errors, diagnostics) {
455
568
  if (!hasConsecutiveUnescapedWildcards(node.value))
456
569
  return;
457
- pushValueError(node, 'wildcard patterns cannot contain two unescaped * characters in a row', errors, diagnostics);
570
+ pushValueError(node, 'wildcard patterns cannot contain two unescaped * characters in a row', errors, diagnostics, {
571
+ id: 'wildcard.consecutive_wildcards',
572
+ });
458
573
  }
459
574
  function hasConsecutiveUnescapedWildcards(pattern) {
460
575
  let previousWildcard = false;
@@ -477,10 +592,11 @@ function hasConsecutiveUnescapedWildcards(pattern) {
477
592
  }
478
593
  return false;
479
594
  }
480
- function pushComparisonError(node, requirement, errors, diagnostics) {
595
+ function pushComparisonError(node, requirement, errors, diagnostics, metadata) {
481
596
  const message = `${requirement}`;
482
597
  errors.push(message);
483
598
  pushDiagnostic(diagnostics, {
599
+ ...metadata,
484
600
  severity: 'error',
485
601
  message,
486
602
  pos: node.pos,
@@ -488,10 +604,11 @@ function pushComparisonError(node, requirement, errors, diagnostics) {
488
604
  source: 'expression',
489
605
  });
490
606
  }
491
- function pushFuncError(node, requirement, errors, diagnostics) {
607
+ function pushFuncError(node, requirement, errors, diagnostics, metadata) {
492
608
  const message = `${requirement}`;
493
609
  errors.push(message);
494
610
  pushDiagnostic(diagnostics, {
611
+ ...metadata,
495
612
  severity: 'error',
496
613
  message,
497
614
  pos: node.pos ?? null,
@@ -499,10 +616,11 @@ function pushFuncError(node, requirement, errors, diagnostics) {
499
616
  source: 'expression',
500
617
  });
501
618
  }
502
- function pushValueError(node, message, errors, diagnostics) {
619
+ function pushValueError(node, message, errors, diagnostics, metadata) {
503
620
  errors.push(message);
504
621
  const span = argSpan(node);
505
622
  pushDiagnostic(diagnostics, {
623
+ ...metadata,
506
624
  severity: 'error',
507
625
  message,
508
626
  pos: span.pos,
@@ -518,6 +636,8 @@ function validateFunctionSupport(node, warnings, diagnostics) {
518
636
  const message = `function '${functionName}' cannot be evaluated reliably: ${reason}`;
519
637
  pushWarning(warnings, message);
520
638
  pushDiagnostic(diagnostics, {
639
+ id: 'function.unreliable',
640
+ params: { functionName, reason },
521
641
  severity: 'warning',
522
642
  message,
523
643
  pos: null,
@@ -548,6 +668,11 @@ function validateFuncArgs(node, guardedHeaders, errors, warnings, diagnostics, e
548
668
  function validateQuantifierPredicateArgs(node, guardedHeaders, errors, warnings, diagnostics, expression, context) {
549
669
  const quantifier = extractQuantifierPredicate(node);
550
670
  if (!quantifier) {
671
+ const comparison = extractQuantifierComparison(node);
672
+ if (comparison && !comparison.comparison.right) {
673
+ validateFieldOrFunc(comparison.source, guardedHeaders, errors, warnings, diagnostics, expression, context);
674
+ return;
675
+ }
551
676
  validateFuncArgs(node, guardedHeaders, errors, warnings, diagnostics, expression, context);
552
677
  return;
553
678
  }
@@ -565,6 +690,8 @@ function pushFunctionSignatureError(signatureError, errors, diagnostics) {
565
690
  errors.push(signatureError.message);
566
691
  const span = argSpan(signatureError.node);
567
692
  pushDiagnostic(diagnostics, {
693
+ id: signatureError.id,
694
+ ...(signatureError.params ? { params: signatureError.params } : {}),
568
695
  severity: 'error',
569
696
  message: signatureError.message,
570
697
  pos: span.pos,
@@ -596,6 +723,8 @@ function validateFieldNode(node, guardedHeaders, errors, warnings, diagnostics,
596
723
  const message = `direct indexed header access '${fieldExpression}' should be grouped with '${guardExpression}' so the header exists before indexing`;
597
724
  pushWarning(warnings, message);
598
725
  pushDiagnostic(diagnostics, {
726
+ id: 'field.header_index_requires_guard',
727
+ params: { fieldExpression, guardExpression, headerName },
599
728
  severity: 'warning',
600
729
  message,
601
730
  pos: node.pos,
@@ -619,6 +748,10 @@ function validateDeprecatedField(node, warnings, diagnostics, expression, contex
619
748
  : `field '${fieldExpression}' is deprecated`;
620
749
  pushWarning(warnings, message);
621
750
  pushDiagnostic(diagnostics, {
751
+ id: 'field.deprecated',
752
+ params: replacement
753
+ ? { fieldExpression, replacement }
754
+ : { fieldExpression },
622
755
  severity: 'notice',
623
756
  message,
624
757
  pos: typeof node.pos === 'number' ? node.pos : null,
@@ -640,6 +773,8 @@ function validateFieldAvailability(node, errors, diagnostics, expression, contex
640
773
  const message = `field '${fieldExpression}' is not available for ${rules.preset} expressions`;
641
774
  errors.push(message);
642
775
  pushDiagnostic(diagnostics, {
776
+ id: 'field.unavailable',
777
+ params: { fieldExpression, fieldName: node.name, preset: rules.preset },
643
778
  severity: 'error',
644
779
  message,
645
780
  pos: typeof node.pos === 'number' ? node.pos : null,
@@ -652,6 +787,8 @@ function pushUnknownFieldError(node, errors, diagnostics, expression) {
652
787
  const message = `unknown field '${fieldExpression}'`;
653
788
  errors.push(message);
654
789
  pushDiagnostic(diagnostics, {
790
+ id: 'field.unknown',
791
+ params: { fieldExpression, fieldName: node.name },
655
792
  severity: 'error',
656
793
  message,
657
794
  pos: typeof node.pos === 'number' ? node.pos : null,