OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /// Common logic needed to provide a Dart SDK to the analyzer's resolver. This | |
6 /// includes logic to determine where the sdk is located in the filesystem, and | |
7 /// definitions to provide mock sdks. | |
8 | |
9 import 'package:analyzer/src/generated/engine.dart' | |
10 show AnalysisContext, AnalysisEngine, TimestampedData; | |
11 import 'package:analyzer/src/generated/sdk.dart' | |
12 show DartSdk, SdkLibrary, SdkLibraryImpl; | |
13 import 'package:analyzer/src/generated/source.dart' | |
14 show DartUriResolver, Source, SourceFactory, UriKind; | |
15 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; | |
16 import 'package:analyzer/src/context/cache.dart' | |
17 show AnalysisCache, CachePartition; | |
18 | |
19 /// Dart SDK which contains a mock implementation of the SDK libraries. May be | |
20 /// used to speed up execution when most of the core libraries is not needed. | |
21 // | |
22 // TODO(jmesserly): replace this with Analyzer's mock SDK, or use summaries | |
23 // to load the real SDK. | |
24 class MockDartSdk implements DartSdk { | |
25 final Map<Uri, _MockSdkSource> _sources = {}; | |
26 final bool reportMissing; | |
27 final Map<String, SdkLibrary> _libs = {}; | |
28 final String sdkVersion = '0'; | |
29 List<String> get uris => _sources.keys.map((uri) => '$uri').toList(); | |
30 | |
31 AnalysisContext context; | |
32 DartUriResolver _resolver; | |
33 DartUriResolver get resolver => _resolver; | |
34 | |
35 MockDartSdk(Map<String, String> sources, {this.reportMissing}) { | |
36 context = new _SdkAnalysisContext(this); | |
37 sources.forEach((uriString, contents) { | |
38 var uri = Uri.parse(uriString); | |
39 _sources[uri] = new _MockSdkSource(uri, contents); | |
40 _libs[uriString] = new SdkLibraryImpl(uri.path) | |
41 ..setDart2JsLibrary() | |
42 ..setVmLibrary(); | |
43 }); | |
44 _resolver = new DartUriResolver(this); | |
45 context.sourceFactory = new SourceFactory([_resolver]); | |
46 } | |
47 | |
48 List<SdkLibrary> get sdkLibraries => _libs.values.toList(); | |
49 SdkLibrary getSdkLibrary(String dartUri) => _libs[dartUri]; | |
50 Source mapDartUri(String dartUri) => _getSource(Uri.parse(dartUri)); | |
51 | |
52 Source fromEncoding(UriKind kind, Uri uri) { | |
53 if (kind != UriKind.DART_URI) { | |
54 throw new UnsupportedError('expected dart: uri kind, got $kind.'); | |
55 } | |
56 return _getSource(uri); | |
57 } | |
58 | |
59 Source _getSource(Uri uri) { | |
60 var src = _sources[uri]; | |
61 if (src == null) { | |
62 if (reportMissing) print('warning: missing mock for $uri.'); | |
63 _sources[uri] = | |
64 src = new _MockSdkSource(uri, 'library dart.${uri.path};'); | |
65 } | |
66 return src; | |
67 } | |
68 | |
69 @override | |
70 Source fromFileUri(Uri uri) { | |
71 throw new UnsupportedError('MockDartSdk.fromFileUri'); | |
72 } | |
73 } | |
74 | |
75 class _MockSdkSource implements Source { | |
76 /// Absolute URI which this source can be imported from. | |
77 final Uri uri; | |
78 final String _contents; | |
79 | |
80 _MockSdkSource(this.uri, this._contents); | |
81 | |
82 bool exists() => true; | |
83 | |
84 int get hashCode => uri.hashCode; | |
85 | |
86 final int modificationStamp = 1; | |
87 | |
88 TimestampedData<String> get contents => | |
89 new TimestampedData(modificationStamp, _contents); | |
90 | |
91 String get encoding => "${uriKind.encoding}$uri"; | |
92 | |
93 Source get source => this; | |
94 | |
95 String get fullName => shortName; | |
96 | |
97 String get shortName => uri.path; | |
98 | |
99 UriKind get uriKind => UriKind.DART_URI; | |
100 | |
101 bool get isInSystemLibrary => true; | |
102 | |
103 Source resolveRelative(Uri relativeUri) => | |
104 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | |
105 | |
106 Uri resolveRelativeUri(Uri relativeUri) => | |
107 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | |
108 } | |
109 | |
110 /// Sample mock SDK sources. | |
111 final Map<String, String> mockSdkSources = { | |
112 // The list of types below is derived from: | |
113 // * types we use via our smoke queries, including HtmlElement and | |
114 // types from `_typeHandlers` (deserialize.dart) | |
115 // * types that are used internally by the resolver (see | |
116 // _initializeFrom in resolver.dart). | |
117 'dart:core': ''' | |
118 library dart.core; | |
119 | |
120 void print(Object o) {} | |
121 | |
122 class Object { | |
123 int get hashCode {} | |
124 Type get runtimeType {} | |
125 String toString(){} | |
126 bool ==(other){} | |
127 } | |
128 class Function {} | |
129 class StackTrace {} | |
130 class Symbol {} | |
131 class Type {} | |
132 | |
133 class String { | |
134 String operator +(String other) {} | |
135 } | |
136 class bool {} | |
137 class num { | |
138 num operator +(num other) {} | |
139 } | |
140 class int extends num { | |
141 bool operator<(num other) {} | |
142 int operator-() {} | |
143 } | |
144 class double extends num {} | |
145 class DateTime {} | |
146 class Null {} | |
147 | |
148 class Deprecated { | |
149 final String expires; | |
150 const Deprecated(this.expires); | |
151 } | |
152 const Object deprecated = const Deprecated("next release"); | |
153 class _Override { const _Override(); } | |
154 const Object override = const _Override(); | |
155 class _Proxy { const _Proxy(); } | |
156 const Object proxy = const _Proxy(); | |
157 | |
158 class Iterable<E> { | |
159 fold(initialValue, combine(previousValue, E element)) {} | |
160 Iterable map(f(E element)) {} | |
161 } | |
162 class List<E> implements Iterable<E> { | |
163 List([int length]); | |
164 List.filled(int length, E fill); | |
165 } | |
166 class Map<K, V> { | |
167 Iterable<K> get keys {} | |
168 } | |
169 ''', | |
170 'dart:async': ''' | |
171 class Future<T> { | |
172 Future(computation()) {} | |
173 Future.value(T t) {} | |
174 Future then(onValue(T value)) {} | |
175 static Future<List> wait(Iterable<Future> futures) {} | |
176 } | |
177 class Stream<T> {} | |
178 ''', | |
179 'dart:html': ''' | |
180 library dart.html; | |
181 class HtmlElement {} | |
182 ''', | |
183 'dart:math': ''' | |
184 library dart.math; | |
185 class Random { | |
186 bool nextBool() {} | |
187 } | |
188 num min(num x, num y) {} | |
189 num max(num x, num y) {} | |
190 ''', | |
191 }; | |
192 | |
193 /// An [AnalysisContextImpl] that only contains sources for a Dart SDK. | |
194 class _SdkAnalysisContext extends AnalysisContextImpl { | |
195 final DartSdk sdk; | |
196 | |
197 _SdkAnalysisContext(this.sdk); | |
198 | |
199 @override | |
200 AnalysisCache createCacheFromSourceFactory(SourceFactory factory) { | |
201 if (factory == null) { | |
202 return super.createCacheFromSourceFactory(factory); | |
203 } | |
204 return new AnalysisCache( | |
205 <CachePartition>[AnalysisEngine.instance.partitionManager.forSdk(sdk)]); | |
206 } | |
207 } | |
OLD | NEW |