2024-08-09 12:04:48 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Rule to flag when using new Function
|
|
|
|
* @author Ilya Volodin
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Requirements
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const astUtils = require("./utils/ast-utils");
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Helpers
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const callMethods = new Set(["apply", "bind", "call"]);
|
|
|
|
|
2024-08-09 12:04:48 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
/** @type {import('../shared/types').Rule} */
|
2024-08-09 12:04:48 +00:00
|
|
|
module.exports = {
|
|
|
|
meta: {
|
|
|
|
type: "suggestion",
|
|
|
|
|
|
|
|
docs: {
|
2024-08-21 06:34:30 +00:00
|
|
|
description: "Disallow `new` operators with the `Function` object",
|
2024-08-09 12:04:48 +00:00
|
|
|
recommended: false,
|
2024-08-21 06:34:30 +00:00
|
|
|
url: "https://eslint.org/docs/latest/rules/no-new-func"
|
2024-08-09 12:04:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
schema: [],
|
|
|
|
|
|
|
|
messages: {
|
|
|
|
noFunctionConstructor: "The Function constructor is eval."
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
create(context) {
|
2024-08-21 06:34:30 +00:00
|
|
|
const sourceCode = context.sourceCode;
|
2024-08-09 12:04:48 +00:00
|
|
|
|
|
|
|
return {
|
2024-08-21 06:34:30 +00:00
|
|
|
"Program:exit"(node) {
|
|
|
|
const globalScope = sourceCode.getScope(node);
|
2024-08-09 12:04:48 +00:00
|
|
|
const variable = globalScope.set.get("Function");
|
|
|
|
|
|
|
|
if (variable && variable.defs.length === 0) {
|
|
|
|
variable.references.forEach(ref => {
|
2024-08-21 06:34:30 +00:00
|
|
|
const idNode = ref.identifier;
|
|
|
|
const { parent } = idNode;
|
|
|
|
let evalNode;
|
|
|
|
|
|
|
|
if (parent) {
|
|
|
|
if (idNode === parent.callee && (
|
|
|
|
parent.type === "NewExpression" ||
|
|
|
|
parent.type === "CallExpression"
|
|
|
|
)) {
|
|
|
|
evalNode = parent;
|
|
|
|
} else if (
|
|
|
|
parent.type === "MemberExpression" &&
|
|
|
|
idNode === parent.object &&
|
|
|
|
callMethods.has(astUtils.getStaticPropertyName(parent))
|
|
|
|
) {
|
|
|
|
const maybeCallee = parent.parent.type === "ChainExpression" ? parent.parent : parent;
|
|
|
|
|
|
|
|
if (maybeCallee.parent.type === "CallExpression" && maybeCallee.parent.callee === maybeCallee) {
|
|
|
|
evalNode = maybeCallee.parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (evalNode) {
|
2024-08-09 12:04:48 +00:00
|
|
|
context.report({
|
2024-08-21 06:34:30 +00:00
|
|
|
node: evalNode,
|
2024-08-09 12:04:48 +00:00
|
|
|
messageId: "noFunctionConstructor"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|