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 7ea5e43

Browse filesBrowse files
TrottMylesBorins
authored andcommitted
tools: lint for alignment of variable assignments
Enforce alignment/indentation on variable assignments that span multiple lines. PR-URL: #6242 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Johan Bergström <bugs@bergstroem.nu> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 90c12a9 commit 7ea5e43
Copy full SHA for 7ea5e43

File tree

Expand file treeCollapse file tree

2 files changed

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

2 files changed

+69
-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
@@ -86,7 +86,7 @@ rules:
8686

8787
# Custom rules in tools/eslint-rules
8888
new-with-error: [2, "Error", "RangeError", "TypeError", "SyntaxError", "ReferenceError"]
89-
89+
align-multiline-assignment: 2
9090

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

0 commit comments

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