@libreflare/worker-runtime 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/README.md +120 -22
  2. package/dist/rule-evaluator/accept-encoding.d.ts +2 -1
  3. package/dist/rule-evaluator/accept-encoding.d.ts.map +1 -1
  4. package/dist/rule-evaluator/accept-encoding.js +27 -29
  5. package/dist/rule-evaluator/accept-encoding.js.map +1 -1
  6. package/dist/rule-evaluator/access-values.d.ts +23 -3
  7. package/dist/rule-evaluator/access-values.d.ts.map +1 -1
  8. package/dist/rule-evaluator/access-values.js +34 -14
  9. package/dist/rule-evaluator/access-values.js.map +1 -1
  10. package/dist/rule-evaluator/analysis.d.ts +3 -7
  11. package/dist/rule-evaluator/analysis.d.ts.map +1 -1
  12. package/dist/rule-evaluator/analysis.js +13 -39
  13. package/dist/rule-evaluator/analysis.js.map +1 -1
  14. package/dist/rule-evaluator/ast.d.ts +23 -3
  15. package/dist/rule-evaluator/ast.d.ts.map +1 -1
  16. package/dist/rule-evaluator/ast.js +5 -0
  17. package/dist/rule-evaluator/ast.js.map +1 -1
  18. package/dist/rule-evaluator/comparison.d.ts +16 -0
  19. package/dist/rule-evaluator/comparison.d.ts.map +1 -0
  20. package/dist/rule-evaluator/comparison.js +43 -0
  21. package/dist/rule-evaluator/comparison.js.map +1 -0
  22. package/dist/rule-evaluator/constants.d.ts +7 -3
  23. package/dist/rule-evaluator/constants.d.ts.map +1 -1
  24. package/dist/rule-evaluator/constants.js +7 -1
  25. package/dist/rule-evaluator/constants.js.map +1 -1
  26. package/dist/rule-evaluator/country-codes.generated.js +1 -1
  27. package/dist/rule-evaluator/custom-lists.d.ts +6 -0
  28. package/dist/rule-evaluator/custom-lists.d.ts.map +1 -1
  29. package/dist/rule-evaluator/custom-lists.js +21 -1
  30. package/dist/rule-evaluator/custom-lists.js.map +1 -1
  31. package/dist/rule-evaluator/diagnostics.d.ts +10 -7
  32. package/dist/rule-evaluator/diagnostics.d.ts.map +1 -1
  33. package/dist/rule-evaluator/diagnostics.js +24 -1
  34. package/dist/rule-evaluator/diagnostics.js.map +1 -1
  35. package/dist/rule-evaluator/evaluator.d.ts +10 -3
  36. package/dist/rule-evaluator/evaluator.d.ts.map +1 -1
  37. package/dist/rule-evaluator/evaluator.js +85 -78
  38. package/dist/rule-evaluator/evaluator.js.map +1 -1
  39. package/dist/rule-evaluator/explain.d.ts +11 -2
  40. package/dist/rule-evaluator/explain.d.ts.map +1 -1
  41. package/dist/rule-evaluator/explain.js +217 -121
  42. package/dist/rule-evaluator/explain.js.map +1 -1
  43. package/dist/rule-evaluator/field-descriptions.generated.d.ts +113 -0
  44. package/dist/rule-evaluator/field-descriptions.generated.d.ts.map +1 -1
  45. package/dist/rule-evaluator/field-descriptions.generated.js +260 -0
  46. package/dist/rule-evaluator/field-descriptions.generated.js.map +1 -1
  47. package/dist/rule-evaluator/fields.d.ts +12 -2
  48. package/dist/rule-evaluator/fields.d.ts.map +1 -1
  49. package/dist/rule-evaluator/fields.js +172 -29
  50. package/dist/rule-evaluator/fields.js.map +1 -1
  51. package/dist/rule-evaluator/function-descriptions.generated.d.ts +13 -0
  52. package/dist/rule-evaluator/function-descriptions.generated.d.ts.map +1 -1
  53. package/dist/rule-evaluator/function-descriptions.generated.js +24 -0
  54. package/dist/rule-evaluator/function-descriptions.generated.js.map +1 -1
  55. package/dist/rule-evaluator/function-signature-validation.d.ts +1 -1
  56. package/dist/rule-evaluator/function-signature-validation.d.ts.map +1 -1
  57. package/dist/rule-evaluator/function-signature-validation.js +56 -5
  58. package/dist/rule-evaluator/function-signature-validation.js.map +1 -1
  59. package/dist/rule-evaluator/functions.d.ts +18 -5
  60. package/dist/rule-evaluator/functions.d.ts.map +1 -1
  61. package/dist/rule-evaluator/functions.js +257 -150
  62. package/dist/rule-evaluator/functions.js.map +1 -1
  63. package/dist/rule-evaluator/minifier.d.ts +2 -6
  64. package/dist/rule-evaluator/minifier.d.ts.map +1 -1
  65. package/dist/rule-evaluator/minifier.js +9 -9
  66. package/dist/rule-evaluator/minifier.js.map +1 -1
  67. package/dist/rule-evaluator/normalizer.d.ts +2 -6
  68. package/dist/rule-evaluator/normalizer.d.ts.map +1 -1
  69. package/dist/rule-evaluator/normalizer.js +8 -8
  70. package/dist/rule-evaluator/normalizer.js.map +1 -1
  71. package/dist/rule-evaluator/operators.d.ts +21 -2
  72. package/dist/rule-evaluator/operators.d.ts.map +1 -1
  73. package/dist/rule-evaluator/operators.js +162 -104
  74. package/dist/rule-evaluator/operators.js.map +1 -1
  75. package/dist/rule-evaluator/outcome.d.ts +63 -0
  76. package/dist/rule-evaluator/outcome.d.ts.map +1 -0
  77. package/dist/rule-evaluator/outcome.js +78 -0
  78. package/dist/rule-evaluator/outcome.js.map +1 -0
  79. package/dist/rule-evaluator/parser.d.ts +7 -1
  80. package/dist/rule-evaluator/parser.d.ts.map +1 -1
  81. package/dist/rule-evaluator/parser.js +55 -25
  82. package/dist/rule-evaluator/parser.js.map +1 -1
  83. package/dist/rule-evaluator/quantifier.d.ts +2 -2
  84. package/dist/rule-evaluator/quantifier.d.ts.map +1 -1
  85. package/dist/rule-evaluator/rule-language-types.d.ts +5 -3
  86. package/dist/rule-evaluator/rule-language-types.d.ts.map +1 -1
  87. package/dist/rule-evaluator/rule-language-types.js +46 -17
  88. package/dist/rule-evaluator/rule-language-types.js.map +1 -1
  89. package/dist/rule-evaluator/tokenizer.d.ts.map +1 -1
  90. package/dist/rule-evaluator/tokenizer.js +25 -0
  91. package/dist/rule-evaluator/tokenizer.js.map +1 -1
  92. package/dist/rule-evaluator/validation.d.ts +8 -9
  93. package/dist/rule-evaluator/validation.d.ts.map +1 -1
  94. package/dist/rule-evaluator/validation.js +185 -64
  95. package/dist/rule-evaluator/validation.js.map +1 -1
  96. package/dist/rule-evaluator/values.d.ts +26 -0
  97. package/dist/rule-evaluator/values.d.ts.map +1 -0
  98. package/dist/rule-evaluator/values.js +55 -0
  99. package/dist/rule-evaluator/values.js.map +1 -0
  100. package/dist/rule-evaluator.d.ts +3 -3
  101. package/dist/rule-evaluator.d.ts.map +1 -1
  102. package/dist/rule-evaluator.js +1 -1
  103. package/dist/rule-evaluator.js.map +1 -1
  104. package/package.json +2 -2
