-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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 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>
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 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,70 @@ | ||
/** | ||
* @fileoverview Align arguments in multiline function calls | ||
* @author Rich Trott | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
function checkArgumentAlignment(context, node) { | ||
|
||
function isNodeFirstInLine(node, byEndLocation) { | ||
const firstToken = byEndLocation === true ? context.getLastToken(node, 1) : | ||
context.getTokenBefore(node); | ||
const startLine = byEndLocation === true ? node.loc.end.line : | ||
node.loc.start.line; | ||
const endLine = firstToken ? firstToken.loc.end.line : -1; | ||
|
||
return startLine !== endLine; | ||
} | ||
|
||
if (node.arguments.length === 0) | ||
return; | ||
|
||
var msg = ''; | ||
const first = node.arguments[0]; | ||
var currentLine = first.loc.start.line; | ||
const firstColumn = first.loc.start.column; | ||
|
||
const ignoreTypes = [ | ||
'ArrowFunctionExpression', | ||
'CallExpression', | ||
'FunctionExpression', | ||
'ObjectExpression', | ||
'TemplateLiteral' | ||
]; | ||
|
||
const args = node.arguments; | ||
|
||
// For now, don't bother trying to validate potentially complicating things | ||
// like closures. Different people will have very different ideas and it's | ||
// probably best to implement configuration options. | ||
if (args.some((node) => { return ignoreTypes.indexOf(node.type) !== -1; })) { | ||
return; | ||
} | ||
|
||
if (!isNodeFirstInLine(node)) { | ||
return; | ||
} | ||
|
||
args.slice(1).forEach((argument) => { | ||
if (argument.loc.start.line === currentLine + 1) { | ||
if (argument.loc.start.column !== firstColumn) { | ||
msg = 'Function called with argument in column ' + | ||
`${argument.loc.start.column}, expected in ${firstColumn}`; | ||
} | ||
} | ||
currentLine = argument.loc.start.line; | ||
}); | ||
|
||
if (msg) | ||
context.report(node, msg); | ||
} | ||
|
||
module.exports = function(context) { | ||
return { | ||
'CallExpression': (node) => checkArgumentAlignment(context, node) | ||
}; | ||
}; |