google_forms/node_modules/eslint/lib/rules/no-catch-shadow.js

83 lines
2.5 KiB
JavaScript
Raw Normal View History

2024-08-09 12:04:48 +00:00
/**
* @fileoverview Rule to flag variable leak in CatchClauses in IE 8 and earlier
* @author Ian Christian Myers
* @deprecated in ESLint v5.1.0
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("./utils/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
2024-08-21 06:34:30 +00:00
/** @type {import('../shared/types').Rule} */
2024-08-09 12:04:48 +00:00
module.exports = {
meta: {
type: "suggestion",
docs: {
2024-08-21 06:34:30 +00:00
description: "Disallow `catch` clause parameters from shadowing variables in the outer scope",
2024-08-09 12:04:48 +00:00
recommended: false,
2024-08-21 06:34:30 +00:00
url: "https://eslint.org/docs/latest/rules/no-catch-shadow"
2024-08-09 12:04:48 +00:00
},
replacedBy: ["no-shadow"],
deprecated: true,
schema: [],
messages: {
mutable: "Value of '{{name}}' may be overwritten in IE 8 and earlier."
}
},
create(context) {
2024-08-21 06:34:30 +00:00
const sourceCode = context.sourceCode;
2024-08-09 12:04:48 +00:00
//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------
/**
* Check if the parameters are been shadowed
* @param {Object} scope current scope
* @param {string} name parameter name
* @returns {boolean} True is its been shadowed
*/
function paramIsShadowing(scope, name) {
return astUtils.getVariableByName(scope, name) !== null;
}
//--------------------------------------------------------------------------
// Public API
//--------------------------------------------------------------------------
return {
"CatchClause[param!=null]"(node) {
2024-08-21 06:34:30 +00:00
let scope = sourceCode.getScope(node);
2024-08-09 12:04:48 +00:00
/*
* When ecmaVersion >= 6, CatchClause creates its own scope
* so start from one upper scope to exclude the current node
*/
if (scope.block === node) {
scope = scope.upper;
}
if (paramIsShadowing(scope, node.param.name)) {
context.report({ node, messageId: "mutable", data: { name: node.param.name } });
}
}
};
}
};