@libreflare/worker-runtime 0.7.0 → 0.8.1

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 (162) hide show
  1. package/README.md +235 -13
  2. package/dist/rule-evaluator/accept-encoding.d.ts +2 -2
  3. package/dist/rule-evaluator/accept-encoding.d.ts.map +1 -1
  4. package/dist/rule-evaluator/accept-encoding.js +62 -15
  5. package/dist/rule-evaluator/accept-encoding.js.map +1 -1
  6. package/dist/rule-evaluator/access-values.d.ts +77 -14
  7. package/dist/rule-evaluator/access-values.d.ts.map +1 -1
  8. package/dist/rule-evaluator/access-values.js +149 -17
  9. package/dist/rule-evaluator/access-values.js.map +1 -1
  10. package/dist/rule-evaluator/analysis.d.ts +43 -8
  11. package/dist/rule-evaluator/analysis.d.ts.map +1 -1
  12. package/dist/rule-evaluator/analysis.js +72 -29
  13. package/dist/rule-evaluator/analysis.js.map +1 -1
  14. package/dist/rule-evaluator/ast.d.ts +145 -11
  15. package/dist/rule-evaluator/ast.d.ts.map +1 -1
  16. package/dist/rule-evaluator/ast.js +102 -4
  17. package/dist/rule-evaluator/ast.js.map +1 -1
  18. package/dist/rule-evaluator/byte-strings.d.ts +23 -0
  19. package/dist/rule-evaluator/byte-strings.d.ts.map +1 -0
  20. package/dist/rule-evaluator/byte-strings.js +150 -0
  21. package/dist/rule-evaluator/byte-strings.js.map +1 -0
  22. package/dist/rule-evaluator/constants.d.ts +1 -1
  23. package/dist/rule-evaluator/constants.d.ts.map +1 -1
  24. package/dist/rule-evaluator/constants.js +1 -0
  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 +9 -0
  28. package/dist/rule-evaluator/custom-lists.d.ts.map +1 -1
  29. package/dist/rule-evaluator/custom-lists.js +43 -0
  30. package/dist/rule-evaluator/custom-lists.js.map +1 -1
  31. package/dist/rule-evaluator/diagnostics.d.ts +1 -3
  32. package/dist/rule-evaluator/diagnostics.d.ts.map +1 -1
  33. package/dist/rule-evaluator/diagnostics.js +10 -53
  34. package/dist/rule-evaluator/diagnostics.js.map +1 -1
  35. package/dist/rule-evaluator/engine-observations.d.ts +37 -0
  36. package/dist/rule-evaluator/engine-observations.d.ts.map +1 -0
  37. package/dist/rule-evaluator/engine-observations.generated.d.ts +991 -0
  38. package/dist/rule-evaluator/engine-observations.generated.d.ts.map +1 -0
  39. package/dist/rule-evaluator/engine-observations.generated.js +292 -0
  40. package/dist/rule-evaluator/engine-observations.generated.js.map +1 -0
  41. package/dist/rule-evaluator/engine-observations.js +13 -0
  42. package/dist/rule-evaluator/engine-observations.js.map +1 -0
  43. package/dist/rule-evaluator/evaluator.d.ts +1 -1
  44. package/dist/rule-evaluator/evaluator.d.ts.map +1 -1
  45. package/dist/rule-evaluator/evaluator.js +16 -15
  46. package/dist/rule-evaluator/evaluator.js.map +1 -1
  47. package/dist/rule-evaluator/explain.d.ts.map +1 -1
  48. package/dist/rule-evaluator/explain.js +35 -20
  49. package/dist/rule-evaluator/explain.js.map +1 -1
  50. package/dist/rule-evaluator/field-availability.d.ts +11 -10
  51. package/dist/rule-evaluator/field-availability.d.ts.map +1 -1
  52. package/dist/rule-evaluator/field-availability.js +136 -97
  53. package/dist/rule-evaluator/field-availability.js.map +1 -1
  54. package/dist/rule-evaluator/field-descriptions.generated.d.ts +1 -1
  55. package/dist/rule-evaluator/field-descriptions.generated.js +1 -1
  56. package/dist/rule-evaluator/field-descriptions.generated.js.map +1 -1
  57. package/dist/rule-evaluator/field-value-domains.d.ts.map +1 -1
  58. package/dist/rule-evaluator/field-value-domains.js +2 -4
  59. package/dist/rule-evaluator/field-value-domains.js.map +1 -1
  60. package/dist/rule-evaluator/fields.d.ts +25 -2
  61. package/dist/rule-evaluator/fields.d.ts.map +1 -1
  62. package/dist/rule-evaluator/fields.js +276 -119
  63. package/dist/rule-evaluator/fields.js.map +1 -1
  64. package/dist/rule-evaluator/function-availability.d.ts +16 -0
  65. package/dist/rule-evaluator/function-availability.d.ts.map +1 -0
  66. package/dist/rule-evaluator/function-availability.js +28 -0
  67. package/dist/rule-evaluator/function-availability.js.map +1 -0
  68. package/dist/rule-evaluator/function-descriptions.d.ts +15 -1
  69. package/dist/rule-evaluator/function-descriptions.d.ts.map +1 -1
  70. package/dist/rule-evaluator/function-descriptions.generated.d.ts +50 -7
  71. package/dist/rule-evaluator/function-descriptions.generated.d.ts.map +1 -1
  72. package/dist/rule-evaluator/function-descriptions.generated.js +98 -12
  73. package/dist/rule-evaluator/function-descriptions.generated.js.map +1 -1
  74. package/dist/rule-evaluator/function-descriptions.js +11 -2
  75. package/dist/rule-evaluator/function-descriptions.js.map +1 -1
  76. package/dist/rule-evaluator/function-signature-validation.d.ts +26 -2
  77. package/dist/rule-evaluator/function-signature-validation.d.ts.map +1 -1
  78. package/dist/rule-evaluator/function-signature-validation.js +311 -181
  79. package/dist/rule-evaluator/function-signature-validation.js.map +1 -1
  80. package/dist/rule-evaluator/functions.d.ts +24 -5
  81. package/dist/rule-evaluator/functions.d.ts.map +1 -1
  82. package/dist/rule-evaluator/functions.js +353 -190
  83. package/dist/rule-evaluator/functions.js.map +1 -1
  84. package/dist/rule-evaluator/hash-in-range.d.ts +33 -0
  85. package/dist/rule-evaluator/hash-in-range.d.ts.map +1 -0
  86. package/dist/rule-evaluator/hash-in-range.js +72 -0
  87. package/dist/rule-evaluator/hash-in-range.js.map +1 -0
  88. package/dist/rule-evaluator/log-sample.d.ts.map +1 -1
  89. package/dist/rule-evaluator/log-sample.js +14 -6
  90. package/dist/rule-evaluator/log-sample.js.map +1 -1
  91. package/dist/rule-evaluator/match-chain.d.ts +3 -3
  92. package/dist/rule-evaluator/match-chain.d.ts.map +1 -1
  93. package/dist/rule-evaluator/match-chain.js +19 -13
  94. package/dist/rule-evaluator/match-chain.js.map +1 -1
  95. package/dist/rule-evaluator/minifier.d.ts.map +1 -1
  96. package/dist/rule-evaluator/minifier.js +16 -20
  97. package/dist/rule-evaluator/minifier.js.map +1 -1
  98. package/dist/rule-evaluator/normalizer.d.ts.map +1 -1
  99. package/dist/rule-evaluator/normalizer.js +6 -6
  100. package/dist/rule-evaluator/normalizer.js.map +1 -1
  101. package/dist/rule-evaluator/operators.d.ts +11 -4
  102. package/dist/rule-evaluator/operators.d.ts.map +1 -1
  103. package/dist/rule-evaluator/operators.js +62 -35
  104. package/dist/rule-evaluator/operators.js.map +1 -1
  105. package/dist/rule-evaluator/outcome.d.ts +2 -0
  106. package/dist/rule-evaluator/outcome.d.ts.map +1 -1
  107. package/dist/rule-evaluator/outcome.js +4 -0
  108. package/dist/rule-evaluator/outcome.js.map +1 -1
  109. package/dist/rule-evaluator/parse-cache.d.ts +3 -3
  110. package/dist/rule-evaluator/parse-cache.d.ts.map +1 -1
  111. package/dist/rule-evaluator/parse-cache.js +5 -13
  112. package/dist/rule-evaluator/parse-cache.js.map +1 -1
  113. package/dist/rule-evaluator/parser.d.ts +318 -11
  114. package/dist/rule-evaluator/parser.d.ts.map +1 -1
  115. package/dist/rule-evaluator/parser.js +1314 -333
  116. package/dist/rule-evaluator/parser.js.map +1 -1
  117. package/dist/rule-evaluator/quantifier.d.ts +1 -1
  118. package/dist/rule-evaluator/quantifier.d.ts.map +1 -1
  119. package/dist/rule-evaluator/quantifier.js +1 -16
  120. package/dist/rule-evaluator/quantifier.js.map +1 -1
  121. package/dist/rule-evaluator/request-headers.d.ts +101 -0
  122. package/dist/rule-evaluator/request-headers.d.ts.map +1 -0
  123. package/dist/rule-evaluator/request-headers.js +344 -0
  124. package/dist/rule-evaluator/request-headers.js.map +1 -0
  125. package/dist/rule-evaluator/rule-language-types.d.ts +80 -21
  126. package/dist/rule-evaluator/rule-language-types.d.ts.map +1 -1
  127. package/dist/rule-evaluator/rule-language-types.js +228 -67
  128. package/dist/rule-evaluator/rule-language-types.js.map +1 -1
  129. package/dist/rule-evaluator/tokenizer.d.ts +14 -4
  130. package/dist/rule-evaluator/tokenizer.d.ts.map +1 -1
  131. package/dist/rule-evaluator/tokenizer.js +24 -11
  132. package/dist/rule-evaluator/tokenizer.js.map +1 -1
  133. package/dist/rule-evaluator/transformation.d.ts.map +1 -1
  134. package/dist/rule-evaluator/transformation.js +10 -6
  135. package/dist/rule-evaluator/transformation.js.map +1 -1
  136. package/dist/rule-evaluator/url-decode.d.ts +8 -0
  137. package/dist/rule-evaluator/url-decode.d.ts.map +1 -0
  138. package/dist/rule-evaluator/url-decode.js +126 -0
  139. package/dist/rule-evaluator/url-decode.js.map +1 -0
  140. package/dist/rule-evaluator/validation.d.ts.map +1 -1
  141. package/dist/rule-evaluator/validation.js +509 -251
  142. package/dist/rule-evaluator/validation.js.map +1 -1
  143. package/dist/rule-evaluator.d.ts +2 -2
  144. package/dist/rule-evaluator.d.ts.map +1 -1
  145. package/dist/rule-evaluator.js.map +1 -1
  146. package/package.json +5 -2
  147. package/dist/rule-evaluator/comparison.d.ts +0 -16
  148. package/dist/rule-evaluator/comparison.d.ts.map +0 -1
  149. package/dist/rule-evaluator/comparison.js +0 -43
  150. package/dist/rule-evaluator/comparison.js.map +0 -1
  151. package/dist/rule-evaluator/custom-security-rule-analysis.d.ts +0 -36
  152. package/dist/rule-evaluator/custom-security-rule-analysis.d.ts.map +0 -1
  153. package/dist/rule-evaluator/custom-security-rule-analysis.js +0 -382
  154. package/dist/rule-evaluator/custom-security-rule-analysis.js.map +0 -1
  155. package/dist/rule-evaluator/values.d.ts +0 -26
  156. package/dist/rule-evaluator/values.d.ts.map +0 -1
  157. package/dist/rule-evaluator/values.js +0 -55
  158. package/dist/rule-evaluator/values.js.map +0 -1
  159. package/dist/rule-evaluator/warnings.d.ts +0 -2
  160. package/dist/rule-evaluator/warnings.d.ts.map +0 -1
  161. package/dist/rule-evaluator/warnings.js +0 -5
  162. package/dist/rule-evaluator/warnings.js.map +0 -1
