From b61d2cd6992826f51cd29e3b793854696c73ca90 Mon Sep 17 00:00:00 2001 From: Ian Obermiller Date: Fri, 31 Aug 2018 15:34:05 -0700 Subject: [PATCH] Update: max-params to only highlight function header (#10815) Update the max-params rule to only report the location of the method header. Some tools, like Nuclide, will otherwise highlight the entire body of the method, which makes the rule visually noisy in practice. Test plan: npm test --- lib/rules/max-params.js | 3 ++- tests/lib/rules/max-params.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/rules/max-params.js b/lib/rules/max-params.js index 902391b74ff..4089e8ae899 100644 --- a/lib/rules/max-params.js +++ b/lib/rules/max-params.js @@ -53,7 +53,7 @@ module.exports = { }, create(context) { - + const sourceCode = context.getSourceCode(); const option = context.options[0]; let numParams = 3; @@ -76,6 +76,7 @@ module.exports = { function checkFunction(node) { if (node.params.length > numParams) { context.report({ + loc: astUtils.getFunctionHeadLoc(node, sourceCode), node, message: "{{name}} has too many parameters ({{count}}). Maximum allowed is {{max}}.", data: { diff --git a/tests/lib/rules/max-params.js b/tests/lib/rules/max-params.js index 3cb47044f97..959f5d7b8e2 100644 --- a/tests/lib/rules/max-params.js +++ b/tests/lib/rules/max-params.js @@ -86,6 +86,20 @@ ruleTester.run("max-params", rule, { message: "Function 'test' has too many parameters (3). Maximum allowed is 2.", type: "FunctionDeclaration" }] + }, + + // Error location should not cover the entire function; just the name. + { + code: `function test(a, b, c) { + // Just to make it longer + }`, + options: [{ max: 2 }], + errors: [{ + line: 1, + column: 1, + endLine: 1, + endColumn: 14 + }] } ] });