no-shadow-restricted-names.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. * Determines if a variable safely shadows undefined.
  8. * This is the case when a variable named `undefined` is never assigned to a value (i.e. it always shares the same value
  9. * as the global).
  10. * @param {eslintScope.Variable} variable The variable to check
  11. * @returns {boolean} true if this variable safely shadows `undefined`
  12. */
  13. function safelyShadowsUndefined(variable) {
  14. return variable.name === "undefined" &&
  15. variable.references.every(ref => !ref.isWrite()) &&
  16. variable.defs.every(def => def.node.type === "VariableDeclarator" && def.node.init === null);
  17. }
  18. //------------------------------------------------------------------------------
  19. // Rule Definition
  20. //------------------------------------------------------------------------------
  21. module.exports = {
  22. meta: {
  23. type: "suggestion",
  24. docs: {
  25. description: "disallow identifiers from shadowing restricted names",
  26. category: "Variables",
  27. recommended: true,
  28. url: "https://eslint.org/docs/rules/no-shadow-restricted-names"
  29. },
  30. schema: [],
  31. messages: {
  32. shadowingRestrictedName: "Shadowing of global property '{{name}}'."
  33. }
  34. },
  35. create(context) {
  36. const RESTRICTED = new Set(["undefined", "NaN", "Infinity", "arguments", "eval"]);
  37. return {
  38. "VariableDeclaration, :function, CatchClause"(node) {
  39. for (const variable of context.getDeclaredVariables(node)) {
  40. if (variable.defs.length > 0 && RESTRICTED.has(variable.name) && !safelyShadowsUndefined(variable)) {
  41. context.report({
  42. node: variable.defs[0].name,
  43. messageId: "shadowingRestrictedName",
  44. data: {
  45. name: variable.name
  46. }
  47. });
  48. }
  49. }
  50. }
  51. };
  52. }
  53. };