Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cc3645c

Browse filesBrowse files
TrottMyles Borins
authored andcommitted
tools: lint for alignment of variable assignments
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>
1 parent aaeeec4 commit cc3645c
Copy full SHA for cc3645c

File tree

Expand file treeCollapse file tree

2 files changed

+71
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+71
-2
lines changed
Open diff view settings
Collapse file

‎.eslintrc‎

Copy file name to clipboardExpand all lines: .eslintrc
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ rules:
1313
no-duplicate-case: 2
1414
no-empty-character-class: 2
1515
no-ex-assign: 2
16-
no-extra-boolean-cast : 2
16+
no-extra-boolean-cast: 2
1717
no-extra-parens: [2, "functions"]
1818
no-extra-semi: 2
1919
no-func-assign: 2
@@ -87,7 +87,7 @@ rules:
8787
# Custom rules in tools/eslint-rules
8888
assert-fail-single-argument: 2
8989
new-with-error: [2, "Error", "RangeError", "TypeError", "SyntaxError", "ReferenceError"]
90-
90+
align-multiline-assignment: 2
9191

9292
# Global scoped method and vars
9393
globals:
Collapse file
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @fileoverview Align multiline variable assignments
3+
* @author Rich Trott
4+
*/
5+
'use strict';
6+
7+
//------------------------------------------------------------------------------
8+
// Rule Definition
9+
//------------------------------------------------------------------------------
10+
function getBinaryExpressionStarts(binaryExpression, starts) {
11+
starts = starts || [];
12+
function getStartsFromOneSide(side, starts) {
13+
starts.push(side.loc.start);
14+
if (side.type === 'BinaryExpression') {
15+
starts = getBinaryExpressionStarts(side, starts);
16+
}
17+
return starts;
18+
}
19+
20+
starts = getStartsFromOneSide(binaryExpression.left, starts);
21+
starts = getStartsFromOneSide(binaryExpression.right, starts);
22+
return starts;
23+
}
24+
25+
function checkExpressionAlignment(expression) {
26+
if (!expression)
27+
return;
28+
29+
var msg = '';
30+
31+
switch (expression.type) {
32+
case 'BinaryExpression':
33+
var starts = getBinaryExpressionStarts(expression);
34+
var startLine = starts[0].line;
35+
const startColumn = starts[0].column;
36+
starts.forEach((loc) => {
37+
if (loc.line > startLine) {
38+
startLine = loc.line;
39+
if (loc.column !== startColumn) {
40+
msg = 'Misaligned multiline assignment';
41+
}
42+
}
43+
});
44+
break;
45+
}
46+
return msg;
47+
}
48+
49+
function testAssignment(context, node) {
50+
const msg = checkExpressionAlignment(node.right);
51+
if (msg)
52+
context.report(node, msg);
53+
}
54+
55+
function testDecleration(context, node) {
56+
node.declarations.forEach((declaration) => {
57+
const msg = checkExpressionAlignment(declaration.init);
58+
// const start = declaration.init.loc.start;
59+
if (msg)
60+
context.report(node, msg);
61+
});
62+
}
63+
64+
module.exports = function(context) {
65+
return {
66+
'AssignmentExpression': (node) => testAssignment(context, node),
67+
'VariableDeclaration': (node) => testDecleration(context, node)
68+
};
69+
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.