Chromium Code Reviews| 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:package_resolver/package_resolver.dart'; |
| 6 import 'package:path/path.dart' as p; | 6 import 'package:path/path.dart' as p; |
| 7 import 'package:source_maps/source_maps.dart'; | 7 import 'package:source_maps/source_maps.dart'; |
| 8 import 'package:stack_trace/stack_trace.dart'; | 8 import 'package:stack_trace/stack_trace.dart'; |
| 9 | 9 |
| 10 /// Convert [stackTrace], a stack trace generated by dart2js-compiled | 10 /// Convert [stackTrace], a stack trace generated by dart2js-compiled |
| 11 /// JavaScript, to a native-looking stack trace using [sourceMap]. | 11 /// JavaScript, to a native-looking stack trace using [sourceMap]. |
| 12 /// | 12 /// |
| 13 /// [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 |
| 14 /// 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. |
| 15 /// | 15 /// |
| 16 /// If [packageResolver] is passed, it's used to reconstruct `package:` URIs for | 16 /// If [packageResolver] is passed, it's used to reconstruct `package:` URIs for |
| 17 /// stack frames that come from packages. | 17 /// stack frames that come from packages. |
| 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 /// | 22 /// |
| 23 /// [includeUnmappedFrames] indicates whether frames that do not match | |
| 24 /// [sourceMap] should be included in the mapped stack trace. | |
|
nweiz
2016/12/08 23:34:31
What happens to those frames if it's `false`?
Jacob
2016/12/09 02:18:28
if it is false they are not included.
| |
| 25 /// | |
| 23 /// [packageRoot] is deprecated and shouldn't be used in new code. This throws | 26 /// [packageRoot] is deprecated and shouldn't be used in new code. This throws |
| 24 /// an [ArgumentError] if [packageRoot] and [packageResolver] are both passed. | 27 /// an [ArgumentError] if [packageRoot] and [packageResolver] are both passed. |
| 25 StackTrace mapStackTrace(Mapping sourceMap, StackTrace stackTrace, | 28 StackTrace mapStackTrace(Mapping sourceMap, StackTrace stackTrace, |
| 26 {bool minified: false, SyncPackageResolver packageResolver, sdkRoot, | 29 {bool minified: false, |
| 27 @Deprecated("Use the packageResolver parameter instead.") packageRoot}) { | 30 SyncPackageResolver packageResolver, |
| 31 sdkRoot, | |
| 32 @Deprecated("Use the packageResolver parameter instead.") packageRoot, | |
| 33 bool includeUnmappedFrames: false}) { | |
| 28 if (packageRoot != null) { | 34 if (packageRoot != null) { |
| 29 if (packageResolver != null) { | 35 if (packageResolver != null) { |
| 30 throw new ArgumentError( | 36 throw new ArgumentError( |
| 31 "packageResolver and packageRoot may not both be passed."); | 37 "packageResolver and packageRoot may not both be passed."); |
| 32 } | 38 } |
| 33 | 39 |
| 34 packageResolver = new SyncPackageResolver.root(packageRoot); | 40 packageResolver = new SyncPackageResolver.root(packageRoot); |
| 35 } | 41 } |
| 36 | 42 |
| 37 if (stackTrace is Chain) { | 43 if (stackTrace is Chain) { |
| 38 return new Chain(stackTrace.traces.map((trace) { | 44 return new Chain(stackTrace.traces.map((trace) { |
| 39 return new Trace.from(mapStackTrace( | 45 return new Trace.from(mapStackTrace(sourceMap, trace, |
| 40 sourceMap, trace, | |
| 41 minified: minified, | 46 minified: minified, |
| 42 packageResolver: packageResolver, | 47 packageResolver: packageResolver, |
| 43 sdkRoot: sdkRoot)); | 48 sdkRoot: sdkRoot, |
| 49 includeUnmappedFrames: includeUnmappedFrames)); | |
| 44 })); | 50 })); |
| 45 } | 51 } |
| 46 | 52 |
| 47 if (sdkRoot != null && sdkRoot is! String && sdkRoot is! Uri) { | 53 if (sdkRoot != null && sdkRoot is! String && sdkRoot is! Uri) { |
| 48 throw new ArgumentError( | 54 throw new ArgumentError( |
| 49 'sdkRoot must be a String or a Uri, was "$sdkRoot".'); | 55 'sdkRoot must be a String or a Uri, was "$sdkRoot".'); |
| 50 } | 56 } |
| 51 | 57 |
| 52 var sdkLib = sdkRoot == null ? null : "$sdkRoot/lib"; | 58 var sdkLib = sdkRoot == null ? null : "$sdkRoot/lib"; |
| 53 | 59 |
| 54 var trace = new Trace.from(stackTrace); | 60 var trace = new Trace.from(stackTrace); |
| 55 return new Trace(trace.frames.map((frame) { | 61 return new Trace(trace.frames.map((frame) { |
| 56 // If there's no line information, there's no way to translate this frame. | 62 // If there's no line information, there's no way to translate this frame. |
| 57 // We could return it as-is, but these lines are usually not useful anyways. | 63 // We could return it as-is, but these lines are usually not useful anyways. |
| 58 if (frame.line == null) return null; | 64 if (frame.line == null) return null; |
| 59 | 65 |
| 60 // If there's no column, try using the first column of the line. | 66 // If there's no column, try using the first column of the line. |
| 61 var column = frame.column == null ? 0 : frame.column; | 67 var column = frame.column == null ? 0 : frame.column; |
| 68 var uri = frame.uri; | |
| 62 | 69 |
| 63 // Subtract 1 because stack traces use 1-indexed lines and columns and | 70 // Subtract 1 because stack traces use 1-indexed lines and columns and |
| 64 // source maps uses 0-indexed. | 71 // source maps uses 0-indexed. |
| 65 var span = sourceMap.spanFor(frame.line - 1, column - 1); | 72 var span = |
| 73 sourceMap.spanFor(frame.line - 1, column - 1, uri: uri?.toString()); | |
| 66 | 74 |
| 67 // If we can't find a source span, ignore the frame. It's probably something | 75 if (span == null) { |
| 68 // internal that the user doesn't care about. | 76 return includeUnmappedFrames ? frame : null; |
|
nweiz
2016/12/08 23:34:31
This comment should still exist, maybe reworded a
Jacob
2016/12/09 20:21:43
obsolete.
| |
| 69 if (span == null) return null; | 77 } |
| 70 | 78 |
| 71 var sourceUrl = span.sourceUrl.toString(); | 79 var sourceUrl = span.sourceUrl.toString(); |
| 72 if (sdkRoot != null && p.url.isWithin(sdkLib, sourceUrl)) { | 80 if (sdkRoot != null && p.url.isWithin(sdkLib, sourceUrl)) { |
| 73 sourceUrl = "dart:" + p.url.relative(sourceUrl, from: sdkLib); | 81 sourceUrl = "dart:" + p.url.relative(sourceUrl, from: sdkLib); |
| 74 } else if (packageResolver != null) { | 82 } else if (packageResolver != null) { |
| 75 if (packageResolver.packageRoot != null && | 83 if (packageResolver.packageRoot != null && |
| 76 p.url.isWithin(packageResolver.packageRoot.toString(), sourceUrl)) { | 84 p.url.isWithin(packageResolver.packageRoot.toString(), sourceUrl)) { |
| 77 sourceUrl = "package:" + p.url.relative(sourceUrl, | 85 sourceUrl = "package:" + |
| 78 from: packageResolver.packageRoot.toString()); | 86 p.url.relative(sourceUrl, |
| 87 from: packageResolver.packageRoot.toString()); | |
|
nweiz
2016/12/08 23:34:31
Please don't include unrelated formatting changes.
Jacob
2016/12/09 02:18:28
I'll send a followup cl that runs dartfm. would be
| |
| 79 } else if (packageResolver.packageConfigMap != null) { | 88 } else if (packageResolver.packageConfigMap != null) { |
| 80 for (var package in packageResolver.packageConfigMap.keys) { | 89 for (var package in packageResolver.packageConfigMap.keys) { |
| 81 var packageUrl = packageResolver.packageConfigMap[package].toString(); | 90 var packageUrl = packageResolver.packageConfigMap[package].toString(); |
| 82 if (!p.url.isWithin(packageUrl, sourceUrl)) continue; | 91 if (!p.url.isWithin(packageUrl, sourceUrl)) continue; |
| 83 | 92 |
| 84 sourceUrl = "package:$package/" + | 93 sourceUrl = |
| 85 p.url.relative(sourceUrl, from: packageUrl); | 94 "package:$package/" + p.url.relative(sourceUrl, from: packageUrl); |
| 86 break; | 95 break; |
| 87 } | 96 } |
| 88 } | 97 } |
| 89 } | 98 } |
| 90 | 99 |
| 91 return new Frame( | 100 return new Frame( |
| 92 Uri.parse(sourceUrl), | 101 Uri.parse(sourceUrl), |
| 93 span.start.line + 1, | 102 span.start.line + 1, |
| 94 span.start.column + 1, | 103 span.start.column + 1, |
| 95 // If the dart2js output is minified, there's no use trying to prettify | 104 // If the dart2js output is minified, there's no use trying to prettify |
| 96 // its member names. Use the span's identifier if available, otherwise | 105 // its member names. Use the span's identifier if available, otherwise |
| 97 // use the minified member name. | 106 // use the minified member name. |
| 98 minified | 107 minified |
| 99 ? (span.isIdentifier ? span.text : frame.member) | 108 ? (span.isIdentifier ? span.text : frame.member) |
| 100 : _prettifyMember(frame.member)); | 109 : _prettifyMember(frame.member)); |
| 101 }).where((frame) => frame != null)); | 110 }).where((frame) => frame != null)); |
| 102 } | 111 } |
| 103 | 112 |
| 104 /// Reformats a JS member name to make it look more Dart-like. | 113 /// Reformats a JS member name to make it look more Dart-like. |
| 105 String _prettifyMember(String member) { | 114 String _prettifyMember(String member) { |
| 106 return member | 115 return member |
| 107 // Get rid of the noise that Firefox sometimes adds. | 116 // Get rid of the noise that Firefox sometimes adds. |
| 108 .replaceAll(new RegExp(r"/?<$"), "") | 117 .replaceAll(new RegExp(r"/?<$"), "") |
| 109 // Get rid of arity indicators and named arguments. | 118 // Get rid of arity indicators and named arguments. |
| 110 .replaceAll(new RegExp(r"\$\d+(\$[a-zA-Z_0-9]+)*$"), "") | 119 .replaceAll(new RegExp(r"\$\d+(\$[a-zA-Z_0-9]+)*$"), "") |
| 111 // Convert closures to <fn>. | 120 // Convert closures to <fn>. |
| 112 .replaceAllMapped(new RegExp(r"(_+)closure\d*\.call$"), | 121 .replaceAllMapped( |
| 122 new RegExp(r"(_+)closure\d*\.call$"), | |
| 113 // The number of underscores before "closure" indicates how nested it | 123 // The number of underscores before "closure" indicates how nested it |
| 114 // is. | 124 // is. |
| 115 (match) => ".<fn>" * match[1].length) | 125 (match) => ".<fn>" * match[1].length) |
| 116 // Get rid of explicitly-generated calls. | 126 // Get rid of explicitly-generated calls. |
| 117 .replaceAll(new RegExp(r"\.call$"), "") | 127 .replaceAll(new RegExp(r"\.call$"), "") |
| 118 // Get rid of the top-level method prefix. | 128 // Get rid of the top-level method prefix. |
| 119 .replaceAll(new RegExp(r"^dart\."), "") | 129 .replaceAll(new RegExp(r"^dart\."), "") |
| 120 // Get rid of library namespaces. | 130 // Get rid of library namespaces. |
| 121 .replaceAll(new RegExp(r"[a-zA-Z_0-9]+\$"), "") | 131 .replaceAll(new RegExp(r"[a-zA-Z_0-9]+\$"), "") |
| 122 // Get rid of the static method prefix. The class name also exists in the | 132 // Get rid of the static method prefix. The class name also exists in the |
| 123 // invocation, so we're not getting rid of any information. | 133 // invocation, so we're not getting rid of any information. |
| 124 .replaceAll(new RegExp(r"^[a-zA-Z_0-9]+.(static|dart)."), "") | 134 .replaceAll(new RegExp(r"^[a-zA-Z_0-9]+.(static|dart)."), "") |
| 125 // Convert underscores after identifiers to dots. This runs the risk of | 135 // Convert underscores after identifiers to dots. This runs the risk of |
| 126 // incorrectly converting members that contain underscores, but those are | 136 // incorrectly converting members that contain underscores, but those are |
| 127 // contrary to the style guide anyway. | 137 // contrary to the style guide anyway. |
| 128 .replaceAllMapped(new RegExp(r"([a-zA-Z0-9]+)_"), | 138 .replaceAllMapped( |
| 129 (match) => match[1] + "."); | 139 new RegExp(r"([a-zA-Z0-9]+)_"), (match) => match[1] + "."); |
| 130 } | 140 } |
| OLD | NEW |