Skip to content

Commit ce40f74

Browse files
authored
feat: update complexity rule to only highlight function header (#20048)
* feat: adjust highlighting to minimize visual noise for the 'complexity' rule Fixes #19984 * Add new test
1 parent e37e590 commit ce40f74

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/rules/complexity.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,23 @@ module.exports = {
171171

172172
if (complexity > threshold) {
173173
let name;
174+
let loc = node.loc;
174175

175176
if (codePath.origin === "class-field-initializer") {
176177
name = "class field initializer";
177178
} else if (codePath.origin === "class-static-block") {
178179
name = "class static block";
179180
} else {
180181
name = astUtils.getFunctionNameWithKind(node);
182+
loc = astUtils.getFunctionHeadLoc(
183+
node,
184+
context.sourceCode,
185+
);
181186
}
182187

183188
context.report({
184189
node,
190+
loc,
185191
messageId: "complex",
186192
data: {
187193
name: upperCaseFirst(name),

tests/lib/rules/complexity.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,17 @@ ruleTester.run("complexity", rule, {
318318
options: [0],
319319
errors: [makeError("Function 'a'", 1, 0)],
320320
},
321+
{
322+
code: "function foo(x) {if (x > 10) {return 'x is greater than 10';} else if (x > 5) {return 'x is greater than 5';} else {return 'x is less than 5';}}",
323+
options: [2],
324+
errors: [
325+
{
326+
...makeError("Function 'foo'", 3, 2),
327+
column: 1,
328+
endColumn: 13,
329+
},
330+
],
331+
},
321332
{
322333
code: "var func = function () {}",
323334
options: [0],
@@ -827,13 +838,38 @@ ruleTester.run("complexity", rule, {
827838
},
828839
],
829840
},
841+
{
842+
code: "class C { x = () => a || b || c; y = f || g || h; }",
843+
options: [2],
844+
languageOptions: { ecmaVersion: 2022 },
845+
errors: [
846+
{
847+
...makeError("Method 'x'", 3, 2),
848+
column: 11,
849+
endColumn: 15,
850+
},
851+
{
852+
...makeError("Class field initializer", 3, 2),
853+
column: 38,
854+
endColumn: 49,
855+
},
856+
],
857+
},
830858

831859
// object property options
832860
{
833861
code: "function a(x) {}",
834862
options: [{ max: 0 }],
835863
errors: [makeError("Function 'a'", 1, 0)],
836864
},
865+
{
866+
code: "const obj = { b: (a) => a?.b?.c, c: function (a) { return a?.b?.c; } };",
867+
options: [{ max: 2 }],
868+
errors: [
869+
{ ...makeError("Method 'b'", 3, 2), column: 15, endColumn: 18 },
870+
{ ...makeError("Method 'c'", 3, 2), column: 34, endColumn: 46 },
871+
],
872+
},
837873

838874
// optional chaining
839875
{
@@ -869,7 +905,13 @@ ruleTester.run("complexity", rule, {
869905
{
870906
code: "function a(b) { b?.c.d?.e; }",
871907
options: [{ max: 2 }],
872-
errors: [makeError("Function 'a'", 3, 2)],
908+
errors: [
909+
{
910+
...makeError("Function 'a'", 3, 2),
911+
column: 1,
912+
endColumn: 11,
913+
},
914+
],
873915
},
874916
{
875917
code: "function a(b) { b?.c?.(); }",

0 commit comments

Comments
 (0)