Skip to content

Commit

Permalink
fix(eslint-plugin): [no-implied-eval] don't report when Function is…
Browse files Browse the repository at this point in the history
… imported (#2348)
  • Loading branch information
soobing committed Aug 2, 2020
1 parent 2027bb1 commit fa169e7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/eslint-plugin/src/rules/no-implied-eval.ts
Expand Up @@ -35,6 +35,7 @@ export default util.createRule({
defaultOptions: [],
create(context) {
const parserServices = util.getParserServices(context);
const program = parserServices.program;
const checker = parserServices.program.getTypeChecker();

function getCalleeName(
Expand Down Expand Up @@ -113,14 +114,29 @@ export default util.createRule({
function checkImpliedEval(
node: TSESTree.NewExpression | TSESTree.CallExpression,
): void {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node.callee);
const type = checker.getTypeAtLocation(tsNode);

const calleeName = getCalleeName(node.callee);
if (calleeName === null) {
return;
}

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

if (node.arguments.length === 0) {
Expand Down
3 changes: 3 additions & 0 deletions packages/eslint-plugin/tests/fixtures/class.ts
Expand Up @@ -8,3 +8,6 @@ export const console = { log() {} };
export class Reducable {
reduce() {}
}

// used by no-implied-eval test function imports
export class Function {}
5 changes: 5 additions & 0 deletions packages/eslint-plugin/tests/rules/no-implied-eval.test.ts
Expand Up @@ -6,6 +6,7 @@ const ruleTester = new RuleTester({
parserOptions: {
tsconfigRootDir: rootDir,
ecmaVersion: 2015,
sourceType: 'module',
project: './tsconfig.json',
},
parser: '@typescript-eslint/parser',
Expand Down Expand Up @@ -241,6 +242,10 @@ const fn = (foo: () => void) => {
execScript(foo);
};
`,
`
import { Function } from './class';
new Function('foo');
`,
],

invalid: [
Expand Down

0 comments on commit fa169e7

Please sign in to comment.