@@ -1,7 +1,7 @@
1
- import { EQUALITY_ORDERING_OPERATORS } from './ast.js';
1
+ import { EQUALITY_ORDERING_OPERATORS, isDynamicValueNode, isEmptyArgument, isUncertainArgument, mayHaveFewerArguments, mayHaveMoreArguments, unhandledNode, } from './ast.js';
2
2
  import { RULE_EXPRESSION_FUNCTION_DESCRIPTIONS } from './function-descriptions.generated.js';
3
- import { collectionElementDataType, compareRuleExpressionDataTypes, formatRuleExpressionDataType, inferFunctionInvocation, inferRuleExpressionArgument, isArrayExpandedNode, isBoolLiteral, isConcreteComparisonFamily, isFieldNode, isIpLiteral, isLiteralNode, isNumberLiteral, isRuleExpressionDataTypeCompatible, isStringLiteral, parseRuleExpressionDataType, ruleExpressionComparisonTypeFamily, } from './rule-language-types.js';
4
- import { extractQuantifierComparison, extractQuantifierPredicate } from './quantifier.js';
3
+ import { collectionElementDataType, compareRuleExpressionDataTypes, formatRuleExpressionDataType, inferFunctionInvocation, inferRuleExpressionArgument, isArrayExpandedNode, isBoolLiteral, isConcreteComparisonFamily, isFieldNode, isIpLiteral, isLiteralNode, isNumberLiteral, isRuleExpressionDataTypeCompatible, isStringLiteral, maximumArgumentCount, parameterForArgument, parseRuleExpressionDataType, ruleExpressionComparisonTypeFamily, } from './rule-language-types.js';
4
+ import { extractQuantifierComparison } from './quantifier.js';
5
5
  const FUNCTION_DESCRIPTIONS = RULE_EXPRESSION_FUNCTION_DESCRIPTIONS;
