OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 |
| 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. |
| 5 |
| 6 import 'package:analyzer/dart/ast/ast.dart'; |
| 7 import 'package:source_maps/source_maps.dart' hide Printer; |
| 8 import 'package:source_span/source_span.dart' show SourceLocation; |
| 9 |
| 10 import '../js_ast/js_ast.dart' as JS; |
| 11 |
| 12 class SourceMapPrintingContext extends JS.SimpleJavaScriptPrintingContext { |
| 13 /// Current line in the buffer; |
| 14 int _line = 0; |
| 15 |
| 16 /// Current column in the buffer. |
| 17 int _column = 0; |
| 18 |
| 19 /// The source_maps builder we write JavaScript code to. |
| 20 final sourceMap = new SourceMapBuilder(); |
| 21 |
| 22 CompilationUnit unit; |
| 23 String sourcePath; |
| 24 AstNode _currentTopLevelDeclaration; |
| 25 |
| 26 @override |
| 27 void emit(String code) { |
| 28 var chars = code.runes.toList(); |
| 29 var length = chars.length; |
| 30 for (int i = 0; i < length; i++) { |
| 31 var c = chars[i]; |
| 32 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. |
| 34 _line++; |
| 35 _column = 0; |
| 36 } else { |
| 37 _column++; |
| 38 } |
| 39 } |
| 40 super.emit(code); |
| 41 } |
| 42 |
| 43 void enterNode(JS.Node jsNode) { |
| 44 AstNode node = jsNode.sourceInformation; |
| 45 if (node == null || node.offset == -1) return; |
| 46 if (unit == null) { |
| 47 // This is a top-level declaration. Note: consecutive top-level |
| 48 // declarations may come from different compilation units due to |
| 49 // parts. |
| 50 _currentTopLevelDeclaration = node; |
| 51 unit = node.getAncestor((n) => n is CompilationUnit); |
| 52 sourcePath = unit.element.source.fullName; |
| 53 } |
| 54 |
| 55 _mark(node.offset, _getIdentifier(node)); |
| 56 } |
| 57 |
| 58 void exitNode(JS.Node jsNode) { |
| 59 AstNode node = jsNode.sourceInformation; |
| 60 if (unit == null || node == null || node.offset == -1) return; |
| 61 |
| 62 // TODO(jmesserly): in many cases marking the end will be unnecessary. |
| 63 _mark(node.end); |
| 64 |
| 65 if (identical(node, _currentTopLevelDeclaration)) { |
| 66 unit = null; |
| 67 sourcePath = null; |
| 68 _currentTopLevelDeclaration == null; |
| 69 } |
| 70 } |
| 71 |
| 72 // TODO(jmesserly): prefix identifiers too, if they map to a named element. |
| 73 String _getIdentifier(AstNode node) => |
| 74 node is SimpleIdentifier ? node.name : null; |
| 75 |
| 76 void _mark(int offset, [String identifier]) { |
| 77 var loc = unit.lineInfo.getLocation(offset); |
| 78 sourceMap.addLocation( |
| 79 new SourceLocation(offset, |
| 80 sourceUrl: sourcePath, |
| 81 line: loc.lineNumber - 1, |
| 82 column: loc.columnNumber - 1), |
| 83 new SourceLocation(buffer.length, line: _line, column: _column), |
| 84 identifier); |
| 85 } |
| 86 } |
| 87 |
| 88 const int _LF = 10; |
| 89 const int _CR = 13; |
OLD | NEW |