From 6b6f78212f6260a5b5bc8ca287d4e06210257d01 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Thu, 15 Feb 2024 19:58:39 +0000 Subject: [PATCH] Macro. Apply code optimizer, update constants. Change-Id: I3d7958121d4b3dbdd32b5b960b55baa2632b54ad Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/352723 Reviewed-by: Brian Wilkerson Commit-Queue: Konstantin Shcheglov --- .../lib/src/dart/analysis/file_state.dart | 71 +- .../lib/src/summary2/library_builder.dart | 57 +- .../lib/src/summary2/macro_merge.dart | 249 +++- .../lib/src/utilities/extensions/ast.dart | 19 + .../src/utilities/extensions/collection.dart | 7 + .../test/src/dart/resolution/macro_test.dart | 8 +- .../test/src/summary/macro/append.dart | 2 +- pkg/analyzer/test/src/summary/macro_test.dart | 1015 +++++++++++++---- .../utilities/extensions/collection_test.dart | 12 + 9 files changed, 1205 insertions(+), 235 deletions(-) diff --git a/pkg/analyzer/lib/src/dart/analysis/file_state.dart b/pkg/analyzer/lib/src/dart/analysis/file_state.dart index a3da5d8855ab..71d8e304685f 100644 --- a/pkg/analyzer/lib/src/dart/analysis/file_state.dart +++ b/pkg/analyzer/lib/src/dart/analysis/file_state.dart @@ -402,7 +402,7 @@ class FileState { /// /// This feature set is then restricted, with the [packageLanguageVersion], /// or with a `@dart` language override token in the file header. - final FeatureSet _featureSet; + final FeatureSet featureSet; /// The language version for the package that contains this file. final Version packageLanguageVersion; @@ -438,7 +438,7 @@ class FileState { this.uri, this.source, this.workspacePackage, - this._featureSet, + this.featureSet, this.packageLanguageVersion, this.analysisOptions, ) : uriProperties = FileUriProperties(uri); @@ -515,6 +515,41 @@ class FileState { } } + /// Parses given [code] with the same features as this file. + CompilationUnitImpl parseCode({ + required String code, + required AnalysisErrorListener errorListener, + }) { + CharSequenceReader reader = CharSequenceReader(code); + Scanner scanner = Scanner(source, reader, errorListener) + ..configureFeatures( + featureSetForOverriding: featureSet, + featureSet: featureSet.restrictToVersion( + packageLanguageVersion, + ), + ); + Token token = scanner.tokenize(reportScannerErrors: false); + LineInfo lineInfo = LineInfo(scanner.lineStarts); + + Parser parser = Parser( + source, + errorListener, + featureSet: scanner.featureSet, + lineInfo: lineInfo, + ); + + var unit = parser.parseCompilationUnit(token); + unit.languageVersion = LibraryLanguageVersion( + package: packageLanguageVersion, + override: scanner.overrideVersion, + ); + + // Ensure the string canonicalization cache size is reasonable. + pruneStringCanonicalizationCache(); + + return unit; + } + /// Read the file content and ensure that all of the file properties are /// consistent with the read content, including API signature. /// @@ -540,7 +575,7 @@ class FileState { { var signature = ApiSignature(); signature.addUint32List(_fsState._saltForUnlinked); - signature.addFeatureSet(_featureSet); + signature.addFeatureSet(featureSet); signature.addLanguageVersion(packageLanguageVersion); signature.addString(contentHash); signature.addBool(exists); @@ -733,34 +768,10 @@ class FileState { } CompilationUnitImpl _parse(AnalysisErrorListener errorListener) { - CharSequenceReader reader = CharSequenceReader(content); - Scanner scanner = Scanner(source, reader, errorListener) - ..configureFeatures( - featureSetForOverriding: _featureSet, - featureSet: _featureSet.restrictToVersion( - packageLanguageVersion, - ), - ); - Token token = scanner.tokenize(reportScannerErrors: false); - LineInfo lineInfo = LineInfo(scanner.lineStarts); - - Parser parser = Parser( - source, - errorListener, - featureSet: scanner.featureSet, - lineInfo: lineInfo, + return parseCode( + code: content, + errorListener: errorListener, ); - - var unit = parser.parseCompilationUnit(token); - unit.languageVersion = LibraryLanguageVersion( - package: packageLanguageVersion, - override: scanner.overrideVersion, - ); - - // Ensure the string canonicalization cache size is reasonable. - pruneStringCanonicalizationCache(); - - return unit; } // TODO(scheglov): write tests diff --git a/pkg/analyzer/lib/src/summary2/library_builder.dart b/pkg/analyzer/lib/src/summary2/library_builder.dart index c795e2a7551a..707683532c3e 100644 --- a/pkg/analyzer/lib/src/summary2/library_builder.dart +++ b/pkg/analyzer/lib/src/summary2/library_builder.dart @@ -3,9 +3,11 @@ // BSD-style license that can be found in the LICENSE file. import 'package:_fe_analyzer_shared/src/field_promotability.dart'; +import 'package:_fe_analyzer_shared/src/macros/code_optimizer.dart' as macro; import 'package:_fe_analyzer_shared/src/macros/executor.dart' as macro; import 'package:analyzer/dart/analysis/features.dart'; import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/error/listener.dart'; import 'package:analyzer/src/dart/analysis/file_state.dart' as file_state; import 'package:analyzer/src/dart/analysis/file_state.dart' hide DirectiveUri; import 'package:analyzer/src/dart/analysis/info_declaration_store.dart'; @@ -16,6 +18,7 @@ import 'package:analyzer/src/dart/element/element.dart'; import 'package:analyzer/src/dart/element/field_name_non_promotability_info.dart' as element_model; import 'package:analyzer/src/dart/resolver/scope.dart'; +import 'package:analyzer/src/dart/scanner/scanner.dart'; import 'package:analyzer/src/summary2/combinator.dart'; import 'package:analyzer/src/summary2/constructor_initializer_resolver.dart'; import 'package:analyzer/src/summary2/default_value_resolver.dart'; @@ -23,6 +26,7 @@ import 'package:analyzer/src/summary2/element_builder.dart'; import 'package:analyzer/src/summary2/export.dart'; import 'package:analyzer/src/summary2/informative_data.dart'; import 'package:analyzer/src/summary2/link.dart'; +import 'package:analyzer/src/summary2/linked_element_factory.dart'; import 'package:analyzer/src/summary2/macro_application.dart'; import 'package:analyzer/src/summary2/macro_merge.dart'; import 'package:analyzer/src/summary2/metadata_resolver.dart'; @@ -459,13 +463,18 @@ class LibraryBuilder { return; } - final augmentationCode = macroApplier.buildAugmentationLibraryCode( + var augmentationCode = macroApplier.buildAugmentationLibraryCode( _macroResults.flattenedToList2, ); if (augmentationCode == null) { return; } + var mergedUnit = kind.file.parseCode( + code: augmentationCode, + errorListener: AnalysisErrorListener.NULL_LISTENER, + ); + kind.disposeMacroAugmentations(disposeFiles: true); // Remove import for partial macro augmentations. @@ -477,8 +486,23 @@ class LibraryBuilder { final partialUnits = units.sublist(units.length - _macroResults.length); units.length -= _macroResults.length; - final importState = kind.addMacroAugmentation( + var optimizedCodeEdits = _CodeOptimizer( + elementFactory: linker.elementFactory, + ).optimize( augmentationCode, + libraryDeclarationNames: element.definingCompilationUnit.children + .map((e) => e.name) + .nonNulls + .toSet(), + scannerConfiguration: Scanner.buildConfig(kind.file.featureSet), + ); + var optimizedCode = macro.Edit.applyList( + optimizedCodeEdits.reversed.toList(), + augmentationCode, + ); + + final importState = kind.addMacroAugmentation( + optimizedCode, addLibraryAugmentDirective: true, partialIndex: null, ); @@ -521,7 +545,13 @@ class LibraryBuilder { unitNode: unitNode, unitElement: unitElement, augmentation: augmentation, - ).perform(); + ).perform(updateConstants: () { + MacroUpdateConstantsForOptimizedCode( + unitNode: mergedUnit, + codeEdits: optimizedCodeEdits, + unitElement: unitElement, + ).perform(); + }); // Set offsets the same way as when reading from summary. InformativeDataApplier( @@ -1216,6 +1246,27 @@ enum MacroDeclarationsPhaseStepResult { topDeclaration, } +class _CodeOptimizer extends macro.CodeOptimizer { + final LinkedElementFactory elementFactory; + final Map> exportedNames = {}; + + _CodeOptimizer({ + required this.elementFactory, + }); + + @override + Set getImportedNames(String uriStr) { + var uri = Uri.parse(uriStr); + var libraryElement = elementFactory.libraryOfUri(uri); + if (libraryElement != null) { + return exportedNames[uri] ??= libraryElement.exportedReferences + .map((exported) => exported.reference.name) + .toSet(); + } + return const {}; + } +} + /// This class examines all the [InterfaceElement]s in a library and determines /// which fields are promotable within that library. class _FieldPromotability extends FieldPromotability { +class MacroUpdateConstantsForOptimizedCode { + /// The parsed merged code. + final ast.CompilationUnit unitNode; + + /// The edits applied to the code that was parsed into [unitNode]. + /// This class does not have the optimized code itself. + final List codeEdits; + + /// The merged element, with elements in the same order as in [unitNode]. + final CompilationUnitElementImpl unitElement; + + MacroUpdateConstantsForOptimizedCode({ + required this.unitNode, + required this.codeEdits, + required this.unitElement, + }); + + void perform() { + var nodeAnnotations = _annotatedNodesInOrder().expand((node) sync* { + for (var annotation in node.metadata) { + yield ( + declaration: node, + annotation: annotation, + ); + } + }).toList(); + + var elementAnnotations = + _annotatedElementsInOrder().expand((element) sync* { + for (var annotation in element.metadata) { + annotation as ElementAnnotationImpl; + yield ( + element: element, + annotation: annotation.annotationAst, + ); + } + }).toList(); + + if (nodeAnnotations.length != elementAnnotations.length) { + throw StateError(''' +Different number of element / node annotations. +nodeAnnotations: ${nodeAnnotations.length} +elementAnnotations: ${elementAnnotations.length} +'''); + } + + for (var i = 0; i < nodeAnnotations.length; i++) { + var nodeRecord = nodeAnnotations[i]; + var elementRecord = elementAnnotations[i]; + + var nodeTokens = nodeRecord.annotation.allTokens; + var elementTokens = elementRecord.annotation.allTokens; + if (nodeTokens.length != elementTokens.length) { + throw StateError(''' +Different number of element / node annotations. +nodeTokens: ${nodeTokens.length} +elementTokens: ${elementTokens.length} +'''); + } + + elementRecord.annotation.accept( + _RemoveImportPrefixesVisitor( + codeEdits: codeEdits, + nodeTokens: nodeTokens, + elementTokens: elementTokens.asElementToIndexMap, + ), + ); + } + } + + List _annotatedElementsInOrder() { + var result = []; + + void addInstanceElement(InstanceElementImpl element) { + result.add(element); + for (var field in element.fields) { + if (!field.isSynthetic) { + result.add(field); + } + } + for (var getter in element.accessors) { + if (getter.isGetter && !getter.isSynthetic) { + result.add(getter); + } + } + for (var setter in element.accessors) { + if (setter.isSetter && !setter.isSynthetic) { + result.add(setter); + } + } + for (var method in element.methods) { + result.add(method); + } + } + + void addInterfaceElement(InterfaceElementImpl element) { + addInstanceElement(element); + for (var constructor in element.constructors) { + result.add(constructor); + } + } + + for (var class_ in unitElement.classes) { + addInterfaceElement(class_); + } + + for (var variable in unitElement.topLevelVariables) { + if (!variable.isSynthetic) { + result.add(variable); + } + } + + for (var getter in unitElement.accessors) { + if (getter.isGetter && !getter.isSynthetic) { + result.add(getter); + } + } + + for (var setter in unitElement.accessors) { + if (setter.isSetter && !setter.isSynthetic) { + result.add(setter); + } + } + + for (var function in unitElement.functions) { + result.add(function); + } + + return result; + } + + List _annotatedNodesInOrder() { + var result = []; + + void addInterfaceMembers(List members) { + for (var field in members) { + if (field is ast.FieldDeclarationImpl) { + result.add(field); + } + } + + for (var getter in members) { + if (getter is ast.MethodDeclarationImpl && getter.isGetter) { + result.add(getter); + } + } + + for (var setter in members) { + if (setter is ast.MethodDeclarationImpl && setter.isSetter) { + result.add(setter); + } + } + + for (var method in members) { + if (method is ast.MethodDeclarationImpl && + method.propertyKeyword == null) { + result.add(method); + } + } + + for (var constructor in members) { + if (constructor is ast.ConstructorDeclarationImpl) { + result.add(constructor); + } + } + } + + for (var class_ in unitNode.declarations) { + if (class_ is ast.ClassDeclarationImpl) { + result.add(class_); + addInterfaceMembers(class_.members); + } + } + + for (var variable in unitNode.declarations) { + if (variable is ast.TopLevelVariableDeclarationImpl) { + result.add(variable); + } + } + + for (var getter in unitNode.declarations) { + if (getter is ast.FunctionDeclarationImpl && getter.isGetter) { + result.add(getter); + } + } + + for (var setter in unitNode.declarations) { + if (setter is ast.FunctionDeclarationImpl && setter.isSetter) { + result.add(setter); + } + } + + for (var function in unitNode.declarations) { + if (function is ast.FunctionDeclarationImpl && + function.propertyKeyword == null) { + result.add(function); + } + } + + return result; + } +} + +class _RemoveImportPrefixesVisitor extends ast.RecursiveAstVisitor { + final List codeEdits; + final List nodeTokens; + final Map elementTokens; + int nodePrefixIndex = 0; + + _RemoveImportPrefixesVisitor({ + required this.codeEdits, + required this.nodeTokens, + required this.elementTokens, + }); + + @override + void visitPrefixedIdentifier(ast.PrefixedIdentifier node) { + var prefix = node.prefix; + if (prefix.name.startsWith('prefix')) { + var index = elementTokens[node.prefix.token]!; + var nodePrefix = nodeTokens[index]; + for (var edit in codeEdits) { + if (edit is macro.RemoveImportPrefixReferenceEdit) { + if (edit.offset == nodePrefix.offset) { + NodeReplacer.replace(node, node.identifier); + break; + } + } + } + } + + super.visitPrefixedIdentifier(node); + } +} + +class _RewriteImportPrefixes extends ast.RecursiveAstVisitor { final Map partialPrefixToMerged; _RewriteImportPrefixes(this.partialPrefixToMerged); diff --git a/pkg/analyzer/lib/src/utilities/extensions/ast.dart b/pkg/analyzer/lib/src/utilities/extensions/ast.dart index 108ed94fa1a9..4036884fde44 100644 --- a/pkg/analyzer/lib/src/utilities/extensions/ast.dart +++ b/pkg/analyzer/lib/src/utilities/extensions/ast.dart @@ -3,8 +3,27 @@ // BSD-style license that can be found in the LICENSE file. import 'package:analyzer/dart/ast/ast.dart'; +import 'package:analyzer/dart/ast/token.dart'; extension AstNodeExtension on AstNode { + /// Returns all tokens, from [beginToken] to [endToken] including. + List get allTokens { + var result = []; + var token = beginToken; + while (true) { + result.add(token); + if (token == endToken) { + break; + } + if (token.next case var next?) { + token = next; + } else { + break; + } + } + return result; + } + /// Return the minimal cover node for the range of characters beginning at the /// [offset] with the given [length], or `null` if the range is outside the /// range covered by the receiver. diff --git a/pkg/analyzer/lib/src/utilities/extensions/collection.dart b/pkg/analyzer/lib/src/utilities/extensions/collection.dart index b47ca858d49f..57faba719273 100644 --- a/pkg/analyzer/lib/src/utilities/extensions/collection.dart +++ b/pkg/analyzer/lib/src/utilities/extensions/collection.dart @@ -7,6 +7,13 @@ import 'dart:collection'; import 'package:collection/collection.dart'; extension IterableExtension on Iterable { + /// Note, elements must be unique. + Map get asElementToIndexMap { + return { + for (final (index, element) in indexed) element: index, + }; + } + /// Returns the fixed-length [List] with elements of `this`. List toFixedList() { var result = toList(growable: false); diff --git a/pkg/analyzer/test/src/dart/resolution/macro_test.dart b/pkg/analyzer/test/src/dart/resolution/macro_test.dart index 35a0591fd216..19ff6b1b8495 100644 --- a/pkg/analyzer/test/src/dart/resolution/macro_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/macro_test.dart @@ -727,10 +727,10 @@ ResolvedLibraryResult #0 --- library augment 'test.dart'; -import 'package:test/diagnostic.dart' as prefix0; +import 'package:test/diagnostic.dart'; augment class A { - @prefix0.ReportAtTypeAnnotation([ + @ReportAtTypeAnnotation([ 'field foo', ]) void starter() {} @@ -1058,9 +1058,9 @@ ResolvedLibraryResult #0 --- library augment 'test.dart'; -import 'dart:core' as prefix0; +import 'dart:core'; -prefix0.int get x => 0; +int get x => 0; --- '''); } diff --git a/pkg/analyzer/test/src/summary/macro/append.dart b/pkg/analyzer/test/src/summary/macro/append.dart index 2c86d110b3ac..bc1f3b37393d 100644 --- a/pkg/analyzer/test/src/summary/macro/append.dart +++ b/pkg/analyzer/test/src/summary/macro/append.dart @@ -17,7 +17,7 @@ Future> resolveIdentifiers( result.add(str); } - final pattern = RegExp(r'\{\{(.+)@(\w+)\}\}'); + final pattern = RegExp(r'\{\{(.+?)@(\w+?)\}\}'); for (final match in pattern.allMatches(withIdentifiers)) { addStringPart(match.start); // ignore: deprecated_member_use diff --git a/pkg/analyzer/test/src/summary/macro_test.dart b/pkg/analyzer/test/src/summary/macro_test.dart index d36aa1f76144..579b1a7f68e6 100644 --- a/pkg/analyzer/test/src/summary/macro_test.dart +++ b/pkg/analyzer/test/src/summary/macro_test.dart @@ -1319,10 +1319,10 @@ class B extends A { _assertMacroCode(library, r''' library augment 'test.dart'; -import 'dart:core' as prefix0; +import 'dart:core'; augment class B { - augment prefix0.num? foo = 0; + augment num? foo = 0; } '''); } @@ -1399,10 +1399,10 @@ class B extends A { _assertMacroCode(library, r''' library augment 'test.dart'; -import 'dart:core' as prefix0; +import 'dart:core'; augment class B { - augment prefix0.int get foo {} + augment int get foo {} } '''); } @@ -1424,10 +1424,10 @@ class B extends A { _assertMacroCode(library, r''' library augment 'test.dart'; -import 'dart:core' as prefix0; +import 'dart:core'; augment class B { - augment void foo(prefix0.int a, ) {} + augment void foo(int a, ) {} } '''); } @@ -1449,10 +1449,10 @@ class B extends A { _assertMacroCode(library, r''' library augment 'test.dart'; -import 'dart:core' as prefix0; +import 'dart:core'; augment class B { - augment prefix0.int foo() {} + augment int foo() {} } '''); } @@ -1516,10 +1516,10 @@ class B extends A { _assertMacroCode(library, r''' library augment 'test.dart'; -import 'dart:core' as prefix0; +import 'dart:core'; augment class B { - augment void set foo(prefix0.int a, ) {} + augment void set foo(int a, ) {} } '''); } @@ -1537,10 +1537,10 @@ class A { _assertMacroCode(library, r''' library augment 'test.dart'; -import 'dart:core' as prefix0; +import 'dart:core'; augment class A { - augment void set foo(prefix0.int _, ) {} + augment void set foo(int _, ) {} } '''); } @@ -1579,10 +1579,10 @@ class A { _assertMacroCode(library, r''' library augment 'test.dart'; -import 'dart:core' as prefix0; +import 'dart:core'; augment class A { - augment static void set foo(prefix0.int _, ) {} + augment static void set foo(int _, ) {} } '''); } @@ -1624,11 +1624,11 @@ class X {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class X { void doReference() { - prefix0.A; + A; } } '''); @@ -1652,11 +1652,11 @@ class X {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class X { void doReference() { - prefix0.A.named; + A.named; } } '''); @@ -1685,11 +1685,11 @@ class X {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class X { void doReference() { - prefix0.A.named; + A.named; } } '''); @@ -1745,11 +1745,11 @@ class X {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class X { void doReference() { - prefix0.A.foo; + A.foo; } } '''); @@ -1778,11 +1778,11 @@ class X {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class X { void doReference() { - prefix0.A.foo; + A.foo; } } '''); @@ -1809,11 +1809,11 @@ class A {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class A { void doReference() { - prefix0.B; + B; } } '''); @@ -1869,11 +1869,11 @@ class X {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class X { void doReference() { - prefix0.A.foo; + A.foo; } } '''); @@ -1902,11 +1902,11 @@ class X {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class X { void doReference() { - prefix0.A.foo; + A.foo; } } '''); @@ -1962,11 +1962,11 @@ class X {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class X { void doReference() { - prefix0.A.foo; + A.foo; } } '''); @@ -1995,11 +1995,11 @@ class X {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class X { void doReference() { - prefix0.A.foo; + A.foo; } } '''); @@ -2021,11 +2021,11 @@ class X {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class X { void doReference() { - prefix0.A; + A; } } '''); @@ -2047,11 +2047,11 @@ class X {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class X { void doReference() { - prefix0.A; + A; } } '''); @@ -2068,9 +2068,9 @@ void foo(int a); _assertMacroCode(library, r''' library augment 'test.dart'; -import 'dart:core' as prefix0; +import 'dart:core'; -augment void foo(prefix0.int a, ) { +augment void foo(int a, ) { a; } '''); @@ -2092,11 +2092,11 @@ class X {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class X { void doReference() { - prefix0.A; + A; } } '''); @@ -2118,11 +2118,11 @@ class X {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class X { void doReference() { - prefix0.A; + A; } } '''); @@ -2161,11 +2161,11 @@ class A {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class A { void doReference() { - prefix0.foo; + foo; } } '''); @@ -2192,11 +2192,11 @@ class A {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class A { void doReference() { - prefix0.foo; + foo; } } '''); @@ -2218,11 +2218,11 @@ class A {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class A { void doReference() { - prefix0.foo; + foo; } } '''); @@ -2249,11 +2249,11 @@ class A {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class A { void doReference() { - prefix0.foo; + foo; } } '''); @@ -2275,11 +2275,11 @@ class A {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class A { void doReference() { - prefix0.foo; + foo; } } '''); @@ -2301,11 +2301,11 @@ class A {} _assertMacroCode(library, r''' library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class A { void doReference() { - prefix0.foo; + foo; } } '''); @@ -2324,10 +2324,10 @@ class A { _assertMacroCode(library, r''' library augment 'test.dart'; -import 'dart:core' as prefix0; +import 'dart:core'; augment class A { - augment prefix0.String toString() { + augment String toString() { return 'A'; } } @@ -2347,10 +2347,10 @@ class A { _assertMacroCode(library, r''' library augment 'test.dart'; -import 'dart:core' as prefix0; +import 'dart:core'; augment class A { - augment prefix0.String toString() => 'A'; + augment String toString() => 'A'; } '''); } @@ -2398,75 +2398,59 @@ library --- library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; -@prefix0.AddMethodFoo() +@AddMethodFoo() class B {} augment class B { - @prefix0.AddMethodBar() + @AddMethodBar() void foo() {} void bar() {} } --- imports - package:test/a.dart as prefix0 @62 + package:test/a.dart definingUnit reference: self::@augmentation::package:test/test.macro.dart classes - class B @102 + class B @83 reference: self::@augmentation::package:test/test.macro.dart::@class::B metadata Annotation - atSign: @ @72 - name: PrefixedIdentifier - prefix: SimpleIdentifier - token: prefix0 @73 - staticElement: self::@augmentation::package:test/test.macro.dart::@prefix::prefix0 - staticType: null - period: . @80 - identifier: SimpleIdentifier - token: AddMethodFoo @81 - staticElement: package:test/a.dart::@class::AddMethodFoo - staticType: null + atSign: @ @61 + name: SimpleIdentifier + token: AddMethodFoo @62 staticElement: package:test/a.dart::@class::AddMethodFoo staticType: null arguments: ArgumentList - leftParenthesis: ( @93 - rightParenthesis: ) @94 + leftParenthesis: ( @74 + rightParenthesis: ) @75 element: package:test/a.dart::@class::AddMethodFoo::@constructor::new augmentation: self::@augmentation::package:test/test.macro.dart::@classAugmentation::B augmented methods self::@augmentation::package:test/test.macro.dart::@classAugmentation::B::@method::bar self::@augmentation::package:test/test.macro.dart::@classAugmentation::B::@method::foo - augment class B @122 + augment class B @103 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::B augmentationTarget: self::@augmentation::package:test/test.macro.dart::@class::B methods - foo @159 + foo @132 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::B::@method::foo metadata Annotation - atSign: @ @128 - name: PrefixedIdentifier - prefix: SimpleIdentifier - token: prefix0 @129 - staticElement: self::@augmentation::package:test/test.macro.dart::@prefix::prefix0 - staticType: null - period: . @136 - identifier: SimpleIdentifier - token: AddMethodBar @137 - staticElement: package:test/a.dart::@class::AddMethodBar - staticType: null + atSign: @ @109 + name: SimpleIdentifier + token: AddMethodBar @110 staticElement: package:test/a.dart::@class::AddMethodBar staticType: null arguments: ArgumentList - leftParenthesis: ( @149 - rightParenthesis: ) @150 + leftParenthesis: ( @122 + rightParenthesis: ) @123 element: package:test/a.dart::@class::AddMethodBar::@constructor::new returnType: void - bar @175 + bar @148 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::B::@method::bar returnType: void '''); @@ -2876,6 +2860,640 @@ augment class A { '''); } + test_codeOptimizer_metadata_class_constructor() async { + newFile('$testPackageLibPath/a.dart', r''' +class A { + const A(); +} +'''); + + var library = await buildLibrary(r''' +import 'append.dart'; +import 'a.dart'; + +@DeclareInType(""" + @{{package:test/a.dart@A}}() + B.named();""") +class B {} +'''); + + configuration + ..forCodeOptimizer() + ..withConstructors = true; + checkElementText(library, r''' +library + imports + package:test/append.dart + package:test/a.dart + augmentationImports + package:test/test.macro.dart + macroGeneratedCode +--- +library augment 'test.dart'; + +import 'package:test/a.dart'; + +augment class B { + @A() + B.named(); +} +--- + imports + package:test/a.dart + definingUnit + classes + augment class B @75 + augmentationTarget: self::@class::B + constructors + named @90 + metadata + Annotation + atSign: @ @81 + name: SimpleIdentifier + token: A @82 + staticElement: package:test/a.dart::@class::A + staticType: null + arguments: ArgumentList + leftParenthesis: ( @83 + rightParenthesis: ) @84 + element: package:test/a.dart::@class::A::@constructor::new + periodOffset: 89 + nameEnd: 95 +'''); + } + + test_codeOptimizer_metadata_class_field() async { + newFile('$testPackageLibPath/a.dart', r''' +class A { + const A(); +} +'''); + + var library = await buildLibrary(r''' +import 'append.dart'; +import 'a.dart'; + +@DeclareInType(""" + @{{package:test/a.dart@A}}() + final int foo = 0;""") +class B {} +'''); + + configuration.forCodeOptimizer(); + checkElementText(library, r''' +library + imports + package:test/append.dart + package:test/a.dart + augmentationImports + package:test/test.macro.dart + macroGeneratedCode +--- +library augment 'test.dart'; + +import 'package:test/a.dart'; + +augment class B { + @A() + final int foo = 0; +} +--- + imports + package:test/a.dart + definingUnit + classes + augment class B @75 + augmentationTarget: self::@class::B + fields + final foo @98 + metadata + Annotation + atSign: @ @81 + name: SimpleIdentifier + token: A @82 + staticElement: package:test/a.dart::@class::A + staticType: null + arguments: ArgumentList + leftParenthesis: ( @83 + rightParenthesis: ) @84 + element: package:test/a.dart::@class::A::@constructor::new + type: int + shouldUseTypeForInitializerInference: true + accessors + synthetic get foo @-1 + returnType: int +'''); + } + + test_codeOptimizer_metadata_class_getter() async { + newFile('$testPackageLibPath/a.dart', r''' +class A { + const A(); +} +'''); + + var library = await buildLibrary(r''' +import 'append.dart'; +import 'a.dart'; + +@DeclareInType(""" + @{{package:test/a.dart@A}}() + int get foo => 0;""") +class B {} +'''); + + configuration.forCodeOptimizer(); + checkElementText(library, r''' +library + imports + package:test/append.dart + package:test/a.dart + augmentationImports + package:test/test.macro.dart + macroGeneratedCode +--- +library augment 'test.dart'; + +import 'package:test/a.dart'; + +augment class B { + @A() + int get foo => 0; +} +--- + imports + package:test/a.dart + definingUnit + classes + augment class B @75 + augmentationTarget: self::@class::B + fields + synthetic foo @-1 + type: int + accessors + get foo @96 + metadata + Annotation + atSign: @ @81 + name: SimpleIdentifier + token: A @82 + staticElement: package:test/a.dart::@class::A + staticType: null + arguments: ArgumentList + leftParenthesis: ( @83 + rightParenthesis: ) @84 + element: package:test/a.dart::@class::A::@constructor::new + returnType: int +'''); + } + + test_codeOptimizer_metadata_class_method() async { + newFile('$testPackageLibPath/a.dart', r''' +class A { + const A(); +} +'''); + + var library = await buildLibrary(r''' +import 'append.dart'; +import 'a.dart'; + +@DeclareInType(""" + @{{package:test/a.dart@A}}() + void foo() {}""") +class B {} +'''); + + configuration.forCodeOptimizer(); + checkElementText(library, r''' +library + imports + package:test/append.dart + package:test/a.dart + augmentationImports + package:test/test.macro.dart + macroGeneratedCode +--- +library augment 'test.dart'; + +import 'package:test/a.dart'; + +augment class B { + @A() + void foo() {} +} +--- + imports + package:test/a.dart + definingUnit + classes + augment class B @75 + augmentationTarget: self::@class::B + methods + foo @93 + metadata + Annotation + atSign: @ @81 + name: SimpleIdentifier + token: A @82 + staticElement: package:test/a.dart::@class::A + staticType: null + arguments: ArgumentList + leftParenthesis: ( @83 + rightParenthesis: ) @84 + element: package:test/a.dart::@class::A::@constructor::new + returnType: void +'''); + } + + test_codeOptimizer_metadata_class_setter() async { + newFile('$testPackageLibPath/a.dart', r''' +class A { + const A(); +} +'''); + + var library = await buildLibrary(r''' +import 'append.dart'; +import 'a.dart'; + +@DeclareInType(""" + @{{package:test/a.dart@A}}() + set foo(int _) {}""") +class B {} +'''); + + configuration.forCodeOptimizer(); + checkElementText(library, r''' +library + imports + package:test/append.dart + package:test/a.dart + augmentationImports + package:test/test.macro.dart + macroGeneratedCode +--- +library augment 'test.dart'; + +import 'package:test/a.dart'; + +augment class B { + @A() + set foo(int _) {} +} +--- + imports + package:test/a.dart + definingUnit + classes + augment class B @75 + augmentationTarget: self::@class::B + fields + synthetic foo @-1 + type: int + accessors + set foo= @92 + metadata + Annotation + atSign: @ @81 + name: SimpleIdentifier + token: A @82 + staticElement: package:test/a.dart::@class::A + staticType: null + arguments: ArgumentList + leftParenthesis: ( @83 + rightParenthesis: ) @84 + element: package:test/a.dart::@class::A::@constructor::new + parameters + requiredPositional _ @100 + type: int + returnType: void +'''); + } + + test_codeOptimizer_metadata_unit_function() async { + newFile('$testPackageLibPath/a.dart', r''' +class A { + const A(); +} +'''); + + var library = await buildLibrary(r''' +import 'append.dart'; +import 'a.dart'; + +@DeclareInLibrary(""" +@{{package:test/a.dart@A}}() +void foo() {}""") +class B {} +'''); + + configuration.forCodeOptimizer(); + checkElementText(library, r''' +library + imports + package:test/append.dart + package:test/a.dart + augmentationImports + package:test/test.macro.dart + macroGeneratedCode +--- +library augment 'test.dart'; + +import 'package:test/a.dart'; + +@A() +void foo() {} +--- + imports + package:test/a.dart + definingUnit + functions + foo @71 + metadata + Annotation + atSign: @ @61 + name: SimpleIdentifier + token: A @62 + staticElement: package:test/a.dart::@class::A + staticType: null + arguments: ArgumentList + leftParenthesis: ( @63 + rightParenthesis: ) @64 + element: package:test/a.dart::@class::A::@constructor::new + returnType: void +'''); + } + + test_codeOptimizer_metadata_unit_getter() async { + newFile('$testPackageLibPath/a.dart', r''' +class A { + const A(); +} +'''); + + var library = await buildLibrary(r''' +import 'append.dart'; +import 'a.dart'; + +@DeclareInLibrary(""" +@{{package:test/a.dart@A}}() +int get foo => 0;""") +class B {} +'''); + + configuration.forCodeOptimizer(); + checkElementText(library, r''' +library + imports + package:test/append.dart + package:test/a.dart + augmentationImports + package:test/test.macro.dart + macroGeneratedCode +--- +library augment 'test.dart'; + +import 'package:test/a.dart'; + +@A() +int get foo => 0; +--- + imports + package:test/a.dart + definingUnit + topLevelVariables + synthetic static foo @-1 + type: int + accessors + static get foo @74 + metadata + Annotation + atSign: @ @61 + name: SimpleIdentifier + token: A @62 + staticElement: package:test/a.dart::@class::A + staticType: null + arguments: ArgumentList + leftParenthesis: ( @63 + rightParenthesis: ) @64 + element: package:test/a.dart::@class::A::@constructor::new + returnType: int +'''); + } + + test_codeOptimizer_metadata_unit_setter() async { + newFile('$testPackageLibPath/a.dart', r''' +class A { + const A(); +} +'''); + + var library = await buildLibrary(r''' +import 'append.dart'; +import 'a.dart'; + +@DeclareInLibrary(""" +@{{package:test/a.dart@A}}() +set foo(int _) {}""") +class B {} +'''); + + configuration.forCodeOptimizer(); + checkElementText(library, r''' +library + imports + package:test/append.dart + package:test/a.dart + augmentationImports + package:test/test.macro.dart + macroGeneratedCode +--- +library augment 'test.dart'; + +import 'package:test/a.dart'; + +@A() +set foo(int _) {} +--- + imports + package:test/a.dart + definingUnit + topLevelVariables + synthetic static foo @-1 + type: int + accessors + static set foo= @70 + metadata + Annotation + atSign: @ @61 + name: SimpleIdentifier + token: A @62 + staticElement: package:test/a.dart::@class::A + staticType: null + arguments: ArgumentList + leftParenthesis: ( @63 + rightParenthesis: ) @64 + element: package:test/a.dart::@class::A::@constructor::new + parameters + requiredPositional _ @78 + type: int + returnType: void +'''); + } + + test_codeOptimizer_metadata_unit_variable() async { + newFile('$testPackageLibPath/a.dart', r''' +class A { + const A(); +} +'''); + + var library = await buildLibrary(r''' +import 'append.dart'; +import 'a.dart'; + +@DeclareInLibrary(""" +@{{package:test/a.dart@A}}() +final foo = 0;""") +class B {} +'''); + + configuration.forCodeOptimizer(); + checkElementText(library, r''' +library + imports + package:test/append.dart + package:test/a.dart + augmentationImports + package:test/test.macro.dart + macroGeneratedCode +--- +library augment 'test.dart'; + +import 'package:test/a.dart'; + +@A() +final foo = 0; +--- + imports + package:test/a.dart + definingUnit + topLevelVariables + static final foo @72 + metadata + Annotation + atSign: @ @61 + name: SimpleIdentifier + token: A @62 + staticElement: package:test/a.dart::@class::A + staticType: null + arguments: ArgumentList + leftParenthesis: ( @63 + rightParenthesis: ) @64 + element: package:test/a.dart::@class::A::@constructor::new + type: int + shouldUseTypeForInitializerInference: false + accessors + synthetic static get foo @-1 + returnType: int +'''); + } + + test_codeOptimizer_namedType_notUniqueImport() async { + newFile('$testPackageLibPath/a.dart', r''' +class X {} +'''); + + newFile('$testPackageLibPath/b.dart', r''' +class X {} +'''); + + var library = await buildLibrary(r''' +import 'append.dart'; +import 'a.dart'; +import 'b.dart'; + +@DeclareInLibrary('void foo({{package:test/a.dart@X}} x1, {{package:test/b.dart@X}} x2) {}') +class A {} +'''); + + configuration.forCodeOptimizer(); + checkElementText(library, r''' +library + imports + package:test/append.dart + package:test/a.dart + package:test/b.dart + augmentationImports + package:test/test.macro.dart + macroGeneratedCode +--- +library augment 'test.dart'; + +import 'package:test/a.dart' as prefix0; +import 'package:test/b.dart' as prefix1; + +void foo(prefix0.X x1, prefix1.X x2) {} +--- + imports + package:test/a.dart as prefix0 @62 + package:test/b.dart as prefix1 @103 + definingUnit + functions + foo @118 + parameters + requiredPositional x1 @132 + type: X + requiredPositional x2 @146 + type: X + returnType: void +'''); + } + + test_codeOptimizer_namedType_shadowedLocally() async { + newFile('$testPackageLibPath/a.dart', r''' +class X {} +'''); + + var library = await buildLibrary(r''' +import 'append.dart'; +import 'a.dart'; + +@DeclareInLibrary('void foo({{package:test/a.dart@X}} x) {}') +class A {} + +class X {} +'''); + + configuration.forCodeOptimizer(); + checkElementText(library, r''' +library + imports + package:test/append.dart + package:test/a.dart + augmentationImports + package:test/test.macro.dart + macroGeneratedCode +--- +library augment 'test.dart'; + +import 'package:test/a.dart' as prefix0; + +void foo(prefix0.X x) {} +--- + imports + package:test/a.dart as prefix0 @62 + definingUnit + functions + foo @77 + parameters + requiredPositional x @91 + type: X + returnType: void +'''); + } + test_unit_function_add() async { var library = await buildLibrary(r''' import 'append.dart'; @@ -3032,32 +3650,32 @@ library --- library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; augment class A { - @prefix0.AugmentConstructor() + @AugmentConstructor() A.named(); augment A.named() { print(42); } } --- imports - package:test/a.dart as prefix0 @62 + package:test/a.dart definingUnit reference: self::@augmentation::package:test/test.macro.dart classes - augment class A @86 + augment class A @75 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A augmentationTarget: self::@class::A constructors - named @126 + named @107 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@constructor::named - periodOffset: 125 - nameEnd: 131 + periodOffset: 106 + nameEnd: 112 augmentation: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@constructorAugmentation::named - augment named @147 + augment named @128 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@constructorAugmentation::named - periodOffset: 146 - nameEnd: 152 + periodOffset: 127 + nameEnd: 133 augmentationTarget: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@constructor::named '''); } @@ -3104,33 +3722,33 @@ library --- library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; -import 'dart:core' as prefix1; +import 'package:test/a.dart'; +import 'dart:core'; augment class A { - @prefix0.AugmentField() - prefix1.int foo; - augment prefix1.int foo = 42; + @AugmentField() + int foo; + augment int foo = 42; } --- imports - package:test/a.dart as prefix0 @62 - dart:core as prefix1 @93 + package:test/a.dart + dart:core definingUnit reference: self::@augmentation::package:test/test.macro.dart classes - augment class A @117 + augment class A @95 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A augmentationTarget: self::@class::A fields - foo @161 + foo @123 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@field::foo type: int id: field_0 getter: getter_0 setter: setter_0 augmentation: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@fieldAugmentation::foo - augment foo @188 + augment foo @142 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@fieldAugmentation::foo type: int shouldUseTypeForInitializerInference: true @@ -3194,22 +3812,22 @@ library --- library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; -import 'dart:core' as prefix1; +import 'package:test/a.dart'; +import 'dart:core'; augment class A { - @prefix0.AugmentGetter() - external prefix1.int get foo; - augment prefix1.int get foo => 42; + @AugmentGetter() + external int get foo; + augment int get foo => 42; } --- imports - package:test/a.dart as prefix0 @62 - dart:core as prefix1 @93 + package:test/a.dart + dart:core definingUnit reference: self::@augmentation::package:test/test.macro.dart classes - augment class A @117 + augment class A @95 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A augmentationTarget: self::@class::A fields @@ -3219,13 +3837,13 @@ augment class A { id: field_0 getter: getter_0 accessors - external get foo @175 + external get foo @137 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@getter::foo returnType: int id: getter_0 variable: field_0 augmentation: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@getterAugmentation::foo - augment get foo @206 + augment get foo @160 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@getterAugmentation::foo returnType: int id: getter_1 @@ -3272,30 +3890,30 @@ library --- library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; -import 'dart:core' as prefix1; +import 'package:test/a.dart'; +import 'dart:core'; augment class A { - @prefix0.AugmentMethod() - external prefix1.int foo(); - augment prefix1.int foo() => 42; + @AugmentMethod() + external int foo(); + augment int foo() => 42; } --- imports - package:test/a.dart as prefix0 @62 - dart:core as prefix1 @93 + package:test/a.dart + dart:core definingUnit reference: self::@augmentation::package:test/test.macro.dart classes - augment class A @117 + augment class A @95 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A augmentationTarget: self::@class::A methods - external foo @171 + external foo @133 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@method::foo returnType: int augmentation: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@methodAugmentation::foo - augment foo @200 + augment foo @154 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@methodAugmentation::foo returnType: int augmentationTarget: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@method::foo @@ -3343,22 +3961,22 @@ library --- library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; -import 'dart:core' as prefix1; +import 'package:test/a.dart'; +import 'dart:core'; augment class A { - @prefix0.AugmentSetter() - external void set foo(prefix1.int value); - augment void set foo(prefix1.int value, ) { print(42); } + @AugmentSetter() + external void set foo(int value); + augment void set foo(int value, ) { print(42); } } --- imports - package:test/a.dart as prefix0 @62 - dart:core as prefix1 @93 + package:test/a.dart + dart:core definingUnit reference: self::@augmentation::package:test/test.macro.dart classes - augment class A @117 + augment class A @95 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A augmentationTarget: self::@class::A fields @@ -3368,19 +3986,19 @@ augment class A { id: field_0 setter: setter_0 accessors - external set foo= @168 + external set foo= @138 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@setter::foo parameters - requiredPositional value @184 + requiredPositional value @146 type: int returnType: void id: setter_0 variable: field_0 augmentation: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@setterAugmentation::foo - augment set foo= @211 + augment set foo= @173 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@setterAugmentation::foo parameters - requiredPositional value @227 + requiredPositional value @181 type: int returnType: void id: setter_1 @@ -5204,54 +5822,54 @@ library --- library augment 'test.dart'; -import 'package:test/json_serializable.dart' as prefix0; -import 'dart:core' as prefix1; +import 'package:test/json_serializable.dart'; +import 'dart:core'; augment class A { - @prefix0.FromJson() - A.fromJson(prefix1.Map json); - @prefix0.ToJson() - prefix1.Map toJson(); - augment A.fromJson(prefix1.Map json, ) : this.foo = json["foo"] as prefix1.int, -this.bar = json["bar"] as prefix1.int{} - augment prefix1.Map toJson() => { + @FromJson() + A.fromJson(Map json); + @ToJson() + Map toJson(); + augment A.fromJson(Map json, ) : this.foo = json["foo"] as int, +this.bar = json["bar"] as int{} + augment Map toJson() => { 'foo': this.foo, 'bar': this.bar, }; } --- imports - package:test/json_serializable.dart as prefix0 @78 - dart:core as prefix1 @109 + package:test/json_serializable.dart + dart:core definingUnit reference: self::@augmentation::package:test/test.macro.dart classes - augment class A @133 + augment class A @111 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A augmentationTarget: self::@class::A constructors - fromJson @163 + fromJson @133 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@constructor::fromJson - periodOffset: 162 - nameEnd: 171 + periodOffset: 132 + nameEnd: 141 parameters - requiredPositional json @217 + requiredPositional json @163 type: Map augmentation: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@constructorAugmentation::fromJson - augment fromJson @313 + augment fromJson @227 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@constructorAugmentation::fromJson - periodOffset: 312 - nameEnd: 321 + periodOffset: 226 + nameEnd: 235 parameters - requiredPositional json @367 + requiredPositional json @257 type: Map augmentationTarget: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@constructor::fromJson methods - abstract toJson @291 + abstract toJson @205 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@method::toJson returnType: Map augmentation: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@methodAugmentation::toJson - augment toJson @512 + augment toJson @362 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@methodAugmentation::toJson returnType: Map augmentationTarget: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@method::toJson @@ -5273,12 +5891,12 @@ class A { _assertMacroCode(library, r''' library augment 'test.dart'; -import 'dart:core' as prefix0; +import 'dart:core'; augment class A { - prefix0.int get foo => this._foo; - set foo(prefix0.int val) { - prefix0.print('Setting foo to ${val}'); + int get foo => this._foo; + set foo(int val) { + print('Setting foo to ${val}'); this._foo = val; } } @@ -10292,21 +10910,21 @@ library --- library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; class MyClass { - void foo(prefix0.A _) {} + void foo(A _) {} } --- imports - package:test/a.dart as prefix0 @62 + package:test/a.dart definingUnit classes - class MyClass @78 + class MyClass @67 methods - foo @95 + foo @84 parameters - requiredPositional _ @109 + requiredPositional _ @90 type: A returnType: void '''); @@ -10372,10 +10990,10 @@ files --- library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; class MyClass { - void foo(prefix0.A _) {} + void foo(A _) {} } --- kind: augmentation_3 @@ -10466,10 +11084,10 @@ files --- library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; class MyClass { - void foo(prefix0.A _) {} + void foo(A _) {} } --- kind: augmentation_3 @@ -10555,10 +11173,10 @@ files --- library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; class MyClass { - void foo(prefix0.A _) {} + void foo(A _) {} } --- kind: augmentation_3 @@ -10656,19 +11274,19 @@ library --- library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; -@prefix0.AddClassB() +@AddClassB() class A {} class B {} --- imports - package:test/a.dart as prefix0 @62 + package:test/a.dart definingUnit classes - class A @99 - class B @111 + class A @80 + class B @92 '''); analyzerStatePrinterConfiguration.filesToPrintContent.add( @@ -10716,9 +11334,9 @@ files --- library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; -@prefix0.AddClassB() +@AddClassB() class A {} class B {} @@ -10788,9 +11406,9 @@ files --- library augment 'test.dart'; -import 'package:test/a.dart' as prefix0; +import 'package:test/a.dart'; -@prefix0.AddClassB() +@AddClassB() class A {} class B {} @@ -11492,6 +12110,17 @@ extension on Folder { } extension on ElementTextConfiguration { + void forCodeOptimizer() { + filter = (obj) { + if (obj is CompilationUnitElement) { + return obj.enclosingElement is LibraryAugmentationElement; + } + return true; + }; + withConstructors = false; + withMetadata = true; + } + void forOrder() { filter = (element) { if (element is CompilationUnitElement) { diff --git a/pkg/analyzer/test/src/utilities/extensions/collection_test.dart b/pkg/analyzer/test/src/utilities/extensions/collection_test.dart index 7dad8fe66274..2099ced791f7 100644 --- a/pkg/analyzer/test/src/utilities/extensions/collection_test.dart +++ b/pkg/analyzer/test/src/utilities/extensions/collection_test.dart @@ -26,6 +26,18 @@ class IterableExtensionTest { @reflectiveTest class IterableIterableExtensionTest { + test_asElementToIndexMap() { + expect( + [].asElementToIndexMap, + {}, + ); + + expect( + ['a', 'b', 'c'].asElementToIndexMap, + {'a': 0, 'b': 1, 'c': 2}, + ); + } + test_flattenedToList() { expect( [