Skip to content

Commit 12c4dc5

Browse files
CopilotDaanV2
andcommitted
Refactor: use repeatable flag on last parameter instead of minParams/repeatableParam fields
Co-authored-by: DaanV2 <[email protected]>
1 parent b8b406b commit 12c4dc5

File tree

3 files changed

+64
-177
lines changed

3 files changed

+64
-177
lines changed

packages/bedrock-diagnoser/src/diagnostics/molang/expressions.ts

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -224,70 +224,58 @@ export function diagnose_molang_function(fn: FunctionCallNode, diagnoser: Diagno
224224
}
225225

226226
if (fnData.parameters) {
227-
// Check if function supports variable arguments with minParams
228-
if (fnData.minParams !== undefined) {
229-
if (fn.arguments.length < fnData.minParams) {
227+
// Check if the last parameter is repeatable
228+
const lastParam = fnData.parameters[fnData.parameters.length - 1];
229+
const hasRepeatableParam = lastParam?.repeatable === true;
230+
const minRequiredParams = fnData.parameters.length;
231+
232+
// Validate parameter count
233+
if (hasRepeatableParam) {
234+
// With repeatable parameter, we need at least the minimum parameters
235+
if (fn.arguments.length < minRequiredParams) {
230236
diagnoser.add(
231237
OffsetWord.create(`${fn.scope}.${fn.names.join('.')}`, fn.position),
232-
`wrong amount of arguments, expected at least ${fnData.minParams} but got ${fn.arguments.length}`,
238+
`wrong amount of arguments, expected at least ${minRequiredParams} but got ${fn.arguments.length}`,
233239
DiagnosticSeverity.error,
234240
'molang.function.arguments',
235241
);
236242
}
237-
238-
// Validate parameter types
239-
for (let i = 0; i < fn.arguments.length; i++) {
240-
const arg = fn.arguments[i];
241-
let expectedParam: MolangParameter | undefined;
242-
243-
// Determine which parameter definition to use
244-
if (i < fnData.parameters.length) {
245-
// Use the fixed parameter definition
246-
expectedParam = fnData.parameters[i];
247-
} else if (fnData.repeatableParam) {
248-
// Use the repeatable parameter definition for additional args
249-
expectedParam = fnData.repeatableParam;
250-
}
251-
252-
// Validate type if specified
253-
if (expectedParam?.type) {
254-
const actualType = getArgumentType(arg);
255-
if (actualType && actualType !== expectedParam.type) {
256-
diagnoser.add(
257-
OffsetWord.create(`${fn.scope}.${fn.names.join('.')}`, fn.position),
258-
`wrong argument type at position ${i + 1}, expected ${expectedParam.type} but got ${actualType}`,
259-
DiagnosticSeverity.error,
260-
'molang.function.arguments.type',
261-
);
262-
}
263-
}
264-
}
265243
} else {
266-
// Check for exact parameter count
244+
// Without repeatable parameter, we need exact match
267245
if (fnData.parameters.length != fn.arguments.length) {
268246
diagnoser.add(
269247
OffsetWord.create(`${fn.scope}.${fn.names.join('.')}`, fn.position),
270248
`wrong amount of arguments, expected ${fnData.parameters.length} but got ${fn.arguments.length}`,
271249
DiagnosticSeverity.error,
272250
'molang.function.arguments',
273251
);
274-
} else {
275-
// Validate parameter types for exact match case
276-
for (let i = 0; i < fn.arguments.length; i++) {
277-
const arg = fn.arguments[i];
278-
const expectedParam = fnData.parameters[i];
279-
280-
if (expectedParam?.type) {
281-
const actualType = getArgumentType(arg);
282-
if (actualType && actualType !== expectedParam.type) {
283-
diagnoser.add(
284-
OffsetWord.create(`${fn.scope}.${fn.names.join('.')}`, fn.position),
285-
`wrong argument type at position ${i + 1}, expected ${expectedParam.type} but got ${actualType}`,
286-
DiagnosticSeverity.error,
287-
'molang.function.arguments.type',
288-
);
289-
}
290-
}
252+
}
253+
}
254+
255+
// Validate parameter types
256+
for (let i = 0; i < fn.arguments.length; i++) {
257+
const arg = fn.arguments[i];
258+
let expectedParam: MolangParameter | undefined;
259+
260+
// Determine which parameter definition to use
261+
if (i < fnData.parameters.length) {
262+
// Use the parameter at this index
263+
expectedParam = fnData.parameters[i];
264+
} else if (hasRepeatableParam) {
265+
// Use the last (repeatable) parameter for additional arguments
266+
expectedParam = lastParam;
267+
}
268+
269+
// Validate type if specified
270+
if (expectedParam?.type) {
271+
const actualType = getArgumentType(arg);
272+
if (actualType && actualType !== expectedParam.type) {
273+
diagnoser.add(
274+
OffsetWord.create(`${fn.scope}.${fn.names.join('.')}`, fn.position),
275+
`wrong argument type at position ${i + 1}, expected ${expectedParam.type} but got ${actualType}`,
276+
DiagnosticSeverity.error,
277+
'molang.function.arguments.type',
278+
);
291279
}
292280
}
293281
}

packages/molang/src/data/general.ts

Lines changed: 20 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -460,16 +460,11 @@ export namespace General {
460460
},
461461
{
462462
id: 'tag',
463-
documentation: 'The first tag',
463+
documentation: 'tag name to check',
464464
type: 'string',
465+
repeatable: true,
465466
},
466467
],
467-
minParams: 4,
468-
repeatableParam: {
469-
id: 'tag',
470-
documentation: 'Additional tag name',
471-
type: 'string',
472-
},
473468
},
474469
{
475470
id: 'block_has_any_tags',
@@ -493,16 +488,11 @@ export namespace General {
493488
},
494489
{
495490
id: 'tag',
496-
documentation: 'The first tag',
491+
documentation: 'tag name to check',
497492
type: 'string',
493+
repeatable: true,
498494
},
499495
],
500-
minParams: 4,
501-
repeatableParam: {
502-
id: 'tag',
503-
documentation: 'Additional tag name',
504-
type: 'string',
505-
},
506496
},
507497
{
508498
id: 'block_neighbor_has_all_tags',
@@ -526,16 +516,11 @@ export namespace General {
526516
},
527517
{
528518
id: 'tag',
529-
documentation: 'The first tag',
519+
documentation: 'tag name to check',
530520
type: 'string',
521+
repeatable: true,
531522
},
532523
],
533-
minParams: 4,
534-
repeatableParam: {
535-
id: 'tag',
536-
documentation: 'Additional tag name',
537-
type: 'string',
538-
},
539524
},
540525
{
541526
id: 'block_neighbor_has_any_tags',
@@ -559,16 +544,11 @@ export namespace General {
559544
},
560545
{
561546
id: 'tag',
562-
documentation: 'The first tag',
547+
documentation: 'tag name to check',
563548
type: 'string',
549+
repeatable: true,
564550
},
565551
],
566-
minParams: 4,
567-
repeatableParam: {
568-
id: 'tag',
569-
documentation: 'Additional tag name',
570-
type: 'string',
571-
},
572552
},
573553
{
574554
id: 'block_property',
@@ -773,29 +753,17 @@ export namespace General {
773753
"takes a slot name followed by any tag you want to check for in the form of 'tag_name' and returns 1 if all of the tags are on that equipped item, 0 otherwise",
774754
parameters: [
775755
{ id: 'slot_name', documentation: 'equipment slot name', type: 'string' },
776-
{ id: 'tag', documentation: 'tag name to check', type: 'string' },
756+
{ id: 'tag', documentation: 'tag name to check', type: 'string', repeatable: true },
777757
],
778-
minParams: 2,
779-
repeatableParam: {
780-
id: 'tag',
781-
documentation: 'Additional tag name',
782-
type: 'string',
783-
},
784758
},
785759
{
786760
id: 'equipped_item_any_tag',
787761
documentation:
788762
"takes a slot name followed by any tag you want to check for in the form of 'tag_name' and returns 0 if none of the tags are on that equipped item or 1 if at least 1 tag exists",
789763
parameters: [
790764
{ id: 'slot_name', documentation: 'equipment slot name', type: 'string' },
791-
{ id: 'tag', documentation: 'tag name to check', type: 'string' },
765+
{ id: 'tag', documentation: 'tag name to check', type: 'string', repeatable: true },
792766
],
793-
minParams: 2,
794-
repeatableParam: {
795-
id: 'tag',
796-
documentation: 'Additional tag name',
797-
type: 'string',
798-
},
799767
},
800768
{
801769
id: 'equipped_item_is_attachable',
@@ -844,14 +812,8 @@ export namespace General {
844812
documentation:
845813
"Takes in one or more arguments ('simple', 'fancy', 'deferred', 'raytraced'). If the graphics mode of the client matches any of the arguments, return 1.0. Available on the Client (Resource Packs) only.",
846814
parameters: [
847-
{ id: 'mode', documentation: "graphics mode ('simple', 'fancy', 'deferred', 'raytraced')", type: 'string' },
815+
{ id: 'mode', documentation: "graphics mode ('simple', 'fancy', 'deferred', 'raytraced')", type: 'string', repeatable: true },
848816
],
849-
minParams: 1,
850-
repeatableParam: {
851-
id: 'mode',
852-
documentation: 'Additional graphics mode',
853-
type: 'string',
854-
},
855817
},
856818
{
857819
id: 'ground_speed',
@@ -1193,14 +1155,8 @@ export namespace General {
11931155
parameters: [
11941156
{ id: 'slot_name', documentation: 'equipment slot name', type: 'string' },
11951157
{ id: 'slot_index', documentation: 'optional slot index value', type: 'float' },
1196-
{ id: 'item', documentation: 'item name to check', type: 'string' },
1158+
{ id: 'item', documentation: 'item name to check', type: 'string', repeatable: true },
11971159
],
1198-
minParams: 3,
1199-
repeatableParam: {
1200-
id: 'item',
1201-
documentation: 'Additional item name',
1202-
type: 'string',
1203-
},
12041160
},
12051161

12061162
{
@@ -1245,14 +1201,8 @@ export namespace General {
12451201
documentation:
12461202
"Takes one or more arguments. If the entity's name is any of the specified string values, returns 1.0. Otherwise returns 0.0. A preferred query to query.get_name, as it can be adjusted by Mojang to avoid breaking content if names are changed.",
12471203
parameters: [
1248-
{ id: 'name', documentation: 'possible entity name', type: 'string' },
1204+
{ id: 'name', documentation: 'possible entity name', type: 'string', repeatable: true },
12491205
],
1250-
minParams: 1,
1251-
repeatableParam: {
1252-
id: 'name',
1253-
documentation: 'Additional entity name',
1254-
type: 'string',
1255-
},
12561206
},
12571207
{
12581208
id: 'is_on_fire',
@@ -1280,14 +1230,8 @@ export namespace General {
12801230
documentation:
12811231
'Takes one or more arguments. Returns whether the root actor identifier is any of the specified strings. A preferred query to query.owner_identifier, as it can be adjusted by Mojang to avoid breaking content if names are changed.',
12821232
parameters: [
1283-
{ id: 'name', documentation: 'possible entity name', type: 'string' },
1233+
{ id: 'name', documentation: 'possible entity name', type: 'string', repeatable: true },
12841234
],
1285-
minParams: 1,
1286-
repeatableParam: {
1287-
id: 'name',
1288-
documentation: 'Additional entity name',
1289-
type: 'string',
1290-
},
12911235
},
12921236
{
12931237
id: 'is_persona_or_premium_skin',
@@ -1486,14 +1430,8 @@ export namespace General {
14861430
documentation:
14871431
"Takes one or more arguments ('keyboard_and_mouse', 'touch', or 'gamepad'). If the last input used is any of the specified string values, returns 1.0. Otherwise returns 0.0. Available on the Client (Resource Packs) only.",
14881432
parameters: [
1489-
{ id: 'mode', documentation: "input mode ('keyboard_and_mouse', 'touch', or 'gamepad')", type: 'string' },
1433+
{ id: 'mode', documentation: "input mode ('keyboard_and_mouse', 'touch', or 'gamepad')", type: 'string', repeatable: true },
14901434
],
1491-
minParams: 1,
1492-
repeatableParam: {
1493-
id: 'mode',
1494-
documentation: 'Additional input mode',
1495-
type: 'string',
1496-
},
14971435
},
14981436
{
14991437
id: 'lie_amount',
@@ -1625,14 +1563,8 @@ export namespace General {
16251563
{ id: 'x', documentation: 'entity-relative position on the x axis', type: 'float' },
16261564
{ id: 'y', documentation: 'entity-relative position on the y axis', type: 'float' },
16271565
{ id: 'z', documentation: 'entity-relative position on the z axis', type: 'float' },
1628-
{ id: 'tag', documentation: 'tag name to check', type: 'string' },
1566+
{ id: 'tag', documentation: 'tag name to check', type: 'string', repeatable: true },
16291567
],
1630-
minParams: 4,
1631-
repeatableParam: {
1632-
id: 'tag',
1633-
documentation: 'Additional tag name',
1634-
type: 'string',
1635-
},
16361568
},
16371569
{
16381570
id: 'relative_block_has_any_tag',
@@ -1642,14 +1574,8 @@ export namespace General {
16421574
{ id: 'x', documentation: 'entity-relative position on the x axis', type: 'float' },
16431575
{ id: 'y', documentation: 'entity-relative position on the y axis', type: 'float' },
16441576
{ id: 'z', documentation: 'entity-relative position on the z axis', type: 'float' },
1645-
{ id: 'tag', documentation: 'tag name to check', type: 'string' },
1577+
{ id: 'tag', documentation: 'tag name to check', type: 'string', repeatable: true },
16461578
],
1647-
minParams: 4,
1648-
repeatableParam: {
1649-
id: 'tag',
1650-
documentation: 'Additional tag name',
1651-
type: 'string',
1652-
},
16531579
},
16541580
{
16551581
id: 'ride_body_x_rotation',
@@ -1870,40 +1796,22 @@ export namespace General {
18701796
id: 'entity_biome_has_all_tags',
18711797
documentation: '(EXPERIMENTAL) Compares the biome the entity is standing in with one or more tag names, and returns either 0 or 1 based on if all of the tag names match. Only supported in resource packs (client-side).',
18721798
parameters: [
1873-
{ id: 'tag', documentation: 'biome tag name to check', type: 'string' },
1799+
{ id: 'tag', documentation: 'biome tag name to check', type: 'string', repeatable: true },
18741800
],
1875-
minParams: 1,
1876-
repeatableParam: {
1877-
id: 'tag',
1878-
documentation: 'Additional biome tag name',
1879-
type: 'string',
1880-
},
18811801
},
18821802
{
18831803
id: 'entity_biome_has_any_identifier',
18841804
documentation: '(EXPERIMENTAL) Compares the biome the entity is standing in with one or more identifier names, and returns either 0 or 1 based on if any of the identifier names match. Only supported in resource packs (client-side).',
18851805
parameters: [
1886-
{ id: 'identifier', documentation: 'biome identifier to check', type: 'string' },
1806+
{ id: 'identifier', documentation: 'biome identifier to check', type: 'string', repeatable: true },
18871807
],
1888-
minParams: 1,
1889-
repeatableParam: {
1890-
id: 'identifier',
1891-
documentation: 'Additional biome identifier',
1892-
type: 'string',
1893-
},
18941808
},
18951809
{
18961810
id: 'entity_biome_has_any_tags',
18971811
documentation: '(EXPERIMENTAL) Compares the biome the entity is standing in with one or more tag names, and returns either 0 or 1 based on if any of the tag names match. Only supported in resource packs (client-side).',
18981812
parameters: [
1899-
{ id: 'tag', documentation: 'biome tag name to check', type: 'string' },
1813+
{ id: 'tag', documentation: 'biome tag name to check', type: 'string', repeatable: true },
19001814
],
1901-
minParams: 1,
1902-
repeatableParam: {
1903-
id: 'tag',
1904-
documentation: 'Additional biome tag name',
1905-
type: 'string',
1906-
},
19071815
},
19081816
{
19091817
id: 'get_pack_setting',

0 commit comments

Comments
 (0)