-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix for unnecessary_null_aware_assignments
Fixes: dart-lang/lints#66 Change-Id: Ib14902c76910d550fbf69161a257d59cd268be9c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/236701 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Phil Quitslund <pquitslund@google.com>
- Loading branch information
Showing
7 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
pkg/analysis_server/lib/src/services/correction/dart/remove_assignment.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,52 @@ | ||
// Copyright (c) 2022, 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/source/source_range.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 RemoveAssignment extends CorrectionProducer { | ||
@override | ||
bool get canBeAppliedInBulk => true; | ||
|
||
@override | ||
bool get canBeAppliedToFile => true; | ||
|
||
@override | ||
FixKind get fixKind => DartFixKind.REMOVE_ASSIGNMENT; | ||
|
||
@override | ||
FixKind get multiFixKind => DartFixKind.REMOVE_ASSIGNMENT_MULTI; | ||
|
||
@override | ||
Future<void> compute(ChangeBuilder builder) async { | ||
var expression = node; | ||
if (expression is! AssignmentExpression) { | ||
return; | ||
} | ||
|
||
SourceRange sourceRange; | ||
var parent = expression.parent; | ||
while (parent is ParenthesizedExpression) { | ||
parent = parent.parent; | ||
} | ||
if (parent is ExpressionStatement) { | ||
sourceRange = utils.getLinesRange(range.node(parent)); | ||
} else { | ||
sourceRange = range.endEnd( | ||
expression.leftHandSide.endToken, expression.rightHandSide.endToken); | ||
} | ||
|
||
await builder.addDartFileEdit(file, (builder) { | ||
builder.addDeletion(sourceRange); | ||
}); | ||
} | ||
|
||
/// Return an instance of this class. Used as a tear-off in `FixProcessor`. | ||
static RemoveAssignment newInstance() => RemoveAssignment(); | ||
} |
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
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
88 changes: 88 additions & 0 deletions
88
pkg/analysis_server/test/src/services/correction/fix/remove_assignment_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,88 @@ | ||
// Copyright (c) 2022, 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:analysis_server/src/services/linter/lint_names.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(RemoveAssignmentBulkTest); | ||
defineReflectiveTests(RemoveAssignmentTest); | ||
}); | ||
} | ||
|
||
@reflectiveTest | ||
class RemoveAssignmentBulkTest extends BulkFixProcessorTest { | ||
@override | ||
String get lintCode => LintNames.unnecessary_null_aware_assignments; | ||
|
||
Future<void> test_singleFile() async { | ||
await resolveTestCode(''' | ||
void f() { | ||
var x; | ||
var y; | ||
x ??= null; | ||
y ??= null; | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
void f() { | ||
var x; | ||
var y; | ||
} | ||
'''); | ||
} | ||
} | ||
|
||
@reflectiveTest | ||
class RemoveAssignmentTest extends FixProcessorLintTest { | ||
@override | ||
FixKind get kind => DartFixKind.REMOVE_ASSIGNMENT; | ||
|
||
@override | ||
String get lintCode => LintNames.unnecessary_null_aware_assignments; | ||
|
||
Future<void> test_assignment() async { | ||
await resolveTestCode(''' | ||
void f() { | ||
var x; | ||
x ??= null; | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
void f() { | ||
var x; | ||
} | ||
'''); | ||
} | ||
|
||
Future<void> test_assignment_compound() async { | ||
await resolveTestCode(''' | ||
void f(x, y) { | ||
y = x ??= null; | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
void f(x, y) { | ||
y = x; | ||
} | ||
'''); | ||
} | ||
|
||
Future<void> test_assignment_parenthesized() async { | ||
await resolveTestCode(''' | ||
void f(int? x) { | ||
(x ??= null); | ||
} | ||
'''); | ||
await assertHasFix(''' | ||
void f(int? 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