123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- "use strict";
- const astUtils = require("./utils/ast-utils");
- const candidatesOfGlobalObject = Object.freeze([
- "global",
- "window",
- "globalThis"
- ]);
- function isMember(node, name) {
- return astUtils.isSpecificMemberAccess(node, null, name);
- }
- module.exports = {
- meta: {
- type: "suggestion",
- defaultOptions: [{
- allowIndirect: false
- }],
- docs: {
- description: "Disallow the use of `eval()`",
- recommended: false,
- url: "https://eslint.org/docs/latest/rules/no-eval"
- },
- schema: [
- {
- type: "object",
- properties: {
- allowIndirect: { type: "boolean" }
- },
- additionalProperties: false
- }
- ],
- messages: {
- unexpected: "eval can be harmful."
- }
- },
- create(context) {
- const [{ allowIndirect }] = context.options;
- const sourceCode = context.sourceCode;
- let funcInfo = null;
-
- function enterThisScope(node) {
- const strict = sourceCode.getScope(node).isStrict;
- funcInfo = {
- upper: funcInfo,
- node,
- strict,
- isTopLevelOfScript: false,
- defaultThis: false,
- initialized: strict
- };
- }
-
- function exitThisScope() {
- funcInfo = funcInfo.upper;
- }
-
- function report(node) {
- const parent = node.parent;
- const locationNode = node.type === "MemberExpression"
- ? node.property
- : node;
- const reportNode = parent.type === "CallExpression" && parent.callee === node
- ? parent
- : node;
- context.report({
- node: reportNode,
- loc: locationNode.loc,
- messageId: "unexpected"
- });
- }
-
- function reportAccessingEvalViaGlobalObject(globalScope) {
- for (let i = 0; i < candidatesOfGlobalObject.length; ++i) {
- const name = candidatesOfGlobalObject[i];
- const variable = astUtils.getVariableByName(globalScope, name);
- if (!variable) {
- continue;
- }
- const references = variable.references;
- for (let j = 0; j < references.length; ++j) {
- const identifier = references[j].identifier;
- let node = identifier.parent;
-
- while (isMember(node, name)) {
- node = node.parent;
- }
-
- if (isMember(node, "eval")) {
- report(node);
- }
- }
- }
- }
-
- function reportAccessingEval(globalScope) {
- const variable = astUtils.getVariableByName(globalScope, "eval");
- if (!variable) {
- return;
- }
- const references = variable.references;
- for (let i = 0; i < references.length; ++i) {
- const reference = references[i];
- const id = reference.identifier;
- if (id.name === "eval" && !astUtils.isCallee(id)) {
-
- report(id);
- }
- }
- }
- if (allowIndirect) {
-
- return {
- "CallExpression:exit"(node) {
- const callee = node.callee;
-
- if (!node.optional && astUtils.isSpecificId(callee, "eval")) {
- report(callee);
- }
- }
- };
- }
- return {
- "CallExpression:exit"(node) {
- const callee = node.callee;
- if (astUtils.isSpecificId(callee, "eval")) {
- report(callee);
- }
- },
- Program(node) {
- const scope = sourceCode.getScope(node),
- features = context.parserOptions.ecmaFeatures || {},
- strict =
- scope.isStrict ||
- node.sourceType === "module" ||
- (features.globalReturn && scope.childScopes[0].isStrict),
- isTopLevelOfScript = node.sourceType !== "module" && !features.globalReturn;
- funcInfo = {
- upper: null,
- node,
- strict,
- isTopLevelOfScript,
- defaultThis: true,
- initialized: true
- };
- },
- "Program:exit"(node) {
- const globalScope = sourceCode.getScope(node);
- exitThisScope();
- reportAccessingEval(globalScope);
- reportAccessingEvalViaGlobalObject(globalScope);
- },
- FunctionDeclaration: enterThisScope,
- "FunctionDeclaration:exit": exitThisScope,
- FunctionExpression: enterThisScope,
- "FunctionExpression:exit": exitThisScope,
- "PropertyDefinition > *.value": enterThisScope,
- "PropertyDefinition > *.value:exit": exitThisScope,
- StaticBlock: enterThisScope,
- "StaticBlock:exit": exitThisScope,
- ThisExpression(node) {
- if (!isMember(node.parent, "eval")) {
- return;
- }
-
- if (!funcInfo.initialized) {
- funcInfo.initialized = true;
- funcInfo.defaultThis = astUtils.isDefaultThisBinding(
- funcInfo.node,
- sourceCode
- );
- }
-
- if (funcInfo.isTopLevelOfScript || (!funcInfo.strict && funcInfo.defaultThis)) {
-
- report(node.parent);
- }
- }
- };
- }
- };
|