@@ -1,5 +1,6 @@
1
- import { pushDiagnostic, } from './diagnostics.js';
2
- import { pushWarning, } from './ast.js';
1
+ import { pushDiagnostic, ruleExpressionDiagnostic, } from './diagnostics.js';
2
+ import { EQUALITY_ORDERING_OPERATORS, isDynamicValueNode, pushWarning, } from './ast.js';
3
+ import { RULE_EXPRESSION_MATCH_ALL_EXPRESSION } from './constants.js';
3
4
  import { getUnreliableRuleExpressionFunctionReason } from './functions.js';
4
5
  import { validateFunctionSignature } from './function-signature-validation.js';
5
6
  import { minifyRuleExpression } from './minifier.js';
@@ -8,8 +9,8 @@ import { isKnownRuleExpressionFieldName, isRuleExpressionFieldAvailable, resolve
8
9
  import { DEPRECATED_FIELD_REPLACEMENTS, KNOWN_DEPRECATED_FIELDS, } from './fields.js';
9
10
  import { isRuleExpressionCountryCode } from './country-codes.js';
10
11
  import { getRuleExpressionFieldValueDomain } from './field-value-domains.js';
11
- import { isRuleExpressionIpListItem, isRuleExpressionListName, } from './custom-lists.js';
12
- import { classifyInlineSetValue, formatRuleExpressionDataType, isArrayExpandedNode, isConcreteComparisonFamily, isExpressionNode, isFieldNode, isFuncNode, inferRuleExpressionArgument, isIpAddressValueType, isUnknownRuleExpressionDataType, ruleExpressionComparisonTypeFamily, ruleExpressionValueTypes, } from './rule-language-types.js';
12
+ import { customList, isRuleExpressionIpListItem, isRuleExpressionListName, } from './custom-lists.js';
13
+ import { formatRuleExpressionDataType, isArrayExpandedNode, isConcreteComparisonFamily, isExpressionNode, isFieldNode, isFuncNode, inferRuleExpressionArgument, isIpAddressValueType, isLiteralNode, isUnknownRuleExpressionDataType, ruleExpressionComparisonTypeFamily, ruleExpressionValueTypes, } from './rule-language-types.js';
13
14
  import { extractQuantifierComparison, extractQuantifierPredicate } from './quantifier.js';
14
15
  export const MAX_CLOUDFLARE_RULE_EXPRESSION_LENGTH = 4096;
15
16
  export const RULE_EXPRESSION_VALIDATION_PRESETS = [
@@ -42,7 +43,6 @@ export function validateRuleExpressionSyntax(source, config = {}) {
42
43
  pushDiagnostic(diagnostics, {
43
44
  id: 'rule_expression.required',
44
45
  severity: 'error',
45
- message,
46
46
  pos: null,
47
47
  length: 0,
48
48
  source: 'expression',
@@ -51,19 +51,20 @@ export function validateRuleExpressionSyntax(source, config = {}) {
51
51
  const parseWarnings = [];
52
52
  const parseDiagnostics = [];
53
53
  const parsedExpression = expression ? parseExpression(expression, parseWarnings, parseDiagnostics, parseOptions) : null;
54
- const minified = minify ? minifyRuleExpression(expression, parseOptions) : { expression, warnings: [], diagnostics: [] };
54
+ const minified = minify ? minifyRuleExpression(expression, parseOptions) : { expression, diagnostics: [] };
55
55
  const minifiedParseWarnings = [];
56
56
  const minifiedParseDiagnostics = [];
57
- const minificationFailed = (minified.diagnostics ?? []).some(diagnostic => diagnostic.severity === 'error');
57
+ const minificationFailed = minified.diagnostics.some(diagnostic => diagnostic.severity === 'error');
58
58
  if (minify && minified.expression && !minificationFailed) {
59
59
  parseExpression(minified.expression, minifiedParseWarnings, minifiedParseDiagnostics);
60
60
  }
61
61
  const lengthCheckedExpression = minify ? minified.expression : expression;
62
- collectValidationDiagnostics(errors, warnings, diagnostics, parseWarnings, parseDiagnostics, 'expression');
62
+ collectValidationDiagnostics(errors, warnings, diagnostics, parseDiagnostics, 'expression');
63
63
  if (parsedExpression)
64
64
  collectSemanticValidationDiagnostics(errors, warnings, diagnostics, parsedExpression, expression, semanticContext);
65
- collectValidationDiagnostics(errors, warnings, diagnostics, minified.warnings, minified.diagnostics ?? [], 'expression');
66
- collectValidationDiagnostics(errors, warnings, diagnostics, minifiedParseWarnings, minifiedParseDiagnostics, 'minifiedExpression');
65
+ for (const diagnostic of minified.diagnostics)
66
+ pushDiagnostic(diagnostics, { ...diagnostic, source: 'expression' });
67
+ collectValidationDiagnostics(errors, warnings, diagnostics, minifiedParseDiagnostics, 'minifiedExpression');
67
68
  if ((config.enforceMaxLength ?? true) && lengthCheckedExpression.length > maxLength) {
68
69
  const expressionLabel = minify ? 'minified rule expression' : 'rule expression';
69
70
  const limitLabel = maxLength === MAX_CLOUDFLARE_RULE_EXPRESSION_LENGTH ? 'Cloudflare limit' : 'configured limit';
@@ -78,19 +79,16 @@ export function validateRuleExpressionSyntax(source, config = {}) {
78
79
  limitKind: maxLength === MAX_CLOUDFLARE_RULE_EXPRESSION_LENGTH ? 'cloudflare' : 'configured',
79
80
  },
80
81
  severity: 'error',
81
- message,
82
82
  pos: null,
83
83
  length: 0,
84
84
  source: minify ? 'minifiedExpression' : 'expression',
85
85
  });
86
86
  }
87
87
  const validation = {
88
- ok: errors.length === 0,
88
+ ok: !diagnostics.some(diagnostic => diagnostic.severity === 'error'),
89
89
  expression,
90
90
  length: expression.length,
91
91
  maxLength,
92
- errors: [...new Set(errors)],
93
- warnings: [...new Set(warnings)],
94
92
  diagnostics,
95
93
  };
96
94
  if (!minify)
@@ -114,6 +112,33 @@ export function validateLoggingSourceKeyRuleExpressionSyntax(source) {
114
112
  rejectListReferences: true,
115
113
  });
116
114
  }
115
+ const evaluationErrors = new WeakMap();
116
+ /**
117
+ * Returns the errors that make Cloudflare reject a predicate expression in any
118
+ * rule: syntax, operator and type, function signature, and list usage errors.
119
+ * Field availability, length limits, and list contents are not checked. The
120
+ * result is cached with the parsed expression.
121
+ */
122
+ export function ruleExpressionEvaluationErrors(expression) {
123
+ const parseWarnings = [];
124
+ const parseDiagnostics = [];
125
+ const node = parseExpression(expression, parseWarnings, parseDiagnostics);
126
+ const cached = evaluationErrors.get(node);
127
+ if (cached)
128
+ return cached;
129
+ const errors = [];
130
+ const diagnostics = [];
131
+ collectValidationDiagnostics(errors, [], diagnostics, parseDiagnostics, 'expression');
132
+ collectSemanticValidationDiagnostics(errors, [], diagnostics, node, expression, {
133
+ preset: 'url-rewrite-filter',
134
+ fieldAvailability: null,
135
+ rejectListReferences: false,
136
+ deprecationNotices: false,
137
+ });
138
+ const result = [...new Set(errors)];
139
+ evaluationErrors.set(node, result);
140
+ return result;
141
+ }
117
142
  function fieldAvailabilityRules(preset, config, errors, diagnostics) {
118
143
  return resolveRuleExpressionFieldAvailability(preset, config, {
119
144
  onUnknownField: (configPath, field) => {
@@ -206,38 +231,15 @@ function pushValidationConfigError(message, errors, diagnostics, metadata) {
206
231
  pushDiagnostic(diagnostics, {
207
232
  ...metadata,
208
233
  severity: 'error',
209
- message,
210
234
  pos: null,
211
235
  length: 0,
212
236
  source: 'expression',
213
237
  });
214
238
  }
215
- function collectValidationDiagnostics(errors, warnings, diagnostics, messages, messageDiagnostics, source) {
216
- const detailedMessages = new Set();
239
+ function collectValidationDiagnostics(errors, warnings, diagnostics, messageDiagnostics, source) {
217
240
  for (const diagnostic of messageDiagnostics) {
218
- detailedMessages.add(diagnostic.message);
219
241
  pushValidationMessage(errors, warnings, diagnostic.message, diagnostic.severity);
220
- pushDiagnostic(diagnostics, {
221
- ...(diagnostic.id ? { id: diagnostic.id } : {}),
222
- ...(diagnostic.params ? { params: diagnostic.params } : {}),
223
- severity: diagnostic.severity,
224
- message: diagnostic.message,
225
- pos: diagnostic.pos ?? null,
226
- length: diagnostic.length,
227
- source,
228
- });
229
- }
230
- for (const message of messages) {
231
- if (detailedMessages.has(message))
232
- continue;
233
- pushValidationMessage(errors, warnings, message, 'warning');
234
- pushDiagnostic(diagnostics, {
235
- severity: 'warning',
236
- message,
237
- pos: null,
238
- length: 0,
239
- source,
240
- });
242
+ pushDiagnostic(diagnostics, ruleExpressionDiagnostic(diagnostic, source));
241
243
  }
242
244
  }
243
245
  function pushValidationMessage(errors, warnings, message, severity) {
@@ -248,6 +250,7 @@ function pushValidationMessage(errors, warnings, message, severity) {
248
250
  }
249
251
  function collectSemanticValidationDiagnostics(errors, warnings, diagnostics, node, expression, context) {
250
252
  const rootPosition = context.preset === 'url-rewrite-target' ? 'value' : 'predicate';
253
+ validateBooleanLiterals(node, errors, diagnostics);
251
254
  validateSemanticNode(node, new Set(), errors, warnings, diagnostics, expression, context, rootPosition);
252
255
  if (context.preset === 'url-rewrite-target')
253
256
  validateRewriteTargetFunctionUsage(node, errors, diagnostics);
@@ -272,6 +275,9 @@ function validateSemanticNode(node, guardedHeaders, errors, warnings, diagnostic
272
275
  return;
273
276
  case 'Comparison':
274
277
  validateFieldOrFunc(node.left, guardedHeaders, errors, warnings, diagnostics, expression, context);
278
+ if (isDynamicValueNode(node.right)) {
279
+ validateFieldOrFunc(node.right, guardedHeaders, errors, warnings, diagnostics, expression, context);
280
+ }
275
281
  validateComparisonRequirements(node, errors, diagnostics, context);
276
282
  return;
277
283
  case 'Func':
@@ -280,6 +286,8 @@ function validateSemanticNode(node, guardedHeaders, errors, warnings, diagnostic
280
286
  validateAccessPath(node, errors, diagnostics);
281
287
  validateFunctionSupport(node, warnings, diagnostics);
282
288
  validateFunctionArgRequirements(node, errors, diagnostics);
289
+ if (node.name === 'coalesce')
290
+ validateCoalesceFallbacks(node, warnings, diagnostics);
283
291
  if (node.name === 'any' || node.name === 'all') {
284
292
  validateQuantifierPredicateArgs(node, guardedHeaders, errors, warnings, diagnostics, expression, context);
285
293
  }
@@ -315,8 +323,24 @@ function validateComparisonRequirements(node, errors, diagnostics, context) {
315
323
  pushFuncError(node.left, '[*] cannot be used directly with comparison operators; wrap the comparison in any() or all()', errors, diagnostics, { id: 'array_expansion.direct_comparison' });
316
324
  invalidArrayComparison = true;
317
325
  }
326
+ const rightInfo = isDynamicValueNode(node.right) ? inferRuleExpressionArgument(node.right) : null;
327
+ if (isDynamicValueNode(node.right) && rightInfo?.expanded && rightInfo.accessIssues.length === 0) {
328
+ pushValueError(node.right, '[*] cannot be used directly with comparison operators; wrap the comparison in any() or all()', errors, diagnostics, { id: 'array_expansion.direct_comparison' });
329
+ invalidArrayComparison = true;
330
+ }
318
331
  if (!node.right)
319
332
  return;
333
+ // Only equality and ordering operators compare dynamic values; `in` reports its own list requirement.
334
+ if (isDynamicValueNode(node.right) && node.op !== 'in' && !EQUALITY_ORDERING_OPERATORS.has(node.op)) {
335
+ pushValueError(node.right, `operator '${node.op}' requires a literal value on the right side`, errors, diagnostics, { id: 'operator.requires_literal_value', params: { operator: node.op } });
336
+ return;
337
+ }
338
+ if (node.right.kind === 'SetLiteral' && node.op !== 'in') {
339
+ pushValueError(node.right, 'inline lists can only be used with the in operator', errors, diagnostics, {
340
+ id: 'inline_list.requires_in_operator',
341
+ });
342
+ return;
343
+ }
320
344
  if (node.right?.kind === 'ListRef') {
321
345
  validateListRef(node.right, node.left, node.op, errors, diagnostics, context);
322
346
  }
@@ -327,7 +351,7 @@ function validateComparisonRequirements(node, errors, diagnostics, context) {
327
351
  validateWildcardPattern(node.right, errors, diagnostics);
328
352
  }
329
353
  validateComparisonValueDomain(node, errors, diagnostics);
330
- if (!invalidArrayComparison && leftInfo.accessIssues.length === 0) {
354
+ if (!invalidArrayComparison && leftInfo.accessIssues.length === 0 && (rightInfo?.accessIssues.length ?? 0) === 0) {
331
355
  validateComparisonTypeCompatibility(node, errors, diagnostics);
332
356
  }
333
357
  }
@@ -337,12 +361,7 @@ function validateComparisonValueDomain(node, errors, diagnostics) {
337
361
  if (getRuleExpressionFieldValueDomain(node.left.name) !== 'country-code')
338
362
  return;
339
363
  const right = node.right;
340
- const values = right?.kind === 'StringLiteral'
341
- ? [right.value]
342
- : right?.kind === 'SetLiteral'
343
- ? right.values.filter((value) => typeof value === 'string')
344
- : [];
345
- for (const value of values) {
364
+ for (const value of stringLiteralValues(right)) {
346
365
  if (isRuleExpressionCountryCode(value))
347
366
  continue;
348
367
  const message = `'${value}' is not a valid country code for field '${node.left.name}'`;
@@ -352,6 +371,14 @@ function validateComparisonValueDomain(node, errors, diagnostics) {
352
371
  });
353
372
  }
354
373
  }
374
+ /** The quoted String values of a literal or inline list. */
375
+ function stringLiteralValues(node) {
376
+ if (node?.kind === 'StringLiteral')
377
+ return [node.value];
378
+ if (node?.kind !== 'SetLiteral')
379
+ return [];
380
+ return node.elements.flatMap(element => element.type === 'string' ? [element.value] : []);
381
+ }
355
382
  function validateComparisonTypeCompatibility(node, errors, diagnostics) {
356
383
  if (node.op === 'in') {
357
384
  if (node.right?.kind === 'ListRef')
@@ -375,6 +402,14 @@ function validateComparisonTypeCompatibility(node, errors, diagnostics) {
375
402
  });
376
403
  return;
377
404
  }
405
+ if (rightFamily === 'unsupported') {
406
+ const rightTypeLabel = formatRuleExpressionDataType(rightType);
407
+ pushComparisonTypeError(node.right, `operator '${node.op}' does not support ${rightTypeLabel} values`, errors, diagnostics, {
408
+ id: 'operator.unsupported_type',
409
+ params: { operator: node.op, side: 'right', valueType: rightTypeLabel },
410
+ });
411
+ return;
412
+ }
378
413
  if (!isConcreteComparisonFamily(leftFamily) || !isConcreteComparisonFamily(rightFamily))
379
414
  return;
380
415
  const leftLabel = comparisonFamilyLabel(leftFamily);
@@ -521,6 +556,9 @@ function collectRewriteTargetFunctions(node, replacementFunctionStack, functions
521
556
  return;
522
557
  case 'Comparison':
523
558
  collectRewriteTargetFunctions(node.left, replacementFunctionStack, functions, errors, diagnostics);
559
+ if (isDynamicValueNode(node.right)) {
560
+ collectRewriteTargetFunctions(node.right, replacementFunctionStack, functions, errors, diagnostics);
561
+ }
524
562
  return;
525
563
  case 'Field':
526
564
  case 'BoolField':
@@ -583,7 +621,7 @@ function validateFunctionListRefArg(node, arg, errors, diagnostics, context) {
583
621
  }
584
622
  }
585
623
  function validateKnownListRefValueType(node, left, errors, diagnostics, context) {
586
- const listKind = context.customLists?.[node.name]?.kind ?? SUPPORTED_LIST_KIND;
624
+ const listKind = customList(context.customLists, node.name)?.kind ?? SUPPORTED_LIST_KIND;
587
625
  const valueTypes = ruleExpressionValueTypes(left);
588
626
  if (valueTypes.length === 0 || valueTypes.some(valueType => listKindAcceptsValueType(listKind, valueType)))
589
627
  return;
@@ -619,7 +657,7 @@ function isRecord(value) {
619
657
  return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
620
658
  }
621
659
  function validateInlineSetLiteral(node, errors, diagnostics) {
622
- const valueTypes = new Set(node.values.map(classifyInlineSetValue));
660
+ const valueTypes = new Set(node.elements.map(element => element.type));
623
661
  if (valueTypes.size <= 1)
624
662
  return;
625
663
  pushValueError(node, 'inline list elements must all have the same data type', errors, diagnostics, {
@@ -661,7 +699,6 @@ function pushComparisonError(node, requirement, errors, diagnostics, metadata) {
661
699
  pushDiagnostic(diagnostics, {
662
700
  ...metadata,
663
701
  severity: 'error',
664
- message,
665
702
  pos: node.pos,
666
703
  length: node.length,
667
704
  source: 'expression',
@@ -673,7 +710,6 @@ function pushFuncError(node, requirement, errors, diagnostics, metadata) {
673
710
  pushDiagnostic(diagnostics, {
674
711
  ...metadata,
675
712
  severity: 'error',
676
- message,
677
713
  pos: node.pos ?? null,
678
714
  length: node.length ?? 0,
679
715
  source: 'expression',
@@ -685,7 +721,6 @@ function pushValueError(node, message, errors, diagnostics, metadata) {
685
721
  pushDiagnostic(diagnostics, {
686
722
  ...metadata,
687
723
  severity: 'error',
688
- message,
689
724
  pos: span.pos,
690
725
  length: span.length,
691
726
  source: 'expression',
@@ -702,12 +737,36 @@ function validateFunctionSupport(node, warnings, diagnostics) {
702
737
  id: 'function.unreliable',
703
738
  params: { functionName, reason },
704
739
  severity: 'warning',
705
- message,
706
740
  pos: null,
707
741
  length: 0,
708
742
  source: 'expression',
709
743
  });
710
744
  }
745
+ /**
746
+ * Warns when a coalesce() argument before the last can never be nil, which
747
+ * makes every argument after it unused. Only literals and fields without
748
+ * access are certain: Cloudflare produces nil from map keys, array indexes,
749
+ * and functions, never from a field itself.
750
+ */
751
+ function validateCoalesceFallbacks(node, warnings, diagnostics) {
752
+ const index = node.args.slice(0, -1).findIndex(arg => (isLiteralNode(arg) && arg.kind !== 'SetLiteral' && arg.kind !== 'ListRef') ||
753
+ (isFieldNode(arg) && arg.accessPath.length === 0));
754
+ if (index < 0)
755
+ return;
756
+ const parameterName = `value${index + 1}`;
757
+ const unusedArgumentCount = node.args.length - index - 1;
758
+ const message = `function 'coalesce' parameter '${parameterName}' is never nil, so the arguments after it are never used`;
759
+ const span = argSpan(node.args[index]);
760
+ pushWarning(warnings, message);
761
+ pushDiagnostic(diagnostics, {
762
+ id: 'function.coalesce.never_nil_argument',
763
+ params: { functionName: 'coalesce', parameterName, unusedArgumentCount },
764
+ severity: 'warning',
765
+ pos: span.pos,
766
+ length: span.length,
767
+ source: 'expression',
768
+ });
769
+ }
711
770
  function validateFieldOrFunc(node, guardedHeaders, errors, warnings, diagnostics, expression, context) {
712
771
  if (isFieldNode(node)) {
713
772
  validateFieldNode(node, guardedHeaders, errors, warnings, diagnostics, expression, context);
@@ -740,6 +799,9 @@ function validateQuantifierPredicateArgs(node, guardedHeaders, errors, warnings,
740
799
  return;
741
800
  }
742
801
  validateFieldOrFunc(quantifier.source, guardedHeaders, errors, warnings, diagnostics, expression, context);
802
+ if (isDynamicValueNode(quantifier.value)) {
803
+ validateFieldOrFunc(quantifier.value, guardedHeaders, errors, warnings, diagnostics, expression, context);
804
+ }
743
805
  if (quantifier.value.kind === 'ListRef') {
744
806
  validateFunctionListRefArg(node, quantifier.value, errors, diagnostics, context);
745
807
  }
@@ -756,7 +818,6 @@ function pushFunctionSignatureError(signatureError, errors, diagnostics) {
756
818
  id: signatureError.id,
757
819
  ...(signatureError.params ? { params: signatureError.params } : {}),
758
820
  severity: 'error',
759
- message: signatureError.message,
760
821
  pos: span.pos,
761
822
  length: span.length,
762
823
  source: 'expression',
@@ -794,7 +855,6 @@ function validateFieldNode(node, guardedHeaders, errors, warnings, diagnostics,
794
855
  id: 'field.header_index_requires_guard',
795
856
  params: { fieldExpression, guardExpression, headerName },
796
857
  severity: 'warning',
797
- message,
798
858
  pos: node.pos,
799
859
  length: node.length,
800
860
  source: 'expression',
@@ -817,7 +877,6 @@ function validateAccessPath(node, errors, diagnostics) {
817
877
  expectedType: issue.expectedType,
818
878
  },
819
879
  severity: 'error',
820
- message,
821
880
  pos: issue.access.pos ?? null,
822
881
  length: issue.access.length ?? 0,
823
882
  source: 'expression',
@@ -825,10 +884,74 @@ function validateAccessPath(node, errors, diagnostics) {
825
884
  }
826
885
  }
827
886
  function validateBoolFieldNode(node, errors, warnings, diagnostics, expression, context) {
887
+ // validateBooleanLiterals() reports `true` and `false`.
828
888
  if (node.name === 'true' || node.name === 'false')
829
889
  return;
830
890
  validateFieldAvailability(node, errors, diagnostics, expression, context);
831
891
  validateDeprecatedField(node, warnings, diagnostics, expression, context);
892
+ if (!isKnownFieldName(node.name))
893
+ return;
894
+ const type = inferRuleExpressionArgument({ kind: 'Field', name: node.name, accessPath: [], pos: node.pos ?? 0, length: node.length ?? 0 }).type;
895
+ if (isUnknownRuleExpressionDataType(type) || (type.kind === 'scalar' && type.name === 'Boolean'))
896
+ return;
897
+ const actualType = formatRuleExpressionDataType(type);
898
+ const message = `field '${node.name}' must be Boolean in predicate position; received ${actualType}`;
899
+ errors.push(message);
900
+ pushDiagnostic(diagnostics, {
901
+ id: 'field.predicate_type_mismatch',
902
+ params: { fieldName: node.name, expectedType: 'Boolean', actualType },
903
+ severity: 'error',
904
+ pos: typeof node.pos === 'number' ? node.pos : null,
905
+ length: typeof node.length === 'number' ? node.length : 0,
906
+ source: 'expression',
907
+ });
908
+ }
909
+ /**
910
+ * Cloudflare has no Boolean literals. It accepts `true` only as an entire
911
+ * expression, which matches every request.
912
+ */
913
+ function validateBooleanLiterals(root, errors, diagnostics) {
914
+ if (root.kind === 'BoolField' && root.name === RULE_EXPRESSION_MATCH_ALL_EXPRESSION)
915
+ return;
916
+ const visit = (node) => {
917
+ switch (node?.kind) {
918
+ case 'BoolField':
919
+ if (node.name === 'true' || node.name === 'false')
920
+ pushBooleanLiteralError(node, node.name, errors, diagnostics);
921
+ return;
922
+ case 'BoolLiteral':
923
+ pushBooleanLiteralError(node, String(node.value), errors, diagnostics);
924
+ return;
925
+ case 'Comparison':
926
+ visit(node.left);
927
+ visit(node.right);
928
+ return;
929
+ case 'Func':
930
+ node.args.forEach(visit);
931
+ return;
932
+ case 'Not':
933
+ visit(node.operand);
934
+ return;
935
+ case 'And':
936
+ case 'Or':
937
+ case 'Xor':
938
+ node.operands.forEach(visit);
939
+ return;
940
+ }
941
+ };
942
+ visit(root);
943
+ }
944
+ function pushBooleanLiteralError(node, literal, errors, diagnostics) {
945
+ const message = `'${literal}' is not a value; Cloudflare accepts 'true' only as the entire expression`;
946
+ errors.push(message);
947
+ pushDiagnostic(diagnostics, {
948
+ id: 'rule_expression.boolean_literal',
949
+ params: { literal },
950
+ severity: 'error',
951
+ pos: typeof node.pos === 'number' ? node.pos : null,
952
+ length: typeof node.length === 'number' ? node.length : 0,
953
+ source: 'expression',
954
+ });
832
955
  }
833
956
  function validateDeprecatedField(node, warnings, diagnostics, expression, context) {
834
957
  if (!context.deprecationNotices || !KNOWN_DEPRECATED_FIELDS.has(node.name))
@@ -845,20 +968,20 @@ function validateDeprecatedField(node, warnings, diagnostics, expression, contex
845
968
  ? { fieldExpression, replacement }
846
969
  : { fieldExpression },
847
970
  severity: 'notice',
848
- message,
849
971
  pos: typeof node.pos === 'number' ? node.pos : null,
850
972
  length: typeof node.length === 'number' ? node.length : 0,
851
973
  source: 'expression',
852
974
  });
853
975
  }
854
976
  function validateFieldAvailability(node, errors, diagnostics, expression, context) {
855
- const rules = context.fieldAvailability;
856
- if (!rules)
857
- return;
977
+ // Unknown fields are invalid in every product, whatever its field availability.
858
978
  if (!isKnownFieldName(node.name)) {
859
979
  pushUnknownFieldError(node, errors, diagnostics, expression);
860
980
  return;
861
981
  }
982
+ const rules = context.fieldAvailability;
983
+ if (!rules)
984
+ return;
862
985
  const map = isFieldNode(node) && node.accessPath.some(access => access.kind === 'MapKeyAccess');
863
986
  if (isRuleExpressionFieldAvailable({ name: node.name, map }, rules))
864
987
  return;
@@ -869,7 +992,6 @@ function validateFieldAvailability(node, errors, diagnostics, expression, contex
869
992
  id: 'field.unavailable',
870
993
  params: { fieldExpression, fieldName: node.name, preset: rules.preset },
871
994
  severity: 'error',
872
- message,
873
995
  pos: typeof node.pos === 'number' ? node.pos : null,
874
996
  length: typeof node.length === 'number' ? node.length : 0,
875
997
  source: 'expression',
@@ -883,7 +1005,6 @@ function pushUnknownFieldError(node, errors, diagnostics, expression) {
883
1005
  id: 'field.unknown',
884
1006
  params: { fieldExpression, fieldName: node.name },
885
1007
  severity: 'error',
886
- message,
887
1008
  pos: typeof node.pos === 'number' ? node.pos : null,
888
1009
  length: typeof node.length === 'number' ? node.length : 0,
889
1010
  source: 'expression',
@@ -924,9 +1045,9 @@ function getHeaderPresenceGuardNames(node) {
924
1045
  names.add(quantifier.value.value.toLowerCase());
925
1046
  }
926
1047
  if (quantifier.op === 'in' && quantifier.value.kind === 'SetLiteral') {
927
- for (const value of quantifier.value.values) {
928
- if (typeof value === 'string')
929
- names.add(value.toLowerCase());
1048
+ for (const element of quantifier.value.elements) {
1049
+ if (element.type === 'string')
1050
+ names.add(element.value.toLowerCase());
930
1051
  }
931
1052
  }
932
1053
  return names;