no-ex-assign.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * @fileoverview Rule to flag assignment of the exception parameter
  3. * @author Stephen Murray <spmurrayzzz>
  4. */
  5. "use strict";
  6. const astUtils = require("./utils/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. /** @type {import('../shared/types').Rule} */
  11. module.exports = {
  12. meta: {
  13. type: "problem",
  14. docs: {
  15. description: "Disallow reassigning exceptions in `catch` clauses",
  16. recommended: true,
  17. url: "https://eslint.org/docs/rules/no-ex-assign"
  18. },
  19. schema: [],
  20. messages: {
  21. unexpected: "Do not assign to the exception parameter."
  22. }
  23. },
  24. create(context) {
  25. /**
  26. * Finds and reports references that are non initializer and writable.
  27. * @param {Variable} variable A variable to check.
  28. * @returns {void}
  29. */
  30. function checkVariable(variable) {
  31. astUtils.getModifyingReferences(variable.references).forEach(reference => {
  32. context.report({ node: reference.identifier, messageId: "unexpected" });
  33. });
  34. }
  35. return {
  36. CatchClause(node) {
  37. context.getDeclaredVariables(node).forEach(checkVariable);
  38. }
  39. };
  40. }
  41. };