You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-shadow-restricted-names.js 1.4 KiB

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. return {
  22. "VariableDeclaration, :function, CatchClause"(node) {
  23. for (const variable of context.getDeclaredVariables(node)) {
  24. if (variable.defs.length > 0 && RESTRICTED.includes(variable.name)) {
  25. context.report({
  26. node: variable.defs[0].name,
  27. message: "Shadowing of global property '{{idName}}'.",
  28. data: {
  29. idName: variable.name
  30. }
  31. });
  32. }
  33. }
  34. }
  35. };
  36. }
  37. };