| 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:analyzer/dart/ast/ast.dart'; | 5 import 'package:analyzer/dart/ast/ast.dart'; |
| 6 import 'package:analyzer/src/generated/source.dart' show LineInfo; | 6 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; |
| 7 import 'package:source_maps/source_maps.dart' hide Printer; | 7 import 'package:source_maps/source_maps.dart' hide Printer; |
| 8 import 'package:source_span/source_span.dart' show SourceLocation; | 8 import 'package:source_span/source_span.dart' show SourceLocation; |
| 9 | 9 |
| 10 import '../js_ast/js_ast.dart' as JS; | 10 import '../js_ast/js_ast.dart' as JS; |
| 11 | 11 |
| 12 class SourceMapPrintingContext extends JS.SimpleJavaScriptPrintingContext { | 12 class SourceMapPrintingContext extends JS.SimpleJavaScriptPrintingContext { |
| 13 /// Current line in the buffer; | 13 /// Current line in the buffer; |
| 14 int _line = 0; | 14 int _line = 0; |
| 15 | 15 |
| 16 /// Current column in the buffer. | 16 /// Current column in the buffer. |
| 17 int _column = 0; | 17 int _column = 0; |
| 18 | 18 |
| 19 /// The source_maps builder we write JavaScript code to. | 19 /// The source_maps builder we write JavaScript code to. |
| 20 final sourceMap = new SourceMapBuilder(); | 20 final sourceMap = new SourceMapBuilder(); |
| 21 | 21 |
| 22 Uri _sourceUri; | 22 /// The cache of URIs for paths. |
| 23 LineInfo _lineInfo; | 23 final _sourceUrlCache = <String, Object>{}; |
| 24 AstNode _topLevelNode; | 24 |
| 25 CompilationUnit unit; |
| 26 String sourcePath; |
| 27 AstNode _currentTopLevelDeclaration; |
| 25 | 28 |
| 26 @override | 29 @override |
| 27 void emit(String code) { | 30 void emit(String code) { |
| 28 var chars = code.runes.toList(); | 31 var chars = code.runes.toList(); |
| 29 var length = chars.length; | 32 var length = chars.length; |
| 30 for (int i = 0; i < length; i++) { | 33 for (int i = 0; i < length; i++) { |
| 31 var c = chars[i]; | 34 var c = chars[i]; |
| 32 if (c == _LF || (c == _CR && (i + 1 == length || chars[i + 1] != _LF))) { | 35 if (c == _LF || (c == _CR && (i + 1 == length || chars[i + 1] != _LF))) { |
| 33 // Return not followed by line-feed is treated as a new line. | 36 // Return not followed by line-feed is treated as a new line. |
| 34 _line++; | 37 _line++; |
| 35 _column = 0; | 38 _column = 0; |
| 36 } else { | 39 } else { |
| 37 _column++; | 40 _column++; |
| 38 } | 41 } |
| 39 } | 42 } |
| 40 super.emit(code); | 43 super.emit(code); |
| 41 } | 44 } |
| 42 | 45 |
| 43 void enterNode(JS.Node jsNode) { | 46 void enterNode(JS.Node jsNode) { |
| 44 AstNode node = jsNode.sourceInformation; | 47 AstNode node = jsNode.sourceInformation; |
| 45 if (node == null || node.offset == -1 || node.isSynthetic) return; | 48 if (node == null || node.offset == -1 || node.isSynthetic) return; |
| 46 if (_topLevelNode == null) { | 49 if (unit == null) { |
| 47 // This is a top-level declaration. Note: consecutive top-level | 50 // This is a top-level declaration. Note: consecutive top-level |
| 48 // declarations may come from different compilation units due to | 51 // declarations may come from different compilation units due to |
| 49 // parts. | 52 // parts. |
| 50 var unit = | 53 _currentTopLevelDeclaration = node; |
| 51 node.getAncestor((n) => n is CompilationUnit) as CompilationUnit; | 54 unit = node.getAncestor((n) => n is CompilationUnit); |
| 52 // This happens for synthetic nodes created by AstFactory. | 55 var source = resolutionMap.elementDeclaredByCompilationUnit(unit).source; |
| 53 // We don't need to mark positions for them because we'll have a position | 56 // Use the uri for dart: uris instead of the path of the source file |
| 54 // for the next thing down. | 57 // on disk as that results in much cleaner stack traces. |
| 55 if (unit == null) return; | 58 // Example: |
| 56 | 59 // source.uri = dart:core/object.dart |
| 57 _topLevelNode = node; | 60 // source.fullName = gen/patched_sdk/lib/core/object.dart |
| 58 var source = unit.element.source; | 61 sourcePath = |
| 59 _lineInfo = unit.lineInfo; | 62 source.isInSystemLibrary ? source.uri.toString() : source.fullName; |
| 60 _sourceUri = source.uri; | |
| 61 // TODO(jmesserly): this is for backwards compat, but it seems cleaner | |
| 62 // to preserve the package URI, as long as devtools can open the | |
| 63 // corresponding file. | |
| 64 if (_sourceUri.scheme == 'package') { | |
| 65 _sourceUri = Uri.parse(source.fullName); | |
| 66 } | |
| 67 } | 63 } |
| 68 | 64 // Skip MethodDeclarations - in the case of a one line function it finds the |
| 65 // declaration rather than the body and confuses devtools. |
| 66 if (node is MethodDeclaration) return; |
| 69 _mark(node.offset, _getIdentifier(node)); | 67 _mark(node.offset, _getIdentifier(node)); |
| 70 } | 68 } |
| 71 | 69 |
| 72 void exitNode(JS.Node jsNode) { | 70 void exitNode(JS.Node jsNode) { |
| 73 AstNode node = jsNode.sourceInformation; | 71 AstNode node = jsNode.sourceInformation; |
| 74 if (_topLevelNode == null || | 72 if (unit == null || node == null || node.offset == -1 || node.isSynthetic) { |
| 75 node == null || | |
| 76 node.offset == -1 || | |
| 77 node.isSynthetic) { | |
| 78 return; | 73 return; |
| 79 } | 74 } |
| 80 | 75 |
| 81 // TODO(jmesserly): in most cases marking the end will be unnecessary. | 76 // TODO(jmesserly): in many cases marking the end will be unnecessary. |
| 82 _mark(node.end); | 77 // Skip MethodDeclarations - in the case of a one line function it finds the |
| 78 // declaration rather than the body and confuses devtools. |
| 79 if (node is! MethodDeclaration) { |
| 80 _mark(node.end); |
| 81 } |
| 83 | 82 |
| 84 if (identical(node, _topLevelNode)) { | 83 if (identical(node, _currentTopLevelDeclaration)) { |
| 85 _sourceUri = null; | 84 unit = null; |
| 86 _lineInfo = null; | 85 sourcePath = null; |
| 87 _topLevelNode = null; | 86 _currentTopLevelDeclaration == null; |
| 88 } | 87 } |
| 89 } | 88 } |
| 90 | 89 |
| 91 // TODO(jmesserly): prefix identifiers too, if they map to a named element. | 90 // TODO(jmesserly): prefix identifiers too, if they map to a named element. |
| 92 String _getIdentifier(AstNode node) => | 91 String _getIdentifier(AstNode node) => |
| 93 node is SimpleIdentifier ? node.name : null; | 92 node is SimpleIdentifier ? node.name : null; |
| 94 | 93 |
| 95 void _mark(int offset, [String identifier]) { | 94 void _mark(int offset, [String identifier]) { |
| 96 var loc = _lineInfo.getLocation(offset); | 95 var loc = unit.lineInfo.getLocation(offset); |
| 97 // Chrome Devtools wants a mapping for the beginning of | 96 // Chrome Devtools wants a mapping for the beginning of |
| 98 // a line, so bump locations at the end of a line to the beginning of | 97 // a line, so bump locations at the end of a line to the beginning of |
| 99 // the next line. | 98 // the next line. |
| 100 var next = _lineInfo.getLocation(offset + 1); | 99 var next = unit.lineInfo.getLocation(offset + 1); |
| 101 if (next.lineNumber == loc.lineNumber + 1) { | 100 if (next.lineNumber == loc.lineNumber + 1) { |
| 102 loc = next; | 101 loc = next; |
| 103 } | 102 } |
| 103 var sourceUrl = _sourceUrlCache.putIfAbsent( |
| 104 sourcePath, |
| 105 () => |
| 106 sourcePath.startsWith('dart:') || sourcePath.startsWith('package:') |
| 107 ? sourcePath |
| 108 : new Uri.file(sourcePath)); |
| 104 sourceMap.addLocation( | 109 sourceMap.addLocation( |
| 105 new SourceLocation(offset, | 110 new SourceLocation(offset, |
| 106 sourceUrl: _sourceUri, | 111 sourceUrl: sourceUrl, |
| 107 line: loc.lineNumber - 1, | 112 line: loc.lineNumber - 1, |
| 108 column: loc.columnNumber - 1), | 113 column: loc.columnNumber - 1), |
| 109 new SourceLocation(buffer.length, line: _line, column: _column), | 114 new SourceLocation(buffer.length, line: _line, column: _column), |
| 110 identifier); | 115 identifier); |
| 111 } | 116 } |
| 112 } | 117 } |
| 113 | 118 |
| 114 const int _LF = 10; | 119 const int _LF = 10; |
| 115 const int _CR = 13; | 120 const int _CR = 13; |
| OLD | NEW |