Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / one-var-declaration-per-line.js
1 /**
2  * @fileoverview Rule to check multiple var declarations per line
3  * @author Alberto Rodríguez
4  */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Rule Definition
9 //------------------------------------------------------------------------------
10
11 module.exports = {
12     meta: {
13         type: "suggestion",
14
15         docs: {
16             description: "require or disallow newlines around variable declarations",
17             category: "Stylistic Issues",
18             recommended: false,
19             url: "https://eslint.org/docs/rules/one-var-declaration-per-line"
20         },
21
22         schema: [
23             {
24                 enum: ["always", "initializations"]
25             }
26         ],
27
28         fixable: "whitespace"
29     },
30
31     create(context) {
32
33         const ERROR_MESSAGE = "Expected variable declaration to be on a new line.";
34         const always = context.options[0] === "always";
35
36         //--------------------------------------------------------------------------
37         // Helpers
38         //--------------------------------------------------------------------------
39
40
41         /**
42          * Determine if provided keyword is a variant of for specifiers
43          * @private
44          * @param {string} keyword keyword to test
45          * @returns {boolean} True if `keyword` is a variant of for specifier
46          */
47         function isForTypeSpecifier(keyword) {
48             return keyword === "ForStatement" || keyword === "ForInStatement" || keyword === "ForOfStatement";
49         }
50
51         /**
52          * Checks newlines around variable declarations.
53          * @private
54          * @param {ASTNode} node `VariableDeclaration` node to test
55          * @returns {void}
56          */
57         function checkForNewLine(node) {
58             if (isForTypeSpecifier(node.parent.type)) {
59                 return;
60             }
61
62             const declarations = node.declarations;
63             let prev;
64
65             declarations.forEach(current => {
66                 if (prev && prev.loc.end.line === current.loc.start.line) {
67                     if (always || prev.init || current.init) {
68                         context.report({
69                             node,
70                             message: ERROR_MESSAGE,
71                             loc: current.loc.start,
72                             fix: fixer => fixer.insertTextBefore(current, "\n")
73                         });
74                     }
75                 }
76                 prev = current;
77             });
78         }
79
80         //--------------------------------------------------------------------------
81         // Public
82         //--------------------------------------------------------------------------
83
84         return {
85             VariableDeclaration: checkForNewLine
86         };
87
88     }
89 };