-
Notifications
You must be signed in to change notification settings - Fork 24
refactor: migrate named_parameters_ordering #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: analysis_server_migration
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,196 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:analysis_server_plugin/edit/dart/correction_producer.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:analysis_server_plugin/edit/dart/dart_fix_kind_priority.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:analyzer/dart/ast/ast.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:analyzer/source/source_range.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:solid_lints/src/lints/named_parameters_ordering/models/parameter_type.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:solid_lints/src/lints/named_parameters_ordering/named_parameters_ordering_rule.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:yaml/yaml.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// A Quick fix for [NamedParametersOrderingRule] rule. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class NamedParametersOrderingFix extends ResolvedCorrectionProducer { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static const _fixKind = FixKind( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'solid_lints.fix.named_parameters_ordering', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DartFixKindPriority.standard, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Sort named parameters", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Creates a new instance of [NamedParametersOrderingFix]. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NamedParametersOrderingFix({required super.context}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FixKind get fixKind => _fixKind; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CorrectionApplicability get applicability => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CorrectionApplicability.automatically; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Future<void> compute(ChangeBuilder builder) async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final parameterList = node.thisOrAncestorOfType<FormalParameterList>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (parameterList == null) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final namedParams = parameterList.parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .where((p) => p.isNamed) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (namedParams.length < 2) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final parametersOrder = _getParametersOrder(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final sortedNamedParams = [...namedParams]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sortedNamedParams.sort((a, b) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final typeA = ParameterType.fromParameter(a); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final typeB = ParameterType.fromParameter(b); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final indexA = parametersOrder.indexOf(typeA); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final indexB = parametersOrder.indexOf(typeB); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return indexA.compareTo(indexB); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+42
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When sorting the named parameters, if a parameter type is not present in the configured To fix this, we should treat
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Check if the order is already correct (if sorting changed nothing) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool isChanged = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < namedParams.length; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (namedParams[i] != sortedNamedParams[i]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isChanged = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!isChanged) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final isMultiline = utils | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .getRangeText(parameterList.sourceRange) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .contains('\n'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!isMultiline) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Single-line: no leading comments, simple text replacement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final sortedTexts = sortedNamedParams | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((p) => utils.getRangeText(p.sourceRange)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final replacementText = sortedTexts.join(', '); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final targetRange = SourceRange( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namedParams.first.offset, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namedParams.last.end - namedParams.first.offset, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await builder.addDartFileEdit(file, (builder) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| builder.addSimpleReplacement(targetRange, replacementText); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Multiline: extract parameter blocks including leading comments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final (:blockTexts, :firstBlockStart) = _extractParamBlocks( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namedParams, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parameterList, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Map sorted parameters to their corresponding block texts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final sortedBlockTexts = sortedNamedParams | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((p) => blockTexts[namedParams.indexOf(p)]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final replacementText = sortedBlockTexts.join(',\n'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final targetRange = SourceRange( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| firstBlockStart, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namedParams.last.end - firstBlockStart, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await builder.addDartFileEdit(file, (builder) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| builder.addSimpleReplacement(targetRange, replacementText); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Extracts text blocks for each named parameter, including any leading | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// comments that belong to that parameter. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Returns the block texts and the start offset of the first block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// (used as the replacement range start). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ({List<String> blockTexts, int firstBlockStart}) _extractParamBlocks( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<FormalParameter> namedParams, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FormalParameterList parameterList, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final blocks = <String>[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int? firstStart; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < namedParams.length; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final param = namedParams[i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final int minOffset = i == 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? (parameterList.leftDelimiter?.end ?? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parameterList.leftParenthesis.end) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : namedParams[i - 1].end; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Find block start: use leading comment offset if present, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // then expand to line start for proper indentation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var blockStart = param.offset; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final comment = param.beginToken.precedingComments; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (comment != null && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| comment.offset >= minOffset && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| comment.offset < param.offset) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| blockStart = comment.offset; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final linePrefix = utils.getLinePrefix(blockStart); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| blockStart -= linePrefix.length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+121
to
+136
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a potential bug when extracting parameter blocks with comments. If a comment is a trailing comment on the same line as the previous parameter (e.g., This causes To prevent this, we should only associate a preceding comment with the current parameter if there is a newline between the end of the previous parameter and the start of the comment. Additionally, we can use
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| firstStart ??= blockStart; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| blocks.add( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| utils.getRangeText(SourceRange(blockStart, param.end - blockStart)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| blockTexts: blocks, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| firstBlockStart: firstStart ?? namedParams.first.offset, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+111
to
+148
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing Comments Deletion BugWhen extracting parameter blocks in Because the quick fix replaces the entire range from To fix this, you should either:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<ParameterType> _getParametersOrder() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final pathContext = resourceProvider.pathContext; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String currentDirectoryPath = pathContext.dirname(file); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (pathContext.dirname(currentDirectoryPath) != currentDirectoryPath) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final candidatePath = pathContext.join( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| currentDirectoryPath, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'analysis_options.yaml', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final candidateFile = resourceProvider.getFile(candidatePath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (candidateFile.exists) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final content = candidateFile.readAsStringSync(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final yaml = loadYaml(content); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (yaml case { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'plugins': { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'solid_lints': { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'diagnostics': { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NamedParametersOrderingRule.lintName: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'order': final List<Object?> orderList, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final order = orderList | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((e) => e is String ? ParameterType.fromType(e) : null) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .whereType<ParameterType>() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (order.isNotEmpty) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return order; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+176
to
+182
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reuse
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (_) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ignore parsing error, fallback to default order | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final parentDir = pathContext.dirname(currentDirectoryPath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| currentDirectoryPath = parentDir; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ParameterType.defaultOrder; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:collection/collection.dart'; | ||
|
|
||
| /// Represents a function parameter type | ||
|
|
@@ -17,11 +18,53 @@ enum ParameterType { | |
| /// Default value parameter type (String parameterName = 'defaultValue') | ||
| defaultValue('default'); | ||
|
|
||
| /// The default ordering of parameter types. | ||
| static const defaultOrder = [ | ||
| ParameterType.requiredInherited, | ||
| ParameterType.inherited, | ||
| ParameterType.required, | ||
| ParameterType.nullable, | ||
| ParameterType.defaultValue, | ||
| ]; | ||
|
|
||
| /// Returns [ParameterType] from type or null if not found | ||
| static ParameterType? fromType(String type) { | ||
| return values.firstWhereOrNull((o) => o.type == type); | ||
| } | ||
|
|
||
| /// Classifies a [FormalParameter] into a [ParameterType]. | ||
| /// | ||
| /// Recursively unwraps [DefaultFormalParameter] wrappers to determine | ||
| /// the underlying parameter kind. | ||
| static ParameterType fromParameter( | ||
| FormalParameter parameter, { | ||
| bool hasDefaultValue = false, | ||
| }) { | ||
| if (parameter is DefaultFormalParameter && | ||
| parameter.parameter is! DefaultFormalParameter) { | ||
| return fromParameter( | ||
| parameter.parameter, | ||
| hasDefaultValue: parameter.defaultValue != null, | ||
| ); | ||
| } | ||
|
|
||
| switch (parameter) { | ||
| case SuperFormalParameter(:final isRequired): | ||
| return isRequired | ||
| ? ParameterType.requiredInherited | ||
| : ParameterType.inherited; | ||
|
|
||
| case DefaultFormalParameter(): | ||
| case _ when hasDefaultValue: | ||
| return ParameterType.defaultValue; | ||
|
Comment on lines
+57
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unreachable Case in Switch StatementThe if (parameter is DefaultFormalParameter && ...)You can safely remove this case to keep the switch statement clean and maintainable. case _ when hasDefaultValue:
return ParameterType.defaultValue; |
||
|
|
||
| case FieldFormalParameter(:final isRequired) || | ||
| FunctionTypedFormalParameter(:final isRequired) || | ||
| SimpleFormalParameter(:final isRequired): | ||
| return isRequired ? ParameterType.required : ParameterType.nullable; | ||
| } | ||
| } | ||
|
|
||
| /// String representation of the parameter type | ||
| final String type; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing Parameter Types Bug in Custom Configuration
If a user configures a custom order in
analysis_options.yamlbut omits some of the parameter types (e.g., only specifyingrequiredandnullable), the omitted types will not be present in the parsed_parametersOrderlist.As a result,
_parametersOrder.indexOf(type)will return-1for those omitted types. This causes severe bugs:_isOrderingWrong,-1is compared with other indices, leading to incorrect lint reports (e.g., reporting that a default parameter should be before a required parameter because-1 < 0).To fix this, we should append any missing parameter types (from
ParameterType.defaultOrder) to the end of the parsed list so that everyParameterTypehas a valid, non-negative index.