0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/tools/eslint-rules/align-multiline-assignment.js
Rich Trott c8e137b1b5 tools: remove default parameters from lint rule
The CI server uses system Node.js for linting, which is currently v5.x.
So default parameters are not supported there. This change removes the
default parameters.

PR-URL: https://github.com/nodejs/node/pull/6411
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-04-28 22:48:37 -07:00

69 lines
1.8 KiB
JavaScript

/**
* @fileoverview Align multiline variable assignments
* @author Rich Trott
*/
'use strict';
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
function getBinaryExpressionStarts(binaryExpression, starts) {
function getStartsFromOneSide(side, starts) {
starts.push(side.loc.start);
if (side.type === 'BinaryExpression') {
starts = getBinaryExpressionStarts(side, starts);
}
return starts;
}
starts = getStartsFromOneSide(binaryExpression.left, starts);
starts = getStartsFromOneSide(binaryExpression.right, starts);
return starts;
}
function checkExpressionAlignment(expression) {
if (!expression)
return;
var msg = '';
switch (expression.type) {
case 'BinaryExpression':
var starts = getBinaryExpressionStarts(expression, []);
var startLine = starts[0].line;
const startColumn = starts[0].column;
starts.forEach((loc) => {
if (loc.line > startLine) {
startLine = loc.line;
if (loc.column !== startColumn) {
msg = 'Misaligned multiline assignment';
}
}
});
break;
}
return msg;
}
function testAssignment(context, node) {
const msg = checkExpressionAlignment(node.right);
if (msg)
context.report(node, msg);
}
function testDeclaration(context, node) {
node.declarations.forEach((declaration) => {
const msg = checkExpressionAlignment(declaration.init);
// const start = declaration.init.loc.start;
if (msg)
context.report(node, msg);
});
}
module.exports = function(context) {
return {
'AssignmentExpression': (node) => testAssignment(context, node),
'VariableDeclaration': (node) => testDeclaration(context, node)
};
};