Skip to content

Commit

Permalink
Merge pull request #16 from canjs/fix-automatic-function-call-warning
Browse files Browse the repository at this point in the history
not warning when functions are not called due to proxyMethods=false
  • Loading branch information
phillipskevin committed Dec 1, 2017
2 parents 584c3ee + 0867f49 commit 274b2bc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
19 changes: 17 additions & 2 deletions can-stache-key-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,23 @@ testHelpers.dev.devOnlyTest("a warning is not displayed when functions are read
var reads = observeReader.reads("@func");

observeReader.read(data, reads, {
callMethodsOnObservables: true,
warnOnFunctionCall: "A Warning"
callMethodsOnObservables: true
});

QUnit.equal(teardown(), 0, "warning not displayed");
});

testHelpers.dev.devOnlyTest("a warning is not displayed when functions are read but not called due to proxyMethods=false (#15)", function() {
var teardown = testHelpers.dev.willWarn(/"func" is being called as a function/);
var func = function() {
QUnit.ok(false, "method not called");
};
var data = new SimpleMap({ func: func });
var reads = observeReader.reads("func");

observeReader.read(data, reads, {
isArgument: true,
proxyMethods: false
});

QUnit.equal(teardown(), 0, "warning not displayed");
Expand Down
35 changes: 27 additions & 8 deletions can-stache-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,41 @@ observeReader = {
}

//!steal-remove-start
dev.warn(
(options.filename ? options.filename + ':' : '') +
(options.lineNumber ? options.lineNumber + ': ' : '') +
'"' + reads[0].key + '" is being called as a function.\n' +
'\tThis will not happen automatically in an upcoming release.\n' +
'\tYou should call it explicitly using "' + reads[0].key + '()".\n\n'
);
var showWarning = function() {
dev.warn(
(options.filename ? options.filename + ':' : '') +
(options.lineNumber ? options.lineNumber + ': ' : '') +
'"' + reads[0].key + '" is being called as a function.\n' +
'\tThis will not happen automatically in an upcoming release.\n' +
'\tYou should call it explicitly using "' + reads[0].key + '()".\n\n'
);
};
//!steal-remove-end


if(options.callMethodsOnObservables && canReflect.isObservableLike(prev) && canReflect.isMapLike(prev)) {
//!steal-remove-start
showWarning();
//!steal-remove-end

return value.apply(prev, options.args || []);
}
else if ( options.isArgument && i === reads.length ) {
return options.proxyMethods !== false ? value.bind(prev) : value;
if (options.proxyMethods === false) {
return value;
}

//!steal-remove-start
showWarning();
//!steal-remove-end

return value.bind(prev);
}

//!steal-remove-start
showWarning();
//!steal-remove-end

return value.apply(prev, options.args || []);
}
},
Expand Down

0 comments on commit 274b2bc

Please sign in to comment.