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