Skip to content

Commit 34d945c

Browse files
bwilkersonCommit Queue
authored andcommitted
Migrate more server tests
Change-Id: Iee3e6b6db9c58e9fcf790e2874f73c8eac6e660d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395404 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 359aa74 commit 34d945c

File tree

14 files changed

+112
-88
lines changed

14 files changed

+112
-88
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,8 @@ lib/src/utilities/extensions/completion_request.dart
9393
lib/src/utilities/extensions/element.dart
9494
test/abstract_single_unit.dart
9595
test/plugin/protocol_dart_test.dart
96-
test/services/correction/name_suggestion_test.dart
9796
test/services/correction/status_test.dart
9897
test/services/refactoring/legacy/abstract_rename.dart
99-
test/services/refactoring/legacy/convert_getter_to_method_test.dart
100-
test/services/refactoring/legacy/convert_method_to_getter_test.dart
101-
test/services/refactoring/legacy/extract_method_test.dart
10298
test/services/refactoring/legacy/rename_class_member_test.dart
10399
test/services/refactoring/legacy/rename_constructor_test.dart
104100
test/services/refactoring/legacy/rename_import_test.dart

pkg/analysis_server/lib/src/handler/legacy/edit_get_available_refactorings.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart
1010
import 'package:analyzer/dart/element/element.dart';
1111
import 'package:analyzer/src/dart/ast/utilities.dart';
1212
import 'package:analyzer/src/util/file_paths.dart';
13+
import 'package:analyzer/src/utilities/extensions/element.dart';
1314
import 'package:analyzer_plugin/protocol/protocol_common.dart';
1415

