Skip to content

Commit

Permalink
[macros] quick fix for EXTENSION_AUGMENTATION_HAS_ON_CLAUSE
Browse files Browse the repository at this point in the history
Change-Id: I4521f742bfb60d7ff03dc76fe3ac6ea2810bb84f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366400
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
pq authored and Commit Queue committed May 14, 2024
1 parent c37cccc commit d73f534
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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/dart/ast/ast.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 RemoveOnClause extends ResolvedCorrectionProducer {
@override
bool get canBeAppliedInBulk => true;

@override
bool get canBeAppliedToFile => true;

@override
FixKind get fixKind => DartFixKind.REMOVE_ON_CLAUSE;

@override
FixKind get multiFixKind => DartFixKind.REMOVE_ON_CLAUSE_MULTI;

@override
Future<void> compute(ChangeBuilder builder) async {
var extensionDeclaration =
node.thisOrAncestorOfType<ExtensionDeclaration>();
if (extensionDeclaration == null) return;

await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(range.startStart(
extensionDeclaration.onClause!, extensionDeclaration.leftBracket));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2828,9 +2828,7 @@ ParserErrorCode.EXPORT_DIRECTIVE_AFTER_PART_DIRECTIVE:
notes: |-
Move the directive to a valid place.
ParserErrorCode.EXTENSION_AUGMENTATION_HAS_ON_CLAUSE:
status: needsFix
notes: |-
Remove the 'on' clause.
status: hasFix
ParserErrorCode.EXTENSION_DECLARES_ABSTRACT_MEMBER:
status: needsFix
notes: |-
Expand Down
10 changes: 10 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,16 @@ class DartFixKind {
DartFixKindPriority.IN_FILE,
"Remove '!'s in file",
);
static const REMOVE_ON_CLAUSE = FixKind(
'dart.fix.remove.on.clause',
DartFixKindPriority.DEFAULT,
"Remove the invalid 'on' clause",
);
static const REMOVE_ON_CLAUSE_MULTI = FixKind(
'dart.fix.remove.on.clause.multi',
DartFixKindPriority.IN_FILE,
"Remove all invalid 'on' clauses in file",
);
static const REMOVE_OPERATOR = FixKind(
'dart.fix.remove.operator',
DartFixKindPriority.DEFAULT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ import 'package:analysis_server/src/services/correction/dart/remove_method_decla
import 'package:analysis_server/src/services/correction/dart/remove_name_from_combinator.dart';
import 'package:analysis_server/src/services/correction/dart/remove_name_from_declaration_clause.dart';
import 'package:analysis_server/src/services/correction/dart/remove_non_null_assertion.dart';
import 'package:analysis_server/src/services/correction/dart/remove_on_clause.dart';
import 'package:analysis_server/src/services/correction/dart/remove_operator.dart';
import 'package:analysis_server/src/services/correction/dart/remove_parameters_in_getter_declaration.dart';
import 'package:analysis_server/src/services/correction/dart/remove_parentheses_in_getter_invocation.dart';
Expand Down Expand Up @@ -1303,6 +1304,9 @@ final _builtInNonLintProducers = <ErrorCode, List<ProducerGenerator>>{
InsertSemicolon.new,
ReplaceWithArrow.new,
],
ParserErrorCode.EXTENSION_AUGMENTATION_HAS_ON_CLAUSE: [
RemoveOnClause.new,
],
ParserErrorCode.EXTENSION_DECLARES_CONSTRUCTOR: [
RemoveConstructor.new,
],
Expand Down
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/generated/parser.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(RemoveOnClauseMultiTest);
defineReflectiveTests(RemoveOnClauseTest);
});
}

@reflectiveTest
class RemoveOnClauseMultiTest extends FixProcessorTest {
@override
FixKind get kind => DartFixKind.REMOVE_ON_CLAUSE_MULTI;

Future<void> test_singleFile() async {
newFile('$testPackageLibPath/a.dart', '''
import augment 'test.dart';
extension E on int { }
''');

await resolveTestCode('''
augment library 'a.dart';
augment extension E on int { }
augment extension E on num { }
''');
await assertHasFixAllFix(
ParserErrorCode.EXTENSION_AUGMENTATION_HAS_ON_CLAUSE, '''
augment library 'a.dart';
augment extension E { }
augment extension E { }
''');
}
}

@reflectiveTest
class RemoveOnClauseTest extends FixProcessorTest {
@override
FixKind get kind => DartFixKind.REMOVE_ON_CLAUSE;

Future<void> test_it() async {
newFile('$testPackageLibPath/a.dart', '''
import augment 'test.dart';
extension E on int { }
''');

await resolveTestCode('''
augment library 'a.dart';
augment extension E on int { }
''');
await assertHasFix('''
augment library 'a.dart';
augment extension E { }
''');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ import 'remove_name_from_combinator_test.dart' as remove_name_from_combinator;
import 'remove_name_from_declaration_clause_test.dart'
as remove_name_from_declaration_clause;
import 'remove_non_null_assertion_test.dart' as remove_non_null_assertion_test;
import 'remove_on_clause_test.dart' as remove_on_clause;
import 'remove_operator_test.dart' as remove_operator;
import 'remove_parameters_in_getter_declaration_test.dart'
as remove_parameters_in_getter_declaration;
Expand Down Expand Up @@ -446,6 +447,7 @@ void main() {
remove_name_from_combinator.main();
remove_name_from_declaration_clause.main();
remove_non_null_assertion_test.main();
remove_on_clause.main();
remove_operator.main();
remove_parameters_in_getter_declaration.main();
remove_parentheses_in_getter_invocation.main();
Expand Down

0 comments on commit d73f534

Please sign in to comment.