|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
-
-
- "use strict";
-
-
-
-
-
- module.exports = {
- meta: {
- type: "suggestion",
-
- docs: {
- description: "disallow the use of `eval()`-like methods",
- category: "Best Practices",
- recommended: false,
- url: "https://eslint.org/docs/rules/no-implied-eval"
- },
-
- schema: []
- },
-
- create(context) {
- const CALLEE_RE = /^(setTimeout|setInterval|execScript)$/;
-
-
-
- const impliedEvalAncestorsStack = [];
-
-
-
-
-
-
-
- function last(arr) {
- return arr ? arr[arr.length - 1] : null;
- }
-
-
-
- function isImpliedEvalMemberExpression(node) {
- const object = node.object,
- property = node.property,
- hasImpliedEvalName = CALLEE_RE.test(property.name) || CALLEE_RE.test(property.value);
-
- return object.name === "window" && hasImpliedEvalName;
- }
-
-
-
- function isImpliedEvalCallExpression(node) {
- const isMemberExpression = (node.callee.type === "MemberExpression"),
- isIdentifier = (node.callee.type === "Identifier"),
- isImpliedEvalCallee =
- (isIdentifier && CALLEE_RE.test(node.callee.name)) ||
- (isMemberExpression && isImpliedEvalMemberExpression(node.callee));
-
- return isImpliedEvalCallee && node.arguments.length;
- }
-
-
-
- function hasImpliedEvalParent(node) {
-
-
- return node.parent === last(last(impliedEvalAncestorsStack)) &&
-
-
- (node.parent.type !== "CallExpression" || node === node.parent.arguments[0]);
- }
-
-
-
- function checkString(node) {
- if (hasImpliedEvalParent(node)) {
-
-
- const substack = impliedEvalAncestorsStack.pop();
-
- context.report({ node: substack[0], message: "Implied eval. Consider passing a function instead of a string." });
- }
- }
-
-
-
-
-
- return {
- CallExpression(node) {
- if (isImpliedEvalCallExpression(node)) {
-
-
- impliedEvalAncestorsStack.push([node]);
- }
- },
-
- "CallExpression:exit"(node) {
- if (node === last(last(impliedEvalAncestorsStack))) {
-
-
-
- impliedEvalAncestorsStack.pop();
- }
- },
-
- BinaryExpression(node) {
- if (node.operator === "+" && hasImpliedEvalParent(node)) {
- last(impliedEvalAncestorsStack).push(node);
- }
- },
-
- "BinaryExpression:exit"(node) {
- if (node === last(last(impliedEvalAncestorsStack))) {
- last(impliedEvalAncestorsStack).pop();
- }
- },
-
- Literal(node) {
- if (typeof node.value === "string") {
- checkString(node);
- }
- },
-
- TemplateLiteral(node) {
- checkString(node);
- }
- };
-
- }
- };
|