| 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 import 'package:package_resolver/package_resolver.dart'; | 
| 5 import 'package:path/path.dart' as p; | 6 import 'package:path/path.dart' as p; | 
| 6 import 'package:source_maps/source_maps.dart'; | 7 import 'package:source_maps/source_maps.dart'; | 
| 7 import 'package:stack_trace/stack_trace.dart'; | 8 import 'package:stack_trace/stack_trace.dart'; | 
| 8 | 9 | 
| 9 /// Convert [stackTrace], a stack trace generated by dart2js-compiled | 10 /// Convert [stackTrace], a stack trace generated by dart2js-compiled | 
| 10 /// JavaScript, to a native-looking stack trace using [sourceMap]. | 11 /// JavaScript, to a native-looking stack trace using [sourceMap]. | 
| 11 /// | 12 /// | 
| 12 /// [minified] indicates whether or not the dart2js code was minified. If it | 13 /// [minified] indicates whether or not the dart2js code was minified. If it | 
| 13 /// hasn't, this tries to clean up the stack frame member names. | 14 /// hasn't, this tries to clean up the stack frame member names. | 
| 14 /// | 15 /// | 
| 15 /// [packageRoot] is the URI (usually a `file:` URI) for the package root that | 16 /// If [packageResolver] is passed, it's used to reconstruct `package:` URIs for | 
| 16 /// was used by dart2js. It can be a [String] or a [Uri]. If it's passed, stack | 17 /// stack frames that come from packages. | 
| 17 /// frames from packages will use `package:` URLs. |  | 
| 18 /// | 18 /// | 
| 19 /// [sdkRoot] is the URI (usually a `file:` URI) for the SDK containing dart2js. | 19 /// [sdkRoot] is the URI (usually a `file:` URI) for the SDK containing dart2js. | 
| 20 /// It can be a [String] or a [Uri]. If it's passed, stack frames from the SDK | 20 /// It can be a [String] or a [Uri]. If it's passed, stack frames from the SDK | 
| 21 /// will have `dart:` URLs. | 21 /// will have `dart:` URLs. | 
|  | 22 /// | 
|  | 23 /// [packageRoot] is deprecated and shouldn't be used in new code. This throws | 
|  | 24 /// an [ArgumentError] if [packageRoot] and [packageResolver] are both passed. | 
| 22 StackTrace mapStackTrace(Mapping sourceMap, StackTrace stackTrace, | 25 StackTrace mapStackTrace(Mapping sourceMap, StackTrace stackTrace, | 
| 23     {bool minified: false, packageRoot, sdkRoot}) { | 26     {bool minified: false, SyncPackageResolver packageResolver, sdkRoot, | 
|  | 27     @Deprecated("Use the packageResolver parameter instead.") packageRoot}) { | 
|  | 28   if (packageRoot != null) { | 
|  | 29     if (packageResolver != null) { | 
|  | 30       throw new ArgumentError( | 
|  | 31           "packageResolver and packageRoot may not both be passed."); | 
|  | 32     } | 
|  | 33 | 
|  | 34     packageResolver = new SyncPackageResolver.root(packageRoot); | 
|  | 35   } | 
|  | 36 | 
| 24   if (stackTrace is Chain) { | 37   if (stackTrace is Chain) { | 
| 25     return new Chain(stackTrace.traces.map((trace) { | 38     return new Chain(stackTrace.traces.map((trace) { | 
| 26       return new Trace.from(mapStackTrace(sourceMap, trace, | 39       return new Trace.from(mapStackTrace( | 
| 27           minified: minified, packageRoot: packageRoot, sdkRoot: sdkRoot)); | 40           sourceMap, trace, | 
|  | 41           minified: minified, | 
|  | 42           packageResolver: packageResolver, | 
|  | 43           sdkRoot: sdkRoot)); | 
| 28     })); | 44     })); | 
| 29   } | 45   } | 
| 30 | 46 | 
| 31   if (packageRoot != null && packageRoot is! String && packageRoot is! Uri) { |  | 
| 32     throw new ArgumentError( |  | 
| 33         'packageRoot must be a String or a Uri, was "$packageRoot".'); |  | 
| 34   } |  | 
| 35 |  | 
| 36   if (sdkRoot != null && sdkRoot is! String && sdkRoot is! Uri) { | 47   if (sdkRoot != null && sdkRoot is! String && sdkRoot is! Uri) { | 
| 37     throw new ArgumentError( | 48     throw new ArgumentError( | 
| 38         'sdkRoot must be a String or a Uri, was "$sdkRoot".'); | 49         'sdkRoot must be a String or a Uri, was "$sdkRoot".'); | 
| 39   } | 50   } | 
| 40 | 51 | 
| 41   packageRoot = packageRoot == null ? null : packageRoot.toString(); |  | 
| 42   var sdkLib = sdkRoot == null ? null : "$sdkRoot/lib"; | 52   var sdkLib = sdkRoot == null ? null : "$sdkRoot/lib"; | 
| 43 | 53 | 
| 44   var trace = new Trace.from(stackTrace); | 54   var trace = new Trace.from(stackTrace); | 
| 45   return new Trace(trace.frames.map((frame) { | 55   return new Trace(trace.frames.map((frame) { | 
| 46     // If there's no line information, there's no way to translate this frame. | 56     // If there's no line information, there's no way to translate this frame. | 
| 47     // We could return it as-is, but these lines are usually not useful anyways. | 57     // We could return it as-is, but these lines are usually not useful anyways. | 
| 48     if (frame.line == null) return null; | 58     if (frame.line == null) return null; | 
| 49 | 59 | 
| 50     // If there's no column, try using the first column of the line. | 60     // If there's no column, try using the first column of the line. | 
| 51     var column = frame.column == null ? 0 : frame.column; | 61     var column = frame.column == null ? 0 : frame.column; | 
| 52 | 62 | 
| 53     // Subtract 1 because stack traces use 1-indexed lines and columns and | 63     // Subtract 1 because stack traces use 1-indexed lines and columns and | 
| 54     // source maps uses 0-indexed. | 64     // source maps uses 0-indexed. | 
| 55     var span = sourceMap.spanFor(frame.line - 1, column - 1); | 65     var span = sourceMap.spanFor(frame.line - 1, column - 1); | 
| 56 | 66 | 
| 57     // If we can't find a source span, ignore the frame. It's probably something | 67     // If we can't find a source span, ignore the frame. It's probably something | 
| 58     // internal that the user doesn't care about. | 68     // internal that the user doesn't care about. | 
| 59     if (span == null) return null; | 69     if (span == null) return null; | 
| 60 | 70 | 
| 61     var sourceUrl = span.sourceUrl.toString(); | 71     var sourceUrl = span.sourceUrl.toString(); | 
| 62     if (sdkRoot != null && p.url.isWithin(sdkLib, sourceUrl)) { | 72     if (sdkRoot != null && p.url.isWithin(sdkLib, sourceUrl)) { | 
| 63       sourceUrl = "dart:" + p.url.relative(sourceUrl, from: sdkLib); | 73       sourceUrl = "dart:" + p.url.relative(sourceUrl, from: sdkLib); | 
| 64     } else if (packageRoot != null && p.url.isWithin(packageRoot, sourceUrl)) { | 74     } else if (packageResolver != null) { | 
| 65       sourceUrl = "package:" + | 75       if (packageResolver.packageRoot != null && | 
| 66           p.url.relative(sourceUrl, from: packageRoot); | 76           p.url.isWithin(packageResolver.packageRoot.toString(), sourceUrl)) { | 
|  | 77         sourceUrl = "package:" + p.url.relative(sourceUrl, | 
|  | 78             from: packageResolver.packageRoot.toString()); | 
|  | 79       } else { | 
|  | 80         for (var package in packageResolver.packageConfigMap.keys) { | 
|  | 81           var packageUrl = packageResolver.packageConfigMap[package].toString(); | 
|  | 82           if (!p.url.isWithin(packageUrl, sourceUrl)) continue; | 
|  | 83 | 
|  | 84           sourceUrl = "package:$package/" + | 
|  | 85               p.url.relative(sourceUrl, from: packageUrl); | 
|  | 86           break; | 
|  | 87         } | 
|  | 88       } | 
| 67     } | 89     } | 
| 68 | 90 | 
| 69     return new Frame( | 91     return new Frame( | 
| 70         Uri.parse(sourceUrl), | 92         Uri.parse(sourceUrl), | 
| 71         span.start.line + 1, | 93         span.start.line + 1, | 
| 72         span.start.column + 1, | 94         span.start.column + 1, | 
| 73         // If the dart2js output is minified, there's no use trying to prettify | 95         // If the dart2js output is minified, there's no use trying to prettify | 
| 74         // its member names. Use the span's identifier if available, otherwise | 96         // its member names. Use the span's identifier if available, otherwise | 
| 75         // use the minified member name. | 97         // use the minified member name. | 
| 76         minified | 98         minified | 
| (...skipping 22 matching lines...) Expand all  Loading... | 
| 99       .replaceAll(new RegExp(r"[a-zA-Z_0-9]+\$"), "") | 121       .replaceAll(new RegExp(r"[a-zA-Z_0-9]+\$"), "") | 
| 100       // Get rid of the static method prefix. The class name also exists in the | 122       // Get rid of the static method prefix. The class name also exists in the | 
| 101       // invocation, so we're not getting rid of any information. | 123       // invocation, so we're not getting rid of any information. | 
| 102       .replaceAll(new RegExp(r"^[a-zA-Z_0-9]+.(static|dart)."), "") | 124       .replaceAll(new RegExp(r"^[a-zA-Z_0-9]+.(static|dart)."), "") | 
| 103       // Convert underscores after identifiers to dots. This runs the risk of | 125       // Convert underscores after identifiers to dots. This runs the risk of | 
| 104       // incorrectly converting members that contain underscores, but those are | 126       // incorrectly converting members that contain underscores, but those are | 
| 105       // contrary to the style guide anyway. | 127       // contrary to the style guide anyway. | 
| 106       .replaceAllMapped(new RegExp(r"([a-zA-Z0-9]+)_"), | 128       .replaceAllMapped(new RegExp(r"([a-zA-Z0-9]+)_"), | 
| 107           (match) => match[1] + "."); | 129           (match) => match[1] + "."); | 
| 108 } | 130 } | 
| OLD | NEW | 
|---|