Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX beta] Throw error if run.bind receives no method #16729

Merged
merged 1 commit into from
Jun 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/@ember/runloop/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,30 @@ export function join() {
@since 1.4.0
@public
*/
export const bind = (...curried) => (...args) => join(...curried.concat(args));
export const bind = (...curried) => {
assert(
'could not find a suitable method to bind',
(function(methodOrTarget, methodOrArg) {
// Applies the same logic as backburner parseArgs for detecting if a method
// is actually being passed.
let length = arguments.length;

if (length === 0) {
return false;
} else if (length === 1) {
return typeof methodOrTarget === 'function';
} else {
let type = typeof methodOrArg;
return (
type === 'function' || // second argument is a function
(methodOrTarget !== null && type === 'string' && methodOrArg in methodOrTarget) || // second argument is the name of a method in first argument
typeof methodOrTarget === 'function' //first argument is a function
);
}
})(...curried)
);
return (...args) => join(...curried.concat(args));
};

/**
Begins a new RunLoop. Any deferred actions invoked after the begin will
Expand Down
19 changes: 19 additions & 0 deletions packages/@ember/runloop/tests/run_bind_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,24 @@ moduleFor(

asyncFunction(bind(asyncCallback, asyncCallback, 1));
}

['@test [GH#16652] bind throws an error if callback is undefined']() {
let assertBindThrows = (msg, ...args) => {
expectAssertion(
function() {
bind(...args);
},
/could not find a suitable method to bind/,
msg
);
};
assertBindThrows('without arguments');
assertBindThrows('with one arguments that is not a function', 'myMethod');
assertBindThrows(
'if second parameter is not a function and not a property in first parameter',
Object.create(null),
'myMethod'
);
}
}
);