|
| 1 | +import 'package:analyzer/error/listener.dart'; |
| 2 | +import 'package:custom_lint_builder/custom_lint_builder.dart'; |
| 3 | +import 'package:solid_lints/src/lints/named_parameters_ordering/models/named_parameters_ordering_parameters.dart'; |
| 4 | +import 'package:solid_lints/src/lints/named_parameters_ordering/visitors/named_parameters_ordering_visitor.dart'; |
| 5 | +import 'package:solid_lints/src/models/rule_config.dart'; |
| 6 | +import 'package:solid_lints/src/models/solid_lint_rule.dart'; |
| 7 | + |
| 8 | +/// A lint which allows to enforce a particular named parameter ordering |
| 9 | +/// conventions. |
| 10 | +/// |
| 11 | +/// ### Configuration format |
| 12 | +/// ```yaml |
| 13 | +/// - named_parameters_ordering: |
| 14 | +/// order: |
| 15 | +/// - (parameterType) |
| 16 | +/// ``` |
| 17 | +/// Where parameterType can be one of: |
| 18 | +/// |
| 19 | +/// - `super` |
| 20 | +/// - `required_super` |
| 21 | +/// - `required` |
| 22 | +/// - `nullable` |
| 23 | +/// - `default` |
| 24 | +/// |
| 25 | +/// ### Example: |
| 26 | +/// |
| 27 | +/// Assuming config: |
| 28 | +/// |
| 29 | +/// ```yaml |
| 30 | +/// custom_lint: |
| 31 | +/// rules: |
| 32 | +/// - named_parameters_ordering: |
| 33 | +/// order: |
| 34 | +/// - required |
| 35 | +/// - required_super |
| 36 | +/// - default |
| 37 | +/// - nullable |
| 38 | +/// - super |
| 39 | +/// ``` |
| 40 | +/// |
| 41 | +/// #### BAD: |
| 42 | +/// |
| 43 | +/// ```dart |
| 44 | +/// class User { |
| 45 | +/// final String accountType; |
| 46 | +/// final String? userId; |
| 47 | +/// |
| 48 | +/// User({ |
| 49 | +/// required this.accountType, |
| 50 | +/// this.userId, |
| 51 | +/// }); |
| 52 | +/// } |
| 53 | +/// |
| 54 | +/// class UserProfile extends User { |
| 55 | +/// final String? age; |
| 56 | +/// final String? country; |
| 57 | +/// final String email; |
| 58 | +/// final bool isActive; |
| 59 | +/// final String name; |
| 60 | +/// |
| 61 | +/// UserProfile({ |
| 62 | +/// this.age, |
| 63 | +/// required super.accountType, // LINT, required super named parameters should be before nullable named parameters |
| 64 | +/// required this.name, // LINT, required named parameters should be before super named parameters |
| 65 | +/// super.userId, |
| 66 | +/// this.country, // LINT, nullable named parameters should be before super named parameters |
| 67 | +/// this.isActive = true, // LINT, default named parameters should be before nullable named parameters |
| 68 | +/// required this.email, // LINT, required named parameters should be before default named parameters |
| 69 | +/// }); |
| 70 | +/// |
| 71 | +/// void doSomething({ |
| 72 | +/// required String name, |
| 73 | +/// int? age, |
| 74 | +/// bool isActive = true, // LINT, default named parameters should be before nullable named parameters |
| 75 | +/// required String email, // LINT, required named parameters should be before default named parameters |
| 76 | +/// }) { |
| 77 | +/// return; |
| 78 | +/// } |
| 79 | +/// } |
| 80 | +/// ``` |
| 81 | +/// |
| 82 | +/// #### GOOD: |
| 83 | +/// |
| 84 | +/// ```dart |
| 85 | +/// class User { |
| 86 | +/// final String accountType; |
| 87 | +/// final String? userId; |
| 88 | +/// |
| 89 | +/// User({ |
| 90 | +/// required this.accountType, |
| 91 | +/// this.userId, |
| 92 | +/// }); |
| 93 | +/// } |
| 94 | +/// |
| 95 | +/// class UserProfile extends User { |
| 96 | +/// final String? age; |
| 97 | +/// final String? country; |
| 98 | +/// final String email; |
| 99 | +/// final bool isActive; |
| 100 | +/// final String name; |
| 101 | +/// |
| 102 | +/// UserProfile({ |
| 103 | +/// required this.name, |
| 104 | +/// required this.email, |
| 105 | +/// required super.accountType, |
| 106 | +/// this.isActive = true, |
| 107 | +/// this.age, |
| 108 | +/// this.country, |
| 109 | +/// super.userId, |
| 110 | +/// }); |
| 111 | +/// |
| 112 | +/// void doSomething({ |
| 113 | +/// required String name, |
| 114 | +/// required String email, |
| 115 | +/// bool isActive = true, |
| 116 | +/// int? age, |
| 117 | +/// }) { |
| 118 | +/// return; |
| 119 | +/// } |
| 120 | +/// } |
| 121 | +/// ``` |
| 122 | +class NamedParametersOrderingRule |
| 123 | + extends SolidLintRule<NamedParametersOrderingParameters> { |
| 124 | + /// This lint rule represents |
| 125 | + /// the error whether we use bad formatted double literals. |
| 126 | + static const lintName = 'named_parameters_ordering'; |
| 127 | + |
| 128 | + static const _warningMessage = 'should be before'; |
| 129 | + |
| 130 | + NamedParametersOrderingRule._(super.config); |
| 131 | + |
| 132 | + /// Creates a new instance of [NamedParametersOrderingRule] |
| 133 | + /// based on the lint configuration. |
| 134 | + factory NamedParametersOrderingRule.createRule(CustomLintConfigs configs) { |
| 135 | + final config = RuleConfig<NamedParametersOrderingParameters>( |
| 136 | + configs: configs, |
| 137 | + name: lintName, |
| 138 | + paramsParser: NamedParametersOrderingParameters.fromJson, |
| 139 | + problemMessage: (_) => "Order of named parameter is wrong", |
| 140 | + ); |
| 141 | + |
| 142 | + return NamedParametersOrderingRule._(config); |
| 143 | + } |
| 144 | + |
| 145 | + @override |
| 146 | + void run( |
| 147 | + CustomLintResolver resolver, |
| 148 | + ErrorReporter reporter, |
| 149 | + CustomLintContext context, |
| 150 | + ) { |
| 151 | + context.registry.addFormalParameterList((node) { |
| 152 | + final visitor = NamedParametersOrderingVisitor(config.parameters.order); |
| 153 | + |
| 154 | + final parametersInfo = visitor.visitFormalParameterList(node); |
| 155 | + |
| 156 | + final wrongOrderParameters = parametersInfo.where( |
| 157 | + (info) => info.parameterOrderingInfo.isWrong, |
| 158 | + ); |
| 159 | + |
| 160 | + for (final parameterInfo in wrongOrderParameters) { |
| 161 | + reporter.atNode( |
| 162 | + parameterInfo.formalParameter, |
| 163 | + _createWrongOrderLintCode(parameterInfo.parameterOrderingInfo), |
| 164 | + ); |
| 165 | + } |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + LintCode _createWrongOrderLintCode(ParameterOrderingInfo info) { |
| 170 | + final parameterOrdering = info.parameterType; |
| 171 | + final previousParameterOrdering = info.previousParameterType; |
| 172 | + |
| 173 | + return LintCode( |
| 174 | + name: lintName, |
| 175 | + problemMessage: "${parameterOrdering.displayName} named parameters" |
| 176 | + " $_warningMessage " |
| 177 | + "${previousParameterOrdering!.displayName} named parameters.", |
| 178 | + ); |
| 179 | + } |
| 180 | +} |
0 commit comments