From a5d7936a0a59c6a5aee99f9c459f936cb1aceb5a Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 13 Mar 2024 16:54:33 +0200 Subject: [PATCH] - Fixed function_lines_of_code for top level fumctions - Refactored tests for function_lines_of_code --- .../function_lines_of_code_metric.dart | 16 +- .../analysis_options.yaml | 11 + .../function_lines_of_code_test.dart | 41 ++++ lint_test/lines_of_code_test.dart | 191 ------------------ 4 files changed, 66 insertions(+), 193 deletions(-) create mode 100644 lint_test/function_lines_of_code_test/analysis_options.yaml create mode 100644 lint_test/function_lines_of_code_test/function_lines_of_code_test.dart delete mode 100644 lint_test/lines_of_code_test.dart diff --git a/lib/src/lints/function_lines_of_code/function_lines_of_code_metric.dart b/lib/src/lints/function_lines_of_code/function_lines_of_code_metric.dart index f8d1aaf1..6b0e09b7 100644 --- a/lib/src/lints/function_lines_of_code/function_lines_of_code_metric.dart +++ b/lib/src/lints/function_lines_of_code/function_lines_of_code_metric.dart @@ -51,6 +51,7 @@ class FunctionLinesOfCodeMetric void checkNode(AstNode node) => _checkNode(resolver, reporter, node); context.registry.addMethodDeclaration(checkNode); + context.registry.addFunctionDeclaration(checkNode); context.registry.addFunctionExpression(checkNode); } @@ -59,8 +60,19 @@ class FunctionLinesOfCodeMetric ErrorReporter reporter, AstNode node, ) { - if (node is MethodDeclaration && - config.parameters.excludeNames.contains(node.name.lexeme)) { + final name = () { + if (node is FunctionDeclaration) { + return node.name.lexeme; + } else if (node is MethodDeclaration) { + return node.name.lexeme; + } else if (node is FunctionExpression) { + return node.declaredElement?.name; + } else { + return null; + } + }(); + + if (name != null && config.parameters.excludeNames.contains(name)) { return; } diff --git a/lint_test/function_lines_of_code_test/analysis_options.yaml b/lint_test/function_lines_of_code_test/analysis_options.yaml new file mode 100644 index 00000000..51f88abf --- /dev/null +++ b/lint_test/function_lines_of_code_test/analysis_options.yaml @@ -0,0 +1,11 @@ +analyzer: + plugins: + - ../custom_lint + +custom_lint: + rules: + - function_lines_of_code: + max_lines: 5 + excludeNames: + - "longFunctionExcluded" + - "longMethodExcluded" diff --git a/lint_test/function_lines_of_code_test/function_lines_of_code_test.dart b/lint_test/function_lines_of_code_test/function_lines_of_code_test.dart new file mode 100644 index 00000000..1306873b --- /dev/null +++ b/lint_test/function_lines_of_code_test/function_lines_of_code_test.dart @@ -0,0 +1,41 @@ +class ClassWithLongMethods { + // expect_lint: function_lines_of_code + int longMethod() { + var i = 0; + i++; + i++; + i++; + i++; + return i; + } + + // Excluded by excludeNames + int longMethodExcluded() { + var i = 0; + i++; + i++; + i++; + i++; + return i; + } +} + +// expect_lint: function_lines_of_code +int longFunction() { + var i = 0; + i++; + i++; + i++; + i++; + return i; +} + +// Excluded by excludeNames +int longFunctionExcluded() { + var i = 0; + i++; + i++; + i++; + i++; + return i; +} diff --git a/lint_test/lines_of_code_test.dart b/lint_test/lines_of_code_test.dart deleted file mode 100644 index 8afc9c61..00000000 --- a/lint_test/lines_of_code_test.dart +++ /dev/null @@ -1,191 +0,0 @@ -// ignore_for_file: no_magic_number, prefer_match_file_name - -import 'package:test/test.dart'; - -/// Check number of lines fail -/// -/// `function_lines_of_code: max_lines` -/// expect_lint: function_lines_of_code -void linesOfCode() { - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); -} - -class A { - /// expect_lint: function_lines_of_code - void b() { - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - } -} - -class B { - /// expect_lint: function_lines_of_code - void anon() { - /// expect_lint: function_lines_of_code - test("addition", () { - expect(1 + 1, equals(2)); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - test("addition", () { - expect(1 + 1, equals(2)); - }); - }); - } - - void m() { - test("addition", () { - expect(1 + 1, equals(2)); - }); - } -}