| 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' show ConstantEvaluator, | 10 import 'package:analyzer/src/generated/constant.dart' show ConstantEvaluator, |
| 11 EvaluationResult; | 11 EvaluationResult; |
| 12 import 'package:analyzer/src/generated/element.dart'; | 12 import 'package:analyzer/src/generated/element.dart'; |
| 13 import 'package:analyzer/src/generated/engine.dart'; | 13 import 'package:analyzer/src/generated/engine.dart'; |
| 14 import 'package:analyzer/src/generated/sdk.dart' show DartSdk; | 14 import 'package:analyzer/src/generated/sdk.dart' show DartSdk; |
| 15 import 'package:analyzer/src/generated/source.dart'; | 15 import 'package:analyzer/src/generated/source.dart'; |
| 16 import 'package:barback/barback.dart'; | 16 import 'package:barback/barback.dart'; |
| 17 import 'package:code_transformers/assets.dart'; | 17 import 'package:code_transformers/assets.dart'; |
| 18 import 'package:path/path.dart' as native_path; | 18 import 'package:path/path.dart' as native_path; |
| 19 import 'package:source_maps/refactor.dart'; | 19 import 'package:source_maps/refactor.dart'; |
| 20 import 'package:source_maps/span.dart' show SourceFile, Span; | 20 import 'package:source_span/source_span.dart'; |
| 21 | 21 |
| 22 import 'resolver.dart'; | 22 import 'resolver.dart'; |
| 23 import 'dart_sdk.dart' show UriAnnotatedSource; | 23 import 'dart_sdk.dart' show UriAnnotatedSource; |
| 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 |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 // Should not be able to encounter any other source types. | 247 // Should not be able to encounter any other source types. |
| 248 throw new StateError('Unable to resolve URI for ${source.runtimeType}'); | 248 throw new StateError('Unable to resolve URI for ${source.runtimeType}'); |
| 249 } | 249 } |
| 250 | 250 |
| 251 AssetId getSourceAssetId(Element element) { | 251 AssetId getSourceAssetId(Element element) { |
| 252 var source = element.source; | 252 var source = element.source; |
| 253 if (source is _AssetBasedSource) return source.assetId; | 253 if (source is _AssetBasedSource) return source.assetId; |
| 254 return null; | 254 return null; |
| 255 } | 255 } |
| 256 | 256 |
| 257 Span getSourceSpan(Element element) { | 257 SourceSpan getSourceSpan(Element element) { |
| 258 var sourceFile = getSourceFile(element); | 258 var sourceFile = getSourceFile(element); |
| 259 if (sourceFile == null) return null; | 259 if (sourceFile == null) return null; |
| 260 return sourceFile.span(element.node.offset, element.node.end); | 260 return sourceFile.span(element.node.offset, element.node.end); |
| 261 } | 261 } |
| 262 | 262 |
| 263 TextEditTransaction createTextEditTransaction(Element element) { | 263 TextEditTransaction createTextEditTransaction(Element element) { |
| 264 if (element.source is! _AssetBasedSource) return null; | 264 if (element.source is! _AssetBasedSource) return null; |
| 265 | 265 |
| 266 // Cannot edit unless there is an active transformer. | 266 // Cannot edit unless there is an active transformer. |
| 267 if (_currentTransform == null) return null; | 267 if (_currentTransform == null) return null; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 278 return new TextEditTransaction(source.rawContents, sourceFile); | 278 return new TextEditTransaction(source.rawContents, sourceFile); |
| 279 } | 279 } |
| 280 | 280 |
| 281 /// Gets the SourceFile for the source of the element. | 281 /// Gets the SourceFile for the source of the element. |
| 282 SourceFile getSourceFile(Element element) { | 282 SourceFile getSourceFile(Element element) { |
| 283 var assetId = getSourceAssetId(element); | 283 var assetId = getSourceAssetId(element); |
| 284 if (assetId == null) return null; | 284 if (assetId == null) return null; |
| 285 | 285 |
| 286 var importUri = _getSourceUri(element); | 286 var importUri = _getSourceUri(element); |
| 287 var spanPath = importUri != null ? importUri.toString() : assetId.path; | 287 var spanPath = importUri != null ? importUri.toString() : assetId.path; |
| 288 return new SourceFile.text(spanPath, sources[assetId].rawContents); | 288 return new SourceFile(sources[assetId].rawContents, url: spanPath); |
| 289 } | 289 } |
| 290 } | 290 } |
| 291 | 291 |
| 292 /// Implementation of Analyzer's Source for Barback based assets. | 292 /// Implementation of Analyzer's Source for Barback based assets. |
| 293 class _AssetBasedSource extends Source { | 293 class _AssetBasedSource extends Source { |
| 294 | 294 |
| 295 /// Asset ID where this source can be found. | 295 /// Asset ID where this source can be found. |
| 296 final AssetId assetId; | 296 final AssetId assetId; |
| 297 | 297 |
| 298 /// The resolver this is being used in. | 298 /// The resolver this is being used in. |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 | 383 |
| 384 // The entire AST should have been parsed and loaded at this point. | 384 // The entire AST should have been parsed and loaded at this point. |
| 385 var source = _resolver.sources[id]; | 385 var source = _resolver.sources[id]; |
| 386 if (source == null) { | 386 if (source == null) { |
| 387 _logger.error('Could not load asset $id'); | 387 _logger.error('Could not load asset $id'); |
| 388 } | 388 } |
| 389 return source; | 389 return source; |
| 390 } | 390 } |
| 391 | 391 |
| 392 /// For logging errors. | 392 /// For logging errors. |
| 393 Span _getSpan(AstNode node, [String contents]) => | 393 SourceSpan _getSpan(AstNode node, [String contents]) => |
| 394 _getSourceFile(contents).span(node.offset, node.end); | 394 _getSourceFile(contents).span(node.offset, node.end); |
| 395 /// For logging errors. | 395 /// For logging errors. |
| 396 SourceFile _getSourceFile([String contents]) { | 396 SourceFile _getSourceFile([String contents]) { |
| 397 var uri = getSourceUri(); | 397 var uri = getSourceUri(); |
| 398 var path = uri != null ? uri.toString() : assetId.path; | 398 var path = uri != null ? uri.toString() : assetId.path; |
| 399 return new SourceFile.text(path, contents != null ? contents : rawContents); | 399 return new SourceFile(contents != null ? contents : rawContents, url: path); |
| 400 } | 400 } |
| 401 | 401 |
| 402 /// Gets a URI which would be appropriate for importing this file. | 402 /// Gets a URI which would be appropriate for importing this file. |
| 403 /// | 403 /// |
| 404 /// Note that this file may represent a non-importable file such as a part. | 404 /// Note that this file may represent a non-importable file such as a part. |
| 405 Uri getSourceUri([AssetId from]) { | 405 Uri getSourceUri([AssetId from]) { |
| 406 if (!assetId.path.startsWith('lib/')) { | 406 if (!assetId.path.startsWith('lib/')) { |
| 407 // Cannot do absolute imports of non lib-based assets. | 407 // Cannot do absolute imports of non lib-based assets. |
| 408 if (from == null) return null; | 408 if (from == null) return null; |
| 409 | 409 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 throw new UnsupportedError('fromEncoding is not supported'); | 441 throw new UnsupportedError('fromEncoding is not supported'); |
| 442 | 442 |
| 443 Uri restoreAbsolute(Source source) => | 443 Uri restoreAbsolute(Source source) => |
| 444 throw new UnsupportedError('restoreAbsolute is not supported'); | 444 throw new UnsupportedError('restoreAbsolute is not supported'); |
| 445 | 445 |
| 446 TransformLogger get logger => _resolver._currentTransform.logger; | 446 TransformLogger get logger => _resolver._currentTransform.logger; |
| 447 } | 447 } |
| 448 | 448 |
| 449 /// Get an asset ID for a URL relative to another source asset. | 449 /// Get an asset ID for a URL relative to another source asset. |
| 450 AssetId _resolve(AssetId source, String url, TransformLogger logger, | 450 AssetId _resolve(AssetId source, String url, TransformLogger logger, |
| 451 Span span) { | 451 SourceSpan span) { |
| 452 if (url == null || url == '') return null; | 452 if (url == null || url == '') return null; |
| 453 var uri = Uri.parse(url); | 453 var uri = Uri.parse(url); |
| 454 | 454 |
| 455 // Workaround for dartbug.com/17156- pub transforms package: imports from | 455 // Workaround for dartbug.com/17156- pub transforms package: imports from |
| 456 // files of the transformers package to have absolute /packages/ URIs. | 456 // files of the transformers package to have absolute /packages/ URIs. |
| 457 if (uri.scheme == '' && path.isAbsolute(url) | 457 if (uri.scheme == '' && path.isAbsolute(url) |
| 458 && uri.pathSegments[0] == 'packages') { | 458 && uri.pathSegments[0] == 'packages') { |
| 459 uri = Uri.parse('package:${uri.pathSegments.skip(1).join(path.separator)}'); | 459 uri = Uri.parse('package:${uri.pathSegments.skip(1).join(path.separator)}'); |
| 460 } | 460 } |
| 461 | 461 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 | 539 |
| 540 void apply(ChangeSet changeSet) { | 540 void apply(ChangeSet changeSet) { |
| 541 if (!source.updateContents(content)) return; | 541 if (!source.updateContents(content)) return; |
| 542 if (source._revision == 1 && source._contents != null) { | 542 if (source._revision == 1 && source._contents != null) { |
| 543 changeSet.addedSource(source); | 543 changeSet.addedSource(source); |
| 544 } else { | 544 } else { |
| 545 changeSet.changedSource(source); | 545 changeSet.changedSource(source); |
| 546 } | 546 } |
| 547 } | 547 } |
| 548 } | 548 } |
| OLD | NEW |