-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The variant of the parameter, optional/required and named/positional, is inferred from the other parameters used in the method. Change-Id: I0c26e5d16e759978410ba854c0fed0a75d33724f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/365826 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Moritz Sümmermann <[email protected]>
- Loading branch information
Showing
5 changed files
with
375 additions
and
0 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
pkg/analysis_server/lib/src/services/correction/dart/create_parameter.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart'; | ||
import 'package:analysis_server/src/services/correction/fix.dart'; | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/element/nullability_suffix.dart'; | ||
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; | ||
import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; | ||
import 'package:analyzer_plugin/utilities/range_factory.dart'; | ||
|
||
class CreateParameter extends ResolvedCorrectionProducer { | ||
String _parameterName = ''; | ||
|
||
@override | ||
CorrectionApplicability get applicability => | ||
CorrectionApplicability.singleLocation; | ||
|
||
@override | ||
List<String> get fixArguments => [_parameterName]; | ||
|
||
@override | ||
FixKind get fixKind => DartFixKind.CREATE_PARAMETER; | ||
|
||
@override | ||
Future<void> compute(ChangeBuilder builder) async { | ||
var nameNode = node; | ||
if (nameNode is! SimpleIdentifier) { | ||
return; | ||
} | ||
_parameterName = nameNode.name; | ||
// prepare target Statement | ||
var parameters = | ||
node.thisOrAncestorOfType<FunctionExpression>()?.parameters ?? | ||
node.thisOrAncestorOfType<MethodDeclaration>()?.parameters ?? | ||
node.thisOrAncestorOfType<ConstructorDeclaration>()?.parameters; | ||
if (parameters == null) { | ||
return; | ||
} | ||
|
||
var requiredPositionals = | ||
parameters.parameters.where((p) => p.isRequiredPositional); | ||
var namedParameters = parameters.parameters.where((p) => p.isNamed); | ||
var somethingAfterPositionals = requiredPositionals.isNotEmpty && | ||
parameters.parameters.any((p) => !p.isRequiredPositional); | ||
var somethingBeforeNamed = requiredPositionals.isEmpty && | ||
parameters.parameters.any((p) => !p.isNamed); | ||
var hasFollowingParameters = | ||
somethingAfterPositionals || somethingBeforeNamed; | ||
|
||
// compute type | ||
var type = | ||
inferUndefinedExpressionType(nameNode) ?? typeProvider.dynamicType; | ||
var lastRequiredPositional = requiredPositionals.lastOrNull; | ||
var lastNamed = namedParameters.lastOrNull; | ||
var hasPreviousParameters = | ||
lastRequiredPositional != null || lastNamed != null; | ||
var last = lastRequiredPositional ?? lastNamed; | ||
var trailingComma = | ||
parameters.parameters.lastOrNull?.endToken.next?.lexeme == ','; | ||
|
||
int insertionToken; | ||
if (hasPreviousParameters) { | ||
if (trailingComma) { | ||
// After comma | ||
insertionToken = last!.endToken.next!.end; | ||
} else if (hasFollowingParameters) { | ||
// After whitespace after comma | ||
insertionToken = last!.endToken.next!.end + 1; | ||
} else { | ||
// After last, as there is no comma | ||
insertionToken = last!.end; | ||
} | ||
} else { | ||
// At first position | ||
insertionToken = parameters.leftParenthesis.end; | ||
} | ||
|
||
await builder.addDartFileEdit(file, (builder) { | ||
builder.addInsertion(insertionToken, (builder) { | ||
//prefix | ||
if (hasPreviousParameters) { | ||
if (trailingComma) { | ||
builder.writeln(); | ||
var whitespace = utils.getNodePrefix(last!); | ||
builder.write(whitespace); | ||
} else if (!hasFollowingParameters) { | ||
builder.write(', '); | ||
} | ||
} | ||
builder.writeParameter( | ||
_parameterName, | ||
type: type, | ||
nameGroupName: 'NAME', | ||
typeGroupName: 'TYPE', | ||
isRequiredType: true, | ||
isRequiredNamed: last != null && | ||
last == lastNamed && | ||
type.nullabilitySuffix != NullabilitySuffix.question, | ||
); | ||
//suffix | ||
if (trailingComma) { | ||
builder.write(','); | ||
} else if (hasFollowingParameters) { | ||
builder.write(', '); | ||
} | ||
}); | ||
builder.addLinkedPosition(range.node(node), 'NAME'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
254 changes: 254 additions & 0 deletions
254
pkg/analysis_server/test/src/services/correction/fix/create_parameter_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:analysis_server/src/services/correction/fix.dart'; | ||
import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; | ||
import 'package:test_reflective_loader/test_reflective_loader.dart'; | ||
|
||
import 'fix_processor.dart'; | ||
|
||
void main() { | ||
defineReflectiveSuite(() { | ||
defineReflectiveTests(CreateParameterTest); | ||
}); | ||
} | ||
|
||
@reflectiveTest | ||
class CreateParameterTest extends FixProcessorTest { | ||
@override | ||
FixKind get kind => DartFixKind.CREATE_PARAMETER; | ||
|
||
Future<void> test_dynamic_type() async { | ||
await resolveTestCode(''' | ||
int f( | ||
int b, | ||
) { | ||
var i = b2; | ||
return i; | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
int f( | ||
int b, | ||
dynamic b2, | ||
) { | ||
var i = b2; | ||
return i; | ||
} | ||
'''); | ||
} | ||
|
||
Future<void> test_final_comma() async { | ||
await resolveTestCode(''' | ||
int f( | ||
int b, | ||
) { | ||
int i = b2; | ||
return i; | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
int f( | ||
int b, | ||
int b2, | ||
) { | ||
int i = b2; | ||
return i; | ||
} | ||
'''); | ||
} | ||
|
||
Future<void> test_method_type() async { | ||
await resolveTestCode(''' | ||
class A{ | ||
int f( | ||
int b, | ||
) { | ||
int i = b2; | ||
return i; | ||
} | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
class A{ | ||
int f( | ||
int b, | ||
int b2, | ||
) { | ||
int i = b2; | ||
return i; | ||
} | ||
} | ||
'''); | ||
} | ||
|
||
Future<void> test_multi() async { | ||
await resolveTestCode(''' | ||
int f(int b) { | ||
int i = b2; | ||
return i; | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
int f(int b, int b2) { | ||
int i = b2; | ||
return i; | ||
} | ||
'''); | ||
} | ||
|
||
Future<void> test_only() async { | ||
await resolveTestCode(''' | ||
int f() { | ||
int i = b; | ||
return i; | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
int f(int b) { | ||
int i = b; | ||
return i; | ||
} | ||
'''); | ||
} | ||
|
||
Future<void> test_with_constructor() async { | ||
await resolveTestCode(''' | ||
class A { | ||
A() { | ||
int i = b; | ||
g(i); | ||
} | ||
} | ||
void g(int n) {} | ||
'''); | ||
await assertHasFix(''' | ||
class A { | ||
A(int b) { | ||
int i = b; | ||
g(i); | ||
} | ||
} | ||
void g(int n) {} | ||
'''); | ||
} | ||
|
||
Future<void> test_with_local_function() async { | ||
await resolveTestCode(''' | ||
void f(int a) { | ||
int g(int a){ | ||
int i = b2; | ||
return i; | ||
} | ||
g(3); | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
void f(int a) { | ||
int g(int a, int b2){ | ||
int i = b2; | ||
return i; | ||
} | ||
g(3); | ||
} | ||
'''); | ||
} | ||
|
||
Future<void> test_with_named() async { | ||
await resolveTestCode(''' | ||
int f({int? b}) { | ||
int i = b2; | ||
return i; | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
int f({int? b, required int b2}) { | ||
int i = b2; | ||
return i; | ||
} | ||
'''); | ||
} | ||
|
||
Future<void> test_with_named_and_positional() async { | ||
await resolveTestCode(''' | ||
int f(int a, {int? b}) { | ||
int i = b2; | ||
return i; | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
int f(int a, int b2, {int? b}) { | ||
int i = b2; | ||
return i; | ||
} | ||
'''); | ||
} | ||
|
||
Future<void> test_with_named_nullable() async { | ||
await resolveTestCode(''' | ||
int? f({int? b}) { | ||
int? i = b2; | ||
return i; | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
int? f({int? b, int? b2}) { | ||
int? i = b2; | ||
return i; | ||
} | ||
'''); | ||
} | ||
|
||
Future<void> test_with_optional_positional() async { | ||
await resolveTestCode(''' | ||
int f([int? b]) { | ||
int i = b2; | ||
return i; | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
int f(int b2, [int? b]) { | ||
int i = b2; | ||
return i; | ||
} | ||
'''); | ||
} | ||
|
||
Future<void> test_with_required_positional_and_optional() async { | ||
await resolveTestCode(''' | ||
int f(int a, [int? b]) { | ||
int i = b2; | ||
return i; | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
int f(int a, int b2, [int? b]) { | ||
int i = b2; | ||
return i; | ||
} | ||
'''); | ||
} | ||
|
||
Future<void> test_with_required_positional_and_optional_trailing() async { | ||
await resolveTestCode(''' | ||
int f( | ||
int a, [ | ||
int? b, | ||
]) { | ||
int i = b2; | ||
return i; | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
int f( | ||
int a, | ||
int b2, [ | ||
int? b, | ||
]) { | ||
int i = b2; | ||
return i; | ||
} | ||
'''); | ||
} | ||
} |
Oops, something went wrong.