Skip to content

Commit fbb8831

Browse files
author
Joscha Rohmann
committed
Fixed bug in checkArgs of jsdebug showing wrong error.
Now optional arguments get skipped when there are less arguments than possible params. Example of the previous behaviour: Imagine a signature like that: testFunction([bool1: bool], bool2: bool, someString: String); And a call like this: ``testFunction(true, "test");`` We would have printed: "Arguments mismatch: testFunction([bool1], bool2, someString) - String specified where bool expected ..." with bool2 marked red. Now we're skipping the first argument and print nothing.
1 parent b11a50d commit fbb8831

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

lib/blocks/jsdebug.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,17 @@ function checkArgsTypes(method, args) {
391391
var maxOptionals = params.length - (params.length - getOptionalParamsCount(method.params));
392392
var paramIndex = 0;
393393
var passDetailValues = blocks.queries && blocks.queries[method.name] && blocks.queries[method.name].passDetailValues;
394+
var mustSkipOptionals = args.length < params.length;
394395
var currentErrors;
395396
var param;
397+
var nextParam;
398+
var hasArguments = hasArgumentsParam(method);
396399
var value;
397400

398401
for (var i = 0; i < args.length; i++) {
399402
param = params[paramIndex];
400403
value = args[i];
404+
nextParam = params[paramIndex + 1];
401405

402406
if (!param) {
403407
break;
@@ -409,20 +413,29 @@ function checkArgsTypes(method, args) {
409413

410414
if (param.optional) {
411415
if (maxOptionals > 0) {
416+
// Skips optional parameters, if it has the same type as the following parameter, not enough parameters are specified and the next param is not optional.
417+
// One exmple function is Application.View([string], string, object)
418+
if (mustSkipOptionals && nextParam && !nextParam.optional && blocks.equals(param.types, nextParam.types, true)) {
419+
paramIndex++;
420+
param = nextParam;
421+
}
412422
currentErrors = checkType(param, value);
413423
if (currentErrors.length === 0) {
414424
maxOptionals -= 1;
415425
if (!param.isArguments) {
416426
paramIndex += 1;
417427
}
428+
} else if (mustSkipOptionals || hasArguments) {
429+
currentErrors = [];
418430
}
419431
}
420432
} else {
421-
errors = errors.concat(checkType(param, value));
433+
currentErrors = checkType(param, value);
422434
if (!param.isArguments) {
423435
paramIndex += 1;
424436
}
425437
}
438+
errors = errors.concat(currentErrors);
426439
}
427440

428441
return errors;
@@ -459,6 +472,15 @@ function checkType(param, value) {
459472
} else {
460473
continue;
461474
}
475+
// htmlelement type used in "blocks.query"
476+
} else if (type == 'htmlelement') {
477+
var element = blocks.$unwrap(unwrapedValue);
478+
if (blocks.isElement(element)) {
479+
satisfied = true;
480+
break;
481+
} else {
482+
continue;
483+
}
462484
} else if (customTypes[type]) {
463485
satisfied = customTypes[type](value);
464486
if (satisfied) {
@@ -552,6 +574,16 @@ function params(method) {
552574
}
553575
}
554576

577+
function hasArgumentsParam(method) {
578+
var params = method.params;
579+
for (var i = 0; i < params.length; i++) {
580+
if (params[i].isArguments) {
581+
return true;
582+
}
583+
}
584+
return false;
585+
}
586+
555587
function ConsoleMessage() {
556588
if (!ConsoleMessage.prototype.isPrototypeOf(this)) {
557589
return new ConsoleMessage();

0 commit comments

Comments
 (0)