UNPKG

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