Skip to content

Commit

Permalink
SPLIT_VARIABLE_DECLARATION assist to handle unreferenceable type
Browse files Browse the repository at this point in the history
Bug: 46910
Change-Id: I92c5e4afb25a75e1c1c28fb6b52c9bb02ca81eb6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/210127
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
asashour authored and commit-bot@chromium.org committed Aug 17, 2021
1 parent 5657acf commit c9d954e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<bool> 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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,22 @@ main() {
}
''');
}

Future<void> 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 ');
}
}

0 comments on commit c9d954e

Please sign in to comment.