node/tools/eslint/lib/rules/one-var-declaration-per-line.js
silverwind d9b8758f47 tools: update ESLint to 2.7.0
PR-URL: https://github.com/nodejs/node/pull/6132
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: thefourtheye <thechargingvolcano@gmail.com>
2016-04-20 16:24:21 -07:00

75 lines
2.2 KiB
JavaScript

/**
* @fileoverview Rule to check multiple var declarations per line
* @author Alberto Rodríguez
* @copyright 2016 Alberto Rodríguez. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
var ERROR_MESSAGE = "Expected variable declaration to be on a new line.";
var always = context.options[0] === "always";
//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------
/**
* Determine if provided keyword is a variant of for specifiers
* @private
* @param {string} keyword - keyword to test
* @returns {boolean} True if `keyword` is a variant of for specifier
*/
function isForTypeSpecifier(keyword) {
return keyword === "ForStatement" || keyword === "ForInStatement" || keyword === "ForOfStatement";
}
/**
* Checks newlines around variable declarations.
* @private
* @param {ASTNode} node - `VariableDeclaration` node to test
* @returns {void}
*/
function checkForNewLine(node) {
if (isForTypeSpecifier(node.parent.type)) {
return;
}
var declarations = node.declarations;
var prev;
declarations.forEach(function(current) {
if (prev && prev.loc.end.line === current.loc.start.line) {
if (always || prev.init || current.init) {
context.report({
node: node,
message: ERROR_MESSAGE,
loc: current.loc.start
});
}
}
prev = current;
});
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
return {
"VariableDeclaration": checkForNewLine
};
};
module.exports.schema = [
{
"enum": ["always", "initializations"]
}
];