-
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] add
remove_ignore
correction producer
Change-Id: I48ed5c612492e79e1b42ffd8e1522e960981fe63 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404524 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
- Loading branch information
Showing
7 changed files
with
219 additions
and
12 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
pkg/analysis_server/lib/src/services/correction/dart/remove_ignore.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,84 @@ | ||
// Copyright (c) 2025, 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_plugin/edit/dart/correction_producer.dart'; | ||
import 'package:analyzer/source/source_range.dart'; | ||
import 'package:analyzer/src/dart/ast/token.dart'; | ||
import 'package:analyzer/src/ignore_comments/ignore_info.dart'; | ||
import 'package:analyzer/src/utilities/extensions/ast.dart'; | ||
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; | ||
import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; | ||
|
||
class RemoveIgnore extends ResolvedCorrectionProducer { | ||
String _diagnosticName = ''; | ||
RemoveIgnore({required super.context}); | ||
|
||
@override | ||
CorrectionApplicability get applicability => | ||
CorrectionApplicability.singleLocation; | ||
|
||
@override | ||
List<String> get fixArguments => [_diagnosticName]; | ||
|
||
@override | ||
FixKind get fixKind => DartFixKind.REMOVE_IGNORED_DIAGNOSTIC; | ||
|
||
@override | ||
Future<void> compute(ChangeBuilder builder) async { | ||
var diagnostic = this.diagnostic; | ||
if (diagnostic == null) return; | ||
|
||
var diagnosticOffset = diagnostic.problemMessage.offset; | ||
|
||
var comment = node.commentTokenCovering(diagnosticOffset); | ||
if (comment is! CommentToken) return; | ||
|
||
SourceRange? rangeToDelete; | ||
|
||
var ignoredElements = comment.ignoredElements.toList(); | ||
|
||
for (var (index, ignoredElement) in ignoredElements.indexed) { | ||
if (ignoredElement is! IgnoredDiagnosticName) continue; | ||
|
||
var ignoredElementOffset = ignoredElement.offset; | ||
if (ignoredElement.offset == diagnosticOffset) { | ||
_diagnosticName = ignoredElement.name; | ||
var scanBack = index != 0; | ||
var commentText = comment.lexeme; | ||
if (scanBack) { | ||
// Scan back for a preceding comma: | ||
for (var offset = ignoredElementOffset; offset > -1; --offset) { | ||
if (commentText[offset] == ',') { | ||
var backSteps = ignoredElementOffset - offset; | ||
rangeToDelete = SourceRange( | ||
diagnosticOffset - backSteps, | ||
_diagnosticName.length + backSteps, | ||
); | ||
break; | ||
} | ||
} | ||
} else { | ||
// Scan forward for a trailing comma: | ||
var chars = commentText.substring(ignoredElementOffset).indexOf(','); | ||
if (chars == -1) return; | ||
|
||
// Eat the comma | ||
chars++; | ||
|
||
// Eat a trailing space if needed | ||
if (commentText[ignoredElementOffset + chars] == ' ') chars++; | ||
|
||
rangeToDelete = SourceRange(ignoredElementOffset, chars); | ||
} | ||
} | ||
} | ||
|
||
if (rangeToDelete != null) { | ||
await builder.addDartFileEdit(file, (builder) { | ||
builder.addDeletion(rangeToDelete!); | ||
}); | ||
} | ||
} | ||
} |
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
92 changes: 92 additions & 0 deletions
92
pkg/analysis_server/test/src/services/correction/fix/remove_ignore_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,92 @@ | ||
// Copyright (c) 2025, 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:linter/src/lint_names.dart'; | ||
import 'package:test_reflective_loader/test_reflective_loader.dart'; | ||
|
||
import 'fix_processor.dart'; | ||
|
||
void main() { | ||
defineReflectiveSuite(() { | ||
defineReflectiveTests(RemoveUnnecessaryIgnoreTest); | ||
}); | ||
} | ||
|
||
// TODO(pq): add bulk fix tests | ||
@reflectiveTest | ||
class RemoveUnnecessaryIgnoreTest extends FixProcessorLintTest { | ||
@override | ||
FixKind get kind => DartFixKind.REMOVE_IGNORED_DIAGNOSTIC; | ||
|
||
@override | ||
String get lintCode => LintNames.unnecessary_ignore; | ||
|
||
Future<void> test_file_first() async { | ||
await resolveTestCode(''' | ||
// ignore_for_file: unused_local_variable, return_of_invalid_type | ||
int f() => null; | ||
'''); | ||
await assertHasFix(''' | ||
// ignore_for_file: return_of_invalid_type | ||
int f() => null; | ||
'''); | ||
} | ||
|
||
Future<void> test_file_last() async { | ||
await resolveTestCode(''' | ||
// ignore_for_file: return_of_invalid_type, unused_local_variable | ||
int f() => null; | ||
'''); | ||
await assertHasFix(''' | ||
// ignore_for_file: return_of_invalid_type | ||
int f() => null; | ||
'''); | ||
} | ||
|
||
Future<void> test_file_middle() async { | ||
await resolveTestCode(''' | ||
// ignore_for_file: return_of_invalid_type, unused_local_variable, non_bool_negation_expression | ||
int f() => !null; | ||
'''); | ||
await assertHasFix(''' | ||
// ignore_for_file: return_of_invalid_type, non_bool_negation_expression | ||
int f() => !null; | ||
'''); | ||
} | ||
|
||
Future<void> test_line_first() async { | ||
await resolveTestCode(''' | ||
// ignore: unused_local_variable, return_of_invalid_type | ||
int f() => null; | ||
'''); | ||
await assertHasFix(''' | ||
// ignore: return_of_invalid_type | ||
int f() => null; | ||
'''); | ||
} | ||
|
||
Future<void> test_line_last() async { | ||
await resolveTestCode(''' | ||
// ignore: return_of_invalid_type, unused_local_variable | ||
int f() => null; | ||
'''); | ||
await assertHasFix(''' | ||
// ignore: return_of_invalid_type | ||
int f() => null; | ||
'''); | ||
} | ||
|
||
Future<void> test_line_middle() async { | ||
await resolveTestCode(''' | ||
// ignore: return_of_invalid_type, unused_local_variable, non_bool_negation_expression | ||
int f() => !null; | ||
'''); | ||
await assertHasFix(''' | ||
// ignore: return_of_invalid_type, non_bool_negation_expression | ||
int f() => !null; | ||
'''); | ||
} | ||
} |
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