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] handle the Function type #2435

13 changes: 13 additions & 0 deletions packages/eslint-plugin/src/rules/no-implied-eval.ts
Expand Up @@ -80,6 +80,19 @@ export default util.createRule({
return true;
}

if (
symbol &&
tsutils.isSymbolFlagSet(
symbol,
ts.SymbolFlags.FunctionScopedVariable |
ts.SymbolFlags.Interface |
ts.SymbolFlags.Transient,
) &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you have any good idea to check symbol.flags === 33554497 please let me know. ✨

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better instead to just do something similar to what we've got for checking if the symbol is declared locally or not:

const declarations = symbol.getDeclarations() ?? [];
for (const declaration of declarations) {
const sourceFile = declaration.getSourceFile();
if (program.isSourceFileDefaultLibrary(sourceFile)) {
context.report({ node, messageId: 'noFunctionConstructor' });
return;
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bradzacher Thank you for review. I updated to check if the symbol is declared locally or not :)

symbol.escapedName === FUNCTION_CONSTRUCTOR
) {
return true;
}

const signatures = checker.getSignaturesOfType(
type,
ts.SignatureKind.Call,
Expand Down
5 changes: 5 additions & 0 deletions packages/eslint-plugin/tests/rules/no-implied-eval.test.ts
Expand Up @@ -246,6 +246,11 @@ const fn = (foo: () => void) => {
import { Function } from './class';
new Function('foo');
`,
`
const foo = (callback: Function) => {
setTimeout(callback, 0);
};
`,
],

invalid: [
Expand Down