6
6
  /** Functions whose arguments must all share the first argument's type. */
7
7
  const SAME_TYPE_ARGUMENT_FUNCTIONS = new Set(['coalesce', 'concat']);
@@ -25,11 +25,12 @@ function functionParameterSignatureError(errorNode, functionNode, parameterName,
25
25
  });
26
26
  }
27
27
  /**
28
- * Validates a function call against generated Cloudflare function parameter metadata.
28
+ * Validates a function call against generated Cloudflare function parameter metadata, with its arguments
29
+ * bound as bindArguments() binds them.
29
30
  */
30
- export function validateFunctionSignature(node) {
31
+ export function validateFunctionSignature(node, binding) {
31
32
  const errors = [];
32
- validateArrayExpansionArgPositions(node, errors);
33
+ validateArrayExpansionArgPositions(node, binding, errors);
33
34
  const functionName = node.name;
34
35
  if (functionName === 'any' || functionName === 'all') {
35
36
  validateQuantifierSignature(node, errors);
@@ -38,44 +39,102 @@ export function validateFunctionSignature(node) {
38
39
  const description = FUNCTION_DESCRIPTIONS[functionName];
39
40
  if (!description)
40
41
  return errors;
41
- validateFunctionArity(node, description, errors);
42
- validateFunctionParameters(node, description, errors);
43
- validateCrossParameterConstraints(node, description, errors);
44
- validateGenericFunctionConstraints(node, functionName, errors);
42
+ validateFunctionArity(node, description, binding, errors);
43
+ validateFunctionParameters(node, description, binding, errors);
44
+ validateCrossParameterConstraints(node, description, binding, errors);
45
+ validateGenericFunctionConstraints(node, functionName, binding, errors);
45
46
  return errors;
46
47
  }
47
- function validateFunctionArity(node, description, errors) {
48
+ /**
49
+ * Whether a comma in a call may be stray, so that an argument after an empty slot may bind one parameter
50
+ * earlier than written: with more slots than required parameters, or where more arguments may follow, as
51
+ * while the call is written. With no more slots than that, every comma is needed to reach the required
52
+ * parameters, so each is read as written.
53
+ */
54
+ function commaMayBeStray(node, parameters) {
55
+ return mayHaveMoreArguments(node) || node.args.length > minimumArgumentCount(parameters);
56
+ }
57
+ /** How a call's arguments bind to its parameters; see ArgumentBinding. */
58
+ export function bindArguments(node) {
59
+ const description = FUNCTION_DESCRIPTIONS[node.name];
60
+ const parameters = description?.parameters ?? [];
61
+ const stray = description !== undefined && commaMayBeStray(node, parameters);
62
+ let values = 0;
63
+ const earliest = node.args.map((arg, index) => {
64
+ if (isEmptyArgument(arg))
65
+ return null;
66
+ const position = values++;
67
+ if (isUncertainArgument(node, index))
68
+ return null;
69
+ return stray ? position : index;
70
+ });
71
+ const positions = earliest.map((position, index) => {
72
+ if (position === null || position === index)
73
+ return position;
74
+ const parameter = parameterForArgument(parameters, position);
75
+ return parameter !== undefined && parameter === parameterForArgument(parameters, index) ? position : null;
76
+ });
77
+ return { stray, earliest, positions };
78
+ }
79
+ function validateFunctionArity(node, description, binding, errors) {
48
80
  const min = minimumArgumentCount(description.parameters);
49
81
  const max = maximumArgumentCount(description.parameters);
50
- if (node.args.length < min) {
51
- const parameter = firstMissingRequiredParameter(description.parameters, node.args.length);
82
+ // The arguments that hold a value. One that holds none may stand for a stray comma, and has its own error.
83
+ const values = node.args.filter(arg => !isEmptyArgument(arg));
84
+ // Where no comma may be stray, the arguments bind to the parameters at their written positions, and
85
+ // one that holds no value is missing for a required parameter, at its slot. Otherwise each value may
86
+ // bind as far back as its place among the values, or at its written position, so a parameter is
87
+ // missing in every reading only after the last value, whatever the empty slots before it are.
88
+ if (!binding.stray) {
89
+ node.args.forEach((arg, index) => {
90
+ const parameter = parameterForArgument(description.parameters, index);
91
+ if (!isEmptyArgument(arg) || isUncertainArgument(node, index) || !parameter || !isRequiredArgument(description.parameters, index))
92
+ return;
93
+ const functionName = node.name;
94
+ const parameterName = parameterNameForArgument(node, parameter, index);
95
+ errors.push(signatureError(arg, `function '${functionName}' parameter '${parameterName}' is required`, {
96
+ id: 'function.parameter.required',
97
+ params: { functionName, parameterName },
98
+ }));
99
+ });
100
+ }
101
+ const bound = binding.stray ? node.args.findLastIndex(arg => !isEmptyArgument(arg)) + 1 : node.args.length;
102
+ if (bound < min && !mayHaveMoreArguments(node)) {
103
+ const parameter = firstMissingRequiredParameter(description.parameters, bound);
52
104
  const functionName = node.name;
53
- const parameterName = parameter ? parameterNameForArgument(node, parameter, node.args.length) : 'argument';
105
+ const parameterName = parameter ? parameterNameForArgument(node, parameter, bound) : 'argument';
54
106
  errors.push(signatureError(node, `function '${functionName}' parameter '${parameterName}' is required`, {
55
107
  id: 'function.parameter.required',
56
108
  params: { functionName, parameterName },
57
109
  }));
58
110
  }
59
- if (max !== Infinity && node.args.length > max) {
111
+ // Recovery from a syntax error, or a missing `)`, may hide arguments but never adds one, so too many
112
+ // is still certain, unless some may be another call's, or a stray token read after a missing comma.
113
+ if (max !== Infinity && values.length > max && !mayHaveFewerArguments(node)) {
60
114
  const functionName = node.name;
61
115
  const argumentLabel = max === 1 ? 'argument' : 'arguments';
62
- errors.push(signatureError(node.args[max] ?? node, `function '${functionName}' accepts at most ${max} ${argumentLabel}`, {
116
+ errors.push(signatureError(values[max] ?? node, `function '${functionName}' accepts at most ${max} ${argumentLabel}`, {
63
117
  id: 'function.too_many_arguments',
64
118
  params: { functionName, max, argumentLabel },
65
119
  }));
66
120
  }
67
121
  }
68
- function validateFunctionParameters(node, description, errors) {
69
- for (let index = 0; index < node.args.length; index++) {
70
- const parameter = parameterForArgument(description.parameters, index);
71
- if (!parameter)
72
- continue;
73
- validateFunctionParameter(node, parameter, index, errors);
74
- }
122
+ function validateFunctionParameters(node, description, binding, errors) {
123
+ binding.positions.forEach((position, index) => {
124
+ if (position === null)
125
+ return;
126
+ const parameter = parameterForArgument(description.parameters, position);
127
+ if (parameter)
128
+ validateFunctionParameter(node, parameter, index, position, errors);
129
+ });
75
130
  }
76
- function validateFunctionParameter(node, parameter, index, errors) {
131
+ /** Checks the argument at an index against the parameter it binds to at a position; see ArgumentBinding.positions. */
132
+ function validateFunctionParameter(node, parameter, index, position, errors) {
77
133
  const arg = node.args[index];
78
- const parameterName = parameterNameForArgument(node, parameter, index);
134
+ const parameterName = parameterNameForArgument(node, parameter, position);
135
+ // Invalid syntax already has its error, and the argument-count checks count an empty one.
136
+ if (arg?.kind === 'Invalid')
137
+ return;
79
138
  // Inline lists and list references are not values; they only work with `in`.
80
139
  if (arg?.kind === 'SetLiteral') {
81
140
  errors.push(signatureError(arg, 'inline lists can only be used with the in operator', {
@@ -111,16 +170,18 @@ function validateFunctionParameter(node, parameter, index, errors) {
111
170
  }
112
171
  return;
113
172
  }
114
- if (parameter.allowedFields && (!isFieldNode(arg) || !parameter.allowedFields.includes(arg.name))) {
173
+ const allowedFields = parameter.allowedFields;
174
+ const disallowedField = allowedFields && fieldsReadBy(arg).find(field => !allowedFields.includes(field));
175
+ if (allowedFields && disallowedField) {
115
176
  errors.push(functionParameterSignatureError(arg, node, parameterName, {
116
177
  id: 'function.parameter.requires_allowed_field',
117
- requirement: `must be one of ${parameter.allowedFields.join(', ')}`,
118
- params: { allowedFields: parameter.allowedFields },
178
+ requirement: `cannot use field '${disallowedField}'; it accepts only ${allowedFields.join(', ')}`,
179
+ params: { allowedFields, fieldName: disallowedField },
119
180
  }));
120
181
  return;
121
182
  }
122
183
  const argInfo = inferRuleExpressionArgument(arg);
123
- if (argInfo.accessIssues.length > 0)
184
+ if (argInfo.invalid)
124
185
  return;
125
186
  if (parameter.acceptsFieldUsageKinds && argInfo.usageKind && !parameter.acceptsFieldUsageKinds.includes(argInfo.usageKind)) {
126
187
  errors.push(functionParameterSignatureError(arg, node, parameterName, {
@@ -151,17 +212,53 @@ function validateLiteralParameter(parameter, arg) {
151
212
  return null;
152
213
  }
153
214
  function literalMatchesParameterType(arg, parameter) {
154
- if (parameter.types.some(type => type === 'String' || type === 'Bytes'))
155
- return isStringLiteral(arg);
156
- if (parameter.types.includes('Integer'))
157
- return isNumberLiteral(arg) && Number.isInteger(arg.value);
158
- if (parameter.types.includes('Number'))
159
- return isNumberLiteral(arg);
160
- if (parameter.types.includes('IP address') || parameter.types.includes('IP range'))
161
- return isIpLiteral(arg);
162
- if (parameter.types.includes('Boolean'))
163
- return isBoolLiteral(arg);
164
- return isLiteralNode(arg);
215
+ return parameter.types.some(type => literalMatchesType(arg, type));
216
+ }
217
+ function literalMatchesType(arg, type) {
218
+ switch (type) {
219
+ case 'String':
220
+ case 'Bytes':
221
+ return isStringLiteral(arg);
222
+ case 'Integer':
223
+ case 'Number':
224
+ return isNumberLiteral(arg);
225
+ case 'IP address':
226
+ case 'IP range':
227
+ return isIpLiteral(arg);
228
+ case 'Boolean':
229
+ return isBoolLiteral(arg);
230
+ default:
231
+ return isLiteralNode(arg);
232
+ }
233
+ }
234
+ /** The fields an argument reads, directly or through nested calls. */
235
+ function fieldsReadBy(node) {
236
+ switch (node.kind) {
237
+ case 'Field':
238
+ case 'BoolField':
239
+ return [node.name];
240
+ case 'Func':
241
+ return node.args.flatMap(fieldsReadBy);
242
+ case 'Comparison':
243
+ return [...fieldsReadBy(node.left), ...(isDynamicValueNode(node.right) ? fieldsReadBy(node.right) : [])];
244
+ case 'And':
245
+ case 'Or':
246
+ case 'Xor':
247
+ return node.operands.flatMap(fieldsReadBy);
248
+ case 'Not':
249
+ return fieldsReadBy(node.operand);
250
+ case 'Invalid':
251
+ return node.operand ? fieldsReadBy(node.operand) : [];
252
+ case 'StringLiteral':
253
+ case 'NumberLiteral':
254
+ case 'IpLiteral':
255
+ case 'BoolLiteral':
256
+ case 'SetLiteral':
257
+ case 'ListRef':
258
+ return [];
259
+ default:
260
+ return unhandledNode(node);
261
+ }
165
262
  }
166
263
  function numberLiteralWithinBounds(value, parameter) {
167
264
  if (parameter.min !== undefined && value < parameter.min)
@@ -170,60 +267,77 @@ function numberLiteralWithinBounds(value, parameter) {
170
267
  return false;
171
268
  return true;
172
269
  }
173
- function validateCrossParameterConstraints(node, description, errors) {
270
+ function validateCrossParameterConstraints(node, description, binding, errors) {
174
271
  if (SAME_TYPE_ARGUMENT_FUNCTIONS.has(node.name))
175
- validateSameTypeArguments(node, description, errors);
272
+ validateSameTypeArguments(node, description, binding, errors);
273
+ const { positions } = binding;
176
274
  for (const [index, parameter] of description.parameters.entries()) {
177
- if (parameter.maxOffsetFromPrevious === undefined || index === 0)
275
+ if (index === 0)
178
276
  continue;
277
+ if (index >= node.args.length)
278
+ break;
179
279
  const previous = node.args[index - 1];
180
280
  const current = node.args[index];
181
- if (!isNumberLiteral(previous) || !isNumberLiteral(current))
281
+ // Both must bind at their written positions; see ArgumentBinding.positions.
282
+ if (positions[index - 1] !== index - 1 || positions[index] !== index)
182
283
  continue;
183
- if (current.value - previous.value <= parameter.maxOffsetFromPrevious)
284
+ if (!isNumberLiteral(previous) || !isNumberLiteral(current))
184
285
  continue;
185
286
  const previousParameterName = description.parameters[index - 1]?.name ?? 'previous parameter';
186
- errors.push(functionParameterSignatureError(current, node, parameter.name, {
187
- id: 'function.parameter.max_offset_from_previous',
188
- requirement: `must be at most ${parameter.maxOffsetFromPrevious} bits after ${previousParameterName}`,
189
- params: { maxOffsetFromPrevious: parameter.maxOffsetFromPrevious, previousParameterName },
190
- }));
287
+ if (parameter.greaterThanPrevious && current.integer <= previous.integer) {
288
+ errors.push(functionParameterSignatureError(current, node, parameter.name, {
289
+ id: 'function.parameter.greater_than_previous',
290
+ requirement: `must be greater than ${previousParameterName}`,
291
+ params: { previousParameterName },
292
+ }));
293
+ }
294
+ const offset = current.integer - previous.integer;
295
+ if (parameter.maxOffsetFromPrevious !== undefined && offset > BigInt(parameter.maxOffsetFromPrevious)) {
296
+ errors.push(functionParameterSignatureError(current, node, parameter.name, {
297
+ id: 'function.parameter.max_offset_from_previous',
298
+ requirement: `must be at most ${parameter.maxOffsetFromPrevious} bits after ${previousParameterName}`,
299
+ params: { maxOffsetFromPrevious: parameter.maxOffsetFromPrevious, previousParameterName },
300
+ }));
301
+ }
191
302
  }
192
303
  }
193
- function validateSameTypeArguments(node, description, errors) {
304
+ function validateSameTypeArguments(node, description, binding, errors) {
194
305
  const invocation = inferFunctionInvocation(node);
195
306
  const firstParameter = parameterForArgument(description.parameters, 0);
196
307
  const firstArgument = invocation.arguments[0];
197
308
  if (!firstParameter ||
198
309
  !firstArgument ||
199
- firstArgument.accessIssues.length > 0 ||
310
+ firstArgument.invalid ||
200
311
  !parameterTypesMatch(firstParameter, firstArgument))
201
312
  return;
202
313
  const argumentTypes = invocation.argumentTypes;
203
314
  const expectedType = argumentTypes[0];
204
315
  if (!expectedType)
205
316
  return;
317
+ const { positions } = binding;
206
318
  for (let index = 1; index < argumentTypes.length; index++) {
207
319
  const arg = node.args[index];
320
+ const position = positions[index];
208
321
  const actualType = argumentTypes[index];
209
- const parameter = parameterForArgument(description.parameters, index);
322
+ const parameter = position === null ? undefined : parameterForArgument(description.parameters, position);
210
323
  const argument = invocation.arguments[index];
211
- if (!actualType ||
324
+ if (position === null ||
325
+ !actualType ||
212
326
  !parameter ||
213
327
  !argument ||
328
+ argument.invalid ||
214
329
  argument.expanded ||
215
- argument.accessIssues.length > 0 ||
216
330
  !parameterTypesMatch(parameter, argument) ||
217
331
  compareRuleExpressionDataTypes(expectedType, actualType) !== 'different')
218
332
  continue;
219
333
  const expectedTypeLabel = formatRuleExpressionDataType(expectedType);
220
334
  const actualTypeLabel = formatRuleExpressionDataType(actualType);
221
- const parameterName = parameterNameForArgument(node, parameter, index);
335
+ const parameterName = parameterNameForArgument(node, parameter, position);
222
336
  errors.push(functionParameterSignatureError(arg, node, parameterName, {
223
337
  id: 'function.parameter.type_mismatch',
224
338
  requirement: `must match first argument type ${expectedTypeLabel}; received ${actualTypeLabel}`,
225
339
  params: {
226
- argumentIndex: index,
340
+ argumentIndex: position,
227
341
  referenceArgumentIndex: 0,
228
342
  expectedType: expectedTypeLabel,
229
343
  actualType: actualTypeLabel,
@@ -231,18 +345,19 @@ function validateSameTypeArguments(node, description, errors) {
231
345
  }));
232
346
  }
233
347
  }
234
- function validateGenericFunctionConstraints(node, functionName, errors) {
348
+ function validateGenericFunctionConstraints(node, functionName, binding, errors) {
235
349
  if (functionName !== 'has_value')
236
350
  return;
351
+ const { positions } = binding;
237
352
  const collectionArg = node.args[0];
238
- const valueArg = node.args[1];
239
- if (!collectionArg || !valueArg)
353
+ if (!collectionArg || positions[0] !== 0)
240
354
  return;
241
355
  const collectionInfo = inferRuleExpressionArgument(collectionArg);
242
- const valueInfo = inferRuleExpressionArgument(valueArg);
243
- if (collectionInfo.accessIssues.length > 0 || valueInfo.accessIssues.length > 0)
356
+ if (collectionInfo.invalid)
244
357
  return;
245
- // Cloudflare accepts maps of single values, but not maps of arrays such as http.request.headers.
358
+ // Cloudflare accepts maps of single values, but not maps of arrays such as http.request.headers. It
359
+ // checks the collection before it reads the value, so neither an error in the value nor a value not
360
+ // yet written hides this one.
246
361
  if (collectionInfo.type.kind === 'map' && collectionInfo.type.value.kind === 'array') {
247
362
  const actualType = formatRuleExpressionDataType(collectionInfo.type);
248
363
  errors.push(functionParameterSignatureError(collectionArg, node, 'collection', {
@@ -252,8 +367,14 @@ function validateGenericFunctionConstraints(node, functionName, errors) {
252
367
  }));
253
368
  return;
254
369
  }
370
+ // Recovery from a syntax error may have cut the value short, or a stray comma moved it; see
371
+ // ArgumentBinding.positions.
372
+ const valueArg = node.args[1];
373
+ if (!valueArg || positions[1] !== 1)
374
+ return;
375
+ const valueInfo = inferRuleExpressionArgument(valueArg);
255
376
  const elementType = collectionElementDataType(collectionInfo.type);
256
- if (!elementType)
377
+ if (valueInfo.invalid || !elementType)
257
378
  return;
258
379
  if (isRuleExpressionDataTypeCompatible(valueInfo.type, elementType))
259
380
  return;
@@ -265,36 +386,29 @@ function validateGenericFunctionConstraints(node, functionName, errors) {
265
386
  }));
266
387
  }
267
388
  function validateQuantifierSignature(node, errors) {
268
- if (node.args.length < 1) {
269
- const functionName = node.name;
270
- errors.push(signatureError(node, `function '${functionName}' parameter 'comparison' is required`, {
271
- id: 'function.parameter.required',
272
- params: { functionName, parameterName: 'comparison' },
273
- }));
389
+ // The parser reads one argument at most, and reports a quantifier without one
390
+ // (`syntax.missing_function_argument`), or leaves the number of arguments uncertain.
391
+ if (node.args.length < 1)
274
392
  return;
275
- }
276
- if (node.args.length > 1) {
277
- const functionName = node.name;
278
- errors.push(signatureError(node.args[1] ?? node, `function '${functionName}' accepts at most 1 array comparison`, {
279
- id: 'function.too_many_array_comparisons',
280
- params: { functionName, max: 1 },
281
- }));
282
- }
283
- const comparison = extractQuantifierComparison(node);
284
- if (comparison && !comparison.comparison.right)
393
+ // Recovery from a syntax error may have cut the argument short; see FuncNode.uncertainFrom. Invalid
394
+ // syntax has its error, such as a comment that may hide the comparison, and `not` before it negates
395
+ // what may be one.
396
+ let predicate = node.args[0];
397
+ while (predicate.kind === 'Not')
398
+ predicate = predicate.operand;
399
+ if (isUncertainArgument(node, 0) || predicate.kind === 'Invalid')
285
400
  return;
286
- const quantifier = extractQuantifierPredicate(node);
287
- if (!quantifier) {
401
+ const comparison = extractQuantifierComparison(node);
402
+ if (!comparison) {
288
403
  errors.push(functionParameterSignatureError(node.args[0], node, 'comparison', {
289
404
  id: 'function.parameter.requires_array_comparison',
290
405
  requirement: 'must use an array comparison',
291
406
  }));
292
407
  return;
293
408
  }
294
- const collectionArg = quantifier.source;
295
- const valueArg = quantifier.value;
409
+ const collectionArg = comparison.source;
296
410
  const collectionInfo = inferRuleExpressionArgument(collectionArg);
297
- if (collectionInfo.accessIssues.length > 0)
411
+ if (collectionInfo.invalid)
298
412
  return;
299
413
  const elementType = collectionElementDataType(collectionInfo.type);
300
414
  if (!isQuantifierCollectionSyntax(collectionArg) || !elementType) {
@@ -304,37 +418,47 @@ function validateQuantifierSignature(node, errors) {
304
418
  }));
305
419
  return;
306
420
  }
307
- // Fields and function results compare only with equality and ordering operators.
308
- if (!isLiteralNode(valueArg) && !EQUALITY_ORDERING_OPERATORS.has(quantifier.op)) {
309
- errors.push(functionParameterSignatureError(valueArg, node, 'comparison', {
310
- id: 'function.parameter.requires_literal_comparison_value',
311
- requirement: 'must compare against a literal value',
312
- }));
313
- return;
314
- }
315
- if (valueArg.kind === 'SetLiteral' && quantifier.op !== 'in') {
316
- errors.push(signatureError(valueArg, 'inline lists can only be used with the in operator', {
317
- id: 'inline_list.requires_in_operator',
318
- }));
319
- return;
421
+ // Nothing is judged by what a value not written, or invalid syntax, might have been, but the
422
+ // operator's check of the collection's type holds whatever the value is.
423
+ const valueArg = comparison.comparison.right?.kind === 'Invalid' ? undefined : comparison.comparison.right;
424
+ if (valueArg) {
425
+ // Fields and function results compare only with equality and ordering operators.
426
+ if (!isLiteralNode(valueArg) && !EQUALITY_ORDERING_OPERATORS.has(comparison.op)) {
427
+ errors.push(functionParameterSignatureError(valueArg, node, 'comparison', {
428
+ id: 'function.parameter.requires_literal_comparison_value',
429
+ requirement: 'must compare against a literal value',
430
+ }));
431
+ return;
432
+ }
433
+ if (valueArg.kind === 'SetLiteral' && comparison.op !== 'in') {
434
+ errors.push(signatureError(valueArg, 'inline lists can only be used with the in operator', {
435
+ id: 'inline_list.requires_in_operator',
436
+ }));
437
+ return;
438
+ }
439
+ if (valueArg.kind === 'ListRef')
440
+ return;
320
441
  }
321
- if (!elementType || valueArg.kind === 'ListRef')
322
- return;
323
- if (inferRuleExpressionArgument(valueArg).accessIssues.length > 0)
324
- return;
325
- const inferredValueType = inferRuleExpressionArgument(valueArg).type;
326
- const valueType = valueArg.kind === 'SetLiteral' && inferredValueType.kind === 'set'
442
+ const valueInfo = valueArg && inferRuleExpressionArgument(valueArg);
443
+ const inferredValueType = valueInfo && !valueInfo.invalid ? valueInfo.type : null;
444
+ const valueType = valueArg?.kind === 'SetLiteral' && inferredValueType?.kind === 'set'
327
445
  ? inferredValueType.element
328
446
  : inferredValueType;
329
- const comparisonRequirement = quantifierComparisonTypeRequirement(quantifier.op, elementType, valueType, valueArg.kind === 'SetLiteral');
447
+ const comparisonRequirement = quantifierComparisonTypeRequirement(comparison.op, elementType, valueType, valueArg?.kind === 'SetLiteral');
330
448
  if (!comparisonRequirement)
331
449
  return;
332
450
  const errorNode = comparisonRequirement.errorNode === 'collection' ? collectionArg : valueArg;
333
451
  errors.push(functionParameterSignatureError(errorNode, node, 'comparison', comparisonRequirement));
334
452
  }
453
+ /**
454
+ * The requirement of the operator that a quantifier's comparison breaks, if
455
+ * any. The collection's type is checked whatever the value is, unless that is
456
+ * of a type no operator supports. A value that is missing, invalid, or of a
457
+ * type not known is not judged, and the params leave out its type.
458
+ */
335
459
  function quantifierComparisonTypeRequirement(op, elementType, valueType, valueIsInlineSet) {
336
460
  const leftFamily = ruleExpressionComparisonTypeFamily(elementType);
337
- const rightFamily = ruleExpressionComparisonTypeFamily(valueType);
461
+ const rightFamily = valueType ? ruleExpressionComparisonTypeFamily(valueType) : 'unknown';
338
462
  if (leftFamily === 'unsupported') {
339
463
  const leftLabel = formatRuleExpressionDataType(elementType);
340
464
  return {
@@ -343,15 +467,14 @@ function quantifierComparisonTypeRequirement(op, elementType, valueType, valueIs
343
467
  params: {
344
468
  operator: op,
345
469
  collectionType: leftLabel,
346
- valueType: formatRuleExpressionDataType(valueType),
347
- valueIsInlineSet,
470
+ ...(valueType ? { valueType: formatRuleExpressionDataType(valueType), valueIsInlineSet } : {}),
348
471
  side: 'collection',
349
472
  issue: 'unsupported_type',
350
473
  },
351
474
  errorNode: 'collection',
352
475
  };
353
476
  }
354
- if (rightFamily === 'unsupported') {
477
+ if (valueType && rightFamily === 'unsupported') {
355
478
  const valueLabel = formatRuleExpressionDataType(valueType);
356
479
  return {
357
480
  id: 'function.parameter.comparison_type_mismatch',
@@ -366,9 +489,15 @@ function quantifierComparisonTypeRequirement(op, elementType, valueType, valueIs
366
489
  },
367
490
  };
368
491
  }
369
- if (!isConcreteComparisonFamily(leftFamily) || !isConcreteComparisonFamily(rightFamily))
492
+ if (!isConcreteComparisonFamily(leftFamily))
370
493
  return null;
371
494
  const leftLabel = comparisonFamilyLabel(leftFamily);
495
+ const valueParams = isConcreteComparisonFamily(rightFamily)
496
+ ? { valueType: comparisonFamilyLabel(rightFamily), valueIsInlineSet }
497
+ : {};
498
+ const collectionRequirement = collectionTypeRequirement(op, leftFamily, { operator: op, collectionType: leftLabel, ...valueParams });
499
+ if (collectionRequirement || !isConcreteComparisonFamily(rightFamily))
500
+ return collectionRequirement;
372
501
  const rightLabel = comparisonFamilyLabel(rightFamily);
373
502
  const valueLabel = valueIsInlineSet ? `inline list of ${rightLabel}` : rightLabel;
374
503
  const baseParams = {
@@ -377,17 +506,14 @@ function quantifierComparisonTypeRequirement(op, elementType, valueType, valueIs
377
506
  valueType: rightLabel,
378
507
  valueIsInlineSet,
379
508
  };
509
+ const typeMismatch = {
510
+ id: 'function.parameter.comparison_type_mismatch',
511
+ requirement: `cannot compare ${leftLabel} with ${valueLabel} using operator '${op}'`,
512
+ params: { ...baseParams, issue: 'type_mismatch' },
513
+ };
380
514
  switch (op) {
381
515
  case 'eq':
382
516
  case 'ne':
383
- if (!isEqualityFamily(leftFamily)) {
384
- return {
385
- id: 'function.parameter.comparison_type_mismatch',
386
- requirement: `does not support ${leftLabel} values with operator '${op}'`,
387
- params: { ...baseParams, side: 'collection', issue: 'unsupported_type' },
388
- errorNode: 'collection',
389
- };
390
- }
391
517
  if (!isEqualityFamily(rightFamily)) {
392
518
  return {
393
519
  id: 'function.parameter.comparison_type_mismatch',
@@ -395,63 +521,58 @@ function quantifierComparisonTypeRequirement(op, elementType, valueType, valueIs
395
521
  params: { ...baseParams, side: 'value', issue: 'unsupported_type' },
396
522
  };
397
523
  }
398
- return leftFamily === rightFamily ? null : {
399
- id: 'function.parameter.comparison_type_mismatch',
400
- requirement: `cannot compare ${leftLabel} with ${valueLabel} using operator '${op}'`,
401
- params: { ...baseParams, issue: 'type_mismatch' },
402
- };
524
+ return leftFamily === rightFamily ? null : typeMismatch;
403
525
  case 'lt':
404
526
  case 'le':
405
527
  case 'gt':
406
528
  case 'ge':
407
- if (!isOrderingFamily(leftFamily)) {
408
- return {
409
- id: 'function.parameter.comparison_type_mismatch',
410
- requirement: `requires String or Number values with operator '${op}'; collection values are ${leftLabel}`,
411
- params: { ...baseParams, side: 'collection', issue: 'requires_type', requiredTypes: ['String', 'Number'] },
412
- errorNode: 'collection',
413
- };
414
- }
415
- return isOrderingFamily(rightFamily) && leftFamily === rightFamily
416
- ? null
417
- : {
418
- id: 'function.parameter.comparison_type_mismatch',
419
- requirement: `cannot compare ${leftLabel} with ${valueLabel} using operator '${op}'`,
420
- params: { ...baseParams, issue: 'type_mismatch' },
421
- };
529
+ case 'in':
530
+ // The collection's family suits the operator, so the same family in the value does too.
531
+ return leftFamily === rightFamily ? null : typeMismatch;
422
532
  case 'contains':
423
533
  case 'matches':
424
534
  case 'wildcard':
425
535
  case 'strict_wildcard':
426
- if (leftFamily !== 'string') {
427
- return {
428
- id: 'function.parameter.comparison_type_mismatch',
429
- requirement: `requires String values with operator '${op}'; collection values are ${leftLabel}`,
430
- params: { ...baseParams, side: 'collection', issue: 'requires_type', requiredTypes: ['String'] },
431
- errorNode: 'collection',
432
- };
433
- }
434
536
  return rightFamily === 'string' ? null : {
435
537
  id: 'function.parameter.comparison_type_mismatch',
436
538
  requirement: `requires String values with operator '${op}'; comparison value is ${valueLabel}`,
437
539
  params: { ...baseParams, side: 'value', issue: 'requires_type', requiredTypes: ['String'] },
438
540
  };
541
+ }
542
+ }
543
+ /** The requirement of the operator that the collection's values break, if any, whatever the value is. */
544
+ function collectionTypeRequirement(op, family, baseParams) {
545
+ const label = comparisonFamilyLabel(family);
546
+ switch (op) {
547
+ case 'eq':
548
+ case 'ne':
439
549
  case 'in':
440
- if (!isEqualityFamily(leftFamily)) {
441
- return {
442
- id: 'function.parameter.comparison_type_mismatch',
443
- requirement: `does not support ${leftLabel} values with operator 'in'`,
444
- params: { ...baseParams, side: 'collection', issue: 'unsupported_type' },
445
- errorNode: 'collection',
446
- };
447
- }
448
- return isEqualityFamily(rightFamily) && leftFamily === rightFamily
449
- ? null
450
- : {
451
- id: 'function.parameter.comparison_type_mismatch',
452
- requirement: `cannot compare ${leftLabel} with ${valueLabel} using operator 'in'`,
453
- params: { ...baseParams, issue: 'type_mismatch' },
454
- };
550
+ return isEqualityFamily(family) ? null : {
551
+ id: 'function.parameter.comparison_type_mismatch',
552
+ requirement: `does not support ${label} values with operator '${op}'`,
553
+ params: { ...baseParams, side: 'collection', issue: 'unsupported_type' },
554
+ errorNode: 'collection',
555
+ };
556
+ case 'lt':
557
+ case 'le':
558
+ case 'gt':
559
+ case 'ge':
560
+ return isOrderingFamily(family) ? null : {
561
+ id: 'function.parameter.comparison_type_mismatch',
562
+ requirement: `requires String or Number values with operator '${op}'; collection values are ${label}`,
563
+ params: { ...baseParams, side: 'collection', issue: 'requires_type', requiredTypes: ['String', 'Number'] },
564
+ errorNode: 'collection',
565
+ };
566
+ case 'contains':
567
+ case 'matches':
568
+ case 'wildcard':
569
+ case 'strict_wildcard':
570
+ return family === 'string' ? null : {
571
+ id: 'function.parameter.comparison_type_mismatch',
572
+ requirement: `requires String values with operator '${op}'; collection values are ${label}`,
573
+ params: { ...baseParams, side: 'collection', issue: 'requires_type', requiredTypes: ['String'] },
574
+ errorNode: 'collection',
575
+ };
455
576
  }
456
577
  }
457
578
  function isEqualityFamily(family) {
@@ -471,16 +592,17 @@ function comparisonFamilyLabel(family) {
471
592
  function isQuantifierCollectionSyntax(arg) {
472
593
  return isArrayExpandedNode(arg);
473
594
  }
474
- function validateArrayExpansionArgPositions(node, errors) {
475
- for (let index = 1; index < node.args.length; index++) {
595
+ function validateArrayExpansionArgPositions(node, binding, errors) {
596
+ // An argument that cannot bind at the first position is never the first, whichever parameter it fills.
597
+ binding.earliest.forEach((position, index) => {
476
598
  const arg = node.args[index];
477
- if (isArrayExpandedNode(arg) && inferRuleExpressionArgument(arg).accessIssues.length === 0) {
478
- errors.push(signatureError(arg, '[*] can only be used in the first argument of a function call', {
479
- id: 'array_expansion.function_first_argument_only',
480
- params: { argumentIndex: index },
481
- }));
482
- }
483
- }
599
+ if (position === null || position === 0 || !isArrayExpandedNode(arg))
600
+ return;
601
+ errors.push(signatureError(arg, '[*] can only be used in the first argument of a function call', {
602
+ id: 'array_expansion.function_first_argument_only',
603
+ params: { argumentIndex: position },
604
+ }));
605
+ });
484
606
  }
485
607
  function parameterTypesMatch(parameter, argInfo) {
486
608
  if (parameter.types.includes('T'))
@@ -533,6 +655,14 @@ function parameterTypeRequirement(parameter, fieldOnly) {
533
655
  };
534
656
  }
535
657
  function literalRequirement(parameter) {
658
+ // A parameter accepting several literal types, such as a lookup_json_*() key, takes any literal of them.
659
+ if (parameter.types.length > 1 && !parameter.types.every(type => type === 'String' || type === 'Bytes')) {
660
+ return {
661
+ id: 'function.parameter.requires_literal',
662
+ requirement: `must be a ${parameter.types.join(' or ')} literal`,
663
+ params: { expectedTypes: parameter.types },
664
+ };
665
+ }
536
666
  if (parameter.nonEmpty && parameter.types.some(type => type === 'String' || type === 'Bytes')) {
537
667
  return {
538
668
  id: 'function.parameter.requires_non_empty_string_literal',
@@ -579,14 +709,20 @@ function literalRequirement(parameter) {
579
709
  function minimumArgumentCount(parameters) {
580
710
  const variadicIndex = parameters.findIndex(parameter => parameter.variadic);
581
711
  if (variadicIndex >= 0) {
582
- const variadic = parameters[variadicIndex];
583
- const minimumOccurrences = variadic?.minimumOccurrences ?? (variadic?.optional ? 0 : 1);
584
- return parameters.slice(0, variadicIndex).filter(parameter => !parameter.optional).length + minimumOccurrences;
712
+ return parameters.slice(0, variadicIndex).filter(parameter => !parameter.optional).length + requiredOccurrences(parameters[variadicIndex]);
585
713
  }
586
714
  return parameters.filter(parameter => !parameter.optional).length;
587
715
  }
588
- function maximumArgumentCount(parameters) {
589
- return parameters.some(parameter => parameter.variadic) ? Infinity : parameters.length;
716
+ /** Whether the argument at an index fills a required parameter, or a required occurrence of a variadic one. */
717
+ function isRequiredArgument(parameters, index) {
718
+ const variadicIndex = parameters.findIndex(parameter => parameter.variadic);
719
+ if (variadicIndex < 0 || index < variadicIndex)
720
+ return parameters[index]?.optional !== true && index < parameters.length;
721
+ return index - variadicIndex < requiredOccurrences(parameters[variadicIndex]);
722
+ }
723
+ /** How many arguments a variadic parameter needs at least. */
724
+ function requiredOccurrences(variadic) {
725
+ return variadic?.minimumOccurrences ?? (variadic?.optional ? 0 : 1);
590
726
  }
591
727
  function firstMissingRequiredParameter(parameters, argCount) {
592
728
  for (let index = argCount; index < parameters.length; index++) {
@@ -596,12 +732,6 @@ function firstMissingRequiredParameter(parameters, argCount) {
596
732
  }
597
733
  return parameters.find(parameter => !parameter.optional);
598
734
  }
599
- function parameterForArgument(parameters, index) {
600
- const variadicIndex = parameters.findIndex(parameter => parameter.variadic);
601
- if (variadicIndex >= 0 && index >= variadicIndex)
602
- return parameters[variadicIndex];
603
- return parameters[index];
604
- }
605
735
  function parameterNameForArgument(node, parameter, index) {
606
736
  const description = FUNCTION_DESCRIPTIONS[node.name];
607
737
  const variadicIndex = description?.parameters.findIndex(candidate => candidate.variadic) ?? -1;