summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/no-catch-shadow.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eslint/lib/rules/no-catch-shadow.js')
-rw-r--r--tools/eslint/lib/rules/no-catch-shadow.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/tools/eslint/lib/rules/no-catch-shadow.js b/tools/eslint/lib/rules/no-catch-shadow.js
new file mode 100644
index 0000000000..af07c46292
--- /dev/null
+++ b/tools/eslint/lib/rules/no-catch-shadow.js
@@ -0,0 +1,50 @@
+/**
+ * @fileoverview Rule to flag variable leak in CatchClauses in IE 8 and earlier
+ * @author Ian Christian Myers
+ */
+
+"use strict";
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = function(context) {
+
+ //--------------------------------------------------------------------------
+ // Helpers
+ //--------------------------------------------------------------------------
+
+ function paramIsShadowing(scope, name) {
+ var found = scope.variables.some(function(variable) {
+ return variable.name === name;
+ });
+
+ if (found) {
+ return true;
+ }
+
+ if (scope.upper) {
+ return paramIsShadowing(scope.upper, name);
+ }
+
+ return false;
+ }
+
+ //--------------------------------------------------------------------------
+ // Public API
+ //--------------------------------------------------------------------------
+
+ return {
+
+ "CatchClause": function(node) {
+ var scope = context.getScope();
+
+ if (paramIsShadowing(scope, node.param.name)) {
+ context.report(node, "Value of '{{name}}' may be overwritten in IE 8 and earlier.",
+ { name: node.param.name });
+ }
+ }
+ };
+
+};