Skip to content

Commit

Permalink
Migrate more server tests
Browse files Browse the repository at this point in the history
Change-Id: Iee3e6b6db9c58e9fcf790e2874f73c8eac6e660d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395404
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
bwilkerson authored and Commit Queue committed Nov 15, 2024
1 parent 359aa74 commit 34d945c
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 88 deletions.
4 changes: 0 additions & 4 deletions pkg/analysis_server/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,8 @@ lib/src/utilities/extensions/completion_request.dart
lib/src/utilities/extensions/element.dart
test/abstract_single_unit.dart
test/plugin/protocol_dart_test.dart
test/services/correction/name_suggestion_test.dart
test/services/correction/status_test.dart
test/services/refactoring/legacy/abstract_rename.dart
test/services/refactoring/legacy/convert_getter_to_method_test.dart
test/services/refactoring/legacy/convert_method_to_getter_test.dart
test/services/refactoring/legacy/extract_method_test.dart
test/services/refactoring/legacy/rename_class_member_test.dart
test/services/refactoring/legacy/rename_constructor_test.dart
test/services/refactoring/legacy/rename_import_test.dart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/dart/ast/utilities.dart';
import 'package:analyzer/src/util/file_paths.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart';

/// The handler for the `edit.getAvailableRefactorings` request.
Expand Down Expand Up @@ -88,7 +89,7 @@ class EditGetAvailableRefactoringsHandler extends LegacyHandler {
if (ConvertMethodToGetterRefactoring(
refactoringWorkspace,
resolvedUnit.session,
element,
element.asElement2,
).isAvailable()) {
kinds.add(RefactoringKind.CONVERT_METHOD_TO_GETTER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import 'package:analysis_server_plugin/src/correction/fix_processor.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/analysis/session.dart'
show InconsistentAnalysisException;
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/src/dart/ast/utilities.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';

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

// Getter to Method
if (element is PropertyAccessorElement &&
if (element is GetterElement &&
ConvertGetterToMethodRefactoring(
server.refactoringWorkspace,
unit.session,
Expand All @@ -339,7 +340,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
}

// Method to Getter
if (element is ExecutableElement &&
if (element is ExecutableElement2 &&
ConvertMethodToGetterRefactoring(
server.refactoringWorkspace,
unit.session,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import 'package:analysis_server/src/lsp/progress.dart';
import 'package:analysis_server/src/protocol_server.dart';
import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/src/dart/ast/utilities.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';

final _manager = LspRefactorManager._();

Expand Down Expand Up @@ -126,38 +127,34 @@ abstract class AbstractRefactorCommandHandler extends SimpleEditCommandHandler

case RefactoringKind.CONVERT_GETTER_TO_METHOD:
var node = NodeLocator(offset).searchWithin(result.unit);
var element = server.getElementOfNode(node);
if (element != null) {
if (element is PropertyAccessorElement) {
var refactor = ConvertGetterToMethodRefactoring(
server.refactoringWorkspace,
result.session,
element,
);
return success(refactor);
}
var element = server.getElementOfNode(node).asElement2;
if (element is GetterElement) {
var refactor = ConvertGetterToMethodRefactoring(
server.refactoringWorkspace,
result.session,
element,
);
return success(refactor);
}
return error(
ServerErrorCodes.InvalidCommandArguments,
'Location supplied to $commandName $kind is not longer valid',
'Location supplied to $commandName $kind is no longer valid',
);

case RefactoringKind.CONVERT_METHOD_TO_GETTER:
var node = NodeLocator(offset).searchWithin(result.unit);
var element = server.getElementOfNode(node);
if (element != null) {
if (element is ExecutableElement) {
var refactor = ConvertMethodToGetterRefactoring(
server.refactoringWorkspace,
result.session,
element,
);
return success(refactor);
}
var element = server.getElementOfNode(node).asElement2;
if (element is ExecutableElement2) {
var refactor = ConvertMethodToGetterRefactoring(
server.refactoringWorkspace,
result.session,
element,
);
return success(refactor);
}
return error(
ServerErrorCodes.InvalidCommandArguments,
'Location supplied to $commandName $kind is not longer valid',
'Location supplied to $commandName $kind is no longer valid',
);

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/dart/element/type_system.dart';
Expand All @@ -35,6 +36,7 @@ import 'package:analyzer/src/dart/ast/utilities.dart';
import 'package:analyzer/src/dart/resolver/exit_detector.dart';
import 'package:analyzer/src/generated/java_core.dart';
import 'package:analyzer/src/utilities/extensions/ast.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:analyzer/src/utilities/extensions/string.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
import 'package:meta/meta.dart';
Expand All @@ -47,16 +49,19 @@ const String _tokenSeparator = '\uFFFF';
Future<void> addLibraryImports(
AnalysisSession session,
SourceChange change,
LibraryElement targetLibrary,
LibraryElement2 targetLibrary2,
Set<Source> libraries,
) async {
var libraryPath = targetLibrary.source.fullName;
var libraryPath = targetLibrary2.firstFragment.source.fullName;

var resolveResult = await session.getResolvedUnit(libraryPath);
if (resolveResult is! ResolvedUnitResult) {
return;
}

// TODO(brianwilkerson): Use `targetLibrary2` everywhere below and rename it
// to `targetLibrary`.
var targetLibrary = targetLibrary2.asElement as LibraryElement;
var libUtils = CorrectionUtils(resolveResult);
var eol = libUtils.endOfLine;
// Prepare information about existing imports.
Expand Down Expand Up @@ -582,7 +587,7 @@ final class ExtractMethodRefactoringImpl extends RefactoringImpl
await addLibraryImports(
_resolveResult.session,
change,
_resolveResult.libraryElement,
_resolveResult.libraryElement2,
_librariesToImport,
);
return change;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ abstract class ConvertGetterToMethodRefactoring implements Refactoring {
factory ConvertGetterToMethodRefactoring(
RefactoringWorkspace workspace,
AnalysisSession session,
PropertyAccessorElement element,
GetterElement element,
) {
return ConvertGetterToMethodRefactoringImpl(workspace, session, element);
return ConvertGetterToMethodRefactoringImpl(
workspace,
session,
element.asElement as PropertyAccessorElement,
);
}

/// Return `true` if refactoring is available, possibly without checking all
Expand All @@ -67,9 +71,13 @@ abstract class ConvertMethodToGetterRefactoring implements Refactoring {
factory ConvertMethodToGetterRefactoring(
RefactoringWorkspace workspace,
AnalysisSession session,
ExecutableElement element,
ExecutableElement2 element,
) {
return ConvertMethodToGetterRefactoringImpl(workspace, session, element);
return ConvertMethodToGetterRefactoringImpl(
workspace,
session,
element.asElement,
);
}

/// Return `true` if refactoring is available, possibly without checking all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart
import 'package:analysis_server/src/services/search/search_engine.dart';
import 'package:analyzer/dart/analysis/session.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/src/dart/ast/utilities.dart';
import 'package:analyzer/src/utilities/cancellation.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';

int test_resetCount = 0;

Expand Down Expand Up @@ -219,30 +220,26 @@ class RefactoringManager {
var resolvedUnit = await server.getResolvedUnit(file);
if (resolvedUnit != null) {
var node = NodeLocator(offset).searchWithin(resolvedUnit.unit);
var element = server.getElementOfNode(node);
if (element != null) {
if (element is PropertyAccessorElement) {
refactoring = ConvertGetterToMethodRefactoring(
refactoringWorkspace,
resolvedUnit.session,
element,
);
}
var element = server.getElementOfNode(node).asElement2;
if (element is GetterElement) {
refactoring = ConvertGetterToMethodRefactoring(
refactoringWorkspace,
resolvedUnit.session,
element,
);
}
}
} else if (kind == RefactoringKind.CONVERT_METHOD_TO_GETTER) {
var resolvedUnit = await server.getResolvedUnit(file);
if (resolvedUnit != null) {
var node = NodeLocator(offset).searchWithin(resolvedUnit.unit);
var element = server.getElementOfNode(node);
if (element != null) {
if (element is ExecutableElement) {
refactoring = ConvertMethodToGetterRefactoring(
refactoringWorkspace,
resolvedUnit.session,
element,
);
}
var element = server.getElementOfNode(node).asElement2;
if (element is ExecutableElement2) {
refactoring = ConvertMethodToGetterRefactoring(
refactoringWorkspace,
resolvedUnit.session,
element,
);
}
}
} else if (kind == RefactoringKind.EXTRACT_LOCAL_VARIABLE) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/analysis_server/test/abstract_single_unit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/src/error/codes.g.dart';
import 'package:analyzer/src/test_utilities/find_element.dart';
import 'package:analyzer/src/test_utilities/find_element2.dart';
import 'package:analyzer/src/test_utilities/find_node.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:test/test.dart';

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

LibraryElement2 get testLibraryElement2 =>
testLibraryElement.asElement2 as LibraryElement2;

void addTestSource(String code) {
testCode = code;
newFile(testFile.path, code);
Expand Down
11 changes: 10 additions & 1 deletion pkg/analysis_server/test/lsp/code_actions_abstract.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ abstract class AbstractCodeActionsTest extends AbstractLspAnalysisServerTest {
triggerKind: triggerKind,
);

return findAction(codeActions, kind: kind, command: command, title: title)!;
var action = findAction(
codeActions,
kind: kind,
command: command,
title: title,
);
if (action == null) {
fail('Failed to find a code action titled "$title".');
}
return action;
}

/// Expects that command [commandName] was logged to the analytics manager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void f() {
}
''');
var excluded = <String>{};
var expectedType = findElement.localVar('node').type;
var expectedType = findElement2.localVar('node').type;
var assignedExpression = findNode.nullLiteral('null;');
var suggestions = getVariableNameSuggestionsForExpression(
expectedType,
Expand All @@ -54,7 +54,7 @@ void f() {
double res = 0.0;
}
''');
var expectedType = findElement.localVar('res').type;
var expectedType = findElement2.localVar('res').type;
var assignedExpression = findNode.doubleLiteral('0.0;');
// first choice for "double" is "d"
expect(
Expand Down Expand Up @@ -82,7 +82,7 @@ void f() {
int res = 0;
}
''');
var expectedType = findElement.localVar('res').type;
var expectedType = findElement2.localVar('res').type;
var assignedExpression = findNode.integerLiteral('0;');
// first choice for "int" is "i"
expect(
Expand Down Expand Up @@ -110,7 +110,7 @@ void f() {
String res = 'abc';
}
''');
var expectedType = findElement.localVar('res').type;
var expectedType = findElement2.localVar('res').type;
var assignedExpression = findNode.stringLiteral("'abc';");
// first choice for "String" is "s"
expect(
Expand Down
Loading

0 comments on commit 34d945c

Please sign in to comment.