From 3f608b16a7d7036b0bfaa3fc93a7c0182b278581 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 18 Apr 2016 21:44:30 -0700 Subject: [PATCH] tools: lint for function argument alignment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/6390 Reviewed-By: James M Snell Reviewed-By: Johan Bergström Reviewed-By: Brian White Reviewed-By: Imran Iqbal Reviewed-By: Ben Noordhuis Reviewed-By: Ryan Graham --- .eslintrc | 1 + .../eslint-rules/align-function-arguments.js | 70 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 tools/eslint-rules/align-function-arguments.js diff --git a/.eslintrc b/.eslintrc index b48f2277c83212..542be9ceaa749d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -85,6 +85,7 @@ rules: prefer-const: 2 # Custom rules in tools/eslint-rules + align-function-arguments: 2 align-multiline-assignment: 2 assert-fail-single-argument: 2 new-with-error: [2, "Error", "RangeError", "TypeError", "SyntaxError", "ReferenceError"] diff --git a/tools/eslint-rules/align-function-arguments.js b/tools/eslint-rules/align-function-arguments.js new file mode 100644 index 00000000000000..a8cd71660d7d60 --- /dev/null +++ b/tools/eslint-rules/align-function-arguments.js @@ -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) + }; +};