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; | 7 import 'dart:io' show Directory, File; |
8 import 'package:analyzer/src/generated/ast.dart'; | 8 import 'package:analyzer/src/generated/ast.dart'; |
9 import 'package:path/path.dart' as path; | 9 import 'package:path/path.dart' as path; |
10 import 'package:source_maps/source_maps.dart' as srcmaps show Printer; | 10 import 'package:source_maps/source_maps.dart' as srcmaps show Printer; |
11 import 'package:source_maps/source_maps.dart' show SourceMapSpan; | 11 import 'package:source_maps/source_maps.dart' show SourceMapSpan; |
12 import 'package:source_span/source_span.dart' show SourceLocation; | 12 import 'package:source_span/source_span.dart' show SourceLocation; |
13 | 13 |
14 import 'package:dev_compiler/src/js/js_ast.dart' as JS; | 14 import 'package:dev_compiler/src/js/js_ast.dart' as JS; |
15 import 'package:dev_compiler/src/utils.dart' | 15 import 'package:dev_compiler/src/utils.dart' |
16 show computeHash, locationForOffset; | 16 show computeHash, locationForOffset; |
17 | 17 |
18 import 'js_names.dart' show JSNamer; | 18 import 'js_names.dart' show TemporaryNamer; |
19 | 19 |
20 String writeJsLibrary(JS.Program jsTree, String outputPath, | 20 String writeJsLibrary(JS.Program jsTree, String outputPath, |
21 {bool emitSourceMaps: false}) { | 21 {bool emitSourceMaps: false}) { |
22 new Directory(path.dirname(outputPath)).createSync(recursive: true); | 22 new Directory(path.dirname(outputPath)).createSync(recursive: true); |
23 if (emitSourceMaps) { | 23 if (emitSourceMaps) { |
24 var outFilename = path.basename(outputPath); | 24 var outFilename = path.basename(outputPath); |
25 var printer = new srcmaps.Printer(outFilename); | 25 var printer = new srcmaps.Printer(outFilename); |
26 writeNodeToContext(jsTree, | 26 writeNodeToContext(jsTree, |
27 new SourceMapPrintingContext(printer, path.dirname(outputPath))); | 27 new SourceMapPrintingContext(printer, path.dirname(outputPath))); |
28 printer.add('//# sourceMappingURL=$outFilename.map'); | 28 printer.add('//# sourceMappingURL=$outFilename.map'); |
29 // Write output file and source map | 29 // Write output file and source map |
30 var text = printer.text; | 30 var text = printer.text; |
31 new File(outputPath).writeAsStringSync(text); | 31 new File(outputPath).writeAsStringSync(text); |
32 new File('$outputPath.map').writeAsStringSync(printer.map); | 32 new File('$outputPath.map').writeAsStringSync(printer.map); |
33 return computeHash(text); | 33 return computeHash(text); |
34 } else { | 34 } else { |
35 var text = jsNodeToString(jsTree); | 35 var text = jsNodeToString(jsTree); |
36 new File(outputPath).writeAsStringSync(text); | 36 new File(outputPath).writeAsStringSync(text); |
37 return computeHash(text); | 37 return computeHash(text); |
38 } | 38 } |
39 } | 39 } |
40 | 40 |
41 void writeNodeToContext(JS.Node node, JS.JavaScriptPrintingContext context) { | 41 void writeNodeToContext(JS.Node node, JS.JavaScriptPrintingContext context) { |
42 var opts = new JS.JavaScriptPrintingOptions(allowKeywordsInProperties: true); | 42 var opts = new JS.JavaScriptPrintingOptions(allowKeywordsInProperties: true); |
43 node.accept(new JS.Printer(opts, context, localNamer: new JSNamer(node))); | 43 node.accept( |
| 44 new JS.Printer(opts, context, localNamer: new TemporaryNamer(node))); |
44 } | 45 } |
45 | 46 |
46 String jsNodeToString(JS.Node node) { | 47 String jsNodeToString(JS.Node node) { |
47 var context = new JS.SimpleJavaScriptPrintingContext(); | 48 var context = new JS.SimpleJavaScriptPrintingContext(); |
48 writeNodeToContext(node, context); | 49 writeNodeToContext(node, context); |
49 return context.getText(); | 50 return context.getText(); |
50 } | 51 } |
51 | 52 |
52 class SourceMapPrintingContext extends JS.JavaScriptPrintingContext { | 53 class SourceMapPrintingContext extends JS.JavaScriptPrintingContext { |
53 final srcmaps.Printer printer; | 54 final srcmaps.Printer printer; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 | 101 |
101 // TODO(jmesserly): in many cases marking the end will be unnecessary. | 102 // TODO(jmesserly): in many cases marking the end will be unnecessary. |
102 printer.mark(_location(node.end)); | 103 printer.mark(_location(node.end)); |
103 } | 104 } |
104 | 105 |
105 String _getIdentifier(AstNode node) { | 106 String _getIdentifier(AstNode node) { |
106 if (node is SimpleIdentifier) return node.name; | 107 if (node is SimpleIdentifier) return node.name; |
107 return null; | 108 return null; |
108 } | 109 } |
109 } | 110 } |
OLD | NEW |