Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

78 строки
2.4 KiB

  1. /**
  2. * @fileoverview Rule to flag variable leak in CatchClauses in IE 8 and earlier
  3. * @author Ian Christian Myers
  4. * @deprecated in ESLint v5.1.0
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Requirements
  9. //------------------------------------------------------------------------------
  10. const astUtils = require("../util/ast-utils");
  11. //------------------------------------------------------------------------------
  12. // Rule Definition
  13. //------------------------------------------------------------------------------
  14. module.exports = {
  15. meta: {
  16. docs: {
  17. description: "disallow `catch` clause parameters from shadowing variables in the outer scope",
  18. category: "Variables",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/no-catch-shadow",
  21. replacedBy: ["no-shadow"]
  22. },
  23. deprecated: true,
  24. schema: [],
  25. messages: {
  26. mutable: "Value of '{{name}}' may be overwritten in IE 8 and earlier."
  27. }
  28. },
  29. create(context) {
  30. //--------------------------------------------------------------------------
  31. // Helpers
  32. //--------------------------------------------------------------------------
  33. /**
  34. * Check if the parameters are been shadowed
  35. * @param {Object} scope current scope
  36. * @param {string} name parameter name
  37. * @returns {boolean} True is its been shadowed
  38. */
  39. function paramIsShadowing(scope, name) {
  40. return astUtils.getVariableByName(scope, name) !== null;
  41. }
  42. //--------------------------------------------------------------------------
  43. // Public API
  44. //--------------------------------------------------------------------------
  45. return {
  46. "CatchClause[param!=null]"(node) {
  47. let scope = context.getScope();
  48. /*
  49. * When ecmaVersion >= 6, CatchClause creates its own scope
  50. * so start from one upper scope to exclude the current node
  51. */
  52. if (scope.block === node) {
  53. scope = scope.upper;
  54. }
  55. if (paramIsShadowing(scope, node.param.name)) {
  56. context.report({ node, messageId: "mutable", data: { name: node.param.name } });
  57. }
  58. }
  59. };
  60. }
  61. };