Apply resolution to directive annotations.

R=brianwilkerson@google.com, paulberry@google.com

Change-Id: Iebc74df3a612cd8bbba417ca9175ab74692a3c85
Reviewed-on: https://dart-review.googlesource.com/64180
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart b/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart
index e837726..d463793 100644
--- a/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart
@@ -750,6 +750,14 @@
 //          file.source, _typeProvider, AnalysisErrorListener.NULL_LISTENER));
 //    }
 
+    for (var directive in unit.directives) {
+      if (directive.metadata.isNotEmpty) {
+        var resolution = resolutions.next();
+        var applier =
+            _createResolutionApplier(null, resolution, unit.localDeclarations);
+        applier.applyToAnnotations(directive);
+      }
+    }
     for (var declaration in unit.declarations) {
       if (declaration is ClassDeclaration) {
         if (declaration.metadata.isNotEmpty) {
diff --git a/pkg/analyzer/test/generated/non_error_resolver_kernel_test.dart b/pkg/analyzer/test/generated/non_error_resolver_kernel_test.dart
index cbecf0a..f071536 100644
--- a/pkg/analyzer/test/generated/non_error_resolver_kernel_test.dart
+++ b/pkg/analyzer/test/generated/non_error_resolver_kernel_test.dart
@@ -48,6 +48,13 @@
 
   @override
   @failingTest
+  @FastaProblem('https://github.com/dart-lang/sdk/issues/33795')
+  test_annotated_partOfDeclaration() async {
+    return super.test_annotated_partOfDeclaration();
+  }
+
+  @override
+  @failingTest
   @FastaProblem('https://github.com/dart-lang/sdk/issues/31604')
   test_commentReference_beforeConstructor() async {
     return super.test_commentReference_beforeConstructor();
diff --git a/pkg/analyzer/test/generated/non_hint_code_kernel_test.dart b/pkg/analyzer/test/generated/non_hint_code_kernel_test.dart
index d0b41db..ede068d 100644
--- a/pkg/analyzer/test/generated/non_hint_code_kernel_test.dart
+++ b/pkg/analyzer/test/generated/non_hint_code_kernel_test.dart
@@ -36,15 +36,6 @@
   @override
   bool get useCFE => true;
 
-  @failingTest
-  @override
-  @potentialAnalyzerProblem
-  test_deprecatedMemberUse_inDeprecatedLibrary() async {
-    // LibraryAnalyzer is not applying resolution data to annotations on
-    // directives.
-    await super.test_deprecatedMemberUse_inDeprecatedLibrary();
-  }
-
   @override
   @failingTest
   test_overrideOnNonOverridingField_inInterface() {
@@ -113,13 +104,6 @@
     await super.test_unusedImport_annotationOnDirective();
   }
 
-  @failingTest
-  @override
-  @potentialAnalyzerProblem
-  test_unusedImport_metadata() async {
-    await super.test_unusedImport_metadata();
-  }
-
   @override
   @failingTest
   @FastaProblem('https://github.com/dart-lang/sdk/issues/33678')
diff --git a/pkg/analyzer/test/src/dart/analysis/driver_resolution_kernel_test.dart b/pkg/analyzer/test/src/dart/analysis/driver_resolution_kernel_test.dart
index d6fef73..f4e2f1d 100644
--- a/pkg/analyzer/test/src/dart/analysis/driver_resolution_kernel_test.dart
+++ b/pkg/analyzer/test/src/dart/analysis/driver_resolution_kernel_test.dart
@@ -21,6 +21,13 @@
   @override
   bool get useCFE => true;
 
+  @override
+  @failingTest
+  @FastaProblem('https://github.com/dart-lang/sdk/issues/33795')
+  test_annotation_onDirective_partOf() async {
+    await super.test_annotation_onDirective_partOf();
+  }
+
   @failingTest
   @override
   test_annotation_onVariableList_topLevelVariable() =>
diff --git a/pkg/analyzer/test/src/dart/analysis/driver_resolution_test.dart b/pkg/analyzer/test/src/dart/analysis/driver_resolution_test.dart
index d0951ad..edb1674 100644
--- a/pkg/analyzer/test/src/dart/analysis/driver_resolution_test.dart
+++ b/pkg/analyzer/test/src/dart/analysis/driver_resolution_test.dart
@@ -236,6 +236,114 @@
     }
   }
 
+  test_annotation_onDirective_export() async {
+    addTestFile(r'''
+@a
+export 'dart:math';
+
+const a = 1;
+''');
+    await resolveTestFile();
+
+    var directive = findNode.export('dart:math');
+
+    expect(directive.metadata, hasLength(1));
+    Annotation annotation = directive.metadata[0];
+    expect(annotation.element, findElement.topGet('a'));
+
+    SimpleIdentifier aRef = annotation.name;
+    assertElement(aRef, findElement.topGet('a'));
+    assertType(aRef, 'int');
+  }
+
+  test_annotation_onDirective_import() async {
+    addTestFile(r'''
+@a
+import 'dart:math';
+
+const a = 1;
+''');
+    await resolveTestFile();
+
+    var directive = findNode.import('dart:math');
+
+    expect(directive.metadata, hasLength(1));
+    Annotation annotation = directive.metadata[0];
+    expect(annotation.element, findElement.topGet('a'));
+
+    SimpleIdentifier aRef = annotation.name;
+    assertElement(aRef, findElement.topGet('a'));
+    assertType(aRef, 'int');
+  }
+
+  test_annotation_onDirective_library() async {
+    addTestFile(r'''
+@a
+library test;
+
+const a = 1;
+''');
+    await resolveTestFile();
+
+    var directive = findNode.libraryDirective;
+
+    expect(directive.metadata, hasLength(1));
+    Annotation annotation = directive.metadata[0];
+    expect(annotation.element, findElement.topGet('a'));
+
+    SimpleIdentifier aRef = annotation.name;
+    assertElement(aRef, findElement.topGet('a'));
+    assertType(aRef, 'int');
+  }
+
+  test_annotation_onDirective_part() async {
+    provider.newFile(_p('/test/lib/a.dart'), r'''
+part of 'test.dart';
+''');
+    addTestFile(r'''
+@a
+part 'a.dart';
+
+const a = 1;
+''');
+    await resolveTestFile();
+
+    var directive = findNode.part('a.dart');
+
+    expect(directive.metadata, hasLength(1));
+    Annotation annotation = directive.metadata[0];
+    expect(annotation.element, findElement.topGet('a'));
+
+    SimpleIdentifier aRef = annotation.name;
+    assertElement(aRef, findElement.topGet('a'));
+    assertType(aRef, 'int');
+  }
+
+  test_annotation_onDirective_partOf() async {
+    var a = _p('/test/lib/a.dart');
+    provider.newFile(a, r'''
+part 'test.dart';
+''');
+    addTestFile(r'''
+@a
+part of 'a.dart';
+
+const a = 1;
+''');
+    driver.addFile(a);
+    await resolveTestFile();
+
+    var directive = findNode.partOf('a.dart');
+
+    expect(directive.metadata, hasLength(1));
+    Annotation annotation = directive.metadata[0];
+    expect(annotation.element, findElement.topGet('a'));
+
+    SimpleIdentifier aRef = annotation.name;
+    assertElement(aRef, findElement.topGet('a'));
+    assertType(aRef, 'int');
+  }
+
   test_annotation_onVariableList_constructor() async {
     String content = r'''
 class C {
@@ -7715,6 +7823,10 @@
 
   FindNode(this.result);
 
+  LibraryDirective get libraryDirective {
+    return result.unit.directives.singleWhere((d) => d is LibraryDirective);
+  }
+
   AssignmentExpression assignment(String search) {
     return _node(search).getAncestor((n) => n is AssignmentExpression);
   }
@@ -7723,10 +7835,18 @@
     return _node(search).getAncestor((n) => n is CascadeExpression);
   }
 
+  ExportDirective export(String search) {
+    return _node(search).getAncestor((n) => n is ExportDirective);
+  }
+
   FunctionExpression functionExpression(String search) {
     return _node(search).getAncestor((n) => n is FunctionExpression);
   }
 
+  ImportDirective import(String search) {
+    return _node(search).getAncestor((n) => n is ImportDirective);
+  }
+
   InstanceCreationExpression instanceCreation(String search) {
     return _node(search).getAncestor((n) => n is InstanceCreationExpression);
   }
@@ -7735,6 +7855,14 @@
     return _node(search).getAncestor((n) => n is MethodInvocation);
   }
 
+  PartDirective part(String search) {
+    return _node(search).getAncestor((n) => n is PartDirective);
+  }
+
+  PartOfDirective partOf(String search) {
+    return _node(search).getAncestor((n) => n is PartOfDirective);
+  }
+
   PostfixExpression postfix(String search) {
     return _node(search).getAncestor((n) => n is PostfixExpression);
   }