Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(491)

Unified Diff: pkg/analysis_server/lib/src/services/correction/fix_internal.dart

Issue 2692983003: Add fix for missing @required params. (Closed)
Patch Set: Fixes. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/lib/src/services/correction/fix_internal.dart
diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
index c823e4ebee013b703d53a1ab20670908d52e2da6..eb45caf2cef0f283d9080d42817a773264135a60 100644
--- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
@@ -267,6 +267,9 @@ class FixProcessor {
_addFix_createConstructor_insteadOfSyntheticDefault();
await _addFix_addMissingParameter();
}
+ if (errorCode == HintCode.MISSING_REQUIRED_PARAM) {
+ _addFix_addMissingRequiredArgument();
+ }
if (errorCode == StaticWarningCode.FUNCTION_WITHOUT_CALL) {
_addFix_addMissingMethodCall();
}
@@ -549,6 +552,57 @@ class FixProcessor {
}
}
+ void _addFix_addMissingRequiredArgument() {
+ Element targetElement;
+ ArgumentList argumentList;
+
+ if (node is SimpleIdentifier) {
+ AstNode invocation = node.parent;
+ if (invocation is MethodInvocation) {
+ targetElement = invocation.methodName.bestElement;
+ argumentList = invocation.argumentList;
+ } else {
+ AstNode ancestor =
+ invocation.getAncestor((p) => p is InstanceCreationExpression);
+ if (ancestor is InstanceCreationExpression) {
+ targetElement = ancestor.staticElement;
+ argumentList = ancestor.argumentList;
+ }
+ }
+ }
+
+ if (targetElement is ExecutableElement) {
+ List<Expression> args = argumentList.arguments;
+ List<String> namedArgs = args
+ .where((e) => e is NamedExpression)
+ .map((e) => (e as NamedExpression).name.label.name)
+ .toList(growable: false);
+
+ List<ParameterElement> missingParams = targetElement.parameters
+ .where((p) => p.isRequired && !namedArgs.contains(p.name))
+ .toList(growable: false);
+ if (missingParams.isEmpty) {
+ return;
+ }
+
+ // add proposal
+
+ SourceBuilder sb;
+
+ if (args.isEmpty) {
+ sb = new SourceBuilder(file, argumentList.leftParenthesis.end);
+ } else {
+ sb = new SourceBuilder(file, args.last.end);
+ sb.append(', ');
+ }
+
+ sb.append(missingParams.map((p) => '${p.name}: null').join(', '));
+
+ _insertBuilder(sb, targetElement);
+ _addFix(DartFixKind.ADD_MISSING_REQUIRED_ARGUMENT, []);
+ }
+ }
+
void _addFix_boolInsteadOfBoolean() {
SourceRange range = rf.rangeError(error);
_addReplaceEdit(range, 'bool');

Powered by Google App Engine
This is Rietveld 408576698