From 535db474a1e6eb97e6c879c9ef58cf903131e80c Mon Sep 17 00:00:00 2001 From: vladimir-beloded Date: Thu, 16 Nov 2023 13:25:51 +0200 Subject: [PATCH 1/2] Ignore magic number in constructor initializer --- lib/lints/no_magic_number/no_magic_number_rule.dart | 7 ++++++- lint_test/no_magic_number_test.dart | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/lints/no_magic_number/no_magic_number_rule.dart b/lib/lints/no_magic_number/no_magic_number_rule.dart index 6f282425..4c80f7bf 100644 --- a/lib/lints/no_magic_number/no_magic_number_rule.dart +++ b/lib/lints/no_magic_number/no_magic_number_rule.dart @@ -70,7 +70,8 @@ class NoMagicNumberRule extends SolidLintRule { .where(_isNotInDateTime) .where(_isNotInsideIndexExpression) .where(_isNotInsideEnumConstantArguments) - .where(_isNotDefaultValue); + .where(_isNotDefaultValue) + .where(_isNotConstructorInitializer); for (final magicNumber in magicNumbers) { reporter.reportErrorForNode(code, magicNumber); @@ -127,4 +128,8 @@ class NoMagicNumberRule extends SolidLintRule { bool _isNotDefaultValue(Literal literal) { return literal.thisOrAncestorOfType() == null; } + + bool _isNotConstructorInitializer(Literal literal) { + return literal.thisOrAncestorOfType() == null; + } } diff --git a/lint_test/no_magic_number_test.dart b/lint_test/no_magic_number_test.dart index 239564a5..454ca326 100644 --- a/lint_test/no_magic_number_test.dart +++ b/lint_test/no_magic_number_test.dart @@ -79,3 +79,10 @@ class DefaultValues { void topLevelFunctionWithDefaultParam({int value = 6}) { ({int value = 7}) {}; } + +// Allowed for numbers in constructor initializer. +class ConstructorInitializer { + final int value; + + ConstructorInitializer() : value = 10; +} From 028b135e4451375e87db31cdd7681fc7473d68da Mon Sep 17 00:00:00 2001 From: vladimir-beloded Date: Thu, 16 Nov 2023 13:26:34 +0200 Subject: [PATCH 2/2] Minor naming improvement --- lib/lints/no_magic_number/no_magic_number_rule.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lints/no_magic_number/no_magic_number_rule.dart b/lib/lints/no_magic_number/no_magic_number_rule.dart index 4c80f7bf..be55635b 100644 --- a/lib/lints/no_magic_number/no_magic_number_rule.dart +++ b/lib/lints/no_magic_number/no_magic_number_rule.dart @@ -71,7 +71,7 @@ class NoMagicNumberRule extends SolidLintRule { .where(_isNotInsideIndexExpression) .where(_isNotInsideEnumConstantArguments) .where(_isNotDefaultValue) - .where(_isNotConstructorInitializer); + .where(_isNotInConstructorInitializer); for (final magicNumber in magicNumbers) { reporter.reportErrorForNode(code, magicNumber); @@ -129,7 +129,7 @@ class NoMagicNumberRule extends SolidLintRule { return literal.thisOrAncestorOfType() == null; } - bool _isNotConstructorInitializer(Literal literal) { + bool _isNotInConstructorInitializer(Literal literal) { return literal.thisOrAncestorOfType() == null; } }