OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 library initialize.transformer; | 4 library initialize.transformer; |
5 | 5 |
6 import 'dart:async'; | 6 import 'dart:async'; |
7 import 'dart:collection' show Queue; | 7 import 'dart:collection' show Queue; |
8 import 'package:analyzer/src/generated/ast.dart'; | 8 import 'package:analyzer/src/generated/ast.dart'; |
9 import 'package:analyzer/src/generated/element.dart'; | 9 import 'package:analyzer/src/generated/element.dart'; |
10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 return new Asset.fromString(_newEntryPoint, _buildNewEntryPoint(entryLib)); | 232 return new Asset.fromString(_newEntryPoint, _buildNewEntryPoint(entryLib)); |
233 } | 233 } |
234 | 234 |
235 /// Reads Initializer annotations on this library and all its dependencies in | 235 /// Reads Initializer annotations on this library and all its dependencies in |
236 /// post-order. | 236 /// post-order. |
237 void _readLibraries(LibraryElement library, [Set<LibraryElement> seen]) { | 237 void _readLibraries(LibraryElement library, [Set<LibraryElement> seen]) { |
238 if (seen == null) seen = new Set<LibraryElement>(); | 238 if (seen == null) seen = new Set<LibraryElement>(); |
239 seen.add(library); | 239 seen.add(library); |
240 | 240 |
241 // Visit all our dependencies. | 241 // Visit all our dependencies. |
242 for (var importedLibrary in _sortedLibraryImports(library)) { | 242 for (var library in _sortedLibraryDependencies(library)) { |
243 // Don't include anything from the sdk. | 243 // Don't include anything from the sdk. |
244 if (importedLibrary.isInSdk) continue; | 244 if (library.isInSdk) continue; |
245 if (seen.contains(importedLibrary)) continue; | 245 if (seen.contains(library)) continue; |
246 _readLibraries(importedLibrary, seen); | 246 _readLibraries(library, seen); |
247 } | 247 } |
248 | 248 |
249 // Read annotations in this order: library, top level methods, classes. | 249 // Read annotations in this order: library, top level methods, classes. |
250 _readAnnotations(library); | 250 _readAnnotations(library); |
251 for (var method in _topLevelMethodsOfLibrary(library, seen)) { | 251 for (var method in _topLevelMethodsOfLibrary(library, seen)) { |
252 _readAnnotations(method); | 252 _readAnnotations(method); |
253 } | 253 } |
254 for (var clazz in _classesOfLibrary(library, seen)) { | 254 for (var clazz in _classesOfLibrary(library, seen)) { |
255 var superClass = clazz.supertype; | 255 var superClass = clazz.supertype; |
256 while (superClass != null) { | 256 while (superClass != null) { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 if (c is ShowElementCombinator) { | 408 if (c is ShowElementCombinator) { |
409 var show = c.shownNames.toSet(); | 409 var show = c.shownNames.toSet(); |
410 elements.retainWhere((e) => show.contains(e.displayName)); | 410 elements.retainWhere((e) => show.contains(e.displayName)); |
411 } else if (c is HideElementCombinator) { | 411 } else if (c is HideElementCombinator) { |
412 var hide = c.hiddenNames.toSet(); | 412 var hide = c.hiddenNames.toSet(); |
413 elements.removeWhere((e) => hide.contains(e.displayName)); | 413 elements.removeWhere((e) => hide.contains(e.displayName)); |
414 } | 414 } |
415 } | 415 } |
416 } | 416 } |
417 | 417 |
418 Iterable<LibraryElement> _sortedLibraryImports(LibraryElement library) => | 418 Iterable<LibraryElement> _sortedLibraryDependencies(LibraryElement library) { |
419 (new List.from(library.imports) | 419 // TODO(jakemac): Investigate supporting annotations on part-of directives. |
420 ..sort((ImportElement a, ImportElement b) { | 420 getLibrary(UriReferencedElement element) { |
| 421 if (element is ImportElement) return element.importedLibrary; |
| 422 if (element is ExportElement) return element.exportedLibrary; |
| 423 } |
| 424 |
| 425 return (new List.from(library.imports) |
| 426 ..addAll(library.exports) |
| 427 ..sort((a, b) { |
421 // dart: imports don't have a uri | 428 // dart: imports don't have a uri |
422 if (a.uri == null && b.uri != null) return -1; | 429 if (a.uri == null && b.uri != null) return -1; |
423 if (b.uri == null && a.uri != null) return 1; | 430 if (b.uri == null && a.uri != null) return 1; |
424 if (a.uri == null && b.uri == null) { | 431 if (a.uri == null && b.uri == null) { |
425 return a.importedLibrary.name.compareTo(b.importedLibrary.name); | 432 return getLibrary(a).name.compareTo(getLibrary(b).name); |
426 } | 433 } |
427 | 434 |
428 // package: imports next | 435 // package: imports next |
429 var aIsPackage = a.uri.startsWith('package:'); | 436 var aIsPackage = a.uri.startsWith('package:'); |
430 var bIsPackage = b.uri.startsWith('package:'); | 437 var bIsPackage = b.uri.startsWith('package:'); |
431 if (aIsPackage && !bIsPackage) { | 438 if (aIsPackage && !bIsPackage) { |
432 return -1; | 439 return -1; |
433 } else if (bIsPackage && !aIsPackage) { | 440 } else if (bIsPackage && !aIsPackage) { |
434 return 1; | 441 return 1; |
435 } else if (bIsPackage && aIsPackage) { | 442 } else if (bIsPackage && aIsPackage) { |
436 return a.uri.compareTo(b.uri); | 443 return a.uri.compareTo(b.uri); |
437 } | 444 } |
438 | 445 |
439 // And finally compare based on the relative uri if both are file paths. | 446 // And finally compare based on the relative uri if both are file paths. |
440 var aUri = path.url.relative(a.source.uri.path, | 447 var aUri = path.url.relative(a.source.uri.path, |
441 from: path.url.dirname(library.source.uri.path)); | 448 from: path.url.dirname(library.source.uri.path)); |
442 var bUri = path.url.relative(b.source.uri.path, | 449 var bUri = path.url.relative(b.source.uri.path, |
443 from: path.url.dirname(library.source.uri.path)); | 450 from: path.url.dirname(library.source.uri.path)); |
444 return aUri.compareTo(bUri); | 451 return aUri.compareTo(bUri); |
445 })).map((import) => import.importedLibrary); | 452 })).map(getLibrary); |
| 453 } |
446 } | 454 } |
447 | 455 |
448 /// An [Initializer] annotation and the target of that annotation. | 456 /// An [Initializer] annotation and the target of that annotation. |
449 class InitializerData { | 457 class InitializerData { |
450 /// The target [Element] of the annotation. | 458 /// The target [Element] of the annotation. |
451 final Element targetElement; | 459 final Element targetElement; |
452 | 460 |
453 /// The [ElementAnnotation] representing the annotation itself. | 461 /// The [ElementAnnotation] representing the annotation itself. |
454 final ElementAnnotation annotationElement; | 462 final ElementAnnotation annotationElement; |
455 | 463 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 error = false; | 506 error = false; |
499 } else { | 507 } else { |
500 error = true; | 508 error = true; |
501 } | 509 } |
502 if (error) { | 510 if (error) { |
503 print('Bad value for "$field" in the initialize transformer. ' | 511 print('Bad value for "$field" in the initialize transformer. ' |
504 'Expected either one String or a list of Strings.'); | 512 'Expected either one String or a list of Strings.'); |
505 } | 513 } |
506 return files; | 514 return files; |
507 } | 515 } |
OLD | NEW |