no-shadow-restricted-names.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * @fileoverview Disallow shadowing of NaN, undefined, and Infinity (ES5 section 15.1.1)
  3. * @author Michael Ficarra
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. description: "disallow identifiers from shadowing restricted names",
  13. category: "Variables",
  14. recommended: false,
  15. url: "https://eslint.org/docs/rules/no-shadow-restricted-names"
  16. },
  17. schema: []
  18. },
  19. create(context) {
  20. const RESTRICTED = ["undefined", "NaN", "Infinity", "arguments", "eval"];
  21. /**
  22. * Check if the node name is present inside the restricted list
  23. * @param {ASTNode} id id to evaluate
  24. * @returns {void}
  25. * @private
  26. */
  27. function checkForViolation(id) {
  28. if (RESTRICTED.indexOf(id.name) > -1) {
  29. context.report({
  30. node: id,
  31. message: "Shadowing of global property '{{idName}}'.",
  32. data: {
  33. idName: id.name
  34. }
  35. });
  36. }
  37. }
  38. return {
  39. VariableDeclarator(node) {
  40. checkForViolation(node.id);
  41. },
  42. ArrowFunctionExpression(node) {
  43. [].map.call(node.params, checkForViolation);
  44. },
  45. FunctionExpression(node) {
  46. if (node.id) {
  47. checkForViolation(node.id);
  48. }
  49. [].map.call(node.params, checkForViolation);
  50. },
  51. FunctionDeclaration(node) {
  52. if (node.id) {
  53. checkForViolation(node.id);
  54. [].map.call(node.params, checkForViolation);
  55. }
  56. },
  57. CatchClause(node) {
  58. checkForViolation(node.param);
  59. }
  60. };
  61. }
  62. };