node/tools/eslint-rules/align-function-arguments.js
Rich Trott d73e189c26 tools,test: make argument linting more stringent
The custom linting rule for argument alignment in multi-line function
calls previously ignored template strings in an effort to avoid false
positives. This isn't really necessary. Enforce for template strings and
adjust whitespace in three tests to abide. (Insert "The test abides"
joke of your choosing here.)

PR-URL: https://github.com/nodejs/node/pull/6720
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2016-05-15 18:22:50 -07:00

70 lines
1.9 KiB
JavaScript

/**
* @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',
];
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)
};
};