-
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.
[macros] quick fix for `CompileTimeErrorCode.AUGMENTATION_MODIFIER_EX…
…TRA` See: Change-Id: I95e1a8d1e5a21c8f00570fe7dc149c308a832750 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366066 Commit-Queue: Phil Quitslund <pquitslund@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
- Loading branch information
Showing
6 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
pkg/analysis_server/lib/src/services/correction/dart/remove_extra_modifier.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) 2024, 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/source/source_range.dart'; | ||
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; | ||
import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; | ||
|
||
class RemoveExtraModifier extends ResolvedCorrectionProducer { | ||
String _modifierName = ''; | ||
|
||
@override | ||
bool get canBeAppliedInBulk => true; | ||
|
||
@override | ||
bool get canBeAppliedToFile => true; | ||
|
||
@override | ||
List<String> get fixArguments => [_modifierName]; | ||
|
||
@override | ||
FixKind get fixKind => DartFixKind.REMOVE_EXTRA_MODIFIER; | ||
|
||
@override | ||
FixKind get multiFixKind => DartFixKind.REMOVE_EXTRA_MODIFIER_MULTI; | ||
|
||
@override | ||
Future<void> compute(ChangeBuilder builder) async { | ||
var diagnostic = this.diagnostic; | ||
if (diagnostic == null) return; | ||
|
||
var problemMessage = diagnostic.problemMessage; | ||
|
||
// Extract the modifier. | ||
var message = problemMessage.messageText(includeUrl: false); | ||
var modifierStart = message.indexOf("'") + 1; | ||
var modifierStop = message.indexOf("'", modifierStart); | ||
|
||
_modifierName = message.substring(modifierStart, modifierStop); | ||
|
||
await builder.addDartFileEdit(file, (builder) { | ||
builder.addDeletion( | ||
SourceRange( | ||
problemMessage.offset, | ||
// TODO(pq): consider a CorrectionUtils utility to get first non whitespace offset. | ||
_modifierName.length + 1, | ||
), | ||
); | ||
}); | ||
} | ||
} |
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
72 changes: 72 additions & 0 deletions
72
pkg/analysis_server/test/src/services/correction/fix/remove_extra_modifier_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,72 @@ | ||
// Copyright (c) 2024, 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/src/error/codes.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(RemoveExtraModifierMultiTest); | ||
defineReflectiveTests(RemoveExtraModifierTest); | ||
}); | ||
} | ||
|
||
@reflectiveTest | ||
class RemoveExtraModifierMultiTest extends FixProcessorTest { | ||
@override | ||
FixKind get kind => DartFixKind.REMOVE_EXTRA_MODIFIER_MULTI; | ||
|
||
Future<void> test_singleFile() async { | ||
newFile('$testPackageLibPath/a.dart', ''' | ||
import augment 'test.dart'; | ||
class A { } | ||
'''); | ||
|
||
await resolveTestCode(''' | ||
augment library 'a.dart'; | ||
augment abstract class A {} | ||
augment final class A {} | ||
'''); | ||
await assertHasFixAllFix( | ||
CompileTimeErrorCode.AUGMENTATION_MODIFIER_EXTRA, ''' | ||
augment library 'a.dart'; | ||
augment class A {} | ||
augment class A {} | ||
'''); | ||
} | ||
} | ||
|
||
@reflectiveTest | ||
class RemoveExtraModifierTest extends FixProcessorTest { | ||
@override | ||
FixKind get kind => DartFixKind.REMOVE_EXTRA_MODIFIER; | ||
|
||
Future<void> test_it() async { | ||
newFile('$testPackageLibPath/a.dart', ''' | ||
import augment 'test.dart'; | ||
class A { } | ||
'''); | ||
|
||
await resolveTestCode(''' | ||
augment library 'a.dart'; | ||
augment abstract class A {} | ||
'''); | ||
await assertHasFix(''' | ||
augment library 'a.dart'; | ||
augment class A {} | ||
'''); | ||
} | ||
} |
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