-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 51931. Fix for LATE_PATTERN_VARIABLE_DECLARATION.
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
Showing
6 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
pkg/analysis_server/lib/src/services/correction/dart/remove_late.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
pkg/analysis_server/test/src/services/correction/fix/remove_late_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
'''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters