@libreflare/worker-runtime 0.6.0 → 0.8.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 (160) hide show
  1. package/README.md +342 -22
  2. package/dist/rule-evaluator/accept-encoding.d.ts +3 -2
  3. package/dist/rule-evaluator/accept-encoding.d.ts.map +1 -1
  4. package/dist/rule-evaluator/accept-encoding.js +80 -35
  5. package/dist/rule-evaluator/accept-encoding.js.map +1 -1
  6. package/dist/rule-evaluator/access-values.d.ts +84 -1
  7. package/dist/rule-evaluator/access-values.d.ts.map +1 -1
  8. package/dist/rule-evaluator/access-values.js +165 -13
  9. package/dist/rule-evaluator/access-values.js.map +1 -1
  10. package/dist/rule-evaluator/analysis.d.ts +44 -13
  11. package/dist/rule-evaluator/analysis.d.ts.map +1 -1
  12. package/dist/rule-evaluator/analysis.js +82 -65
  13. package/dist/rule-evaluator/analysis.js.map +1 -1
  14. package/dist/rule-evaluator/ast.d.ts +165 -11
  15. package/dist/rule-evaluator/ast.d.ts.map +1 -1
  16. package/dist/rule-evaluator/ast.js +106 -3
  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 +7 -3
  23. package/dist/rule-evaluator/constants.d.ts.map +1 -1
  24. package/dist/rule-evaluator/constants.js +8 -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 +15 -0
  28. package/dist/rule-evaluator/custom-lists.d.ts.map +1 -1
  29. package/dist/rule-evaluator/custom-lists.js +64 -1
  30. package/dist/rule-evaluator/custom-lists.js.map +1 -1
  31. package/dist/rule-evaluator/diagnostics.d.ts +10 -9
  32. package/dist/rule-evaluator/diagnostics.d.ts.map +1 -1
  33. package/dist/rule-evaluator/diagnostics.js +32 -52
  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 +10 -3
  44. package/dist/rule-evaluator/evaluator.d.ts.map +1 -1
  45. package/dist/rule-evaluator/evaluator.js +91 -83
  46. package/dist/rule-evaluator/evaluator.js.map +1 -1
  47. package/dist/rule-evaluator/explain.d.ts +11 -2
  48. package/dist/rule-evaluator/explain.d.ts.map +1 -1
  49. package/dist/rule-evaluator/explain.js +234 -123
  50. package/dist/rule-evaluator/explain.js.map +1 -1
  51. package/dist/rule-evaluator/field-availability.d.ts +11 -10
  52. package/dist/rule-evaluator/field-availability.d.ts.map +1 -1
  53. package/dist/rule-evaluator/field-availability.js +136 -97
  54. package/dist/rule-evaluator/field-availability.js.map +1 -1
  55. package/dist/rule-evaluator/field-descriptions.generated.d.ts +114 -1
  56. package/dist/rule-evaluator/field-descriptions.generated.d.ts.map +1 -1
  57. package/dist/rule-evaluator/field-descriptions.generated.js +261 -1
  58. package/dist/rule-evaluator/field-descriptions.generated.js.map +1 -1
  59. package/dist/rule-evaluator/field-value-domains.d.ts.map +1 -1
  60. package/dist/rule-evaluator/field-value-domains.js +2 -4
  61. package/dist/rule-evaluator/field-value-domains.js.map +1 -1
  62. package/dist/rule-evaluator/fields.d.ts +37 -4
  63. package/dist/rule-evaluator/fields.d.ts.map +1 -1
  64. package/dist/rule-evaluator/fields.js +387 -87
  65. package/dist/rule-evaluator/fields.js.map +1 -1
  66. package/dist/rule-evaluator/function-availability.d.ts +16 -0
  67. package/dist/rule-evaluator/function-availability.d.ts.map +1 -0
  68. package/dist/rule-evaluator/function-availability.js +28 -0
  69. package/dist/rule-evaluator/function-availability.js.map +1 -0
  70. package/dist/rule-evaluator/function-descriptions.d.ts +15 -1
  71. package/dist/rule-evaluator/function-descriptions.d.ts.map +1 -1
  72. package/dist/rule-evaluator/function-descriptions.generated.d.ts +63 -7
  73. package/dist/rule-evaluator/function-descriptions.generated.d.ts.map +1 -1
  74. package/dist/rule-evaluator/function-descriptions.generated.js +122 -12
  75. package/dist/rule-evaluator/function-descriptions.generated.js.map +1 -1
  76. package/dist/rule-evaluator/function-descriptions.js +11 -2
  77. package/dist/rule-evaluator/function-descriptions.js.map +1 -1
  78. package/dist/rule-evaluator/function-signature-validation.d.ts +27 -3
  79. package/dist/rule-evaluator/function-signature-validation.d.ts.map +1 -1
  80. package/dist/rule-evaluator/function-signature-validation.js +351 -170
  81. package/dist/rule-evaluator/function-signature-validation.js.map +1 -1
  82. package/dist/rule-evaluator/functions.d.ts +38 -6
  83. package/dist/rule-evaluator/functions.d.ts.map +1 -1
  84. package/dist/rule-evaluator/functions.js +549 -279
  85. package/dist/rule-evaluator/functions.js.map +1 -1
  86. package/dist/rule-evaluator/hash-in-range.d.ts +33 -0
  87. package/dist/rule-evaluator/hash-in-range.d.ts.map +1 -0
  88. package/dist/rule-evaluator/hash-in-range.js +72 -0
  89. package/dist/rule-evaluator/hash-in-range.js.map +1 -0
  90. package/dist/rule-evaluator/log-sample.d.ts.map +1 -1
  91. package/dist/rule-evaluator/log-sample.js +14 -6
  92. package/dist/rule-evaluator/log-sample.js.map +1 -1
  93. package/dist/rule-evaluator/match-chain.d.ts +3 -3
  94. package/dist/rule-evaluator/match-chain.d.ts.map +1 -1
  95. package/dist/rule-evaluator/match-chain.js +19 -13
  96. package/dist/rule-evaluator/match-chain.js.map +1 -1
  97. package/dist/rule-evaluator/minifier.d.ts +2 -6
  98. package/dist/rule-evaluator/minifier.d.ts.map +1 -1
  99. package/dist/rule-evaluator/minifier.js +24 -28
  100. package/dist/rule-evaluator/minifier.js.map +1 -1
  101. package/dist/rule-evaluator/normalizer.d.ts +2 -6
  102. package/dist/rule-evaluator/normalizer.d.ts.map +1 -1
  103. package/dist/rule-evaluator/normalizer.js +13 -13
  104. package/dist/rule-evaluator/normalizer.js.map +1 -1
  105. package/dist/rule-evaluator/operators.d.ts +30 -4
  106. package/dist/rule-evaluator/operators.d.ts.map +1 -1
  107. package/dist/rule-evaluator/operators.js +217 -132
  108. package/dist/rule-evaluator/operators.js.map +1 -1
  109. package/dist/rule-evaluator/outcome.d.ts +65 -0
  110. package/dist/rule-evaluator/outcome.d.ts.map +1 -0
  111. package/dist/rule-evaluator/outcome.js +82 -0
  112. package/dist/rule-evaluator/outcome.js.map +1 -0
  113. package/dist/rule-evaluator/parse-cache.d.ts +3 -3
  114. package/dist/rule-evaluator/parse-cache.d.ts.map +1 -1
  115. package/dist/rule-evaluator/parse-cache.js +5 -13
  116. package/dist/rule-evaluator/parse-cache.js.map +1 -1
  117. package/dist/rule-evaluator/parser.d.ts +325 -12
  118. package/dist/rule-evaluator/parser.d.ts.map +1 -1
  119. package/dist/rule-evaluator/parser.js +1351 -340
  120. package/dist/rule-evaluator/parser.js.map +1 -1
  121. package/dist/rule-evaluator/quantifier.d.ts +2 -2
  122. package/dist/rule-evaluator/quantifier.d.ts.map +1 -1
  123. package/dist/rule-evaluator/quantifier.js +1 -16
  124. package/dist/rule-evaluator/quantifier.js.map +1 -1
  125. package/dist/rule-evaluator/request-headers.d.ts +101 -0
  126. package/dist/rule-evaluator/request-headers.d.ts.map +1 -0
  127. package/dist/rule-evaluator/request-headers.js +344 -0
  128. package/dist/rule-evaluator/request-headers.js.map +1 -0
  129. package/dist/rule-evaluator/rule-language-types.d.ts +84 -23
  130. package/dist/rule-evaluator/rule-language-types.d.ts.map +1 -1
  131. package/dist/rule-evaluator/rule-language-types.js +266 -76
  132. package/dist/rule-evaluator/rule-language-types.js.map +1 -1
  133. package/dist/rule-evaluator/tokenizer.d.ts +14 -4
  134. package/dist/rule-evaluator/tokenizer.d.ts.map +1 -1
  135. package/dist/rule-evaluator/tokenizer.js +49 -11
  136. package/dist/rule-evaluator/tokenizer.js.map +1 -1
  137. package/dist/rule-evaluator/transformation.d.ts.map +1 -1
  138. package/dist/rule-evaluator/transformation.js +10 -6
  139. package/dist/rule-evaluator/transformation.js.map +1 -1
  140. package/dist/rule-evaluator/url-decode.d.ts +8 -0
  141. package/dist/rule-evaluator/url-decode.d.ts.map +1 -0
  142. package/dist/rule-evaluator/url-decode.js +126 -0
  143. package/dist/rule-evaluator/url-decode.js.map +1 -0
  144. package/dist/rule-evaluator/validation.d.ts +8 -9
  145. package/dist/rule-evaluator/validation.d.ts.map +1 -1
  146. package/dist/rule-evaluator/validation.js +544 -253
  147. package/dist/rule-evaluator/validation.js.map +1 -1
  148. package/dist/rule-evaluator.d.ts +5 -5
  149. package/dist/rule-evaluator.d.ts.map +1 -1
  150. package/dist/rule-evaluator.js +1 -1
  151. package/dist/rule-evaluator.js.map +1 -1
  152. package/package.json +6 -3
  153. package/dist/rule-evaluator/custom-security-rule-analysis.d.ts +0 -36
  154. package/dist/rule-evaluator/custom-security-rule-analysis.d.ts.map +0 -1
  155. package/dist/rule-evaluator/custom-security-rule-analysis.js +0 -382
  156. package/dist/rule-evaluator/custom-security-rule-analysis.js.map +0 -1
  157. package/dist/rule-evaluator/warnings.d.ts +0 -2
  158. package/dist/rule-evaluator/warnings.d.ts.map +0 -1
  159. package/dist/rule-evaluator/warnings.js +0 -5
  160. package/dist/rule-evaluator/warnings.js.map +0 -1
