-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools: lint for alignment of variable assignments
Enforce alignment/indentation on variable assignments that span multiple lines. PR-URL: #6869 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Johan Bergström <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Myles Borins <[email protected]>
- Loading branch information
Showing
2 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}; | ||
}; |