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 library dev_compiler.src.checker.dart_sdk; | |
9 | |
10 import 'package:analyzer/src/generated/engine.dart'; | |
11 import 'package:analyzer/src/generated/sdk.dart'; | |
12 import 'package:analyzer/src/generated/source.dart'; | |
13 | |
14 /// Dart SDK which contains a mock implementation of the SDK libraries. May be | |
15 /// used to speed up execution when most of the core libraries is not needed. | |
16 class MockDartSdk implements DartSdk { | |
17 final Map<Uri, _MockSdkSource> _sources = {}; | |
18 final bool reportMissing; | |
19 final Map<String, SdkLibrary> _libs = {}; | |
20 final String sdkVersion = '0'; | |
21 List<String> get uris => _sources.keys.map((uri) => '$uri').toList(); | |
22 final AnalysisContext context = new SdkAnalysisContext(); | |
23 DartUriResolver _resolver; | |
24 DartUriResolver get resolver => _resolver; | |
25 | |
26 MockDartSdk(Map<String, String> sources, {this.reportMissing}) { | |
27 sources.forEach((uriString, contents) { | |
28 var uri = Uri.parse(uriString); | |
29 _sources[uri] = new _MockSdkSource(uri, contents); | |
30 _libs[uriString] = new SdkLibraryImpl(uri.path) | |
31 ..setDart2JsLibrary() | |
32 ..setVmLibrary(); | |
33 }); | |
34 _resolver = new DartUriResolver(this); | |
35 context.sourceFactory = new SourceFactory([_resolver]); | |
36 } | |
37 | |
38 List<SdkLibrary> get sdkLibraries => _libs.values.toList(); | |
39 SdkLibrary getSdkLibrary(String dartUri) => _libs[dartUri]; | |
40 Source mapDartUri(String dartUri) => _getSource(Uri.parse(dartUri)); | |
41 | |
42 Source fromEncoding(UriKind kind, Uri uri) { | |
43 if (kind != UriKind.DART_URI) { | |
44 throw new UnsupportedError('expected dart: uri kind, got $kind.'); | |
45 } | |
46 return _getSource(uri); | |
47 } | |
48 | |
49 Source _getSource(Uri uri) { | |
50 var src = _sources[uri]; | |
51 if (src == null) { | |
52 if (reportMissing) print('warning: missing mock for $uri.'); | |
53 _sources[uri] = | |
54 src = new _MockSdkSource(uri, 'library dart.${uri.path};'); | |
55 } | |
56 return src; | |
57 } | |
58 | |
59 @override | |
60 Source fromFileUri(Uri uri) { | |
61 throw new UnsupportedError('MockDartSdk.fromFileUri'); | |
62 } | |
63 } | |
64 | |
65 class _MockSdkSource implements Source { | |
66 /// Absolute URI which this source can be imported from. | |
67 final Uri uri; | |
68 final String _contents; | |
69 | |
70 _MockSdkSource(this.uri, this._contents); | |
71 | |
72 bool exists() => true; | |
73 | |
74 int get hashCode => uri.hashCode; | |
75 | |
76 final int modificationStamp = 1; | |
77 | |
78 TimestampedData<String> get contents => | |
79 new TimestampedData(modificationStamp, _contents); | |
80 | |
81 String get encoding => "${uriKind.encoding}$uri"; | |
82 | |
83 Source get source => this; | |
84 | |
85 String get fullName => shortName; | |
86 | |
87 String get shortName => uri.path; | |
88 | |
89 UriKind get uriKind => UriKind.DART_URI; | |
90 | |
91 bool get isInSystemLibrary => true; | |
92 | |
93 Source resolveRelative(Uri relativeUri) => | |
94 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | |
95 | |
96 Uri resolveRelativeUri(Uri relativeUri) => | |
97 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | |
98 } | |
99 | |
100 /// Sample mock SDK sources. | |
101 final Map<String, String> mockSdkSources = { | |
102 // The list of types below is derived from: | |
103 // * types we use via our smoke queries, including HtmlElement and | |
104 // types from `_typeHandlers` (deserialize.dart) | |
105 // * types that are used internally by the resolver (see | |
106 // _initializeFrom in resolver.dart). | |
107 'dart:core': ''' | |
108 library dart.core; | |
109 | |
110 void print(Object o) {} | |
111 | |
112 class Object { | |
113 int get hashCode {} | |
114 Type get runtimeType {} | |
115 String toString(){} | |
116 } | |
117 class Function {} | |
118 class StackTrace {} | |
119 class Symbol {} | |
120 class Type {} | |
121 | |
122 class String {} | |
123 class bool {} | |
124 class num { | |
125 num operator +(num other) {} | |
126 } | |
127 class int extends num { | |
128 int operator-() {} | |
129 } | |
130 class double extends num {} | |
131 class DateTime {} | |
132 class Null {} | |
133 | |
134 class Deprecated { | |
135 final String expires; | |
136 const Deprecated(this.expires); | |
137 } | |
138 const Object deprecated = const Deprecated("next release"); | |
139 class _Override { const _Override(); } | |
140 const Object override = const _Override(); | |
141 class _Proxy { const _Proxy(); } | |
142 const Object proxy = const _Proxy(); | |
143 | |
144 class Iterable<E> {} | |
145 class List<E> implements Iterable<E> { | |
146 List([int length]); | |
147 List.filled(int length, E fill); | |
148 } | |
149 class Map<K, V> { | |
150 Iterable<K> get keys {} | |
151 } | |
152 ''', | |
153 'dart:async': ''' | |
154 class Future<T> { | |
155 Future then(callback) {} | |
156 } | |
157 class Stream<T> {} | |
158 ''', | |
159 'dart:html': ''' | |
160 library dart.html; | |
161 class HtmlElement {} | |
162 ''', | |
163 }; | |
OLD | NEW |