Skip to content

Commit

Permalink
Migrate inline_local
Browse files Browse the repository at this point in the history
Change-Id: Id743d49f0691cb9f9b4415b234fdccad69a72c8f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/397102
Commit-Queue: Brian Wilkerson <[email protected]>
Reviewed-by: Konstantin Shcheglov <[email protected]>
  • Loading branch information
bwilkerson authored and Commit Queue committed Nov 22, 2024
1 parent 132ad35 commit de9f149
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
1 change: 0 additions & 1 deletion pkg/analysis_server/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ lib/src/services/kythe/kythe_visitors.dart
lib/src/services/refactoring/agnostic/change_method_signature.dart
lib/src/services/refactoring/framework/formal_parameter.dart
lib/src/services/refactoring/legacy/extract_method.dart
lib/src/services/refactoring/legacy/inline_local.dart
lib/src/services/refactoring/legacy/move_file.dart
lib/src/services/refactoring/legacy/refactoring.dart
lib/src/services/refactoring/legacy/refactoring_internal.dart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:analysis_server_plugin/edit/correction_utils.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/src/dart/analysis/session_helper.dart';
import 'package:analyzer/src/dart/ast/utilities.dart';
import 'package:analyzer/src/generated/java_core.dart';
Expand Down Expand Up @@ -41,7 +41,7 @@ class InlineLocalRefactoringImpl extends RefactoringImpl

@override
String? get variableName {
return _initialState?.element.name;
return _initialState?.element.name3;
}

@override
Expand All @@ -53,20 +53,22 @@ class InlineLocalRefactoringImpl extends RefactoringImpl
@override
Future<RefactoringStatus> checkInitialConditions() async {
// prepare variable
Element? element;
Element2? element;
var offsetNode = NodeLocator(offset).searchWithin(resolveResult.unit);
if (offsetNode is SimpleIdentifier) {
element = offsetNode.staticElement;
element = offsetNode.element;
} else if (offsetNode is VariableDeclaration) {
element = offsetNode.declaredElement;
element = offsetNode.declaredFragment?.element;
}

if (element is! LocalVariableElement) {
if (element is! LocalVariableElement2) {
return _noLocalVariableStatus();
}

var helper = AnalysisSessionHelper(resolveResult.session);
var declarationResult = await helper.getElementDeclaration(element);
var declarationResult = await helper.getElementDeclaration2(
element.firstFragment,
);
var node = declarationResult?.node;
if (node is! VariableDeclaration) {
return _noLocalVariableStatus();
Expand All @@ -86,7 +88,7 @@ class InlineLocalRefactoringImpl extends RefactoringImpl
return RefactoringStatus.fatal(message, newLocation_fromNode(node));
}
// prepare references
var references = await searchEngine.searchReferences(element);
var references = await searchEngine.searchReferences2(element);
// should not have assignments
for (var reference in references) {
if (reference.kind != MatchKind.READ) {
Expand Down Expand Up @@ -114,14 +116,14 @@ class InlineLocalRefactoringImpl extends RefactoringImpl
@override
Future<SourceChange> createChange() {
var change = SourceChange(refactoringName);
var unitElement = resolveResult.unit.declaredElement!;
var libraryFragment = resolveResult.unit.declaredFragment!;
var state = _initialState!;
// remove declaration
{
var range = utils.getLinesRangeStatements([state.declarationStatement]);
doSourceChange_addElementEdit(
doSourceChange_addFragmentEdit(
change,
unitElement,
libraryFragment,
newSourceEdit_range(range, ''),
);
}
Expand Down Expand Up @@ -160,9 +162,9 @@ class InlineLocalRefactoringImpl extends RefactoringImpl
codeForReference = initializerCode;
}
// do replace
doSourceChange_addElementEdit(
doSourceChange_addFragmentEdit(
change,
unitElement,
libraryFragment,
newSourceEdit_range(editRange, codeForReference),
);
}
Expand All @@ -177,15 +179,15 @@ class InlineLocalRefactoringImpl extends RefactoringImpl

/// Checks if [offset] is a variable that can be inlined.
RefactoringStatus _checkOffset() {
Element? element;
Element2? element;
var offsetNode = NodeLocator(offset).searchWithin(resolveResult.unit);
if (offsetNode is SimpleIdentifier) {
element = offsetNode.staticElement;
element = offsetNode.element;
} else if (offsetNode is VariableDeclaration) {
element = offsetNode.declaredElement;
element = offsetNode.declaredFragment?.element;
}

if (element is! LocalVariableElement) {
if (element is! LocalVariableElement2) {
return _noLocalVariableStatus();
}

Expand Down Expand Up @@ -256,7 +258,7 @@ class InlineLocalRefactoringImpl extends RefactoringImpl
}

class _InitialState {
final LocalVariableElement element;
final LocalVariableElement2 element;
final VariableDeclaration node;
final Expression initializer;
final VariableDeclarationStatement declarationStatement;
Expand Down

0 comments on commit de9f149

Please sign in to comment.