@@ -1,7 +1,10 @@
1
+ import { EQUALITY_ORDERING_OPERATORS, isDynamicValueNode, isEmptyArgument, isUncertainArgument, mayHaveFewerArguments, mayHaveMoreArguments, unhandledNode, } from './ast.js';
1
2
  import { RULE_EXPRESSION_FUNCTION_DESCRIPTIONS } from './function-descriptions.generated.js';
2
- import { collectionElementDataType, compareRuleExpressionDataTypes, formatRuleExpressionDataType, inferFunctionInvocation, inferRuleExpressionArgument, isArrayExpandedNode, isBoolLiteral, isConcreteComparisonFamily, isFieldNode, isIpLiteral, isLiteralNode, isNumberLiteral, isRuleExpressionDataTypeCompatible, isStringLiteral, parseRuleExpressionDataType, ruleExpressionComparisonTypeFamily, } from './rule-language-types.js';
3
- 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';
4
5
  const FUNCTION_DESCRIPTIONS = RULE_EXPRESSION_FUNCTION_DESCRIPTIONS;
6
+ /** Functions whose arguments must all share the first argument's type. */
7
+ const SAME_TYPE_ARGUMENT_FUNCTIONS = new Set(['coalesce', 'concat']);
5
8
  function signatureError(node, message, metadata) {
6
9
  return {
7
10
  node,
@@ -22,11 +25,12 @@ function functionParameterSignatureError(errorNode, functionNode, parameterName,
22
25
  });
23
26
  }
