| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library code_transformers.src.dart_sdk; | |
| 6 | |
| 7 import 'dart:io' show Directory; | |
| 8 | |
| 9 import 'package:analyzer/src/generated/engine.dart'; | |
| 10 import 'package:analyzer/src/generated/java_io.dart'; | |
| 11 import 'package:analyzer/src/generated/sdk.dart'; | |
| 12 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; | |
| 13 import 'package:analyzer/src/generated/source.dart'; | |
| 14 import 'package:cli_util/cli_util.dart' as cli_util; | |
| 15 | |
| 16 /// Attempts to provide the current Dart SDK directory. | |
| 17 /// | |
| 18 /// This will return null if the SDK cannot be found | |
| 19 /// | |
| 20 /// Note that this may not be correct when executing outside of `pub`. | |
| 21 String get dartSdkDirectory { | |
| 22 Directory sdkDir = cli_util.getSdkDir(); | |
| 23 return sdkDir != null ? sdkDir.path : null; | |
| 24 } | |
| 25 | |
| 26 /// Sources that are annotated with a source uri, so it is easy to resolve how | |
| 27 /// to support `Resolver.getImportUri`. | |
| 28 abstract class UriAnnotatedSource extends Source { | |
| 29 Uri get uri; | |
| 30 } | |
| 31 | |
| 32 /// Dart SDK which wraps all Dart sources as [UriAnnotatedSource] to ensure they | |
| 33 /// are tracked with Uris. | |
| 34 class DirectoryBasedDartSdkProxy extends DirectoryBasedDartSdk { | |
| 35 DirectoryBasedDartSdkProxy(String sdkDirectory) | |
| 36 : super(new JavaFile(sdkDirectory)); | |
| 37 | |
| 38 Source mapDartUri(String dartUri) => | |
| 39 DartSourceProxy.wrap(super.mapDartUri(dartUri), Uri.parse(dartUri)); | |
| 40 } | |
| 41 | |
| 42 /// Dart SDK resolver which wraps all Dart sources to ensure they are tracked | |
| 43 /// with URIs. | |
| 44 class DartUriResolverProxy implements DartUriResolver { | |
| 45 final DartUriResolver _proxy; | |
| 46 DartUriResolverProxy(DartSdk sdk) : _proxy = new DartUriResolver(sdk); | |
| 47 | |
| 48 Source resolveAbsolute(Uri uri) => | |
| 49 DartSourceProxy.wrap(_proxy.resolveAbsolute(uri), uri); | |
| 50 | |
| 51 DartSdk get dartSdk => _proxy.dartSdk; | |
| 52 | |
| 53 Source fromEncoding(UriKind kind, Uri uri) => | |
| 54 throw new UnsupportedError('fromEncoding is not supported'); | |
| 55 | |
| 56 Uri restoreAbsolute(Source source) => | |
| 57 throw new UnsupportedError('restoreAbsolute is not supported'); | |
| 58 } | |
| 59 | |
| 60 /// Source file for dart: sources which track the sources with dart: URIs. | |
| 61 /// | |
| 62 /// This is primarily to support [Resolver.getImportUri] for Dart SDK (dart:) | |
| 63 /// based libraries. | |
| 64 class DartSourceProxy implements UriAnnotatedSource { | |
| 65 | |
| 66 /// Absolute URI which this source can be imported from | |
| 67 final Uri uri; | |
| 68 | |
| 69 /// Underlying source object. | |
| 70 final Source _proxy; | |
| 71 | |
| 72 Source get source => this; | |
| 73 | |
| 74 DartSourceProxy(this._proxy, this.uri); | |
| 75 | |
| 76 /// Ensures that [source] is a DartSourceProxy. | |
| 77 static DartSourceProxy wrap(Source source, Uri uri) { | |
| 78 if (source == null || source is DartSourceProxy) return source; | |
| 79 return new DartSourceProxy(source, uri); | |
| 80 } | |
| 81 | |
| 82 // Note: to support both analyzer versions <0.22.0 and analyzer >=0.22.0, we | |
| 83 // implement both `resolveRelative` and `resolveRelativeUri`. Only one of them | |
| 84 // is available at a time in the analyzer package, so we use the `as dynamic` | |
| 85 // in these methods to hide warnings for the code that is missing. These APIs | |
| 86 // are invoked from the analyzer itself, so we don't expect them to cause | |
| 87 // failures. | |
| 88 Source resolveRelative(Uri relativeUri) { | |
| 89 // Assume that the type can be accessed via this URI, since these | |
| 90 // should only be parts for dart core files. | |
| 91 return wrap((_proxy as dynamic).resolveRelative(relativeUri), uri); | |
| 92 } | |
| 93 | |
| 94 Uri resolveRelativeUri(Uri relativeUri) { | |
| 95 return (_proxy as dynamic).resolveRelativeUri(relativeUri); | |
| 96 } | |
| 97 | |
| 98 bool exists() => _proxy.exists(); | |
| 99 | |
| 100 bool operator ==(Object other) => | |
| 101 (other is DartSourceProxy && _proxy == other._proxy); | |
| 102 | |
| 103 int get hashCode => _proxy.hashCode; | |
| 104 | |
| 105 TimestampedData<String> get contents => _proxy.contents; | |
| 106 | |
| 107 String get encoding => _proxy.encoding; | |
| 108 | |
| 109 String get fullName => _proxy.fullName; | |
| 110 | |
| 111 int get modificationStamp => _proxy.modificationStamp; | |
| 112 | |
| 113 String get shortName => _proxy.shortName; | |
| 114 | |
| 115 UriKind get uriKind => _proxy.uriKind; | |
| 116 | |
| 117 bool get isInSystemLibrary => _proxy.isInSystemLibrary; | |
| 118 } | |
| 119 | |
| 120 /// Dart SDK which contains a mock implementation of the SDK libraries. May be | |
| 121 /// used to speed up resultion when most of the core libraries is not needed. | |
| 122 class MockDartSdk implements DartSdk { | |
| 123 final Map<Uri, _MockSdkSource> _sources = {}; | |
| 124 final bool reportMissing; | |
| 125 final Map<String, SdkLibrary> _libs = {}; | |
| 126 final String sdkVersion = '0'; | |
| 127 List<String> get uris => _sources.keys.map((uri) => '$uri').toList(); | |
| 128 final AnalysisContext context = new SdkAnalysisContext(); | |
| 129 DartUriResolver _resolver; | |
| 130 DartUriResolver get resolver => _resolver; | |
| 131 | |
| 132 MockDartSdk(Map<String, String> sources, {this.reportMissing}) { | |
| 133 sources.forEach((uriString, contents) { | |
| 134 var uri = Uri.parse(uriString); | |
| 135 _sources[uri] = new _MockSdkSource(uri, contents); | |
| 136 _libs[uriString] = new SdkLibraryImpl(uri.path) | |
| 137 ..setDart2JsLibrary() | |
| 138 ..setVmLibrary(); | |
| 139 }); | |
| 140 _resolver = new DartUriResolver(this); | |
| 141 context.sourceFactory = new SourceFactory([_resolver]); | |
| 142 } | |
| 143 | |
| 144 List<SdkLibrary> get sdkLibraries => _libs.values.toList(); | |
| 145 SdkLibrary getSdkLibrary(String dartUri) => _libs[dartUri]; | |
| 146 Source mapDartUri(String dartUri) => _getSource(Uri.parse(dartUri)); | |
| 147 | |
| 148 Source fromEncoding(UriKind kind, Uri uri) { | |
| 149 if (kind != UriKind.DART_URI) { | |
| 150 throw new UnsupportedError('expected dart: uri kind, got $kind.'); | |
| 151 } | |
| 152 return _getSource(uri); | |
| 153 } | |
| 154 | |
| 155 Source _getSource(Uri uri) { | |
| 156 var src = _sources[uri]; | |
| 157 if (src == null) { | |
| 158 if (reportMissing) print('warning: missing mock for $uri.'); | |
| 159 _sources[uri] = | |
| 160 src = new _MockSdkSource(uri, 'library dart.${uri.path};'); | |
| 161 } | |
| 162 return src; | |
| 163 } | |
| 164 | |
| 165 @override | |
| 166 Source fromFileUri(Uri uri) { | |
| 167 throw new UnsupportedError('MockDartSdk.fromFileUri'); | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 class _MockSdkSource implements UriAnnotatedSource { | |
| 172 /// Absolute URI which this source can be imported from. | |
| 173 final Uri uri; | |
| 174 final String _contents; | |
| 175 | |
| 176 Source get source => this; | |
| 177 | |
| 178 _MockSdkSource(this.uri, this._contents); | |
| 179 | |
| 180 bool exists() => true; | |
| 181 | |
| 182 int get hashCode => uri.hashCode; | |
| 183 | |
| 184 final int modificationStamp = 1; | |
| 185 | |
| 186 TimestampedData<String> get contents => | |
| 187 new TimestampedData(modificationStamp, _contents); | |
| 188 | |
| 189 String get encoding => "${uriKind.encoding}$uri"; | |
| 190 | |
| 191 String get fullName => shortName; | |
| 192 | |
| 193 String get shortName => uri.path; | |
| 194 | |
| 195 UriKind get uriKind => UriKind.DART_URI; | |
| 196 | |
| 197 bool get isInSystemLibrary => true; | |
| 198 | |
| 199 Source resolveRelative(Uri relativeUri) => | |
| 200 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | |
| 201 | |
| 202 Uri resolveRelativeUri(Uri relativeUri) => | |
| 203 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | |
| 204 } | |
| 205 | |
| 206 /// Sample mock SDK sources. | |
| 207 final Map<String, String> mockSdkSources = { | |
| 208 // The list of types below is derived from types that are used internally by | |
| 209 // the resolver (see _initializeFrom in analyzer/src/generated/resolver.dart). | |
| 210 'dart:core': ''' | |
| 211 library dart.core; | |
| 212 | |
| 213 void print(Object o) {} | |
| 214 | |
| 215 class Object { | |
| 216 String toString(){} | |
| 217 } | |
| 218 class Function {} | |
| 219 class StackTrace {} | |
| 220 class Symbol {} | |
| 221 class Type {} | |
| 222 | |
| 223 class String {} | |
| 224 class bool {} | |
| 225 class num { | |
| 226 num operator +(num other) {} | |
| 227 } | |
| 228 class int extends num { | |
| 229 int operator-() {} | |
| 230 } | |
| 231 class double extends num {} | |
| 232 class DateTime {} | |
| 233 class Null {} | |
| 234 | |
| 235 class Deprecated { | |
| 236 final String expires; | |
| 237 const Deprecated(this.expires); | |
| 238 } | |
| 239 const Object deprecated = const Deprecated("next release"); | |
| 240 class _Override { const _Override(); } | |
| 241 const Object override = const _Override(); | |
| 242 class _Proxy { const _Proxy(); } | |
| 243 const Object proxy = const _Proxy(); | |
| 244 | |
| 245 class Iterable<E> {} | |
| 246 class List<E> implements Iterable<E> {} | |
| 247 class Map<K, V> {} | |
| 248 ''', | |
| 249 'dart:async': ''' | |
| 250 class Future<T> { | |
| 251 Future then(callback) {} | |
| 252 class Stream<T> {} | |
| 253 ''', | |
| 254 'dart:html': ''' | |
| 255 library dart.html; | |
| 256 class HtmlElement {} | |
| 257 ''', | |
| 258 }; | |
| OLD | NEW |