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"]
28 fixable: "whitespace",
31 expectVarOnNewline: "Expected variable declaration to be on a new line."
37 const always = context.options[0] === "always";
39 //--------------------------------------------------------------------------
41 //--------------------------------------------------------------------------
45 * Determine if provided keyword is a variant of for specifiers
47 * @param {string} keyword keyword to test
48 * @returns {boolean} True if `keyword` is a variant of for specifier
50 function isForTypeSpecifier(keyword) {
51 return keyword === "ForStatement" || keyword === "ForInStatement" || keyword === "ForOfStatement";
55 * Checks newlines around variable declarations.
57 * @param {ASTNode} node `VariableDeclaration` node to test
60 function checkForNewLine(node) {
61 if (isForTypeSpecifier(node.parent.type)) {
65 const declarations = node.declarations;
68 declarations.forEach(current => {
69 if (prev && prev.loc.end.line === current.loc.start.line) {
70 if (always || prev.init || current.init) {
73 messageId: "expectVarOnNewline",
75 fix: fixer => fixer.insertTextBefore(current, "\n")
83 //--------------------------------------------------------------------------
85 //--------------------------------------------------------------------------
88 VariableDeclaration: checkForNewLine