项目原始demo,不改动
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.
 
 
 
 

75 строки
2.3 KiB

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