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 3f608b1

Browse filesBrowse files
TrottFishrock123
authored andcommitted
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: #6390 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Johan Bergström <bugs@bergstroem.nu> Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: Imran Iqbal <imran@imraniqbal.org> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Ryan Graham <r.m.graham@gmail.com>
1 parent 6d1606e commit 3f608b1
Copy full SHA for 3f608b1

File tree

Expand file treeCollapse file tree

2 files changed

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

2 files changed

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

‎.eslintrc‎

Copy file name to clipboardExpand all lines: .eslintrc
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ rules:
8585
prefer-const: 2
8686

8787
# Custom rules in tools/eslint-rules
88+
align-function-arguments: 2
8889
align-multiline-assignment: 2
8990
assert-fail-single-argument: 2
9091
new-with-error: [2, "Error", "RangeError", "TypeError", "SyntaxError", "ReferenceError"]
Collapse file
+70Lines changed: 70 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.