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 library dev_compiler.src.codegen.js_printer; | 5 library dev_compiler.src.codegen.js_printer; |
6 | 6 |
7 import 'dart:io' show Directory, File, Platform, Process; | 7 import 'dart:io' show Directory, File, Platform, Process; |
8 | 8 |
9 import 'package:analyzer/src/generated/ast.dart'; | 9 import 'package:analyzer/src/generated/ast.dart'; |
10 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 | 63 |
64 CompilationUnit unit; | 64 CompilationUnit unit; |
65 Uri uri; | 65 Uri uri; |
66 | 66 |
67 SourceMapPrintingContext(this.printer, this.outputDir); | 67 SourceMapPrintingContext(this.printer, this.outputDir); |
68 | 68 |
69 void emit(String string) { | 69 void emit(String string) { |
70 printer.add(string); | 70 printer.add(string); |
71 } | 71 } |
72 | 72 |
| 73 AstNode _currentTopLevelDeclaration; |
| 74 |
73 void enterNode(JS.Node jsNode) { | 75 void enterNode(JS.Node jsNode) { |
74 AstNode node = jsNode.sourceInformation; | 76 AstNode node = jsNode.sourceInformation; |
75 if (node is CompilationUnit) { | 77 if (node == null || node.offset == -1) return; |
76 unit = node; | 78 if (unit == null) { |
| 79 // This is a top-level declaration. Note: consecutive top-level |
| 80 // declarations may come from different compilation units due to |
| 81 // parts. |
| 82 _currentTopLevelDeclaration = node; |
| 83 unit = node.getAncestor((n) => n is CompilationUnit); |
77 uri = _makeRelativeUri(unit.element.source.uri); | 84 uri = _makeRelativeUri(unit.element.source.uri); |
78 return; | |
79 } | 85 } |
80 if (unit == null || node == null || node.offset == -1) return; | |
81 | 86 |
| 87 assert(unit != null); |
82 var loc = _location(node.offset); | 88 var loc = _location(node.offset); |
83 var name = _getIdentifier(node); | 89 var name = _getIdentifier(node); |
84 if (name != null) { | 90 if (name != null) { |
85 // TODO(jmesserly): mark only uses the beginning of the span, but | 91 // TODO(jmesserly): mark only uses the beginning of the span, but |
86 // we're required to pass this as a valid span. | 92 // we're required to pass this as a valid span. |
87 var end = _location(node.end); | 93 var end = _location(node.end); |
88 printer.mark(new SourceMapSpan(loc, end, name, isIdentifier: true)); | 94 printer.mark(new SourceMapSpan(loc, end, name, isIdentifier: true)); |
89 } else { | 95 } else { |
90 printer.mark(loc); | 96 printer.mark(loc); |
91 } | 97 } |
92 } | 98 } |
93 | 99 |
94 SourceLocation _location(int offset) => | 100 SourceLocation _location(int offset) => |
95 locationForOffset(unit.lineInfo, uri, offset); | 101 locationForOffset(unit.lineInfo, uri, offset); |
96 | 102 |
97 Uri _makeRelativeUri(Uri src) { | 103 Uri _makeRelativeUri(Uri src) { |
98 return new Uri(path: path.relative(src.path, from: outputDir)); | 104 return new Uri(path: path.relative(src.path, from: outputDir)); |
99 } | 105 } |
100 | 106 |
101 void exitNode(JS.Node jsNode) { | 107 void exitNode(JS.Node jsNode) { |
102 AstNode node = jsNode.sourceInformation; | 108 AstNode node = jsNode.sourceInformation; |
103 if (node is CompilationUnit) { | |
104 unit = null; | |
105 uri = null; | |
106 return; | |
107 } | |
108 if (unit == null || node == null || node.offset == -1) return; | 109 if (unit == null || node == null || node.offset == -1) return; |
109 | 110 |
110 // TODO(jmesserly): in many cases marking the end will be unnecessary. | 111 // TODO(jmesserly): in many cases marking the end will be unnecessary. |
111 printer.mark(_location(node.end)); | 112 printer.mark(_location(node.end)); |
| 113 |
| 114 if (_currentTopLevelDeclaration == node) { |
| 115 unit = null; |
| 116 uri = null; |
| 117 _currentTopLevelDeclaration == null; |
| 118 return; |
| 119 } |
112 } | 120 } |
113 | 121 |
114 String _getIdentifier(AstNode node) { | 122 String _getIdentifier(AstNode node) { |
115 if (node is SimpleIdentifier) return node.name; | 123 if (node is SimpleIdentifier) return node.name; |
116 return null; | 124 return null; |
117 } | 125 } |
118 } | 126 } |
OLD | NEW |