summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/no-var.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eslint/lib/rules/no-var.js')
-rw-r--r--tools/eslint/lib/rules/no-var.js11
1 files changed, 11 insertions, 0 deletions
diff --git a/tools/eslint/lib/rules/no-var.js b/tools/eslint/lib/rules/no-var.js
index c74e0b9ad9..d3c163e557 100644
--- a/tools/eslint/lib/rules/no-var.js
+++ b/tools/eslint/lib/rules/no-var.js
@@ -16,6 +16,15 @@ const astUtils = require("../ast-utils");
//------------------------------------------------------------------------------
/**
+ * Check whether a given variable is a global variable or not.
+ * @param {eslint-scope.Variable} variable The variable to check.
+ * @returns {boolean} `true` if the variable is a global variable.
+ */
+function isGlobal(variable) {
+ return Boolean(variable.scope) && variable.scope.type === "global";
+}
+
+/**
* Finds the nearest function scope or global scope walking up the scope
* hierarchy.
*
@@ -203,6 +212,7 @@ module.exports = {
* Checks whether it can fix a given variable declaration or not.
* It cannot fix if the following cases:
*
+ * - A variable is a global variable.
* - A variable is declared on a SwitchCase node.
* - A variable is redeclared.
* - A variable is used from outside the scope.
@@ -256,6 +266,7 @@ module.exports = {
if (node.parent.type === "SwitchCase" ||
node.declarations.some(hasSelfReferenceInTDZ) ||
+ variables.some(isGlobal) ||
variables.some(isRedeclared) ||
variables.some(isUsedFromOutsideOf(scopeNode))
) {