Skip to content

Commit

Permalink
fix(eslint-plugin): [no-implied-eval] handle bind on nested member ex…
Browse files Browse the repository at this point in the history
…pressions (#3598)
  • Loading branch information
a-tarasyuk committed Jul 31, 2021
1 parent 6e9796f commit f5a6806
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/eslint-plugin/src/rules/no-implied-eval.ts
Expand Up @@ -99,6 +99,12 @@ export default util.createRule({
return signatures.length > 0;
}

function isBind(node: TSESTree.Node): boolean {
return node.type === AST_NODE_TYPES.MemberExpression
? isBind(node.property)
: node.type === AST_NODE_TYPES.Identifier && node.name === 'bind';
}

function isFunction(node: TSESTree.Node): boolean {
switch (node.type) {
case AST_NODE_TYPES.ArrowFunctionExpression:
Expand All @@ -112,11 +118,7 @@ export default util.createRule({
return isFunctionType(node);

case AST_NODE_TYPES.CallExpression:
return (
(node.callee.type === AST_NODE_TYPES.Identifier &&
node.callee.name === 'bind') ||
isFunctionType(node)
);
return isBind(node.callee) || isFunctionType(node);

default:
return false;
Expand Down
20 changes: 20 additions & 0 deletions packages/eslint-plugin/tests/rules/no-implied-eval.test.ts
Expand Up @@ -257,6 +257,26 @@ const bar = () => {};
setTimeout(Math.radom() > 0.5 ? foo : bar, 0);
`,
`
class Foo {
func1() {}
func2(): void {
setTimeout(this.func1.bind(this), 1);
}
}
`,
`
class Foo {
private a = {
b: {
c: function () {},
},
};
funcw(): void {
setTimeout(this.a.b.c.bind(this), 1);
}
}
`,
],

invalid: [
Expand Down

0 comments on commit f5a6806

Please sign in to comment.