Skip to content

Commit 0067bfe

Browse files
Update documentation for custom lints: add descriptions, examples (#102)
* Update avoid_using_api doc * Update avoid-global-state doc * Update avoid_late doc * Update avoid_non_null_assertion docs * Update avoid_returning_widget doc * Update avoid_unnecessary_setstate doc * Update avoid_unnecessary_type_assertions doc * Add example to avoid_unused_parameters doc * Update cyclomatic_complexity doc * Update double_literal_format doc * Update function_lines_of_code doc * Update member_ordering doc * Update newline_before_return doc * Update no_empty_block doc * Update no_equal_then_else doc * Update no_magic_number doc * Update number_of_parameters doc * Update prefer_conditional_expressions doc * Update prefer_first doc * Update prefer_last doc * Add example for prefer_match_file_name * Update proper_super_calls doc * Update README * Add config examples to parametrised lints * Update README * typos * consistent markdown headers for examples * more header fixes
1 parent bdb895a commit 0067bfe

File tree

32 files changed

+1707
-114
lines changed

32 files changed

+1707
-114
lines changed

README.md

Lines changed: 842 additions & 47 deletions
Large diffs are not rendered by default.

lib/lints/avoid_global_state/avoid_global_state_rule.dart

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,37 @@ import 'package:custom_lint_builder/custom_lint_builder.dart';
44
import 'package:solid_lints/models/rule_config.dart';
55
import 'package:solid_lints/models/solid_lint_rule.dart';
66

7-
/// A global state rule which forbids using variables
8-
/// that can be globally modified.
7+
/// Avoid top-level and static mutable variables.
8+
///
9+
/// Top-level variables can be modified from anywhere,
10+
/// which leads to hard to debug applications.
11+
///
12+
/// Prefer using a state management solution.
13+
///
14+
/// ### Example
15+
///
16+
/// #### BAD:
17+
///
18+
/// ```dart
19+
/// var globalMutable = 0; // LINT
20+
///
21+
/// class Test {
22+
/// static int globalMutable = 0; // LINT
23+
/// }
24+
/// ```
25+
///
26+
///
27+
/// #### GOOD:
28+
///
29+
/// ```dart
30+
/// final globalFinal = 1;
31+
/// const globalConst = 1;
32+
///
33+
/// class Test {
34+
/// static const int globalConst = 1;
35+
/// static final int globalFinal = 1;
36+
/// }
37+
/// ```
938
class AvoidGlobalStateRule extends SolidLintRule {
1039
/// The [LintCode] of this lint rule that represents
1140
/// the error whether we use global state.

lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,47 @@ import 'package:solid_lints/models/rule_config.dart';
66
import 'package:solid_lints/models/solid_lint_rule.dart';
77
import 'package:solid_lints/utils/types_utils.dart';
88

9-
/// A `late` keyword rule which forbids using it to avoid runtime exceptions.
9+
/// Avoid `late` keyword
10+
///
11+
/// Using `late` disables compile time safety for what would else be a nullable
12+
/// variable. Instead, a runtime check is made, which may throw an unexpected
13+
/// exception for an uninitialized variable.
14+
///
15+
/// ### Example config:
16+
///
17+
/// ```yaml
18+
/// custom_lint:
19+
/// rules:
20+
/// - avoid_late_keyword:
21+
/// allow_initialized: false
22+
/// ignored_types:
23+
/// - AnimationController
24+
/// - ColorTween
25+
/// ```
26+
///
27+
/// ### Example
28+
///
29+
/// #### BAD:
30+
/// ```dart
31+
/// class AvoidLateKeyword {
32+
/// late final int field; // LINT
33+
///
34+
/// void test() {
35+
/// late final local = ''; // LINT
36+
/// }
37+
/// }
38+
/// ```
39+
///
40+
/// #### GOOD:
41+
/// ```dart
42+
/// class AvoidLateKeyword {
43+
/// final int? field; // LINT
44+
///
45+
/// void test() {
46+
/// final local = ''; // LINT
47+
/// }
48+
/// }
49+
/// ```
1050
class AvoidLateKeywordRule extends SolidLintRule<AvoidLateKeywordParameters> {
1151
/// The [LintCode] of this lint rule that represents
1252
/// the error whether we use `late` keyword.

lib/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// A data model class that represents the "avoid late keyword" input
22
/// parameters.
33
class AvoidLateKeywordParameters {
4-
/// Allow immediately initialised late variables.
4+
/// Allow immediately initialized late variables.
55
///
66
/// ```dart
77
/// late var ok = 0; // ok when allowInitialized == true
@@ -10,6 +10,21 @@ class AvoidLateKeywordParameters {
1010
final bool allowInitialized;
1111

1212
/// Types that would be ignored by avoid-late rule
13+
///
14+
/// Example:
15+
///
16+
/// ```yaml
17+
/// custom_lint:
18+
/// rules:
19+
/// - avoid_late_keyword:
20+
/// ignored_types:
21+
/// - ColorTween
22+
/// ```
23+
///
24+
/// ```dart
25+
/// late ColorTween tween; // OK
26+
/// late int colorValue; // LINT
27+
/// ```
1328
final Iterable<String> ignoredTypes;
1429

1530
/// Constructor for [AvoidLateKeywordParameters] model

lib/lints/avoid_non_null_assertion/avoid_non_null_assertion_rule.dart

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,37 @@ import 'package:custom_lint_builder/custom_lint_builder.dart';
66
import 'package:solid_lints/models/rule_config.dart';
77
import 'package:solid_lints/models/solid_lint_rule.dart';
88

9-
/// Rule which forbids using bang operator ("!")
10-
/// as it may result in runtime exceptions.
9+
/// Rule which warns about usages of bang operator ("!")
10+
/// as it may result in unexpected runtime exceptions.
11+
///
12+
/// "Bang" operator with Maps is allowed, as [Dart docs](https://dart.dev/null-safety/understanding-null-safety#the-map-index-operator-is-nullable)
13+
/// recommend using it for accessing Map values that are known to be present.
14+
///
15+
/// ### Example
16+
/// #### BAD:
17+
///
18+
/// ```dart
19+
/// Object? object;
20+
/// int? number;
21+
///
22+
/// final int computed = 1 + number!; // LINT
23+
/// object!.method(); // LINT
24+
/// ```
25+
///
26+
/// #### GOOD:
27+
/// ```dart
28+
/// Object? object;
29+
/// int? number;
30+
///
31+
/// if (number != null) {
32+
/// final int computed = 1 + number;
33+
/// }
34+
/// object?.method();
35+
///
36+
/// // No lint on maps
37+
/// final map = {'key': 'value'};
38+
/// map['key']!;
39+
/// ```
1140
class AvoidNonNullAssertionRule extends SolidLintRule {
1241
/// The [LintCode] of this lint rule that represents
1342
/// the error whether we use bang operator.

lib/lints/avoid_returning_widgets/avoid_returning_widgets_rule.dart

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,37 @@ import 'package:solid_lints/models/rule_config.dart';
55
import 'package:solid_lints/models/solid_lint_rule.dart';
66
import 'package:solid_lints/utils/types_utils.dart';
77

8-
/// A rule which forbids returning widgets from functions and methods.
8+
/// A rule which warns about returning widgets from functions and methods.
9+
///
10+
/// Using functions instead of Widget subclasses for decomposing Widget trees
11+
/// may cause unexpected behavior and performance issues.
12+
///
13+
/// More details: https://github.com/flutter/flutter/issues/19269
14+
///
15+
/// ### Example
16+
///
17+
/// #### BAD:
18+
///
19+
/// ```dart
20+
/// Widget avoidReturningWidgets() => const SizedBox(); // LINT
21+
///
22+
/// class MyWidget extends StatelessWidget {
23+
/// Widget _test1() => const SizedBox(); // LINT
24+
/// Widget get _test3 => const SizedBox(); // LINT
25+
/// }
26+
/// ```
27+
///
28+
///
29+
/// #### GOOD:
30+
///
31+
/// ```dart
32+
/// class MyWidget extends StatelessWidget {
33+
/// @override
34+
/// Widget build(BuildContext context) {
35+
/// return const SizedBox();
36+
/// }
37+
/// }
38+
/// ```
939
class AvoidReturningWidgetsRule extends SolidLintRule {
1040
/// The [LintCode] of this lint rule that represents
1141
/// the error whether we return a widget.

lib/lints/avoid_unnecessary_setstate/avoid_unnecessary_set_state_rule.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,27 @@ import 'package:solid_lints/models/solid_lint_rule.dart';
2121
/// - async methods
2222
/// - callbacks
2323
///
24-
/// Example:
24+
/// ### Example:
25+
/// #### BAD:
2526
/// ```dart
2627
/// void initState() {
2728
/// setState(() => foo = 'bar'); // lint
2829
/// changeState(); // lint
30+
/// }
31+
///
32+
/// void changeState() {
33+
/// setState(() => foo = 'bar');
34+
/// }
35+
///
36+
/// void triggerFetch() async {
37+
/// await fetch();
38+
/// if (mounted) setState(() => foo = 'bar');
39+
/// }
40+
/// ```
41+
///
42+
/// #### GOOD:
43+
/// ```dart
44+
/// void initState() {
2945
/// triggerFetch(); // OK
3046
/// stream.listen((event) => setState(() => foo = event)); // OK
3147
/// }

lib/lints/avoid_unnecessary_type_assertions/avoid_unnecessary_type_assertions_rule.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,29 @@ const operatorIsName = 'is';
1717
/// The name of 'whereType' method
1818
const whereTypeMethodName = 'whereType';
1919

20-
/// An `avoid_unnecessary_type_assertions` rule which
21-
/// warns about unnecessary usage of `is` and `whereType` operators
20+
/// Warns about unnecessary usage of `is` and `whereType` operators.
21+
///
22+
/// ### Example:
23+
/// #### BAD:
24+
/// ```dart
25+
/// final testList = [1.0, 2.0, 3.0];
26+
/// final result = testList is List<double>; // LINT
27+
/// final negativeResult = testList is! List<double>; // LINT
28+
/// testList.whereType<double>(); // LINT
29+
///
30+
/// final double d = 2.0;
31+
/// final casted = d is double; // LINT
32+
/// ```
33+
///
34+
/// #### OK:
35+
/// ```dart
36+
/// final dynamicList = <dynamic>[1.0, 2.0];
37+
/// dynamicList.whereType<double>();
38+
///
39+
/// final double? nullableD = 2.0;
40+
/// // casting `Type? is Type` is allowed
41+
/// final castedD = nullableD is double;
42+
/// ```
2243
class AvoidUnnecessaryTypeAssertions extends SolidLintRule {
2344
/// The [LintCode] of this lint rule that represents
2445
/// the error whether we use bad formatted double literals.

lib/lints/avoid_unused_parameters/avoid_unused_parameters_rule.dart

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,44 @@ import 'package:solid_lints/lints/avoid_unused_parameters/avoid_unused_parameter
44
import 'package:solid_lints/models/rule_config.dart';
55
import 'package:solid_lints/models/solid_lint_rule.dart';
66

7-
/// A `avoid_unused_parameters` rule which
8-
/// warns about unused parameters
7+
/// Warns about unused function, method, constructor or factory parameters.
8+
///
9+
/// ### Example:
10+
/// #### BAD:
11+
/// ```dart
12+
/// void fun(String x) {} // LINT
13+
/// void fun2(String x, String y) { // LINT
14+
/// print(y);
15+
/// }
16+
///
17+
/// class TestClass {
18+
/// static void staticMethod(int a) {} // LINT
19+
/// void method(String s) {} // LINT
20+
///
21+
/// TestClass([int a]); // LINT
22+
/// factory TestClass.named(int a) { // LINT
23+
/// return TestClass();
24+
/// }
25+
/// }
26+
/// ```
27+
///
28+
/// #### OK:
29+
/// ```dart
30+
/// void fun(String _) {} // Replacing with underscores silences the warning
31+
/// void fun2(String _, String y) {
32+
/// print(y);
33+
/// }
34+
///
35+
/// class TestClass {
36+
/// static void staticMethod(int _) {} // OK
37+
/// void method(String _) {} // OK
38+
///
39+
/// TestClass([int _]); // OK
40+
/// factory TestClass.named(int _) { // OK
41+
/// return TestClass();
42+
/// }
43+
/// }
44+
/// ```
945
class AvoidUnusedParametersRule extends SolidLintRule {
1046
/// The [LintCode] of this lint rule that represents
1147
/// the error whether we use bad formatted double literals.

lib/lints/avoid_using_api/avoid_using_api_rule.dart

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,57 @@ import 'package:solid_lints/utils/path_utils.dart';
1010

1111
/// A `avoid_using_api` rule which
1212
/// warns about usage of avoided APIs
13+
///
14+
///
15+
/// ### Usage
16+
///
17+
/// External code can be "deprecated" when there is a better option available:
18+
///
19+
/// ```yaml
20+
/// custom_lint:
21+
/// rules:
22+
/// - avoid_using_api:
23+
/// severity: info
24+
/// entries:
25+
/// - class_name: Future
26+
/// identifier: wait
27+
/// source: dart:async
28+
/// reason: "Future.wait should be avoided because it loses type
29+
/// safety for the results. Use a Record's `wait` method
30+
/// instead."
31+
/// severity: warning
32+
/// ```
33+
///
34+
/// Result:
35+
///
36+
/// ```dart
37+
/// void main() async {
38+
/// await Future.wait([...]); // LINT
39+
/// await (...).wait; // OK
40+
/// }
41+
/// ```
42+
///
43+
/// ### Advanced Usage
44+
///
45+
/// Each entry also has `includes` and `excludes` parameters.
46+
/// These paths utilize [Glob](https://pub.dev/packages/glob)
47+
/// patterns to determine if a lint entry should be applied to a file.
48+
///
49+
/// For example, a lint to prevent usage of a domain-only package outside of the
50+
/// domain folder:
51+
///
52+
/// ```yaml
53+
/// custom_lint:
54+
/// rules:
55+
/// - avoid_using_api:
56+
/// severity: info
57+
/// entries:
58+
/// - source: package:domain_models
59+
/// excludes:
60+
/// - "**/domain/**.dart"
61+
/// reason: "domain_models is only intended to be used in the domain
62+
/// layer."
63+
/// ```
1364
class AvoidUsingApiRule extends SolidLintRule<AvoidUsingApiParameters> {
1465
/// The [LintCode] of this lint rule that represents
1566
/// the error whether we use bad formatted double literals.

0 commit comments

Comments
 (0)