Skip to content

Commit 767b57b

Browse files
committed
Extract Local refactoring: skip method names in invocations.
[email protected] BUG= Review URL: https://codereview.chromium.org/1498523005 .
1 parent 6cababc commit 767b57b

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

pkg/analysis_server/lib/src/services/refactoring/extract_local.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,15 @@ class ExtractLocalRefactoringImpl extends RefactoringImpl
207207
// fatal selection problems
208208
if (coveringExpressionOffsets.isEmpty) {
209209
if (node is SimpleIdentifier) {
210-
Element element = node.bestElement;
211-
if (element is FunctionElement || element is MethodElement) {
212-
return new RefactoringStatus.fatal(
213-
'Cannot extract a single method name.',
214-
newLocation_fromNode(node));
215-
}
216210
if (node.inDeclarationContext()) {
217211
return new RefactoringStatus.fatal(
218212
'Cannot extract the name part of a declaration.',
219213
newLocation_fromNode(node));
220214
}
215+
Element element = node.bestElement;
216+
if (element is FunctionElement || element is MethodElement) {
217+
continue;
218+
}
221219
}
222220
if (parent is AssignmentExpression && parent.leftHandSide == node) {
223221
return new RefactoringStatus.fatal(

pkg/analysis_server/test/services/refactoring/extract_local_test.dart

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,16 @@ main() {
6969
expectedMessage: 'Cannot extract the left-hand side of an assignment.');
7070
}
7171

72-
test_checkInitialConditions_methodName_reference() async {
72+
test_checkInitialConditions_namePartOfDeclaration_function() async {
7373
indexTestUnit('''
7474
main() {
75-
main();
7675
}
7776
''');
78-
_createRefactoringWithSuffix('main', '();');
77+
_createRefactoringWithSuffix('main', '()');
7978
// check conditions
8079
RefactoringStatus status = await refactoring.checkAllConditions();
8180
assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL,
82-
expectedMessage: 'Cannot extract a single method name.');
81+
expectedMessage: 'Cannot extract the name part of a declaration.');
8382
}
8483

8584
test_checkInitialConditions_namePartOfDeclaration_variable() async {
@@ -859,6 +858,24 @@ main() {
859858
''');
860859
}
861860

861+
test_singleExpression_methodName_reference() async {
862+
indexTestUnit('''
863+
main() {
864+
var v = foo().length;
865+
}
866+
String foo() => '';
867+
''');
868+
_createRefactoringWithSuffix('foo', '().');
869+
// apply refactoring
870+
return _assertSuccessfulRefactoring('''
871+
main() {
872+
var res = foo();
873+
var v = res.length;
874+
}
875+
String foo() => '';
876+
''');
877+
}
878+
862879
test_singleExpression_nameOfProperty_prefixedIdentifier() async {
863880
indexTestUnit('''
864881
main(p) {
@@ -894,7 +911,7 @@ String foo() => '';
894911
}
895912

896913
/**
897-
* Here we use knowledge how exactly `1 + 2 + 3 + 41 is parsed. We know that
914+
* Here we use knowledge how exactly `1 + 2 + 3 + 4` is parsed. We know that
898915
* `1 + 2` will be a separate and complete binary expression, so it can be
899916
* handled as a single expression.
900917
*/

0 commit comments

Comments
 (0)