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