no-new-func.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * @fileoverview Rule to flag when using new Function
  3. * @author Ilya Volodin
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. description: "disallow `new` operators with the `Function` object",
  13. category: "Best Practices",
  14. recommended: false,
  15. url: "https://eslint.org/docs/rules/no-new-func"
  16. },
  17. schema: []
  18. },
  19. create(context) {
  20. //--------------------------------------------------------------------------
  21. // Helpers
  22. //--------------------------------------------------------------------------
  23. /**
  24. * Reports a node.
  25. * @param {ASTNode} node The node to report
  26. * @returns {void}
  27. * @private
  28. */
  29. function report(node) {
  30. context.report({ node, message: "The Function constructor is eval." });
  31. }
  32. return {
  33. "NewExpression[callee.name = 'Function']": report,
  34. "CallExpression[callee.name = 'Function']": report
  35. };
  36. }
  37. };