Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: lib/source_map_stack_trace.dart

Issue 1115783002: Initial release. (Closed) Base URL: git@github.com:dart-lang/source_map_stack_trace.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « codereview.settings ('k') | pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 library source_map_stack_trace;
6
7 import 'package:path/path.dart' as p;
8 import 'package:source_maps/source_maps.dart';
9 import 'package:stack_trace/stack_trace.dart';
10
11 /// Convert [stackTrace], a stack trace generated by dart2js-compiled
12 /// JavaScript, to a native-looking stack trace using [sourceMap].
13 ///
14 /// [minified] indicates whether or not the dart2js code was minified. If it
15 /// hasn't, this tries to clean up the stack frame member names.
16 ///
17 /// [packageRoot] is the URI (usually a `file:` URI) for the package root that
18 /// was used by dart2js. It can be a [String] or a [Uri]. If it's passed, stack
19 /// frames from packages will use `package:` URLs.
20 ///
21 /// [sdkRoot] is the URI (usually a `file:` URI) for the SDK containing dart2js.
22 /// It can be a [String] or a [Uri]. If it's passed, stack frames from the SDK
23 /// will have `dart:` URLs.
24 StackTrace mapStackTrace(Mapping sourceMap, StackTrace stackTrace,
25 {bool minified: false, packageRoot, sdkRoot}) {
26 if (stackTrace is Chain) {
27 return new Chain(stackTrace.traces.map((trace) {
28 return mapStackTrace(trace, sourceMap,
29 minified: minified, packageRoot: packageRoot, sdkRoot: sdkRoot);
30 }));
31 }
32
33 if (packageRoot != null && packageRoot is! String && packageRoot is! Uri) {
34 throw new ArgumentError(
35 'packageRoot must be a String or a Uri, was "$packageRoot".');
36 }
37
38 if (sdkRoot != null && sdkRoot is! String && sdkRoot is! Uri) {
39 throw new ArgumentError(
40 'sdkRoot must be a String or a Uri, was "$sdkRoot".');
41 }
42
43 packageRoot = packageRoot == null ? null : packageRoot.toString();
44 var sdkLib = sdkRoot == null ? null : "$sdkRoot/lib";
45
46 var trace = new Trace.from(stackTrace);
47 return new Trace(trace.frames.map((frame) {
48 // If there's no line information, there's no way to translate this frame.
49 // We could return it as-is, but these lines are usually not useful anyways.
50 if (frame.line == null) return null;
51
52 // If there's no column, try using the first column of the line.
53 var column = frame.column == null ? 0 : frame.column;
54 var span = sourceMap.spanFor(frame.line, column);
55
56 // If we can't find a source span, ignore the frame. It's probably something
57 // internal that the user doesn't care about.
58 if (span == null) return null;
59
60 var sourceUrl = span.sourceUrl.toString();
61 if (packageRoot != null && p.url.isWithin(packageRoot, sourceUrl)) {
62 sourceUrl = "package:" +
63 p.url.relative(sourceUrl, from: packageRoot);
64 } else if (sdkRoot != null && p.url.isWithin(sdkLib, sourceUrl)) {
65 sourceUrl = "dart:" + p.url.relative(sourceUrl, from: sdkLib);
66 }
67
68 return new Frame(
69 Uri.parse(sourceUrl),
70 span.start.line + 1,
71 span.start.column + 1,
72 // If the dart2js output is minified, there's no use trying to prettify
73 // its member names. Use the span's identifier if available, otherwise
74 // use the minified member name.
75 minified
76 ? (span.isIdentifier ? span.text : frame.member)
77 : _prettifyMember(frame.member));
78 }).where((frame) => frame != null));
79 }
80
81 /// Reformats a JS member name to make it look more Dart-like.
82 String _prettifyMember(String member) {
83 return member
84 // Get rid of the noise that Firefox sometimes adds.
85 .replaceAll(new RegExp(r"/?<$"), "")
86 // Get rid of arity indicators.
87 .replaceAll(new RegExp(r"\$\d+$"), "")
88 // Convert closures to <fn>.
89 .replaceAllMapped(new RegExp(r"(_+)closure\d*\.call$"),
90 // The number of underscores before "closure" indicates how nested it
91 // is.
92 (match) => ".<fn>" * match[1].length)
93 // Get rid of explicitly-generated calls.
94 .replaceAll(new RegExp(r"\.call$"), "")
95 // Get rid of the top-level method prefix.
96 .replaceAll(new RegExp(r"^dart\."), "")
97 // Get rid of library namespaces.
98 .replaceAll(new RegExp(r"[a-zA-Z_0-9]+\$"), "")
99 // Get rid of the static method prefix. The class name also exists in the
100 // invocation, so we're not getting rid of any information.
101 .replaceAll(new RegExp(r"^[a-zA-Z_0-9]+.static."), "")
102 // Convert underscores after identifiers to dots. This runs the risk of
103 // incorrectly converting members that contain underscores, but those are
104 // contrary to the style guide anyway.
105 .replaceAllMapped(new RegExp(r"([a-zA-Z0-9]+)_"),
106 (match) => match[1] + ".");
107 }
OLDNEW
« no previous file with comments | « codereview.settings ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698