no-shadow-restricted-names.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. },
  32. create(context) {
  33. const RESTRICTED = new Set(["undefined", "NaN", "Infinity", "arguments", "eval"]);
  34. return {
  35. "VariableDeclaration, :function, CatchClause"(node) {
  36. for (const variable of context.getDeclaredVariables(node)) {
  37. if (variable.defs.length > 0 && RESTRICTED.has(variable.name) && !safelyShadowsUndefined(variable)) {
  38. context.report({
  39. node: variable.defs[0].name,
  40. message: "Shadowing of global property '{{idName}}'.",
  41. data: {
  42. idName: variable.name
  43. }
  44. });
  45. }
  46. }
  47. }
  48. };
  49. }
  50. };