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

fix(eslint-plugin): [no-implied-eval] correct logic for ts3.8 #1652

Merged
merged 1 commit into from Feb 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 7 additions & 8 deletions packages/eslint-plugin/src/rules/no-implied-eval.ts
Expand Up @@ -3,6 +3,7 @@ import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import * as tsutils from 'tsutils';
import * as util from '../util';

const FUNCTION_CONSTRUCTOR = 'Function';
Expand Down Expand Up @@ -68,8 +69,11 @@ export default util.createRule({
const symbol = type.getSymbol();

if (
symbol?.flags === ts.SymbolFlags.Function ||
symbol?.flags === ts.SymbolFlags.Method
symbol &&
tsutils.isSymbolFlagSet(
symbol,
ts.SymbolFlags.Function | ts.SymbolFlags.Method,
)
) {
return true;
}
Expand All @@ -79,12 +83,7 @@ export default util.createRule({
ts.SignatureKind.Call,
);

if (signatures.length) {
const [{ declaration }] = signatures;
return declaration?.kind === ts.SyntaxKind.FunctionType;
}

return false;
return signatures.length > 0;
}

function isFunction(node: TSESTree.Node): boolean {
Expand Down
126 changes: 42 additions & 84 deletions packages/eslint-plugin/tests/rules/no-implied-eval.test.ts
Expand Up @@ -36,38 +36,31 @@ ruleTester.run('no-implied-eval', rule, {
`window.execScript(() => {});`,
`window['execScript'](() => {});`,

{
code: `
`
const foo = () => {};

setTimeout(foo, 0);
setInterval(foo, 0);
setImmediate(foo);
execScript(foo);
`,
},
{
code: `
`,
`
const foo = function () {};

setTimeout(foo, 0);
setInterval(foo, 0);
setImmediate(foo);
execScript(foo);
`,
},
{
code: `
`,
`
function foo() {};

setTimeout(foo, 0);
setInterval(foo, 0);
setImmediate(foo);
execScript(foo);
`,
},
{
code: `
`,
`
const foo = {
fn: () => {},
}
Expand All @@ -76,10 +69,8 @@ setTimeout(foo.fn, 0);
setInterval(foo.fn, 0);
setImmediate(foo.fn);
execScript(foo.fn);
`,
},
{
code: `
`,
`
const foo = {
fn: function () {},
}
Expand All @@ -88,10 +79,8 @@ setTimeout(foo.fn, 0);
setInterval(foo.fn, 0);
setImmediate(foo.fn);
execScript(foo.fn);
`,
},
{
code: `
`,
`
const foo = {
fn: function foo() {},
}
Expand All @@ -100,10 +89,8 @@ setTimeout(foo.fn, 0);
setInterval(foo.fn, 0);
setImmediate(foo.fn);
execScript(foo.fn);
`,
},
{
code: `
`,
`
const foo = {
fn() {},
}
Expand All @@ -112,10 +99,8 @@ setTimeout(foo.fn, 0);
setInterval(foo.fn, 0);
setImmediate(foo.fn);
execScript(foo.fn);
`,
},
{
code: `
`,
`
const foo = {
fn: () => {},
}
Expand All @@ -125,10 +110,8 @@ setTimeout(foo[fn], 0);
setInterval(foo[fn], 0);
setImmediate(foo[fn]);
execScript(foo[fn]);
`,
},
{
code: `
`,
`
const foo = {
fn: () => {},
}
Expand All @@ -137,21 +120,17 @@ setTimeout(foo['fn'], 0);
setInterval(foo['fn'], 0);
setImmediate(foo['fn']);
execScript(foo['fn']);
`,
},
{
code: `
`,
`
const foo: () => void = () => {
};

setTimeout(foo, 0);
setInterval(foo, 0);
setImmediate(foo);
execScript(foo);
`,
},
{
code: `
`,
`
const foo: (() => () => void) = () => {
return () => {};
}
Expand All @@ -160,30 +139,24 @@ setTimeout(foo(), 0);
setInterval(foo(), 0);
setImmediate(foo());
execScript(foo());
`,
},
{
code: `
`,
`
const foo: (() => () => void) = () => () => {};

setTimeout(foo(), 0);
setInterval(foo(), 0);
setImmediate(foo());
execScript(foo());
`,
},
{
code: `
`,
`
const foo = () => () => {};

setTimeout(foo(), 0);
setInterval(foo(), 0);
setImmediate(foo());
execScript(foo());
`,
},
{
code: `
`,
`
const foo = function foo () {
return function foo() {}
}
Expand All @@ -192,10 +165,8 @@ setTimeout(foo(), 0);
setInterval(foo(), 0);
setImmediate(foo());
execScript(foo());
`,
},
{
code: `
`,
`
const foo = function () {
return function () {
return '';
Expand All @@ -206,10 +177,8 @@ setTimeout(foo(), 0);
setInterval(foo(), 0);
setImmediate(foo());
execScript(foo());
`,
},
{
code: `
`,
`
const foo: (() => () => void) = function foo () {
return function foo() {}
}
Expand All @@ -218,10 +187,8 @@ setTimeout(foo(), 0);
setInterval(foo(), 0);
setImmediate(foo());
execScript(foo());
`,
},
{
code: `
`,
`
function foo() {
return function foo() {
return () => {};
Expand All @@ -232,10 +199,8 @@ setTimeout(foo()(), 0);
setInterval(foo()(), 0);
setImmediate(foo()());
execScript(foo()());
`,
},
{
code: `
`,
`
class Foo {
static fn = () => {};
}
Expand All @@ -244,10 +209,8 @@ setTimeout(Foo.fn, 0);
setInterval(Foo.fn, 0);
setImmediate(Foo.fn);
execScript(Foo.fn);
`,
},
{
code: `
`,
`
class Foo {
fn() {}
}
Expand All @@ -258,10 +221,8 @@ setTimeout(foo.fn, 0);
setInterval(foo.fn, 0);
setImmediate(foo.fn);
execScript(foo.fn);
`,
},
{
code: `
`,
`
class Foo {
fn() {}
}
Expand All @@ -272,18 +233,15 @@ setTimeout(fn.bind(null), 0);
setInterval(fn.bind(null), 0);
setImmediate(fn.bind(null));
execScript(fn.bind(null));
`,
},
{
code: `
`,
`
const fn = (foo: () => void) => {
setTimeout(foo, 0);
setInterval(foo, 0);
setImmediate(foo);
execScript(foo);
}
`,
},
`,
],

invalid: [
Expand Down