Skip to content

Commit

Permalink
[macros] quick fix for `CompileTimeErrorCode.AUGMENTATION_MODIFIER_EX…
Browse files Browse the repository at this point in the history
…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
pq authored and Commit Queue committed May 13, 2024
1 parent d0a2ba8 commit 4f5bd1c
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 1 deletion.
@@ -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,
),
);
});
}
}
Expand Up @@ -210,7 +210,7 @@ CompileTimeErrorCode.ASSIGNMENT_TO_TYPE:
CompileTimeErrorCode.ASYNC_FOR_IN_WRONG_CONTEXT:
status: hasFix
CompileTimeErrorCode.AUGMENTATION_MODIFIER_EXTRA:
status: needsFix
status: hasFix
CompileTimeErrorCode.AUGMENTATION_MODIFIER_MISSING:
status: needsFix
CompileTimeErrorCode.AUGMENTATION_OF_DIFFERENT_DECLARATION_KIND:
Expand Down
10 changes: 10 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix.dart
Expand Up @@ -1075,6 +1075,16 @@ class DartFixKind {
DartFixKindPriority.IN_FILE,
'Remove empty statements everywhere in file',
);
static const REMOVE_EXTRA_MODIFIER = FixKind(
'dart.fix.remove.extra.modifier',
DartFixKindPriority.DEFAULT,
'Remove extra {0} modifier',
);
static const REMOVE_EXTRA_MODIFIER_MULTI = FixKind(
'dart.fix.remove.extra.modifier.multi',
DartFixKindPriority.IN_FILE,
'Remove extra modifiers everywhere in file',
);
static const REMOVE_IF_NULL_OPERATOR = FixKind(
'dart.fix.remove.ifNullOperator',
DartFixKindPriority.DEFAULT,
Expand Down
Expand Up @@ -135,6 +135,7 @@ import 'package:analysis_server/src/services/correction/dart/remove_empty_catch.
import 'package:analysis_server/src/services/correction/dart/remove_empty_constructor_body.dart';
import 'package:analysis_server/src/services/correction/dart/remove_empty_else.dart';
import 'package:analysis_server/src/services/correction/dart/remove_empty_statement.dart';
import 'package:analysis_server/src/services/correction/dart/remove_extra_modifier.dart';
import 'package:analysis_server/src/services/correction/dart/remove_if_null_operator.dart';
import 'package:analysis_server/src/services/correction/dart/remove_initializer.dart';
import 'package:analysis_server/src/services/correction/dart/remove_interpolation_braces.dart';
Expand Down Expand Up @@ -864,6 +865,9 @@ final _builtInNonLintProducers = <ErrorCode, List<ProducerGenerator>>{
CompileTimeErrorCode.ASYNC_FOR_IN_WRONG_CONTEXT: [
AddAsync.new,
],
CompileTimeErrorCode.AUGMENTATION_MODIFIER_EXTRA: [
RemoveExtraModifier.new,
],
CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT: [
AddAsync.new,
],
Expand Down
@@ -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 {}
''');
}
}
Expand Up @@ -175,6 +175,7 @@ import 'remove_empty_constructor_body_test.dart'
as remove_empty_constructor_body;
import 'remove_empty_else_test.dart' as remove_empty_else;
import 'remove_empty_statement_test.dart' as remove_empty_statement;
import 'remove_extra_modifier_test.dart' as remove_extra_modifier;
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;
Expand Down Expand Up @@ -433,6 +434,7 @@ void main() {
remove_empty_constructor_body.main();
remove_empty_else.main();
remove_empty_statement.main();
remove_extra_modifier.main();
remove_if_null_operator.main();
remove_initializer.main();
remove_interpolation_braces.main();
Expand Down

0 comments on commit 4f5bd1c

Please sign in to comment.