Macro. Code optimizer. Consider library declarations, pass ScannerConfiguration.

Change-Id: Ibe9ef926d6c2527493e7f3729c1e29e4cc6b3f3c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/350405
Reviewed-by: Jake Macdonald <jakemac@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2024-02-06 17:03:18 +00:00 committed by Commit Queue
parent 166c796179
commit 0d3910fca3
2 changed files with 24 additions and 5 deletions

View file

@ -12,17 +12,15 @@ abstract class CodeOptimizer {
List<Edit> optimize(
String code, {
required Set<String> libraryDeclarationNames,
required ScannerConfiguration scannerConfiguration,
bool throwIfHasErrors = false,
}) {
List<Edit> edits = [];
ScannerResult result = scanString(
code,
configuration: new ScannerConfiguration(
enableExtensionMethods: true,
enableNonNullable: true,
forAugmentationLibrary: true,
),
configuration: scannerConfiguration,
includeComments: true,
languageVersionChanged: (scanner, languageVersion) {
throw new UnimplementedError();
@ -39,6 +37,7 @@ abstract class CodeOptimizer {
_Listener listener = new _Listener(
getImportedNames: getImportedNames,
);
listener.libraryScope.globalNames.addAll(libraryDeclarationNames);
Parser parser = new Parser(
listener,

View file

@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
import 'package:_fe_analyzer_shared/src/macros/code_optimizer.dart';
import 'package:_fe_analyzer_shared/src/scanner/scanner.dart';
import 'package:test/test.dart';
void main() {
@ -226,6 +227,16 @@ typedef F<String> = void Function();
});
group('Shadowed | ', () {
test('By library declaration name', () {
assertEditsNoChanges(code: r'''
import 'dart:core' as prefix0;
class A {
prefix0.String foo() {}
}
''', libraryDeclarationNames: {'String'});
});
test('By local class, before', () {
assertEditsNoChanges(code: r'''
import 'dart:core' as prefix0;
@ -461,6 +472,7 @@ const _dartImports = {
void assertEdits({
Map<String, Set<String>> importedNames = const {},
Set<String> libraryDeclarationNames = const {},
required String code,
required String expected,
bool throwIfHasErrors = true,
@ -474,6 +486,12 @@ void assertEdits({
var edits = optimizer.optimize(
code,
libraryDeclarationNames: libraryDeclarationNames,
scannerConfiguration: ScannerConfiguration(
enableExtensionMethods: true,
enableNonNullable: true,
forAugmentationLibrary: true,
),
throwIfHasErrors: throwIfHasErrors,
);
@ -501,11 +519,13 @@ void assertEdits({
void assertEditsNoChanges({
Map<String, Set<String>> importedNames = const {},
Set<String> libraryDeclarationNames = const {},
required String code,
bool throwIfHasErrors = true,
}) {
assertEdits(
importedNames: importedNames,
libraryDeclarationNames: libraryDeclarationNames,
code: code,
throwIfHasErrors: throwIfHasErrors,
expected: '${'-' * 16}\n$code',