2 * @fileoverview Rule to check multiple var declarations per line
3 * @author Alberto RodrÃguez
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
16 description: "require or disallow newlines around variable declarations",
17 category: "Stylistic Issues",
19 url: "https://eslint.org/docs/rules/one-var-declaration-per-line"
24 enum: ["always", "initializations"]
33 const ERROR_MESSAGE = "Expected variable declaration to be on a new line.";
34 const always = context.options[0] === "always";
36 //--------------------------------------------------------------------------
38 //--------------------------------------------------------------------------
42 * Determine if provided keyword is a variant of for specifiers
44 * @param {string} keyword keyword to test
45 * @returns {boolean} True if `keyword` is a variant of for specifier
47 function isForTypeSpecifier(keyword) {
48 return keyword === "ForStatement" || keyword === "ForInStatement" || keyword === "ForOfStatement";
52 * Checks newlines around variable declarations.
54 * @param {ASTNode} node `VariableDeclaration` node to test
57 function checkForNewLine(node) {
58 if (isForTypeSpecifier(node.parent.type)) {
62 const declarations = node.declarations;
65 declarations.forEach(current => {
66 if (prev && prev.loc.end.line === current.loc.start.line) {
67 if (always || prev.init || current.init) {
70 message: ERROR_MESSAGE,
71 loc: current.loc.start,
72 fix: fixer => fixer.insertTextBefore(current, "\n")
80 //--------------------------------------------------------------------------
82 //--------------------------------------------------------------------------
85 VariableDeclaration: checkForNewLine