OLD | NEW |
---|---|
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:collection' show HashSet, Queue; | 5 import 'dart:collection' show HashSet, Queue; |
6 import 'dart:convert' show BASE64, JSON, UTF8; | 6 import 'dart:convert' show BASE64, JSON, UTF8; |
7 import 'dart:io' show File; | 7 import 'dart:io' show File; |
8 import 'package:analyzer/dart/element/element.dart' show LibraryElement; | 8 import 'package:analyzer/dart/element/element.dart' show LibraryElement; |
9 import 'package:analyzer/analyzer.dart' | 9 import 'package:analyzer/analyzer.dart' |
10 show AnalysisError, CompilationUnit, ErrorSeverity; | 10 show AnalysisError, CompilationUnit, ErrorSeverity; |
11 import 'package:analyzer/file_system/file_system.dart' show ResourceProvider; | 11 import 'package:analyzer/file_system/file_system.dart' show ResourceProvider; |
12 import 'package:analyzer/src/generated/engine.dart' | 12 import 'package:analyzer/src/generated/engine.dart' |
13 show AnalysisContext, AnalysisEngine; | 13 show AnalysisContext, AnalysisEngine; |
14 import 'package:analyzer/src/generated/source.dart' show DartUriResolver; | 14 import 'package:analyzer/src/generated/source.dart' show DartUriResolver; |
15 import 'package:analyzer/src/generated/source_io.dart' | 15 import 'package:analyzer/src/generated/source_io.dart' |
16 show Source, SourceKind, UriResolver; | 16 show Source, SourceKind, UriResolver; |
17 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 17 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
18 show InSummarySource, InputPackagesResultProvider, SummaryDataStore; | 18 show InSummarySource, InputPackagesResultProvider, SummaryDataStore; |
19 import 'package:analyzer/src/error/codes.dart' show StaticTypeWarningCode; | |
19 import 'package:args/args.dart' show ArgParser, ArgResults; | 20 import 'package:args/args.dart' show ArgParser, ArgResults; |
20 import 'package:args/src/usage_exception.dart' show UsageException; | 21 import 'package:args/src/usage_exception.dart' show UsageException; |
21 import 'package:func/func.dart' show Func1; | 22 import 'package:func/func.dart' show Func1; |
22 import 'package:path/path.dart' as path; | 23 import 'package:path/path.dart' as path; |
23 import 'package:source_maps/source_maps.dart'; | 24 import 'package:source_maps/source_maps.dart'; |
24 | 25 |
25 import '../analyzer/context.dart' | 26 import '../analyzer/context.dart' |
26 show | 27 show |
27 AnalyzerOptions, | 28 AnalyzerOptions, |
28 createAnalysisContext, | 29 createAnalysisContext, |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 trees.add(tree); | 153 trees.add(tree); |
153 errors.addAll(context.computeErrors(library.source)); | 154 errors.addAll(context.computeErrors(library.source)); |
154 | 155 |
155 for (var part in library.parts) { | 156 for (var part in library.parts) { |
156 trees.add(context.resolveCompilationUnit(part.source, library)); | 157 trees.add(context.resolveCompilationUnit(part.source, library)); |
157 errors.addAll(context.computeErrors(part.source)); | 158 errors.addAll(context.computeErrors(part.source)); |
158 } | 159 } |
159 } | 160 } |
160 | 161 |
161 sortErrors(context, errors); | 162 sortErrors(context, errors); |
163 | |
162 var messages = <String>[]; | 164 var messages = <String>[]; |
163 for (var e in errors) { | 165 for (var e in errors) { |
164 var m = formatError(context, e); | 166 var m = formatError(context, e); |
165 if (m != null) messages.add(m); | 167 if (m != null) messages.add(m); |
166 } | 168 } |
167 | 169 |
168 if (!options.unsafeForceCompile && | 170 if (!options.unsafeForceCompile && |
169 errors.any((e) => errorSeverity(context, e) == ErrorSeverity.ERROR)) { | 171 errors.any(options.replCompile |
172 ? (e) { | |
Jennifer Messerly
2016/10/18 18:46:01
style: could we pull this out into its own functio
| |
173 // These errors are not fatal in the REPL compile mode as we | |
174 // allow access to private members across library boundaries | |
175 // and those accesses will show up as undefined members unless | |
176 // additional analyzer changes are made to support them. | |
177 // TODO(jacobr): consider checking that the identifier name | |
178 // referenced by the error is private. | |
179 return errorSeverity(context, e) == ErrorSeverity.ERROR && | |
180 e.errorCode != StaticTypeWarningCode.UNDEFINED_GETTER && | |
181 e.errorCode != StaticTypeWarningCode.UNDEFINED_SETTER && | |
182 e.errorCode != StaticTypeWarningCode.UNDEFINED_METHOD; | |
183 } | |
184 : (e) => errorSeverity(context, e) == ErrorSeverity.ERROR)) { | |
170 return new JSModuleFile.invalid(unit.name, messages, options); | 185 return new JSModuleFile.invalid(unit.name, messages, options); |
171 } | 186 } |
172 | |
173 var codeGenerator = | 187 var codeGenerator = |
174 new CodeGenerator(context, summaryData, options, _extensionTypes); | 188 new CodeGenerator(context, summaryData, options, _extensionTypes); |
175 return codeGenerator.compile(unit, trees, messages); | 189 return codeGenerator.compile(unit, trees, messages); |
176 } | 190 } |
177 } | 191 } |
178 | 192 |
179 class CompilerOptions { | 193 class CompilerOptions { |
180 /// Whether to emit the source mapping file. | 194 /// Whether to emit the source mapping file. |
181 /// | 195 /// |
182 /// This supports debugging the original source code instead of the generated | 196 /// This supports debugging the original source code instead of the generated |
(...skipping 14 matching lines...) Expand all Loading... | |
197 | 211 |
198 /// The file extension for summaries. | 212 /// The file extension for summaries. |
199 final String summaryExtension; | 213 final String summaryExtension; |
200 | 214 |
201 /// Whether to preserve metdata only accessible via mirrors | 215 /// Whether to preserve metdata only accessible via mirrors |
202 final bool emitMetadata; | 216 final bool emitMetadata; |
203 | 217 |
204 /// Whether to force compilation of code with static errors. | 218 /// Whether to force compilation of code with static errors. |
205 final bool unsafeForceCompile; | 219 final bool unsafeForceCompile; |
206 | 220 |
221 /// Whether to compile code in a more permissive REPL mode allowing access | |
222 /// to private members across library boundaries. | |
223 final bool replCompile; | |
224 | |
207 /// Whether to emit Closure Compiler-friendly code. | 225 /// Whether to emit Closure Compiler-friendly code. |
208 final bool closure; | 226 final bool closure; |
209 | 227 |
210 /// Hoist the types at instance creation sites | 228 /// Hoist the types at instance creation sites |
211 final bool hoistInstanceCreation; | 229 final bool hoistInstanceCreation; |
212 | 230 |
213 /// Hoist types from class signatures | 231 /// Hoist types from class signatures |
214 final bool hoistSignatureTypes; | 232 final bool hoistSignatureTypes; |
215 | 233 |
216 /// Name types in type tests | 234 /// Name types in type tests |
(...skipping 24 matching lines...) Expand all Loading... | |
241 /// source maps. | 259 /// source maps. |
242 final Map<String, String> bazelMapping; | 260 final Map<String, String> bazelMapping; |
243 | 261 |
244 const CompilerOptions( | 262 const CompilerOptions( |
245 {this.sourceMap: true, | 263 {this.sourceMap: true, |
246 this.sourceMapComment: true, | 264 this.sourceMapComment: true, |
247 this.inlineSourceMap: false, | 265 this.inlineSourceMap: false, |
248 this.summarizeApi: true, | 266 this.summarizeApi: true, |
249 this.summaryExtension: 'sum', | 267 this.summaryExtension: 'sum', |
250 this.unsafeForceCompile: false, | 268 this.unsafeForceCompile: false, |
269 this.replCompile: false, | |
251 this.emitMetadata: false, | 270 this.emitMetadata: false, |
252 this.closure: false, | 271 this.closure: false, |
253 this.destructureNamedParams: false, | 272 this.destructureNamedParams: false, |
254 this.hoistInstanceCreation: true, | 273 this.hoistInstanceCreation: true, |
255 this.hoistSignatureTypes: false, | 274 this.hoistSignatureTypes: false, |
256 this.nameTypeTests: true, | 275 this.nameTypeTests: true, |
257 this.hoistTypeTests: true, | 276 this.hoistTypeTests: true, |
258 this.useAngular2Whitelist: false, | 277 this.useAngular2Whitelist: false, |
259 this.bazelMapping: const {}}); | 278 this.bazelMapping: const {}}); |
260 | 279 |
261 CompilerOptions.fromArguments(ArgResults args) | 280 CompilerOptions.fromArguments(ArgResults args) |
262 : sourceMap = args['source-map'], | 281 : sourceMap = args['source-map'], |
263 sourceMapComment = args['source-map-comment'], | 282 sourceMapComment = args['source-map-comment'], |
264 inlineSourceMap = args['inline-source-map'], | 283 inlineSourceMap = args['inline-source-map'], |
265 summarizeApi = args['summarize'], | 284 summarizeApi = args['summarize'], |
266 summaryExtension = args['summary-extension'], | 285 summaryExtension = args['summary-extension'], |
267 unsafeForceCompile = args['unsafe-force-compile'], | 286 unsafeForceCompile = args['unsafe-force-compile'], |
287 replCompile = args['repl-compile'], | |
268 emitMetadata = args['emit-metadata'], | 288 emitMetadata = args['emit-metadata'], |
269 closure = args['closure-experimental'], | 289 closure = args['closure-experimental'], |
270 destructureNamedParams = args['destructure-named-params'], | 290 destructureNamedParams = args['destructure-named-params'], |
271 hoistInstanceCreation = args['hoist-instance-creation'], | 291 hoistInstanceCreation = args['hoist-instance-creation'], |
272 hoistSignatureTypes = args['hoist-signature-types'], | 292 hoistSignatureTypes = args['hoist-signature-types'], |
273 nameTypeTests = args['name-type-tests'], | 293 nameTypeTests = args['name-type-tests'], |
274 hoistTypeTests = args['hoist-type-tests'], | 294 hoistTypeTests = args['hoist-type-tests'], |
275 useAngular2Whitelist = args['unsafe-angular2-whitelist'], | 295 useAngular2Whitelist = args['unsafe-angular2-whitelist'], |
276 bazelMapping = _parseBazelMappings(args['bazel-mapping']); | 296 bazelMapping = _parseBazelMappings(args['bazel-mapping']); |
277 | 297 |
(...skipping 18 matching lines...) Expand all Loading... | |
296 ..addFlag('closure-experimental', | 316 ..addFlag('closure-experimental', |
297 help: 'emit Closure Compiler-friendly code (experimental)', | 317 help: 'emit Closure Compiler-friendly code (experimental)', |
298 defaultsTo: false) | 318 defaultsTo: false) |
299 ..addFlag('destructure-named-params', | 319 ..addFlag('destructure-named-params', |
300 help: 'Destructure named parameters', defaultsTo: false, hide: true) | 320 help: 'Destructure named parameters', defaultsTo: false, hide: true) |
301 ..addFlag('unsafe-force-compile', | 321 ..addFlag('unsafe-force-compile', |
302 help: 'Compile code even if it has errors. ಠ_ಠ\n' | 322 help: 'Compile code even if it has errors. ಠ_ಠ\n' |
303 'This has undefined behavior!', | 323 'This has undefined behavior!', |
304 defaultsTo: false, | 324 defaultsTo: false, |
305 hide: true) | 325 hide: true) |
326 ..addFlag('repl-compile', | |
327 help: 'Compile code more permissively when in REPL mode allowing ' | |
328 'access to private members across library boundaries.', | |
329 defaultsTo: false, | |
330 hide: true) | |
306 ..addFlag('hoist-instance-creation', | 331 ..addFlag('hoist-instance-creation', |
307 help: 'Hoist the class type from generic instance creations', | 332 help: 'Hoist the class type from generic instance creations', |
308 defaultsTo: true, | 333 defaultsTo: true, |
309 hide: true) | 334 hide: true) |
310 ..addFlag('hoist-signature-types', | 335 ..addFlag('hoist-signature-types', |
311 help: 'Hoist types from class signatures', | 336 help: 'Hoist types from class signatures', |
312 defaultsTo: false, | 337 defaultsTo: false, |
313 hide: true) | 338 hide: true) |
314 ..addFlag('name-type-tests', | 339 ..addFlag('name-type-tests', |
315 help: 'Name types used in type tests', defaultsTo: true, hide: true) | 340 help: 'Name types used in type tests', defaultsTo: true, hide: true) |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
505 // Fall back to a relative path. | 530 // Fall back to a relative path. |
506 return path.toUri(path.relative(path.fromUri(uri), from: dir)).toString(); | 531 return path.toUri(path.relative(path.fromUri(uri), from: dir)).toString(); |
507 } | 532 } |
508 | 533 |
509 for (int i = 0; i < list.length; i++) { | 534 for (int i = 0; i < list.length; i++) { |
510 list[i] = transformUri(list[i]); | 535 list[i] = transformUri(list[i]); |
511 } | 536 } |
512 map['file'] = transformUri(map['file']); | 537 map['file'] = transformUri(map['file']); |
513 return map; | 538 return map; |
514 } | 539 } |
OLD | NEW |