Skip to content

Commit

Permalink
Fix for LibraryDirective annotations while applying resynthesized ele…
Browse files Browse the repository at this point in the history
…ment model.

This allows us to clean up the analysis driver.

I will NOT land this without your review.

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

Review URL: https://codereview.chromium.org/2445193003 .
  • Loading branch information
scheglov committed Oct 26, 2016
1 parent 22a57bb commit f797154
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
20 changes: 0 additions & 20 deletions pkg/analyzer/lib/src/dart/analysis/driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -367,26 +367,6 @@ class AnalysisDriver {
* Compute the [AnalysisResult] for the [file].
*/
AnalysisResult _computeAnalysisResult(_File file) {
// TODO(scheglov) Computing resolved unit fails for these units.
// pkg/analyzer/lib/plugin/embedded_resolver_provider.dart
// pkg/analyzer/lib/plugin/embedded_resolver_provider.dart
if (file.path.endsWith(
'pkg/analyzer/lib/plugin/embedded_resolver_provider.dart') ||
file.path.endsWith('pkg/analyzer/lib/source/embedder.dart') ||
file.path.endsWith('pkg/analyzer/lib/src/generated/ast.dart') ||
file.path.endsWith('pkg/analyzer/lib/src/generated/element.dart') ||
file.path
.endsWith('pkg/analyzer/lib/src/generated/element_handle.dart') ||
file.path.endsWith('pkg/analyzer/lib/src/generated/error.dart') ||
file.path.endsWith('pkg/analyzer/lib/src/generated/scanner.dart') ||
file.path.endsWith('pkg/analyzer/lib/src/generated/sdk_io.dart') ||
file.path.endsWith('pkg/analyzer/lib/src/generated/visitors.dart') ||
file.path.endsWith('pkg/analyzer/test/generated/constant_test.dart') ||
file.path.endsWith('pkg/analyzer/test/source/embedder_test.dart')) {
return new AnalysisResult(
file.path, file.uri, null, file.contentHash, null, []);
}

return _logger.run('Compute analysis result for $file', () {
_LibraryContext libraryContext = _createLibraryContext(file);
AnalysisContext analysisContext = _createAnalysisContext(libraryContext);
Expand Down
8 changes: 6 additions & 2 deletions pkg/analyzer/lib/src/generated/declaration_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,12 @@ class DeclarationResolver extends RecursiveAstVisitor<Object> {
@override
Object visitLibraryDirective(LibraryDirective node) {
super.visitLibraryDirective(node);
_resolveAnnotations(
node, node.metadata, _enclosingUnit.getAnnotations(node.offset));
List<ElementAnnotation> annotations =
_enclosingUnit.getAnnotations(node.offset);
if (annotations.isEmpty && node.metadata.isNotEmpty) {
annotations = _walker.element.library.metadata;
}
_resolveAnnotations(node, node.metadata, annotations);
return null;
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/analyzer/test/generated/declaration_resolver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/ast/utilities.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/generated/declaration_resolver.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/source.dart';
Expand Down Expand Up @@ -167,6 +168,20 @@ class DeclarationResolverMetadataTest extends ResolverTestCase {
checkMetadata('L');
}

void test_metadata_libraryDirective_resynthesized() {
CompilationUnit unit = resolveSource('@a library L; const a = null;');
expect(unit.directives.single.metadata.single.name.name, 'a');
var unitElement = unit.element as CompilationUnitElementImpl;
// Damage the unit element - as if "setAnnotations" were not called.
// The LibraryElement still has the metadata, we should use it.
unitElement.setAnnotations(unit.directives.single.offset, []);
expect(unitElement.library.metadata, hasLength(1));
// DeclarationResolver on the clone should succeed.
CompilationUnit clonedUnit = AstCloner.clone(unit);
new DeclarationResolver().resolve(clonedUnit, unit.element);
expect(clonedUnit.directives.single.metadata.single.name.name, 'a');
}

void test_metadata_localFunctionDeclaration() {
setupCode('f() { @a g() {} }');
// Note: metadata on local function declarations is ignored by the
Expand Down

0 comments on commit f797154

Please sign in to comment.