Skip to content

Commit

Permalink
Issue 51931. Fix for LATE_PATTERN_VARIABLE_DECLARATION.
Browse files Browse the repository at this point in the history
Bug: #51931
Change-Id: I2b3c42389a5184086e66a1fc35c045d2581a2d8b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/293004
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Apr 3, 2023
1 parent 693367b commit 4cb8bd0
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';

class RemoveLate extends CorrectionProducer {
@override
bool get canBeAppliedInBulk => true;

@override
bool get canBeAppliedToFile => true;

@override
FixKind get fixKind => DartFixKind.REMOVE_LATE;

@override
FixKind get multiFixKind => DartFixKind.REMOVE_LATE_MULTI;

_LateKeywordLocation? get _lateKeywordLocation {
final node = this.node;
if (node is Block) {
// The `late` token does not belong any node, so when we look for a
// node that covers it, we find the enclosing `Block`. So, we iterate
// over statements to find the actual declaration statement.
for (final statement in node.statements) {
if (statement is PatternVariableDeclarationStatement) {
final beginToken = statement.beginToken;
final lateKeyword = beginToken.previous;
if (lateKeyword != null &&
lateKeyword.keyword == Keyword.LATE &&
lateKeyword.offset == selectionOffset &&
lateKeyword.end == selectionEnd) {
return _LateKeywordLocation(
lateKeyword: lateKeyword,
nextToken: beginToken,
);
}
}
}
}

return null;
}

@override
Future<void> compute(ChangeBuilder builder) async {
final location = _lateKeywordLocation;
if (location != null) {
await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(
range.startStart(
location.lateKeyword,
location.nextToken,
),
);
});
}
}
}

class _LateKeywordLocation {
final Token lateKeyword;
final Token nextToken;

_LateKeywordLocation({
required this.lateKeyword,
required this.nextToken,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2528,7 +2528,7 @@ ParserErrorCode.INVALID_UNICODE_ESCAPE_U_STARTED:
ParserErrorCode.INVALID_USE_OF_COVARIANT_IN_EXTENSION:
status: needsEvaluation
ParserErrorCode.LATE_PATTERN_VARIABLE_DECLARATION:
status: needsEvaluation
status: hasFix
ParserErrorCode.LIBRARY_DIRECTIVE_NOT_FIRST:
status: needsEvaluation
ParserErrorCode.LITERAL_WITH_CLASS_AND_NEW:
Expand Down
10 changes: 10 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,16 @@ class DartFixKind {
DartFixKindPriority.IN_FILE,
'Remove unnecessary interpolation braces everywhere in file',
);
static const REMOVE_LATE = FixKind(
'dart.fix.remove.late',
DartFixKindPriority.DEFAULT,
"Remove the 'late' keyword",
);
static const REMOVE_LATE_MULTI = FixKind(
'dart.fix.remove.late.multi',
DartFixKindPriority.DEFAULT,
"Remove the 'late' keyword everywhere in file",
);
static const REMOVE_LEADING_UNDERSCORE = FixKind(
'dart.fix.remove.leadingUnderscore',
DartFixKindPriority.DEFAULT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ import 'package:analysis_server/src/services/correction/dart/remove_if_null_oper
import 'package:analysis_server/src/services/correction/dart/remove_initializer.dart';
import 'package:analysis_server/src/services/correction/dart/remove_interpolation_braces.dart';
import 'package:analysis_server/src/services/correction/dart/remove_invocation.dart';
import 'package:analysis_server/src/services/correction/dart/remove_late.dart';
import 'package:analysis_server/src/services/correction/dart/remove_leading_underscore.dart';
import 'package:analysis_server/src/services/correction/dart/remove_method_declaration.dart';
import 'package:analysis_server/src/services/correction/dart/remove_name_from_combinator.dart';
Expand Down Expand Up @@ -1418,6 +1419,9 @@ class FixProcessor extends BaseProcessor {
ParserErrorCode.INVALID_INSIDE_UNARY_PATTERN: [
SurroundWithParentheses.new,
],
ParserErrorCode.LATE_PATTERN_VARIABLE_DECLARATION: [
RemoveLate.new,
],
ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE: [
AddTypeAnnotation.new,
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import 'fix_processor.dart';

void main() {
defineReflectiveSuite(() {
defineReflectiveTests(RemoveLateBulkTest);
defineReflectiveTests(RemoveLateTest);
});
}

@reflectiveTest
class RemoveLateBulkTest extends BulkFixProcessorTest {
Future<void> test_singleFile() async {
await resolveTestCode('''
void f(Object? x) {
late var (_) = x;
late var (_) = x;
}
''');
await assertHasFix('''
void f(Object? x) {
var (_) = x;
var (_) = x;
}
''');
}
}

@reflectiveTest
class RemoveLateTest extends FixProcessorTest {
@override
FixKind get kind => DartFixKind.REMOVE_LATE;

Future<void> test_it() async {
await resolveTestCode('''
void f(Object? x) {
late var (_) = x;
}
''');
await assertHasFix('''
void f(Object? x) {
var (_) = x;
}
''');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ import 'remove_if_null_operator_test.dart' as remove_if_null_operator;
import 'remove_initializer_test.dart' as remove_initializer;
import 'remove_interpolation_braces_test.dart' as remove_interpolation_braces;
import 'remove_invocation_test.dart' as remove_invocation;
import 'remove_late_test.dart' as remove_late;
import 'remove_leading_underscore_test.dart' as remove_leading_underscore;
import 'remove_method_declaration_test.dart' as remove_method_declaration;
import 'remove_name_from_combinator_test.dart' as remove_name_from_combinator;
Expand Down Expand Up @@ -408,6 +409,7 @@ void main() {
remove_initializer.main();
remove_interpolation_braces.main();
remove_invocation.main();
remove_late.main();
remove_leading_underscore.main();
remove_method_declaration.main();
remove_name_from_combinator.main();
Expand Down

0 comments on commit 4cb8bd0

Please sign in to comment.