24
27
  /**
25
- * 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.
26
30
  */
27
- export function validateFunctionSignature(node) {
31
+ export function validateFunctionSignature(node, binding) {
28
32
  const errors = [];
29
- validateArrayExpansionArgPositions(node, errors);
33
+ validateArrayExpansionArgPositions(node, binding, errors);
30
34
  const functionName = node.name;
31
35
  if (functionName === 'any' || functionName === 'all') {
32
36
  validateQuantifierSignature(node, errors);
@@ -35,44 +39,116 @@ export function validateFunctionSignature(node) {
35
39
  const description = FUNCTION_DESCRIPTIONS[functionName];
36
40
  if (!description)
37
41
  return errors;
38
- validateFunctionArity(node, description, errors);
39
- validateFunctionParameters(node, description, errors);
40
- validateCrossParameterConstraints(node, description, errors);
41
- 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);
42
46
  return errors;
43
47
  }
44
- 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) {
45
80
  const min = minimumArgumentCount(description.parameters);
46
81
  const max = maximumArgumentCount(description.parameters);
47
- if (node.args.length < min) {
48
- 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);
49
104
  const functionName = node.name;
50
- const parameterName = parameter?.name ?? 'argument';
105
+ const parameterName = parameter ? parameterNameForArgument(node, parameter, bound) : 'argument';
51
106
  errors.push(signatureError(node, `function '${functionName}' parameter '${parameterName}' is required`, {
52
107
  id: 'function.parameter.required',
53
108
  params: { functionName, parameterName },
54
109
  }));
55
110
  }
56
- 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)) {
57
114
  const functionName = node.name;
58
115
  const argumentLabel = max === 1 ? 'argument' : 'arguments';
59
- 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}`, {
60
117
  id: 'function.too_many_arguments',
61
118
  params: { functionName, max, argumentLabel },
62
119
  }));
63
120
  }
64
121
  }
65
- function validateFunctionParameters(node, description, errors) {
66
- for (let index = 0; index < node.args.length; index++) {
67
- const parameter = parameterForArgument(description.parameters, index);
68
- if (!parameter)
69
- continue;
70
- validateFunctionParameter(node, parameter, index, errors);
71
- }
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
+ });
72
130
  }
73
- 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) {
74
133
  const arg = node.args[index];
75
- 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;
138
+ // Inline lists and list references are not values; they only work with `in`.
139
+ if (arg?.kind === 'SetLiteral') {
140
+ errors.push(signatureError(arg, 'inline lists can only be used with the in operator', {
141
+ id: 'inline_list.requires_in_operator',
142
+ }));
143
+ return;
144
+ }
145
+ if (arg?.kind === 'ListRef') {
146
+ errors.push(signatureError(arg, `list '$${arg.name}' can only be used with the in operator`, {
147
+ id: 'list.reference_requires_in_operator',
148
+ params: { listName: arg.name },
149
+ }));
150
+ return;
151
+ }
76
152
  if (parameter.field && !isFieldNode(arg)) {
77
153
  errors.push(functionParameterSignatureError(arg, node, parameterName, {
78
154
  id: 'function.parameter.requires_field',
@@ -94,16 +170,18 @@ function validateFunctionParameter(node, parameter, index, errors) {
94
170
  }
95
171
  return;
96
172
  }
97
- 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) {
98
176
  errors.push(functionParameterSignatureError(arg, node, parameterName, {
99
177
  id: 'function.parameter.requires_allowed_field',
100
- requirement: `must be one of ${parameter.allowedFields.join(', ')}`,
101
- params: { allowedFields: parameter.allowedFields },
178
+ requirement: `cannot use field '${disallowedField}'; it accepts only ${allowedFields.join(', ')}`,
179
+ params: { allowedFields, fieldName: disallowedField },
102
180
  }));
103
181
  return;
104
182
  }
105
183
  const argInfo = inferRuleExpressionArgument(arg);
106
- if (argInfo.accessIssues.length > 0)
184
+ if (argInfo.invalid)
107
185
  return;
108
186
  if (parameter.acceptsFieldUsageKinds && argInfo.usageKind && !parameter.acceptsFieldUsageKinds.includes(argInfo.usageKind)) {
109
187
  errors.push(functionParameterSignatureError(arg, node, parameterName, {
@@ -134,17 +212,53 @@ function validateLiteralParameter(parameter, arg) {
134
212
  return null;
135
213
  }
136
214
  function literalMatchesParameterType(arg, parameter) {
137
- if (parameter.types.some(type => type === 'String' || type === 'Bytes'))
138
- return isStringLiteral(arg);
139
- if (parameter.types.includes('Integer'))
140
- return isNumberLiteral(arg) && Number.isInteger(arg.value);
141
- if (parameter.types.includes('Number'))
142
- return isNumberLiteral(arg);
143
- if (parameter.types.includes('IP address') || parameter.types.includes('IP range'))
144
- return isIpLiteral(arg);
145
- if (parameter.types.includes('Boolean'))
146
- return isBoolLiteral(arg);
147
- 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
+ }
148
262
  }
149
263
  function numberLiteralWithinBounds(value, parameter) {
150
264
  if (parameter.min !== undefined && value < parameter.min)
@@ -153,60 +267,77 @@ function numberLiteralWithinBounds(value, parameter) {
153
267
  return false;
154
268
  return true;
155
269
  }
156
- function validateCrossParameterConstraints(node, description, errors) {
157
- if (node.name === 'concat')
158
- validateConcatArgumentTypes(node, description, errors);
270
+ function validateCrossParameterConstraints(node, description, binding, errors) {
271
+ if (SAME_TYPE_ARGUMENT_FUNCTIONS.has(node.name))
272
+ validateSameTypeArguments(node, description, binding, errors);
273
+ const { positions } = binding;
159
274
  for (const [index, parameter] of description.parameters.entries()) {
160
- if (parameter.maxOffsetFromPrevious === undefined || index === 0)
275
+ if (index === 0)
161
276
  continue;
277
+ if (index >= node.args.length)
278
+ break;
162
279
  const previous = node.args[index - 1];
163
280
  const current = node.args[index];
164
- 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)
165
283
  continue;
166
- if (current.value - previous.value <= parameter.maxOffsetFromPrevious)
284
+ if (!isNumberLiteral(previous) || !isNumberLiteral(current))
167
285
  continue;
168
286
  const previousParameterName = description.parameters[index - 1]?.name ?? 'previous parameter';
169
- errors.push(functionParameterSignatureError(current, node, parameter.name, {
170
- id: 'function.parameter.max_offset_from_previous',
171
- requirement: `must be at most ${parameter.maxOffsetFromPrevious} bits after ${previousParameterName}`,
172
- params: { maxOffsetFromPrevious: parameter.maxOffsetFromPrevious, previousParameterName },
173
- }));
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
+ }
174
302
  }
175
303
  }
176
- function validateConcatArgumentTypes(node, description, errors) {
304
+ function validateSameTypeArguments(node, description, binding, errors) {
177
305
  const invocation = inferFunctionInvocation(node);
178
306
  const firstParameter = parameterForArgument(description.parameters, 0);
179
307
  const firstArgument = invocation.arguments[0];
180
308
  if (!firstParameter ||
181
309
  !firstArgument ||
182
- firstArgument.accessIssues.length > 0 ||
310
+ firstArgument.invalid ||
183
311
  !parameterTypesMatch(firstParameter, firstArgument))
184
312
  return;
185
313
  const argumentTypes = invocation.argumentTypes;
186
314
  const expectedType = argumentTypes[0];
187
315
  if (!expectedType)
188
316
  return;
317
+ const { positions } = binding;
189
318
  for (let index = 1; index < argumentTypes.length; index++) {
190
319
  const arg = node.args[index];
320
+ const position = positions[index];
191
321
  const actualType = argumentTypes[index];
192
- const parameter = parameterForArgument(description.parameters, index);
322
+ const parameter = position === null ? undefined : parameterForArgument(description.parameters, position);
193
323
  const argument = invocation.arguments[index];
194
- if (!actualType ||
324
+ if (position === null ||
325
+ !actualType ||
195
326
  !parameter ||
196
327
  !argument ||
328
+ argument.invalid ||
197
329
  argument.expanded ||
198
- argument.accessIssues.length > 0 ||
199
330
  !parameterTypesMatch(parameter, argument) ||
200
331
  compareRuleExpressionDataTypes(expectedType, actualType) !== 'different')
201
332
  continue;
202
333
  const expectedTypeLabel = formatRuleExpressionDataType(expectedType);
203
334
  const actualTypeLabel = formatRuleExpressionDataType(actualType);
204
- const parameterName = parameterNameForArgument(node, parameter, index);
335
+ const parameterName = parameterNameForArgument(node, parameter, position);
205
336
  errors.push(functionParameterSignatureError(arg, node, parameterName, {
206
337
  id: 'function.parameter.type_mismatch',
207
338
  requirement: `must match first argument type ${expectedTypeLabel}; received ${actualTypeLabel}`,
208
339
  params: {
209
- argumentIndex: index,
340
+ argumentIndex: position,
210
341
  referenceArgumentIndex: 0,
211
342
  expectedType: expectedTypeLabel,
212
343
  actualType: actualTypeLabel,
@@ -214,19 +345,36 @@ function validateConcatArgumentTypes(node, description, errors) {
214
345
  }));
215
346
  }
216
347
  }
217
- function validateGenericFunctionConstraints(node, functionName, errors) {
348
+ function validateGenericFunctionConstraints(node, functionName, binding, errors) {
218
349
  if (functionName !== 'has_value')
219
350
  return;
351
+ const { positions } = binding;
220
352
  const collectionArg = node.args[0];
221
- const valueArg = node.args[1];
222
- if (!collectionArg || !valueArg)
353
+ if (!collectionArg || positions[0] !== 0)
223
354
  return;
224
355
  const collectionInfo = inferRuleExpressionArgument(collectionArg);
225
- const valueInfo = inferRuleExpressionArgument(valueArg);
226
- if (collectionInfo.accessIssues.length > 0 || valueInfo.accessIssues.length > 0)
356
+ if (collectionInfo.invalid)
357
+ return;
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.
361
+ if (collectionInfo.type.kind === 'map' && collectionInfo.type.value.kind === 'array') {
362
+ const actualType = formatRuleExpressionDataType(collectionInfo.type);
363
+ errors.push(functionParameterSignatureError(collectionArg, node, 'collection', {
364
+ id: 'function.parameter.requires_scalar_map',
365
+ requirement: `must be an Array or a map of single values; received ${actualType}`,
366
+ params: { actualType },
367
+ }));
368
+ return;
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)
227
374
  return;
375
+ const valueInfo = inferRuleExpressionArgument(valueArg);
228
376
  const elementType = collectionElementDataType(collectionInfo.type);
229
- if (!elementType)
377
+ if (valueInfo.invalid || !elementType)
230
378
  return;
231
379
  if (isRuleExpressionDataTypeCompatible(valueInfo.type, elementType))
232
380
  return;
@@ -238,36 +386,29 @@ function validateGenericFunctionConstraints(node, functionName, errors) {
238
386
  }));
239
387
  }
240
388
  function validateQuantifierSignature(node, errors) {
241
- if (node.args.length < 1) {
242
- const functionName = node.name;
243
- errors.push(signatureError(node, `function '${functionName}' parameter 'comparison' is required`, {
244
- id: 'function.parameter.required',
245
- params: { functionName, parameterName: 'comparison' },
246
- }));
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)
247
392
  return;
248
- }
249
- if (node.args.length > 1) {
250
- const functionName = node.name;
251
- errors.push(signatureError(node.args[1] ?? node, `function '${functionName}' accepts at most 1 array comparison`, {
252
- id: 'function.too_many_array_comparisons',
253
- params: { functionName, max: 1 },
254
- }));
255
- }
256
- const comparison = extractQuantifierComparison(node);
257
- 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')
258
400
  return;
259
- const quantifier = extractQuantifierPredicate(node);
260
- if (!quantifier) {
401
+ const comparison = extractQuantifierComparison(node);
402
+ if (!comparison) {
261
403
  errors.push(functionParameterSignatureError(node.args[0], node, 'comparison', {
262
404
  id: 'function.parameter.requires_array_comparison',
263
405
  requirement: 'must use an array comparison',
264
406
  }));
265
407
  return;
266
408
  }
267
- const collectionArg = quantifier.source;
268
- const valueArg = quantifier.value;
409
+ const collectionArg = comparison.source;
269
410
  const collectionInfo = inferRuleExpressionArgument(collectionArg);
270
- if (collectionInfo.accessIssues.length > 0)
411
+ if (collectionInfo.invalid)
271
412
  return;
272
413
  const elementType = collectionElementDataType(collectionInfo.type);
273
414
  if (!isQuantifierCollectionSyntax(collectionArg) || !elementType) {
@@ -277,28 +418,47 @@ function validateQuantifierSignature(node, errors) {
277
418
  }));
278
419
  return;
279
420
  }
280
- if (!isLiteralNode(valueArg)) {
281
- errors.push(functionParameterSignatureError(valueArg, node, 'comparison', {
282
- id: 'function.parameter.requires_literal_comparison_value',
283
- requirement: 'must compare against a literal value',
284
- }));
285
- 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;
286
441
  }
287
- if (!elementType || valueArg.kind === 'ListRef')
288
- return;
289
- const inferredValueType = inferRuleExpressionArgument(valueArg).type;
290
- 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'
291
445
  ? inferredValueType.element
292
446
  : inferredValueType;
293
- const comparisonRequirement = quantifierComparisonTypeRequirement(quantifier.op, elementType, valueType, valueArg.kind === 'SetLiteral');
447
+ const comparisonRequirement = quantifierComparisonTypeRequirement(comparison.op, elementType, valueType, valueArg?.kind === 'SetLiteral');
294
448
  if (!comparisonRequirement)
295
449
  return;
296
450
  const errorNode = comparisonRequirement.errorNode === 'collection' ? collectionArg : valueArg;
297
451
  errors.push(functionParameterSignatureError(errorNode, node, 'comparison', comparisonRequirement));
298
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
+ */
299
459
  function quantifierComparisonTypeRequirement(op, elementType, valueType, valueIsInlineSet) {
300
460
  const leftFamily = ruleExpressionComparisonTypeFamily(elementType);
301
- const rightFamily = ruleExpressionComparisonTypeFamily(valueType);
461
+ const rightFamily = valueType ? ruleExpressionComparisonTypeFamily(valueType) : 'unknown';
302
462
  if (leftFamily === 'unsupported') {
303
463
  const leftLabel = formatRuleExpressionDataType(elementType);
304
464
  return {
@@ -307,17 +467,37 @@ function quantifierComparisonTypeRequirement(op, elementType, valueType, valueIs
307
467
  params: {
308
468
  operator: op,
309
469
  collectionType: leftLabel,
310
- valueType: formatRuleExpressionDataType(valueType),
311
- valueIsInlineSet,
470
+ ...(valueType ? { valueType: formatRuleExpressionDataType(valueType), valueIsInlineSet } : {}),
312
471
  side: 'collection',
313
472
  issue: 'unsupported_type',
314
473
  },
315
474
  errorNode: 'collection',
316
475
  };
317
476
  }
318
- if (!isConcreteComparisonFamily(leftFamily) || !isConcreteComparisonFamily(rightFamily))
477
+ if (valueType && rightFamily === 'unsupported') {
478
+ const valueLabel = formatRuleExpressionDataType(valueType);
479
+ return {
480
+ id: 'function.parameter.comparison_type_mismatch',
481
+ requirement: `does not support ${valueLabel} values with operator '${op}'`,
482
+ params: {
483
+ operator: op,
484
+ collectionType: formatRuleExpressionDataType(elementType),
485
+ valueType: valueLabel,
486
+ valueIsInlineSet,
487
+ side: 'value',
488
+ issue: 'unsupported_type',
489
+ },
490
+ };
491
+ }
492
+ if (!isConcreteComparisonFamily(leftFamily))
319
493
  return null;
320
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;
321
501
  const rightLabel = comparisonFamilyLabel(rightFamily);
322
502
  const valueLabel = valueIsInlineSet ? `inline list of ${rightLabel}` : rightLabel;
323
503
  const baseParams = {
@@ -326,17 +506,14 @@ function quantifierComparisonTypeRequirement(op, elementType, valueType, valueIs
326
506
  valueType: rightLabel,
327
507
  valueIsInlineSet,
328
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
+ };
329
514
  switch (op) {
330
515
  case 'eq':
331
516
  case 'ne':
332
- if (!isEqualityFamily(leftFamily)) {
333
- return {
334
- id: 'function.parameter.comparison_type_mismatch',
335
- requirement: `does not support ${leftLabel} values with operator '${op}'`,
336
- params: { ...baseParams, side: 'collection', issue: 'unsupported_type' },
337
- errorNode: 'collection',
338
- };
339
- }
340
517
  if (!isEqualityFamily(rightFamily)) {
341
518
  return {
342
519
  id: 'function.parameter.comparison_type_mismatch',
@@ -344,63 +521,58 @@ function quantifierComparisonTypeRequirement(op, elementType, valueType, valueIs
344
521
  params: { ...baseParams, side: 'value', issue: 'unsupported_type' },
345
522
  };
346
523
  }
347
- return leftFamily === rightFamily ? null : {
348
- id: 'function.parameter.comparison_type_mismatch',
349
- requirement: `cannot compare ${leftLabel} with ${valueLabel} using operator '${op}'`,
350
- params: { ...baseParams, issue: 'type_mismatch' },
351
- };
524
+ return leftFamily === rightFamily ? null : typeMismatch;
352
525
  case 'lt':
353
526
  case 'le':
354
527
  case 'gt':
355
528
  case 'ge':
356
- if (!isOrderingFamily(leftFamily)) {
357
- return {
358
- id: 'function.parameter.comparison_type_mismatch',
359
- requirement: `requires String or Number values with operator '${op}'; collection values are ${leftLabel}`,
360
- params: { ...baseParams, side: 'collection', issue: 'requires_type', requiredTypes: ['String', 'Number'] },
361
- errorNode: 'collection',
362
- };
363
- }
364
- return isOrderingFamily(rightFamily) && leftFamily === rightFamily
365
- ? null
366
- : {
367
- id: 'function.parameter.comparison_type_mismatch',
368
- requirement: `cannot compare ${leftLabel} with ${valueLabel} using operator '${op}'`,
369
- params: { ...baseParams, issue: 'type_mismatch' },
370
- };
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;
371
532
  case 'contains':
372
533
  case 'matches':
373
534
  case 'wildcard':
374
535
  case 'strict_wildcard':
375
- if (leftFamily !== 'string') {
376
- return {
377
- id: 'function.parameter.comparison_type_mismatch',
378
- requirement: `requires String values with operator '${op}'; collection values are ${leftLabel}`,
379
- params: { ...baseParams, side: 'collection', issue: 'requires_type', requiredTypes: ['String'] },
380
- errorNode: 'collection',
381
- };
382
- }
383
536
  return rightFamily === 'string' ? null : {
384
537
  id: 'function.parameter.comparison_type_mismatch',
385
538
  requirement: `requires String values with operator '${op}'; comparison value is ${valueLabel}`,
386
539
  params: { ...baseParams, side: 'value', issue: 'requires_type', requiredTypes: ['String'] },
387
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':
388
549
  case 'in':
389
- if (!isEqualityFamily(leftFamily)) {
390
- return {
391
- id: 'function.parameter.comparison_type_mismatch',
392
- requirement: `does not support ${leftLabel} values with operator 'in'`,
393
- params: { ...baseParams, side: 'collection', issue: 'unsupported_type' },
394
- errorNode: 'collection',
395
- };
396
- }
397
- return isEqualityFamily(rightFamily) && leftFamily === rightFamily
398
- ? null
399
- : {
400
- id: 'function.parameter.comparison_type_mismatch',
401
- requirement: `cannot compare ${leftLabel} with ${valueLabel} using operator 'in'`,
402
- params: { ...baseParams, issue: 'type_mismatch' },
403
- };
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
+ };
404
576
  }
405
577
  }
406
578
  function isEqualityFamily(family) {
@@ -420,16 +592,17 @@ function comparisonFamilyLabel(family) {
420
592
  function isQuantifierCollectionSyntax(arg) {
421
593
  return isArrayExpandedNode(arg);
422
594
  }
423
- function validateArrayExpansionArgPositions(node, errors) {
424
- 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) => {
425
598
  const arg = node.args[index];
426
- if (isArrayExpandedNode(arg) && inferRuleExpressionArgument(arg).accessIssues.length === 0) {
427
- errors.push(signatureError(arg, '[*] can only be used in the first argument of a function call', {
428
- id: 'array_expansion.function_first_argument_only',
429
- params: { argumentIndex: index },
430
- }));
431
- }
432
- }
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
+ });
433
606
  }
434
607
  function parameterTypesMatch(parameter, argInfo) {
435
608
  if (parameter.types.includes('T'))
@@ -482,6 +655,14 @@ function parameterTypeRequirement(parameter, fieldOnly) {
482
655
  };
483
656
  }
484
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
+ }
485
666
  if (parameter.nonEmpty && parameter.types.some(type => type === 'String' || type === 'Bytes')) {
486
667
  return {
487
668
  id: 'function.parameter.requires_non_empty_string_literal',
@@ -528,14 +709,20 @@ function literalRequirement(parameter) {
528
709
  function minimumArgumentCount(parameters) {
529
710
  const variadicIndex = parameters.findIndex(parameter => parameter.variadic);
530
711
  if (variadicIndex >= 0) {
531
- const variadic = parameters[variadicIndex];
532
- const minimumOccurrences = variadic?.minimumOccurrences ?? (variadic?.optional ? 0 : 1);
533
- 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]);
534
713
  }
535
714
  return parameters.filter(parameter => !parameter.optional).length;
536
715
  }
537
- function maximumArgumentCount(parameters) {
538
- 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);
539
726
  }
540
727
  function firstMissingRequiredParameter(parameters, argCount) {
541
728
  for (let index = argCount; index < parameters.length; index++) {
@@ -545,12 +732,6 @@ function firstMissingRequiredParameter(parameters, argCount) {
545
732
  }
546
733
  return parameters.find(parameter => !parameter.optional);
547
734
  }
548
- function parameterForArgument(parameters, index) {
549
- const variadicIndex = parameters.findIndex(parameter => parameter.variadic);
550
- if (variadicIndex >= 0 && index >= variadicIndex)
551
- return parameters[variadicIndex];
552
- return parameters[index];
553
- }
554
735
  function parameterNameForArgument(node, parameter, index) {
555
736
  const description = FUNCTION_DESCRIPTIONS[node.name];
556
737
  const variadicIndex = description?.parameters.findIndex(candidate => candidate.variadic) ?? -1;