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