1516
/// The handler for the `edit.getAvailableRefactorings` request.
@@ -88,7 +89,7 @@ class EditGetAvailableRefactoringsHandler extends LegacyHandler {
8889
if (ConvertMethodToGetterRefactoring(
8990
refactoringWorkspace,
9091
resolvedUnit.session,
91-
element,
92+
element.asElement2,
9293
).isAvailable()) {
9394
kinds.add(RefactoringKind.CONVERT_METHOD_TO_GETTER);
9495
}

pkg/analysis_server/lib/src/lsp/handlers/code_actions/dart.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import 'package:analysis_server_plugin/src/correction/fix_processor.dart';
2121
import 'package:analyzer/dart/analysis/results.dart';
2222
import 'package:analyzer/dart/analysis/session.dart'
2323
show InconsistentAnalysisException;
24-
import 'package:analyzer/dart/element/element.dart';
24+
import 'package:analyzer/dart/element/element2.dart';
2525
import 'package:analyzer/src/dart/ast/utilities.dart';
26+
import 'package:analyzer/src/utilities/extensions/element.dart';
2627

2728
/// Produces [CodeAction]s from Dart source commands, fixes, assists and
2829
/// refactors from the server.
@@ -320,10 +321,10 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
320321
// Converts/Rewrites
321322
if (shouldIncludeKind(CodeActionKind.RefactorRewrite)) {
322323
var node = NodeLocator(offset).searchWithin(unit.unit);
323-
var element = server.getElementOfNode(node);
324+
var element = server.getElementOfNode(node).asElement2;
324325

325326
// Getter to Method
326-
if (element is PropertyAccessorElement &&
327+
if (element is GetterElement &&
327328
ConvertGetterToMethodRefactoring(
328329
server.refactoringWorkspace,
329330
unit.session,
@@ -339,7 +340,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
339340
}
340341

341342
// Method to Getter
342-
if (element is ExecutableElement &&
343+
if (element is ExecutableElement2 &&
343344
ConvertMethodToGetterRefactoring(
344345
server.refactoringWorkspace,
345346
unit.session,

pkg/analysis_server/lib/src/lsp/handlers/commands/abstract_refactor.dart

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import 'package:analysis_server/src/lsp/progress.dart';
1414
import 'package:analysis_server/src/protocol_server.dart';
1515
import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart';
1616
import 'package:analyzer/dart/analysis/results.dart';
17-
import 'package:analyzer/dart/element/element.dart';
17+
import 'package:analyzer/dart/element/element2.dart';
1818
import 'package:analyzer/src/dart/ast/utilities.dart';
19+
import 'package:analyzer/src/utilities/extensions/element.dart';
1920

2021
final _manager = LspRefactorManager._();
2122

@@ -126,38 +127,34 @@ abstract class AbstractRefactorCommandHandler extends SimpleEditCommandHandler
126127

127128
case RefactoringKind.CONVERT_GETTER_TO_METHOD:
128129
var node = NodeLocator(offset).searchWithin(result.unit);
129-
var element = server.getElementOfNode(node);
130-
if (element != null) {
131-
if (element is PropertyAccessorElement) {
132-
var refactor = ConvertGetterToMethodRefactoring(
133-
server.refactoringWorkspace,
134-
result.session,
135-
element,
136-
);
137-
return success(refactor);
138-
}
130+
var element = server.getElementOfNode(node).asElement2;
131+
if (element is GetterElement) {
132+
var refactor = ConvertGetterToMethodRefactoring(
133+
server.refactoringWorkspace,
134+
result.session,
135+
element,
136+
);
137+
return success(refactor);
139138
}
140139
return error(
141140
ServerErrorCodes.InvalidCommandArguments,
142-
'Location supplied to $commandName $kind is not longer valid',
141+
'Location supplied to $commandName $kind is no longer valid',
143142
);
144143

145144
case RefactoringKind.CONVERT_METHOD_TO_GETTER:
146145
var node = NodeLocator(offset).searchWithin(result.unit);
147-
var element = server.getElementOfNode(node);
148-
if (element != null) {
149-
if (element is ExecutableElement) {
150-
var refactor = ConvertMethodToGetterRefactoring(
151-
server.refactoringWorkspace,
152-
result.session,
153-
element,
154-
);
155-
return success(refactor);
156-
}
146+
var element = server.getElementOfNode(node).asElement2;
147+
if (element is ExecutableElement2) {
148+
var refactor = ConvertMethodToGetterRefactoring(
149+
server.refactoringWorkspace,
150+
result.session,
151+
element,
152+
);
153+
return success(refactor);
157154
}
158155
return error(
159156
ServerErrorCodes.InvalidCommandArguments,
160-
'Location supplied to $commandName $kind is not longer valid',
157+
'Location supplied to $commandName $kind is no longer valid',
161158
);
162159

163160
default:

pkg/analysis_server/lib/src/services/refactoring/legacy/extract_method.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import 'package:analyzer/dart/ast/ast.dart';
2323
import 'package:analyzer/dart/ast/token.dart';
2424
import 'package:analyzer/dart/ast/visitor.dart';
2525
import 'package:analyzer/dart/element/element.dart';
26+
import 'package:analyzer/dart/element/element2.dart';
2627
import 'package:analyzer/dart/element/nullability_suffix.dart';
2728
import 'package:analyzer/dart/element/type.dart';
2829
import 'package:analyzer/dart/element/type_system.dart';
@@ -35,6 +36,7 @@ import 'package:analyzer/src/dart/ast/utilities.dart';
3536
import 'package:analyzer/src/dart/resolver/exit_detector.dart';
3637
import 'package:analyzer/src/generated/java_core.dart';
3738
import 'package:analyzer/src/utilities/extensions/ast.dart';
39+
import 'package:analyzer/src/utilities/extensions/element.dart';
3840
import 'package:analyzer/src/utilities/extensions/string.dart';
3941
import 'package:analyzer_plugin/utilities/range_factory.dart';
4042
import 'package:meta/meta.dart';
@@ -47,16 +49,19 @@ const String _tokenSeparator = '\uFFFF';
4749
Future<void> addLibraryImports(
4850
AnalysisSession session,
4951
SourceChange change,
50-
LibraryElement targetLibrary,
52+
LibraryElement2 targetLibrary2,
5153
Set<Source> libraries,
5254
) async {
53-
var libraryPath = targetLibrary.source.fullName;
55+
var libraryPath = targetLibrary2.firstFragment.source.fullName;
5456

5557
var resolveResult = await session.getResolvedUnit(libraryPath);
5658
if (resolveResult is! ResolvedUnitResult) {
5759
return;
5860
}
5961

62+
// TODO(brianwilkerson): Use `targetLibrary2` everywhere below and rename it
63+
// to `targetLibrary`.
64+
var targetLibrary = targetLibrary2.asElement as LibraryElement;
6065
var libUtils = CorrectionUtils(resolveResult);
6166
var eol = libUtils.endOfLine;
6267
// Prepare information about existing imports.
@@ -582,7 +587,7 @@ final class ExtractMethodRefactoringImpl extends RefactoringImpl
582587
await addLibraryImports(
583588
_resolveResult.session,
584589
change,
585-
_resolveResult.libraryElement,
590+
_resolveResult.libraryElement2,
586591
_librariesToImport,
587592
);
588593
return change;

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ abstract class ConvertGetterToMethodRefactoring implements Refactoring {
4444
factory ConvertGetterToMethodRefactoring(
4545
RefactoringWorkspace workspace,
4646
AnalysisSession session,
47-
PropertyAccessorElement element,
47+
GetterElement element,
4848
) {
49-
return ConvertGetterToMethodRefactoringImpl(workspace, session, element);
49+
return ConvertGetterToMethodRefactoringImpl(
50+
workspace,
51+
session,
52+
element.asElement as PropertyAccessorElement,
53+
);
5054
}
5155

5256
/// Return `true` if refactoring is available, possibly without checking all
@@ -67,9 +71,13 @@ abstract class ConvertMethodToGetterRefactoring implements Refactoring {
6771
factory ConvertMethodToGetterRefactoring(
6872
RefactoringWorkspace workspace,
6973
AnalysisSession session,
70-
ExecutableElement element,
74+
ExecutableElement2 element,
7175
) {
72-
return ConvertMethodToGetterRefactoringImpl(workspace, session, element);
76+
return ConvertMethodToGetterRefactoringImpl(
77+
workspace,
78+
session,
79+
element.asElement,
80+
);
7381
}
7482

7583
/// Return `true` if refactoring is available, possibly without checking all

pkg/analysis_server/lib/src/services/refactoring/legacy/refactoring_manager.dart

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart
1313
import 'package:analysis_server/src/services/search/search_engine.dart';
1414
import 'package:analyzer/dart/analysis/session.dart';
1515
import 'package:analyzer/dart/ast/ast.dart';
16-
import 'package:analyzer/dart/element/element.dart';
16+
import 'package:analyzer/dart/element/element2.dart';
1717
import 'package:analyzer/src/dart/ast/utilities.dart';
1818
import 'package:analyzer/src/utilities/cancellation.dart';
19+
import 'package:analyzer/src/utilities/extensions/element.dart';
1920

2021
int test_resetCount = 0;
2122

@@ -219,30 +220,26 @@ class RefactoringManager {
219220
var resolvedUnit = await server.getResolvedUnit(file);
220221
if (resolvedUnit != null) {
221222
var node = NodeLocator(offset).searchWithin(resolvedUnit.unit);
222-
var element = server.getElementOfNode(node);
223-
if (element != null) {
224-
if (element is PropertyAccessorElement) {
225-
refactoring = ConvertGetterToMethodRefactoring(
226-
refactoringWorkspace,
227-
resolvedUnit.session,
228-
element,
229-
);
230-
}
223+
var element = server.getElementOfNode(node).asElement2;
224+
if (element is GetterElement) {
225+
refactoring = ConvertGetterToMethodRefactoring(
226+
refactoringWorkspace,
227+
resolvedUnit.session,
228+
element,
229+
);
231230
}
232231
}
233232
} else if (kind == RefactoringKind.CONVERT_METHOD_TO_GETTER) {
234233
var resolvedUnit = await server.getResolvedUnit(file);
235234
if (resolvedUnit != null) {
236235
var node = NodeLocator(offset).searchWithin(resolvedUnit.unit);
237-
var element = server.getElementOfNode(node);
238-
if (element != null) {
239-
if (element is ExecutableElement) {
240-
refactoring = ConvertMethodToGetterRefactoring(
241-
refactoringWorkspace,
242-
resolvedUnit.session,
243-
element,
244-
);
245-
}
236+
var element = server.getElementOfNode(node).asElement2;
237+
if (element is ExecutableElement2) {
238+
refactoring = ConvertMethodToGetterRefactoring(
239+
refactoringWorkspace,
240+
resolvedUnit.session,
241+
element,
242+
);
246243
}
247244
}
248245
} else if (kind == RefactoringKind.EXTRACT_LOCAL_VARIABLE) {

pkg/analysis_server/test/abstract_single_unit.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import 'package:analyzer/dart/analysis/results.dart';
66
import 'package:analyzer/dart/ast/ast.dart';
77
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/element2.dart';
89
import 'package:analyzer/error/error.dart';
910
import 'package:analyzer/file_system/file_system.dart';
1011
import 'package:analyzer/src/error/codes.g.dart';
1112
import 'package:analyzer/src/test_utilities/find_element.dart';
1213
import 'package:analyzer/src/test_utilities/find_element2.dart';
1314
import 'package:analyzer/src/test_utilities/find_node.dart';
15+
import 'package:analyzer/src/utilities/extensions/element.dart';
1416
import 'package:test/test.dart';
1517

1618
import 'abstract_context.dart';
@@ -28,6 +30,9 @@ class AbstractSingleUnitTest extends AbstractContextTest {
2830
late FindElement findElement;
2931
late FindElement2 findElement2;
3032

33+
LibraryElement2 get testLibraryElement2 =>
34+
testLibraryElement.asElement2 as LibraryElement2;
35+
3136
void addTestSource(String code) {
3237
testCode = code;
3338
newFile(testFile.path, code);

pkg/analysis_server/test/lsp/code_actions_abstract.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@ abstract class AbstractCodeActionsTest extends AbstractLspAnalysisServerTest {
4242
triggerKind: triggerKind,
4343
);
4444

45-
return findAction(codeActions, kind: kind, command: command, title: title)!;
45+
var action = findAction(
46+
codeActions,
47+
kind: kind,
48+
command: command,
49+
title: title,
50+
);
51+
if (action == null) {
52+
fail('Failed to find a code action titled "$title".');
53+
}
54+
return action;
4655
}
4756

4857
/// Expects that command [commandName] was logged to the analytics manager.

pkg/analysis_server/test/services/correction/name_suggestion_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void f() {
3838
}
3939
''');
4040
var excluded = <String>{};
41-
var expectedType = findElement.localVar('node').type;
41+
var expectedType = findElement2.localVar('node').type;
4242
var assignedExpression = findNode.nullLiteral('null;');
4343
var suggestions = getVariableNameSuggestionsForExpression(
4444
expectedType,
@@ -54,7 +54,7 @@ void f() {
5454
double res = 0.0;
5555
}
5656
''');
57-
var expectedType = findElement.localVar('res').type;
57+
var expectedType = findElement2.localVar('res').type;
5858
var assignedExpression = findNode.doubleLiteral('0.0;');
5959
// first choice for "double" is "d"
6060
expect(
@@ -82,7 +82,7 @@ void f() {
8282
int res = 0;
8383
}
8484
''');
85-
var expectedType = findElement.localVar('res').type;
85+
var expectedType = findElement2.localVar('res').type;
8686
var assignedExpression = findNode.integerLiteral('0;');
8787
// first choice for "int" is "i"
8888
expect(
@@ -110,7 +110,7 @@ void f() {
110110
String res = 'abc';
111111
}
112112
''');
113-
var expectedType = findElement.localVar('res').type;
113+
var expectedType = findElement2.localVar('res').type;
114114
var assignedExpression = findNode.stringLiteral("'abc';");
115115
// first choice for "String" is "s"
116116
expect(

0 commit comments

Comments
 (0)