-
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.
Merge 081cff0 into dev
- Loading branch information
Showing
18 changed files
with
194 additions
and
163 deletions.
There are no files selected for viewing
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
44 changes: 44 additions & 0 deletions
44
pkg/analysis_server/lib/src/services/correction/dart/replace_with_named_constant.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,44 @@ | ||
// 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/error/error.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 ReplaceWithNamedConstant extends ResolvedCorrectionProducer { | ||
@override | ||
FixKind get fixKind => DartFixKind.USE_NAMED_CONSTANTS; | ||
|
||
@override | ||
Future<void> compute(ChangeBuilder builder) async { | ||
final diagnostic = this.diagnostic; | ||
if (diagnostic is AnalysisError) { | ||
String? correctionMessage = diagnostic.correctionMessage; | ||
if(correctionMessage == null){ | ||
return; | ||
} | ||
|
||
String? correction = _getCorrection(correctionMessage); | ||
if(correction == null){ | ||
return; | ||
} | ||
|
||
await builder.addDartFileEdit(file, (builder) { | ||
builder.addSimpleReplacement(range.error(diagnostic), correction); | ||
}); | ||
} | ||
} | ||
|
||
|
||
static String? _getCorrection(String message) { | ||
final match = RegExp(r"'(.*)'").firstMatch(message); | ||
if (match == null) { | ||
return null; | ||
} | ||
return match.group(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
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
47 changes: 47 additions & 0 deletions
47
pkg/analysis_server/test/src/services/correction/fix/replace_with_named_constant_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,47 @@ | ||
// 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: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(ReplaceWithNamedConstantTest); | ||
}); | ||
} | ||
|
||
@reflectiveTest | ||
class ReplaceWithNamedConstantTest extends FixProcessorLintTest { | ||
@override | ||
FixKind get kind => DartFixKind.USE_NAMED_CONSTANTS; | ||
|
||
@override | ||
String get lintCode => LintNames.use_named_constants; | ||
|
||
Future<void> test_use_named_constant() async { | ||
await resolveTestCode(''' | ||
final value = const C(0); | ||
class C { | ||
static const C zero = C(0); | ||
final int f; | ||
const C(this.f); | ||
} | ||
'''); | ||
|
||
await assertHasFix(''' | ||
final value = C.zero; | ||
class C { | ||
static const C zero = C(0); | ||
final int f; | ||
const C(this.f); | ||
} | ||
'''); | ||
} | ||
} |
Oops, something went wrong.