OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 | 4 |
5 library code_transformer.src.resolver_impl; | 5 library code_transformer.src.resolver_impl; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'package:analyzer/analyzer.dart' show parseDirectives; | 8 import 'package:analyzer/analyzer.dart' show parseDirectives; |
9 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; | 9 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; |
10 import 'package:analyzer/src/generated/constant.dart' | 10 import 'package:analyzer/src/generated/constant.dart' |
(...skipping 13 matching lines...) Expand all Loading... |
24 | 24 |
25 // We should always be using url paths here since it's always Dart/pub code. | 25 // We should always be using url paths here since it's always Dart/pub code. |
26 final path = native_path.url; | 26 final path = native_path.url; |
27 | 27 |
28 /// Resolves and updates an AST based on Barback-based assets. | 28 /// Resolves and updates an AST based on Barback-based assets. |
29 /// | 29 /// |
30 /// This also provides a handful of useful APIs for traversing and working | 30 /// This also provides a handful of useful APIs for traversing and working |
31 /// with the resolved AST. | 31 /// with the resolved AST. |
32 class ResolverImpl implements Resolver { | 32 class ResolverImpl implements Resolver { |
33 /// Cache of all asset sources currently referenced. | 33 /// Cache of all asset sources currently referenced. |
34 final Map<AssetId, _AssetBasedSource> sources = <AssetId, _AssetBasedSource>{ | 34 final Map<AssetId, _AssetBasedSource> sources = |
35 }; | 35 <AssetId, _AssetBasedSource>{}; |
36 | 36 |
37 final InternalAnalysisContext _context = | 37 final InternalAnalysisContext _context = |
38 AnalysisEngine.instance.createAnalysisContext(); | 38 AnalysisEngine.instance.createAnalysisContext(); |
39 | 39 |
40 /// Transform for which this is currently updating, or null when not updating. | 40 /// Transform for which this is currently updating, or null when not updating. |
41 Transform _currentTransform; | 41 Transform _currentTransform; |
42 | 42 |
43 /// The currently resolved entry libraries, or null if nothing is resolved. | 43 /// The currently resolved entry libraries, or null if nothing is resolved. |
44 List<LibraryElement> _entryLibraries; | 44 List<LibraryElement> _entryLibraries; |
45 Set<LibraryElement> _libraries; | 45 Set<LibraryElement> _libraries; |
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 var source = _resolver.sources[id]; | 399 var source = _resolver.sources[id]; |
400 if (source == null) { | 400 if (source == null) { |
401 _logger.error('Could not load asset $id'); | 401 _logger.error('Could not load asset $id'); |
402 } | 402 } |
403 return source.uri; | 403 return source.uri; |
404 } | 404 } |
405 | 405 |
406 /// For logging errors. | 406 /// For logging errors. |
407 SourceSpan _getSpan(AstNode node, [String contents]) => | 407 SourceSpan _getSpan(AstNode node, [String contents]) => |
408 _getSourceFile(contents).span(node.offset, node.end); | 408 _getSourceFile(contents).span(node.offset, node.end); |
| 409 |
409 /// For logging errors. | 410 /// For logging errors. |
410 SourceFile _getSourceFile([String contents]) { | 411 SourceFile _getSourceFile([String contents]) { |
411 var uri = assetIdToUri(assetId); | 412 var uri = assetIdToUri(assetId); |
412 var path = uri != null ? uri : assetId.path; | 413 var path = uri != null ? uri : assetId.path; |
413 return new SourceFile(contents != null ? contents : rawContents, url: path); | 414 return new SourceFile(contents != null ? contents : rawContents, url: path); |
414 } | 415 } |
415 } | 416 } |
416 | 417 |
417 /// Implementation of Analyzer's UriResolver for Barback based assets. | 418 /// Implementation of Analyzer's UriResolver for Barback based assets. |
418 class _AssetUriResolver implements UriResolver { | 419 class _AssetUriResolver implements UriResolver { |
419 final ResolverImpl _resolver; | 420 final ResolverImpl _resolver; |
420 _AssetUriResolver(this._resolver); | 421 _AssetUriResolver(this._resolver); |
421 | 422 |
422 Source resolveAbsolute(Uri uri) { | 423 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
423 assert(uri.scheme != 'dart'); | 424 assert(uri.scheme != 'dart'); |
424 var assetId; | 425 var assetId; |
425 if (uri.scheme == 'asset') { | 426 if (uri.scheme == 'asset') { |
426 var parts = path.split(uri.path); | 427 var parts = path.split(uri.path); |
427 assetId = new AssetId(parts[0], path.joinAll(parts.skip(1))); | 428 assetId = new AssetId(parts[0], path.joinAll(parts.skip(1))); |
428 } else { | 429 } else { |
429 assetId = _resolve(null, uri.toString(), logger, null); | 430 assetId = _resolve(null, uri.toString(), logger, null); |
430 if (assetId == null) { | 431 if (assetId == null) { |
431 logger.error('Unable to resolve asset ID for "$uri"'); | 432 logger.error('Unable to resolve asset ID for "$uri"'); |
432 return null; | 433 return null; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 | 545 |
545 void apply(ChangeSet changeSet) { | 546 void apply(ChangeSet changeSet) { |
546 if (!source.updateContents(content)) return; | 547 if (!source.updateContents(content)) return; |
547 if (source._revision == 1 && source._contents != null) { | 548 if (source._revision == 1 && source._contents != null) { |
548 changeSet.addedSource(source); | 549 changeSet.addedSource(source); |
549 } else { | 550 } else { |
550 changeSet.changedSource(source); | 551 changeSet.changedSource(source); |
551 } | 552 } |
552 } | 553 } |
553 } | 554 } |
OLD | NEW |