Skip to content

Commit 216486c

Browse files
TrottMyles Borins
authored and
Myles Borins
committed
tools: lint for function argument alignment
In function calls that span multiple lines, apply a custom lint rule to enforce argument alignment. With this rule, the following code will be flagged as an error by the linter because the arguments on the second line start in a different column than on the first line: myFunction(a, b, c, d); The following code will not be flagged as an error by the linter: myFunction(a, b, c, d); PR-URL: #7100 Refs: #6390 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Johan Bergström <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Imran Iqbal <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Ryan Graham <[email protected]>
1 parent 7e739ae commit 216486c

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ rules:
8585
prefer-const: 2
8686

8787
# Custom rules in tools/eslint-rules
88+
align-function-arguments: 2
89+
align-multiline-assignment: 2
8890
assert-fail-single-argument: 2
8991
new-with-error: [2, "Error", "RangeError", "TypeError", "SyntaxError", "ReferenceError"]
90-
align-multiline-assignment: 2
9192

9293
# Global scoped method and vars
9394
globals:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @fileoverview Align arguments in multiline function calls
3+
* @author Rich Trott
4+
*/
5+
'use strict';
6+
7+
//------------------------------------------------------------------------------
8+
// Rule Definition
9+
//------------------------------------------------------------------------------
10+
11+
function checkArgumentAlignment(context, node) {
12+
13+
function isNodeFirstInLine(node, byEndLocation) {
14+
const firstToken = byEndLocation === true ? context.getLastToken(node, 1) :
15+
context.getTokenBefore(node);
16+
const startLine = byEndLocation === true ? node.loc.end.line :
17+
node.loc.start.line;
18+
const endLine = firstToken ? firstToken.loc.end.line : -1;
19+
20+
return startLine !== endLine;
21+
}
22+
23+
if (node.arguments.length === 0)
24+
return;
25+
26+
var msg = '';
27+
const first = node.arguments[0];
28+
var currentLine = first.loc.start.line;
29+
const firstColumn = first.loc.start.column;
30+
31+
const ignoreTypes = [
32+
'ArrowFunctionExpression',
33+
'CallExpression',
34+
'FunctionExpression',
35+
'ObjectExpression',
36+
'TemplateLiteral'
37+
];
38+
39+
const args = node.arguments;
40+
41+
// For now, don't bother trying to validate potentially complicating things
42+
// like closures. Different people will have very different ideas and it's
43+
// probably best to implement configuration options.
44+
if (args.some((node) => { return ignoreTypes.indexOf(node.type) !== -1; })) {
45+
return;
46+
}
47+
48+
if (!isNodeFirstInLine(node)) {
49+
return;
50+
}
51+
52+
args.slice(1).forEach((argument) => {
53+
if (argument.loc.start.line === currentLine + 1) {
54+
if (argument.loc.start.column !== firstColumn) {
55+
msg = 'Function called with argument in column ' +
56+
`${argument.loc.start.column}, expected in ${firstColumn}`;
57+
}
58+
}
59+
currentLine = argument.loc.start.line;
60+
});
61+
62+
if (msg)
63+
context.report(node, msg);
64+
}
65+
66+
module.exports = function(context) {
67+
return {
68+
'CallExpression': (node) => checkArgumentAlignment(context, node)
69+
};
70+
};

0 commit comments

Comments
 (0)