]> git.proxmox.com Git - pve-eslint.git/blobdiff - eslint/lib/rules/one-var.js
import 8.4.0 source
[pve-eslint.git] / eslint / lib / rules / one-var.js
index c31a0d2b13c187987a0478413d522824673713f2..1818c02e6e14156a5c42460fe7488299406e9277 100644 (file)
@@ -5,17 +5,36 @@
 
 "use strict";
 
+//------------------------------------------------------------------------------
+// Requirements
+//------------------------------------------------------------------------------
+
+const astUtils = require("./utils/ast-utils");
+
+//------------------------------------------------------------------------------
+// Helpers
+//------------------------------------------------------------------------------
+
+/**
+ * Determines whether the given node is in a statement list.
+ * @param {ASTNode} node node to check
+ * @returns {boolean} `true` if the given node is in a statement list
+ */
+function isInStatementList(node) {
+    return astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type);
+}
+
 //------------------------------------------------------------------------------
 // Rule Definition
 //------------------------------------------------------------------------------
 
+/** @type {import('../shared/types').Rule} */
 module.exports = {
     meta: {
         type: "suggestion",
 
         docs: {
             description: "enforce variables to be declared either together or separately in functions",
-            category: "Stylistic Issues",
             recommended: false,
             url: "https://eslint.org/docs/rules/one-var"
         },
@@ -190,7 +209,7 @@ module.exports = {
 
         /**
          * Determines the current scope (function or block)
-         * @param  {string} statementType node.kind, one of: "var", "let", or "const"
+         * @param {string} statementType node.kind, one of: "var", "let", or "const"
          * @returns {Object} The scope associated with statementType
          */
         function getCurrentScope(statementType) {
@@ -268,8 +287,8 @@ module.exports = {
 
         /**
          * Fixer to join VariableDeclaration's into a single declaration
-         * @param   {VariableDeclarator[]} declarations The `VariableDeclaration` to join
-         * @returns {Function}                         The fixer function
+         * @param {VariableDeclarator[]} declarations The `VariableDeclaration` to join
+         * @returns {Function} The fixer function
          */
         function joinDeclarations(declarations) {
             const declaration = declarations[0];
@@ -297,10 +316,17 @@ module.exports = {
 
         /**
          * Fixer to split a VariableDeclaration into individual declarations
-         * @param   {VariableDeclaration}   declaration The `VariableDeclaration` to split
-         * @returns {Function}                          The fixer function
+         * @param {VariableDeclaration} declaration The `VariableDeclaration` to split
+         * @returns {Function|null} The fixer function
          */
         function splitDeclarations(declaration) {
+            const { parent } = declaration;
+
+            // don't autofix code such as: if (foo) var x, y;
+            if (!isInStatementList(parent.type === "ExportNamedDeclaration" ? parent : declaration)) {
+                return null;
+            }
+
             return fixer => declaration.declarations.map(declarator => {
                 const tokenAfterDeclarator = sourceCode.getTokenAfter(declarator);
 
@@ -314,12 +340,14 @@ module.exports = {
                     return null;
                 }
 
+                const exportPlacement = declaration.parent.type === "ExportNamedDeclaration" ? "export " : "";
+
                 /*
                  * `var x,y`
                  * tokenAfterDeclarator ^^ afterComma
                  */
                 if (afterComma.range[0] === tokenAfterDeclarator.range[1]) {
-                    return fixer.replaceText(tokenAfterDeclarator, `; ${declaration.kind} `);
+                    return fixer.replaceText(tokenAfterDeclarator, `; ${exportPlacement}${declaration.kind} `);
                 }
 
                 /*
@@ -341,11 +369,11 @@ module.exports = {
 
                     return fixer.replaceTextRange(
                         [tokenAfterDeclarator.range[0], lastComment.range[0]],
-                        `;${sourceCode.text.slice(tokenAfterDeclarator.range[1], lastComment.range[0])}${declaration.kind} `
+                        `;${sourceCode.text.slice(tokenAfterDeclarator.range[1], lastComment.range[0])}${exportPlacement}${declaration.kind} `
                     );
                 }
 
-                return fixer.replaceText(tokenAfterDeclarator, `; ${declaration.kind}`);
+                return fixer.replaceText(tokenAfterDeclarator, `; ${exportPlacement}${declaration.kind}`);
             }).filter(x => x);
         }
 
@@ -514,6 +542,8 @@ module.exports = {
             FunctionDeclaration: startFunction,
             FunctionExpression: startFunction,
             ArrowFunctionExpression: startFunction,
+            StaticBlock: startFunction, // StaticBlock creates a new scope for `var` variables
+
             BlockStatement: startBlock,
             ForStatement: startBlock,
             ForInStatement: startBlock,
@@ -525,10 +555,12 @@ module.exports = {
             "ForInStatement:exit": endBlock,
             "SwitchStatement:exit": endBlock,
             "BlockStatement:exit": endBlock,
+
             "Program:exit": endFunction,
             "FunctionDeclaration:exit": endFunction,
             "FunctionExpression:exit": endFunction,
-            "ArrowFunctionExpression:exit": endFunction
+            "ArrowFunctionExpression:exit": endFunction,
+            "StaticBlock:exit": endFunction
         };
 
     }