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 library linter.src.sdk; |
| 6 |
| 7 import 'package:analyzer/file_system/file_system.dart' as resource; |
| 8 import 'package:analyzer/file_system/memory_file_system.dart' as resource; |
| 9 import 'package:analyzer/src/context/cache.dart' |
| 10 show AnalysisCache, CachePartition; |
| 11 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; |
| 12 import 'package:analyzer/src/generated/engine.dart' |
| 13 show AnalysisEngine, ChangeSet; |
| 14 import 'package:analyzer/src/generated/java_io.dart'; |
| 15 import 'package:analyzer/src/generated/sdk.dart' |
| 16 show DartSdk, LibraryMap, SdkLibrary; |
| 17 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; |
| 18 import 'package:analyzer/src/generated/source.dart' |
| 19 show DartUriResolver, Source, SourceFactory; |
| 20 |
| 21 /// Mock SDK for testing purposes. |
| 22 class MockSdk implements DartSdk, DirectoryBasedDartSdk { |
| 23 static const _MockSdkLibrary LIB_CORE = const _MockSdkLibrary( |
| 24 'dart:core', |
| 25 '/lib/core/core.dart', |
| 26 ''' |
| 27 library dart.core; |
| 28 |
| 29 import 'dart:async'; |
| 30 |
| 31 class Object { |
| 32 bool operator ==(other) => identical(this, other); |
| 33 String toString() => 'a string'; |
| 34 int get hashCode => 0; |
| 35 } |
| 36 |
| 37 class Function {} |
| 38 class StackTrace {} |
| 39 class Symbol {} |
| 40 class Type {} |
| 41 |
| 42 abstract class Comparable<T> { |
| 43 int compareTo(T other); |
| 44 } |
| 45 |
| 46 abstract class String implements Comparable<String> { |
| 47 external factory String.fromCharCodes(Iterable<int> charCodes, |
| 48 [int start = 0, int end]); |
| 49 bool get isEmpty => false; |
| 50 bool get isNotEmpty => false; |
| 51 int get length => 0; |
| 52 String toUpperCase(); |
| 53 List<int> get codeUnits; |
| 54 } |
| 55 |
| 56 class bool extends Object {} |
| 57 abstract class num implements Comparable<num> { |
| 58 bool operator <(num other); |
| 59 bool operator <=(num other); |
| 60 bool operator >(num other); |
| 61 bool operator >=(num other); |
| 62 num operator +(num other); |
| 63 num operator -(num other); |
| 64 num operator *(num other); |
| 65 num operator /(num other); |
| 66 int toInt(); |
| 67 num abs(); |
| 68 int round(); |
| 69 } |
| 70 abstract class int extends num { |
| 71 bool get isEven => false; |
| 72 int operator -(); |
| 73 external static int parse(String source, |
| 74 { int radix, |
| 75 int onError(String source) }); |
| 76 } |
| 77 class double extends num {} |
| 78 class DateTime extends Object {} |
| 79 class Null extends Object {} |
| 80 |
| 81 class Deprecated extends Object { |
| 82 final String expires; |
| 83 const Deprecated(this.expires); |
| 84 } |
| 85 const Object deprecated = const Deprecated("next release"); |
| 86 |
| 87 class Iterator<E> { |
| 88 bool moveNext(); |
| 89 E get current; |
| 90 } |
| 91 |
| 92 abstract class Iterable<E> { |
| 93 Iterator<E> get iterator; |
| 94 bool get isEmpty; |
| 95 } |
| 96 |
| 97 abstract class List<E> implements Iterable<E> { |
| 98 void add(E value); |
| 99 E operator [](int index); |
| 100 void operator []=(int index, E value); |
| 101 Iterator<E> get iterator => null; |
| 102 void clear(); |
| 103 } |
| 104 |
| 105 abstract class Map<K, V> extends Object { |
| 106 Iterable<K> get keys; |
| 107 } |
| 108 |
| 109 external bool identical(Object a, Object b); |
| 110 |
| 111 void print(Object object) {} |
| 112 |
| 113 class _Override { |
| 114 const _Override(); |
| 115 } |
| 116 const Object override = const _Override(); |
| 117 '''); |
| 118 |
| 119 static const _MockSdkLibrary LIB_ASYNC = const _MockSdkLibrary( |
| 120 'dart:async', |
| 121 '/lib/async/async.dart', |
| 122 ''' |
| 123 library dart.async; |
| 124 |
| 125 import 'dart:math'; |
| 126 |
| 127 part 'stream.dart'; |
| 128 |
| 129 class Future<T> { |
| 130 factory Future.delayed(Duration duration, [T computation()]) => null; |
| 131 factory Future.value([value]) => null; |
| 132 static Future wait(List<Future> futures) => null; |
| 133 } |
| 134 ''', |
| 135 const <_MockSdkFile>[ |
| 136 const _MockSdkFile( |
| 137 '/lib/async/stream.dart', |
| 138 r''' |
| 139 part of dart.async; |
| 140 class Stream<T> {} |
| 141 abstract class StreamTransformer<S, T> {} |
| 142 ''') |
| 143 ]); |
| 144 |
| 145 static const _MockSdkLibrary LIB_COLLECTION = const _MockSdkLibrary( |
| 146 'dart:collection', |
| 147 '/lib/collection/collection.dart', |
| 148 ''' |
| 149 library dart.collection; |
| 150 |
| 151 abstract class HashMap<K, V> implements Map<K, V> {} |
| 152 '''); |
| 153 |
| 154 static const _MockSdkLibrary LIB_CONVERT = const _MockSdkLibrary( |
| 155 'dart:convert', |
| 156 '/lib/convert/convert.dart', |
| 157 ''' |
| 158 library dart.convert; |
| 159 |
| 160 import 'dart:async'; |
| 161 |
| 162 abstract class Converter<S, T> implements StreamTransformer {} |
| 163 class JsonDecoder extends Converter<String, Object> {} |
| 164 '''); |
| 165 |
| 166 static const _MockSdkLibrary LIB_MATH = const _MockSdkLibrary( |
| 167 'dart:math', |
| 168 '/lib/math/math.dart', |
| 169 ''' |
| 170 library dart.math; |
| 171 const double E = 2.718281828459045; |
| 172 const double PI = 3.1415926535897932; |
| 173 const double LN10 = 2.302585092994046; |
| 174 num min(num a, num b) => 0; |
| 175 num max(num a, num b) => 0; |
| 176 external double cos(num x); |
| 177 external double sin(num x); |
| 178 external double sqrt(num x); |
| 179 class Random { |
| 180 bool nextBool() => true; |
| 181 double nextDouble() => 2.0; |
| 182 int nextInt() => 1; |
| 183 } |
| 184 '''); |
| 185 |
| 186 static const _MockSdkLibrary LIB_HTML = const _MockSdkLibrary( |
| 187 'dart:html', |
| 188 '/lib/html/dartium/html_dartium.dart', |
| 189 ''' |
| 190 library dart.html; |
| 191 class HtmlElement {} |
| 192 '''); |
| 193 |
| 194 static const List<SdkLibrary> LIBRARIES = const [ |
| 195 LIB_CORE, |
| 196 LIB_ASYNC, |
| 197 LIB_COLLECTION, |
| 198 LIB_CONVERT, |
| 199 LIB_MATH, |
| 200 LIB_HTML, |
| 201 ]; |
| 202 |
| 203 final resource.MemoryResourceProvider provider = |
| 204 new resource.MemoryResourceProvider(); |
| 205 |
| 206 /** |
| 207 * The [AnalysisContextImpl] which is used for all of the sources. |
| 208 */ |
| 209 AnalysisContextImpl _analysisContext; |
| 210 |
| 211 MockSdk() { |
| 212 LIBRARIES.forEach((_MockSdkLibrary library) { |
| 213 provider.newFile(library.path, library.content); |
| 214 library.parts.forEach((file) { |
| 215 provider.newFile(file.path, file.content); |
| 216 }); |
| 217 }); |
| 218 } |
| 219 |
| 220 @override |
| 221 AnalysisContextImpl get context { |
| 222 if (_analysisContext == null) { |
| 223 _analysisContext = new _SdkAnalysisContext(this); |
| 224 SourceFactory factory = new SourceFactory([new DartUriResolver(this)]); |
| 225 _analysisContext.sourceFactory = factory; |
| 226 ChangeSet changeSet = new ChangeSet(); |
| 227 for (String uri in uris) { |
| 228 Source source = factory.forUri(uri); |
| 229 changeSet.addedSource(source); |
| 230 } |
| 231 _analysisContext.applyChanges(changeSet); |
| 232 } |
| 233 return _analysisContext; |
| 234 } |
| 235 |
| 236 @override |
| 237 JavaFile get dart2JsExecutable => null; |
| 238 |
| 239 @override |
| 240 String get dartiumBinaryName => null; |
| 241 |
| 242 @override |
| 243 JavaFile get dartiumExecutable => null; |
| 244 |
| 245 @override |
| 246 JavaFile get dartiumWorkingDirectory => null; |
| 247 |
| 248 @override |
| 249 JavaFile get directory => null; |
| 250 |
| 251 @override |
| 252 JavaFile get docDirectory => null; |
| 253 |
| 254 @override |
| 255 bool get hasDocumentation => null; |
| 256 |
| 257 @override |
| 258 bool get isDartiumInstalled => null; |
| 259 |
| 260 @override |
| 261 JavaFile get libraryDirectory => null; |
| 262 |
| 263 @override |
| 264 JavaFile get pubExecutable => null; |
| 265 |
| 266 @override |
| 267 List<SdkLibrary> get sdkLibraries => LIBRARIES; |
| 268 |
| 269 @override |
| 270 String get sdkVersion => throw unimplemented; |
| 271 |
| 272 UnimplementedError get unimplemented => new UnimplementedError(); |
| 273 |
| 274 @override |
| 275 List<String> get uris { |
| 276 List<String> uris = <String>[]; |
| 277 for (SdkLibrary library in LIBRARIES) { |
| 278 uris.add(library.shortName); |
| 279 } |
| 280 return uris; |
| 281 } |
| 282 |
| 283 @override |
| 284 String get vmBinaryName => null; |
| 285 |
| 286 @override |
| 287 JavaFile get vmExecutable => null; |
| 288 |
| 289 @override |
| 290 Source fromFileUri(Uri uri) { |
| 291 String filePath = uri.path; |
| 292 String libPath = '/lib'; |
| 293 if (!filePath.startsWith("$libPath/")) { |
| 294 return null; |
| 295 } |
| 296 for (SdkLibrary library in LIBRARIES) { |
| 297 String libraryPath = library.path; |
| 298 if (filePath.replaceAll('\\', '/') == libraryPath) { |
| 299 try { |
| 300 resource.File file = provider.getResource(uri.path); |
| 301 Uri dartUri = Uri.parse(library.shortName); |
| 302 return file.createSource(dartUri); |
| 303 } catch (exception) { |
| 304 return null; |
| 305 } |
| 306 } |
| 307 if (filePath.startsWith("$libraryPath/")) { |
| 308 String pathInLibrary = filePath.substring(libraryPath.length + 1); |
| 309 String path = '${library.shortName}/${pathInLibrary}'; |
| 310 try { |
| 311 resource.File file = provider.getResource(uri.path); |
| 312 Uri dartUri = new Uri(scheme: 'dart', path: path); |
| 313 return file.createSource(dartUri); |
| 314 } catch (exception) { |
| 315 return null; |
| 316 } |
| 317 } |
| 318 } |
| 319 return null; |
| 320 } |
| 321 |
| 322 @override |
| 323 JavaFile getDartiumWorkingDirectory(JavaFile installDir) => null; |
| 324 |
| 325 @override |
| 326 JavaFile getDocFileFor(String libraryName) => null; |
| 327 |
| 328 @override |
| 329 SdkLibrary getSdkLibrary(String dartUri) { |
| 330 // getSdkLibrary() is only used to determine whether a library is internal |
| 331 // to the SDK. The mock SDK doesn't have any internals, so it's safe to |
| 332 // return null. |
| 333 return null; |
| 334 } |
| 335 |
| 336 @override |
| 337 LibraryMap initialLibraryMap(bool useDart2jsPaths) => null; |
| 338 |
| 339 @override |
| 340 Source mapDartUri(String dartUri) { |
| 341 const Map<String, String> uriToPath = const { |
| 342 "dart:core": "/lib/core/core.dart", |
| 343 "dart:html": "/lib/html/dartium/html_dartium.dart", |
| 344 "dart:async": "/lib/async/async.dart", |
| 345 "dart:async/stream.dart": "/lib/async/stream.dart", |
| 346 "dart:collection": "/lib/collection/collection.dart", |
| 347 "dart:convert": "/lib/convert/convert.dart", |
| 348 "dart:math": "/lib/math/math.dart" |
| 349 }; |
| 350 |
| 351 String path = uriToPath[dartUri]; |
| 352 if (path != null) { |
| 353 resource.File file = provider.getResource(path); |
| 354 Uri uri = new Uri(scheme: 'dart', path: dartUri.substring(5)); |
| 355 return file.createSource(uri); |
| 356 } |
| 357 |
| 358 // If we reach here then we tried to use a dartUri that's not in the |
| 359 // table above. |
| 360 return null; |
| 361 } |
| 362 } |
| 363 |
| 364 class _MockSdkFile { |
| 365 final String path; |
| 366 final String content; |
| 367 |
| 368 const _MockSdkFile(this.path, this.content); |
| 369 } |
| 370 |
| 371 class _MockSdkLibrary implements SdkLibrary { |
| 372 final String shortName; |
| 373 final String path; |
| 374 final String content; |
| 375 final List<_MockSdkFile> parts; |
| 376 |
| 377 const _MockSdkLibrary(this.shortName, this.path, this.content, |
| 378 [this.parts = const <_MockSdkFile>[]]); |
| 379 |
| 380 @override |
| 381 String get category => throw unimplemented; |
| 382 |
| 383 @override |
| 384 bool get isDart2JsLibrary => throw unimplemented; |
| 385 |
| 386 @override |
| 387 bool get isDocumented => throw unimplemented; |
| 388 |
| 389 @override |
| 390 bool get isImplementation => throw unimplemented; |
| 391 |
| 392 @override |
| 393 bool get isInternal => throw unimplemented; |
| 394 |
| 395 @override |
| 396 bool get isShared => throw unimplemented; |
| 397 |
| 398 @override |
| 399 bool get isVmLibrary => throw unimplemented; |
| 400 |
| 401 UnimplementedError get unimplemented => new UnimplementedError(); |
| 402 } |
| 403 |
| 404 /** |
| 405 * An [AnalysisContextImpl] that only contains sources for a Dart SDK. |
| 406 */ |
| 407 class _SdkAnalysisContext extends AnalysisContextImpl { |
| 408 final DartSdk sdk; |
| 409 |
| 410 _SdkAnalysisContext(this.sdk); |
| 411 |
| 412 @override |
| 413 AnalysisCache createCacheFromSourceFactory(SourceFactory factory) { |
| 414 if (factory == null) { |
| 415 return super.createCacheFromSourceFactory(factory); |
| 416 } |
| 417 return new AnalysisCache(<CachePartition>[ |
| 418 AnalysisEngine.instance.partitionManager_new.forSdk(sdk) |
| 419 ]); |
| 420 } |
| 421 } |
OLD | NEW |