UNPKG

1.24 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 },
21
22 schema: []
23 },
24
25 create(context) {
26
27 /**
28 * Finds and reports references that are non initializer and writable.
29 * @param {Variable} variable - A variable to check.
30 * @returns {void}
31 */
32 function checkVariable(variable) {
33 astUtils.getModifyingReferences(variable.references).forEach(reference => {
34 context.report({ node: reference.identifier, message: "Do not assign to the exception parameter." });
35 });
36 }
37
38 return {
39 CatchClause(node) {
40 context.getDeclaredVariables(node).forEach(checkVariable);
41 }
42 };
43
44 }
45};