From c9d954efe7c7a71d2bf2c2ca385855b82fbfb379 Mon Sep 17 00:00:00 2001 From: Ahmed Ashour Date: Tue, 17 Aug 2021 04:40:52 +0000 Subject: [PATCH] `SPLIT_VARIABLE_DECLARATION` assist to handle unreferenceable type Bug: 46910 Change-Id: I92c5e4afb25a75e1c1c28fb6b52c9bb02ca81eb6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/210127 Reviewed-by: Brian Wilkerson Commit-Queue: Brian Wilkerson --- .../dart/split_variable_declaration.dart | 37 +++++++++++++------ .../split_variable_declaration_test.dart | 18 +++++++++ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/pkg/analysis_server/lib/src/services/correction/dart/split_variable_declaration.dart b/pkg/analysis_server/lib/src/services/correction/dart/split_variable_declaration.dart index 83073d8ece5b7..b6b39107a7883 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/split_variable_declaration.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/split_variable_declaration.dart @@ -6,6 +6,7 @@ import 'package:_fe_analyzer_shared/src/scanner/token.dart'; import 'package:analysis_server/src/services/correction/assist.dart'; import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart'; import 'package:analyzer/dart/ast/ast.dart'; +import 'package:analyzer_plugin/src/utilities/change_builder/change_builder_core.dart'; import 'package:analyzer_plugin/utilities/assist/assist.dart'; import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; import 'package:analyzer_plugin/utilities/range_factory.dart'; @@ -50,22 +51,34 @@ class SplitVariableDeclaration extends CorrectionProducer { return; } - await builder.addDartFileEdit(file, (builder) { - if (variableList.type == null) { - final type = variable.declaredElement!.type; - if (!type.isDynamic && keyword != null) { - builder.addReplacement(range.token(keyword), (builder) { - builder.writeType(type); - }); + Future tryToApplyChange(ChangeBuilder builder) async { + var validChange = true; + await builder.addDartFileEdit(file, (builder) { + if (variableList.type == null) { + final type = variable.declaredElement!.type; + if (!type.isDynamic && keyword != null) { + builder.addReplacement(range.token(keyword), (builder) { + validChange = builder.writeType(type); + }); + } } - } - var indent = utils.getNodePrefix(statement); - var name = variable.name.name; - builder.addSimpleInsertion(variable.name.end, ';' + eol + indent + name); - }); + var indent = utils.getNodePrefix(statement); + var name = variable.name.name; + builder.addSimpleInsertion( + variable.name.end, ';' + eol + indent + name); + }); + return validChange; + } + + if (await tryToApplyChange(_temporaryBuilder(builder))) { + await tryToApplyChange(builder); + } } + ChangeBuilder _temporaryBuilder(ChangeBuilder builder) => + ChangeBuilder(workspace: (builder as ChangeBuilderImpl).workspace); + /// Return an instance of this class. Used as a tear-off in `AssistProcessor`. static SplitVariableDeclaration newInstance() => SplitVariableDeclaration(); } diff --git a/pkg/analysis_server/test/src/services/correction/assist/split_variable_declaration_test.dart b/pkg/analysis_server/test/src/services/correction/assist/split_variable_declaration_test.dart index 255f6e547e8fe..cca64e3aa0415 100644 --- a/pkg/analysis_server/test/src/services/correction/assist/split_variable_declaration_test.dart +++ b/pkg/analysis_server/test/src/services/correction/assist/split_variable_declaration_test.dart @@ -118,4 +118,22 @@ main() { } '''); } + + Future test_unknownType() async { + addSource('/home/test/lib/a.dart', ''' +class A { + _B b => _B(); +} +class _B {} +'''); + + await resolveTestCode(''' +import 'package:test/a.dart'; + +f(A a) { + var x = a.b(); +} +'''); + await assertNoAssistAt('var '); + } }