Skip to content

Commit

Permalink
Fixed bug in checkArgs of jsdebug showing wrong error.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Joscha Rohmann committed Aug 18, 2016
1 parent b11a50d commit fbb8831
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion lib/blocks/jsdebug.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,17 @@ function checkArgsTypes(method, args) {
var maxOptionals = params.length - (params.length - getOptionalParamsCount(method.params));
var paramIndex = 0;
var passDetailValues = blocks.queries && blocks.queries[method.name] && blocks.queries[method.name].passDetailValues;
var mustSkipOptionals = args.length < params.length;
var currentErrors;
var param;
var nextParam;
var hasArguments = hasArgumentsParam(method);
var value;

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

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

if (param.optional) {
if (maxOptionals > 0) {
// 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.
// One exmple function is Application.View([string], string, object)
if (mustSkipOptionals && nextParam && !nextParam.optional && blocks.equals(param.types, nextParam.types, true)) {
paramIndex++;
param = nextParam;
}
currentErrors = checkType(param, value);
if (currentErrors.length === 0) {
maxOptionals -= 1;
if (!param.isArguments) {
paramIndex += 1;
}
} else if (mustSkipOptionals || hasArguments) {
currentErrors = [];
}
}
} else {
errors = errors.concat(checkType(param, value));
currentErrors = checkType(param, value);
if (!param.isArguments) {
paramIndex += 1;
}
}
errors = errors.concat(currentErrors);
}

return errors;
Expand Down Expand Up @@ -459,6 +472,15 @@ function checkType(param, value) {
} else {
continue;
}
// htmlelement type used in "blocks.query"
} else if (type == 'htmlelement') {
var element = blocks.$unwrap(unwrapedValue);
if (blocks.isElement(element)) {
satisfied = true;
break;
} else {
continue;
}
} else if (customTypes[type]) {
satisfied = customTypes[type](value);
if (satisfied) {
Expand Down Expand Up @@ -552,6 +574,16 @@ function params(method) {
}
}

function hasArgumentsParam(method) {
var params = method.params;
for (var i = 0; i < params.length; i++) {
if (params[i].isArguments) {
return true;
}
}
return false;
}

function ConsoleMessage() {
if (!ConsoleMessage.prototype.isPrototypeOf(this)) {
return new ConsoleMessage();
Expand Down

0 comments on commit fbb8831

Please sign in to comment.