OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 library unittest.util.dart; | 5 library unittest.util.dart; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 | 10 |
11 import 'package:analyzer/analyzer.dart'; | 11 import 'package:analyzer/analyzer.dart'; |
12 import 'package:path/path.dart' as p; | 12 import 'package:path/path.dart' as p; |
| 13 import 'package:source_span/source_span.dart'; |
13 | 14 |
| 15 import 'string_literal_iterator.dart'; |
14 import 'io.dart'; | 16 import 'io.dart'; |
15 import 'isolate_wrapper.dart'; | 17 import 'isolate_wrapper.dart'; |
16 import 'remote_exception.dart'; | 18 import 'remote_exception.dart'; |
17 | 19 |
18 /// Runs [code] in an isolate. | 20 /// Runs [code] in an isolate. |
19 /// | 21 /// |
20 /// [code] should be the contents of a Dart entrypoint. It may contain imports; | 22 /// [code] should be the contents of a Dart entrypoint. It may contain imports; |
21 /// they will be resolved in the same context as the host isolate. [message] is | 23 /// they will be resolved in the same context as the host isolate. [message] is |
22 /// passed to the [main] method of the code being run; the caller is responsible | 24 /// passed to the [main] method of the code being run; the caller is responsible |
23 /// for using this to establish communication with the isolate. | 25 /// for using this to establish communication with the isolate. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 Isolate.spawnUri(Uri.parse(message['uri']), [], message['message'], | 66 Isolate.spawnUri(Uri.parse(message['uri']), [], message['message'], |
65 packageRoot: packageRoot) | 67 packageRoot: packageRoot) |
66 .then((_) => replyTo.send({'type': 'success'})) | 68 .then((_) => replyTo.send({'type': 'success'})) |
67 .catchError((error, stackTrace) { | 69 .catchError((error, stackTrace) { |
68 replyTo.send({ | 70 replyTo.send({ |
69 'type': 'error', | 71 'type': 'error', |
70 'error': RemoteException.serialize(error, stackTrace) | 72 'error': RemoteException.serialize(error, stackTrace) |
71 }); | 73 }); |
72 }); | 74 }); |
73 } | 75 } |
| 76 |
| 77 // TODO(nweiz): Move this into the analyzer once it starts using SourceSpan |
| 78 // (issue 22977). |
| 79 /// Takes a span whose source is the value of a string that has been parsed from |
| 80 /// a Dart file and returns the corresponding span from within that Dart file. |
| 81 /// |
| 82 /// For example, suppose a Dart file contains `@Eval("1 + a")`. The |
| 83 /// [StringLiteral] `"1 + a"` is extracted; this is [context]. Its contents are |
| 84 /// then parsed, producing an error pointing to [span]: |
| 85 /// |
| 86 /// line 1, column 5: |
| 87 /// 1 + a |
| 88 /// ^ |
| 89 /// |
| 90 /// This span isn't very useful, since it only shows the location within the |
| 91 /// [StringLiteral]'s value. So it's passed to [contextualizeSpan] along with |
| 92 /// [context] and [file] (which contains the source of the entire Dart file), |
| 93 /// which then returns: |
| 94 /// |
| 95 /// line 4, column 12 of file.dart: |
| 96 /// @Eval("1 + a") |
| 97 /// ^ |
| 98 /// |
| 99 /// This properly handles multiline literals, adjacent literals, and literals |
| 100 /// containing escape sequences. It does not support interpolated literals. |
| 101 /// |
| 102 /// This will return `null` if [context] contains an invalid string or does not |
| 103 /// contain [span]. |
| 104 SourceSpan contextualizeSpan(SourceSpan span, StringLiteral context, |
| 105 SourceFile file) { |
| 106 var contextRunes = new StringLiteralIterator(context)..moveNext(); |
| 107 |
| 108 for (var i = 0; i < span.start.offset; i++) { |
| 109 if (!contextRunes.moveNext()) return null; |
| 110 } |
| 111 |
| 112 var start = contextRunes.offset; |
| 113 for (var spanRune in span.text.runes) { |
| 114 if (spanRune != contextRunes.current) return null; |
| 115 contextRunes.moveNext(); |
| 116 } |
| 117 |
| 118 return file.span(start, contextRunes.offset); |
| 119 } |
OLD | NEW |