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_transformers.src.dart_sdk; | 5 library code_transformers.src.dart_sdk; |
6 | 6 |
7 import 'dart:io' show Directory; | 7 import 'dart:io' show Directory; |
8 | 8 |
| 9 import 'package:analyzer/file_system/file_system.dart'; |
| 10 import 'package:analyzer/src/context/context.dart'; |
| 11 import 'package:analyzer/src/dart/sdk/sdk.dart'; |
9 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
10 import 'package:analyzer/src/generated/java_io.dart'; | |
11 import 'package:analyzer/src/generated/sdk.dart'; | 13 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:analyzer/src/generated/source.dart'; |
| 15 import 'package:analyzer/src/summary/idl.dart'; |
14 import 'package:cli_util/cli_util.dart' as cli_util; | 16 import 'package:cli_util/cli_util.dart' as cli_util; |
15 | 17 |
16 /// Attempts to provide the current Dart SDK directory. | 18 /// Attempts to provide the current Dart SDK directory. |
17 /// | 19 /// |
18 /// This will return null if the SDK cannot be found | 20 /// This will return null if the SDK cannot be found |
19 /// | 21 /// |
20 /// Note that this may not be correct when executing outside of `pub`. | 22 /// Note that this may not be correct when executing outside of `pub`. |
21 String get dartSdkDirectory { | 23 String get dartSdkDirectory { |
22 Directory sdkDir = cli_util.getSdkDir(); | 24 Directory sdkDir = cli_util.getSdkDir(); |
23 return sdkDir != null ? sdkDir.path : null; | 25 return sdkDir != null ? sdkDir.path : null; |
24 } | 26 } |
25 | 27 |
26 /// Sources that are annotated with a source uri, so it is easy to resolve how | 28 /// Sources that are annotated with a source uri, so it is easy to resolve how |
27 /// to support `Resolver.getImportUri`. | 29 /// to support `Resolver.getImportUri`. |
28 abstract class UriAnnotatedSource extends Source { | 30 abstract class UriAnnotatedSource extends Source { |
29 Uri get uri; | 31 Uri get uri; |
30 } | 32 } |
31 | 33 |
32 /// Dart SDK which wraps all Dart sources as [UriAnnotatedSource] to ensure they | 34 /// Dart SDK which wraps all Dart sources as [UriAnnotatedSource] to ensure they |
33 /// are tracked with Uris. | 35 /// are tracked with Uris. |
34 class DirectoryBasedDartSdkProxy extends DirectoryBasedDartSdk { | 36 class FolderBasedDartSdkProxy extends FolderBasedDartSdk { |
35 DirectoryBasedDartSdkProxy(String sdkDirectory) | 37 FolderBasedDartSdkProxy( |
36 : super(new JavaFile(sdkDirectory)); | 38 ResourceProvider resourceProvider, String sdkDirectory) |
| 39 : super(resourceProvider, resourceProvider.getFolder(sdkDirectory)); |
37 | 40 |
38 Source mapDartUri(String dartUri) => | 41 Source mapDartUri(String dartUri) => |
39 DartSourceProxy.wrap(super.mapDartUri(dartUri), Uri.parse(dartUri)); | 42 DartSourceProxy.wrap(super.mapDartUri(dartUri), Uri.parse(dartUri)); |
40 } | 43 } |
41 | 44 |
42 /// Dart SDK resolver which wraps all Dart sources to ensure they are tracked | 45 /// Dart SDK resolver which wraps all Dart sources to ensure they are tracked |
43 /// with URIs. | 46 /// with URIs. |
44 class DartUriResolverProxy implements DartUriResolver { | 47 class DartUriResolverProxy implements DartUriResolver { |
45 final DartUriResolver _proxy; | 48 final DartUriResolver _proxy; |
46 DartUriResolverProxy(DartSdk sdk) : _proxy = new DartUriResolver(sdk); | 49 DartUriResolverProxy(DartSdk sdk) : _proxy = new DartUriResolver(sdk); |
47 | 50 |
48 Source resolveAbsolute(Uri uri, [Uri actualUri]) => | 51 Source resolveAbsolute(Uri uri, [Uri actualUri]) => |
49 DartSourceProxy.wrap(_proxy.resolveAbsolute(uri, actualUri), uri); | 52 DartSourceProxy.wrap(_proxy.resolveAbsolute(uri, actualUri), uri); |
50 | 53 |
51 DartSdk get dartSdk => _proxy.dartSdk; | 54 DartSdk get dartSdk => _proxy.dartSdk; |
52 | 55 |
53 Source fromEncoding(UriKind kind, Uri uri) => | 56 Source fromEncoding(UriKind kind, Uri uri) => |
54 throw new UnsupportedError('fromEncoding is not supported'); | 57 throw new UnsupportedError('fromEncoding is not supported'); |
55 | 58 |
56 Uri restoreAbsolute(Source source) => | 59 Uri restoreAbsolute(Source source) => |
57 throw new UnsupportedError('restoreAbsolute is not supported'); | 60 throw new UnsupportedError('restoreAbsolute is not supported'); |
58 } | 61 } |
59 | 62 |
60 /// Source file for dart: sources which track the sources with dart: URIs. | 63 /// Source file for dart: sources which track the sources with dart: URIs. |
61 /// | 64 /// |
62 /// This is primarily to support [Resolver.getImportUri] for Dart SDK (dart:) | 65 /// This is primarily to support [Resolver.getImportUri] for Dart SDK (dart:) |
63 /// based libraries. | 66 /// based libraries. |
64 class DartSourceProxy implements UriAnnotatedSource { | 67 class DartSourceProxy implements UriAnnotatedSource { |
65 | |
66 /// Absolute URI which this source can be imported from | 68 /// Absolute URI which this source can be imported from |
67 final Uri uri; | 69 final Uri uri; |
68 | 70 |
69 /// Underlying source object. | 71 /// Underlying source object. |
70 final Source _proxy; | 72 final Source _proxy; |
71 | 73 |
72 Source get source => this; | 74 Source get source => this; |
73 | 75 |
74 DartSourceProxy(this._proxy, this.uri); | 76 DartSourceProxy(this._proxy, this.uri); |
75 | 77 |
(...skipping 25 matching lines...) Expand all Loading... |
101 (other is DartSourceProxy && _proxy == other._proxy); | 103 (other is DartSourceProxy && _proxy == other._proxy); |
102 | 104 |
103 int get hashCode => _proxy.hashCode; | 105 int get hashCode => _proxy.hashCode; |
104 | 106 |
105 TimestampedData<String> get contents => _proxy.contents; | 107 TimestampedData<String> get contents => _proxy.contents; |
106 | 108 |
107 String get encoding => _proxy.encoding; | 109 String get encoding => _proxy.encoding; |
108 | 110 |
109 String get fullName => _proxy.fullName; | 111 String get fullName => _proxy.fullName; |
110 | 112 |
| 113 Source get librarySource => _proxy.librarySource; |
| 114 |
111 int get modificationStamp => _proxy.modificationStamp; | 115 int get modificationStamp => _proxy.modificationStamp; |
112 | 116 |
113 String get shortName => _proxy.shortName; | 117 String get shortName => _proxy.shortName; |
114 | 118 |
115 UriKind get uriKind => _proxy.uriKind; | 119 UriKind get uriKind => _proxy.uriKind; |
116 | 120 |
117 bool get isInSystemLibrary => _proxy.isInSystemLibrary; | 121 bool get isInSystemLibrary => _proxy.isInSystemLibrary; |
118 } | 122 } |
119 | 123 |
120 /// Dart SDK which contains a mock implementation of the SDK libraries. May be | 124 /// 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. | 125 /// used to speed up resultion when most of the core libraries is not needed. |
122 class MockDartSdk implements DartSdk { | 126 class MockDartSdk implements DartSdk { |
123 final Map<Uri, _MockSdkSource> _sources = {}; | 127 final Map<Uri, _MockSdkSource> _sources = {}; |
124 final bool reportMissing; | 128 final bool reportMissing; |
125 final Map<String, SdkLibrary> _libs = {}; | 129 final Map<String, SdkLibrary> _libs = {}; |
126 final String sdkVersion = '0'; | 130 final String sdkVersion = '0'; |
127 List<String> get uris => _sources.keys.map((uri) => '$uri').toList(); | 131 List<String> get uris => _sources.keys.map((uri) => '$uri').toList(); |
128 final AnalysisContext context = new SdkAnalysisContext(); | 132 final InternalAnalysisContext context; |
129 DartUriResolver _resolver; | 133 DartUriResolver _resolver; |
130 DartUriResolver get resolver => _resolver; | 134 DartUriResolver get resolver => _resolver; |
131 | 135 |
132 MockDartSdk(Map<String, String> sources, {this.reportMissing}) { | 136 MockDartSdk(Map<String, String> sources, AnalysisOptions options, |
| 137 {this.reportMissing}) |
| 138 : this.context = new SdkAnalysisContext(options) { |
133 sources.forEach((uriString, contents) { | 139 sources.forEach((uriString, contents) { |
134 var uri = Uri.parse(uriString); | 140 var uri = Uri.parse(uriString); |
135 _sources[uri] = new _MockSdkSource(uri, contents); | 141 _sources[uri] = new _MockSdkSource(uri, contents); |
136 _libs[uriString] = new SdkLibraryImpl(uri.path) | 142 _libs[uriString] = new SdkLibraryImpl(uri.path) |
137 ..setDart2JsLibrary() | 143 ..setDart2JsLibrary() |
138 ..setVmLibrary(); | 144 ..setVmLibrary(); |
139 }); | 145 }); |
140 _resolver = new DartUriResolver(this); | 146 _resolver = new DartUriResolver(this); |
141 context.sourceFactory = new SourceFactory([_resolver]); | 147 context.sourceFactory = new SourceFactory([_resolver]); |
142 } | 148 } |
(...skipping 16 matching lines...) Expand all Loading... |
159 _sources[uri] = | 165 _sources[uri] = |
160 src = new _MockSdkSource(uri, 'library dart.${uri.path};'); | 166 src = new _MockSdkSource(uri, 'library dart.${uri.path};'); |
161 } | 167 } |
162 return src; | 168 return src; |
163 } | 169 } |
164 | 170 |
165 @override | 171 @override |
166 Source fromFileUri(Uri uri) { | 172 Source fromFileUri(Uri uri) { |
167 throw new UnsupportedError('MockDartSdk.fromFileUri'); | 173 throw new UnsupportedError('MockDartSdk.fromFileUri'); |
168 } | 174 } |
| 175 |
| 176 @override |
| 177 PackageBundle getLinkedBundle() => null; |
169 } | 178 } |
170 | 179 |
171 class _MockSdkSource implements UriAnnotatedSource { | 180 class _MockSdkSource implements UriAnnotatedSource { |
172 /// Absolute URI which this source can be imported from. | 181 /// Absolute URI which this source can be imported from. |
173 final Uri uri; | 182 final Uri uri; |
174 final String _contents; | 183 final String _contents; |
175 | 184 |
176 Source get source => this; | 185 Source get source => this; |
177 | 186 |
| 187 Source get librarySource => null; |
| 188 |
178 _MockSdkSource(this.uri, this._contents); | 189 _MockSdkSource(this.uri, this._contents); |
179 | 190 |
180 bool exists() => true; | 191 bool exists() => true; |
181 | 192 |
182 int get hashCode => uri.hashCode; | 193 int get hashCode => uri.hashCode; |
183 | 194 |
184 final int modificationStamp = 1; | 195 final int modificationStamp = 1; |
185 | 196 |
186 TimestampedData<String> get contents => | 197 TimestampedData<String> get contents => |
187 new TimestampedData(modificationStamp, _contents); | 198 new TimestampedData(modificationStamp, _contents); |
188 | 199 |
189 String get encoding => "${uriKind.encoding}$uri"; | 200 String get encoding => "${uriKind.encoding}$uri"; |
190 | 201 |
191 String get fullName => shortName; | 202 String get fullName => shortName; |
192 | 203 |
193 String get shortName => uri.path; | 204 String get shortName => uri.path; |
194 | 205 |
195 UriKind get uriKind => UriKind.DART_URI; | 206 UriKind get uriKind => UriKind.DART_URI; |
196 | 207 |
197 bool get isInSystemLibrary => true; | 208 bool get isInSystemLibrary => true; |
198 | 209 |
199 Source resolveRelative(Uri relativeUri) => | 210 Source resolveRelative(Uri relativeUri) => |
200 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | 211 throw new UnsupportedError('not expecting relative urls in dart: mocks'); |
201 | 212 |
202 Uri resolveRelativeUri(Uri relativeUri) => | 213 Uri resolveRelativeUri(Uri relativeUri) => |
203 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | 214 throw new UnsupportedError('not expecting relative urls in dart: mocks'); |
| 215 |
| 216 bool operator ==(Object other) => identical(this, other); |
204 } | 217 } |
205 | 218 |
206 /// Sample mock SDK sources. | 219 /// Sample mock SDK sources. |
207 final Map<String, String> mockSdkSources = { | 220 final Map<String, String> mockSdkSources = { |
208 // The list of types below is derived from types that are used internally by | 221 // 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). | 222 // the resolver (see _initializeFrom in analyzer/src/generated/resolver.dart). |
210 'dart:core': ''' | 223 'dart:core': ''' |
211 library dart.core; | 224 library dart.core; |
212 | 225 |
213 void print(Object o) {} | 226 void print(Object o) {} |
(...skipping 28 matching lines...) Expand all Loading... |
242 class _Proxy { const _Proxy(); } | 255 class _Proxy { const _Proxy(); } |
243 const Object proxy = const _Proxy(); | 256 const Object proxy = const _Proxy(); |
244 | 257 |
245 class Iterable<E> {} | 258 class Iterable<E> {} |
246 class List<E> implements Iterable<E> {} | 259 class List<E> implements Iterable<E> {} |
247 class Map<K, V> {} | 260 class Map<K, V> {} |
248 ''', | 261 ''', |
249 'dart:async': ''' | 262 'dart:async': ''' |
250 class Future<T> { | 263 class Future<T> { |
251 Future then(callback) {} | 264 Future then(callback) {} |
| 265 } |
| 266 class FutureOr<T> {} |
252 class Stream<T> {} | 267 class Stream<T> {} |
253 ''', | 268 ''', |
254 'dart:html': ''' | 269 'dart:html': ''' |
255 library dart.html; | 270 library dart.html; |
256 class HtmlElement {} | 271 class HtmlElement {} |
257 ''', | 272 ''', |
258 }; | 273 }; |
OLD | NEW |