Skip to content

Commit

Permalink
tools: lint for alignment of variable assignments
Browse files Browse the repository at this point in the history
Enforce alignment/indentation on variable assignments that span multiple
lines.

PR-URL: #6869
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
  • Loading branch information
Trott authored and Myles Borins committed May 24, 2016
1 parent 23acece commit 146364b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rules:
no-duplicate-case: 2
no-empty-character-class: 2
no-ex-assign: 2
no-extra-boolean-cast : 2
no-extra-boolean-cast: 2
no-extra-parens: [2, "functions"]
no-extra-semi: 2
no-func-assign: 2
Expand Down Expand Up @@ -87,7 +87,7 @@ rules:
# Custom rules in tools/eslint-rules
assert-fail-single-argument: 2
new-with-error: [2, "Error", "RangeError", "TypeError", "SyntaxError", "ReferenceError"]

align-multiline-assignment: 2

# Global scoped method and vars
globals:
Expand Down
69 changes: 69 additions & 0 deletions tools/eslint-rules/align-multiline-assignment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @fileoverview Align multiline variable assignments
* @author Rich Trott
*/
'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
function getBinaryExpressionStarts(binaryExpression, starts) {
starts = 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 testDecleration(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) => testDecleration(context, node)
};
};

0 comments on commit 146364b

Please sign in to comment.