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 js; | 5 library js; |
6 | 6 |
7 import 'package:js_ast/js_ast.dart'; | 7 import 'package:js_ast/js_ast.dart'; |
8 export 'package:js_ast/js_ast.dart'; | 8 export 'package:js_ast/js_ast.dart'; |
9 | 9 |
10 import '../io/code_output.dart' show CodeBuffer; | 10 import '../io/code_output.dart' show CodeOutput, CodeBuffer; |
11 import '../io/source_information.dart' show SourceInformation; | |
12 import '../js_emitter/js_emitter.dart' show USE_NEW_EMITTER; | 11 import '../js_emitter/js_emitter.dart' show USE_NEW_EMITTER; |
13 import '../dart2jslib.dart' as leg; | 12 import '../dart2jslib.dart' as leg; |
14 import '../util/util.dart' show NO_LOCATION_SPANNABLE; | 13 import '../util/util.dart' show NO_LOCATION_SPANNABLE, Indentation, Tagging; |
15 import '../dump_info.dart' show DumpInfoTask; | 14 import '../dump_info.dart' show DumpInfoTask; |
| 15 import 'js_source_mapping.dart'; |
16 | 16 |
17 CodeBuffer prettyPrint(Node node, leg.Compiler compiler, | 17 CodeBuffer prettyPrint(Node node, |
| 18 leg.Compiler compiler, |
18 {DumpInfoTask monitor, | 19 {DumpInfoTask monitor, |
19 bool allowVariableMinification: true}) { | 20 bool allowVariableMinification: true}) { |
| 21 JavaScriptSourceInformationStrategy sourceInformationFactory = |
| 22 compiler.backend.sourceInformationStrategy; |
20 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions( | 23 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions( |
21 shouldCompressOutput: compiler.enableMinification, | 24 shouldCompressOutput: compiler.enableMinification, |
22 minifyLocalVariables: allowVariableMinification, | 25 minifyLocalVariables: allowVariableMinification, |
23 preferSemicolonToNewlineInMinifiedOutput: USE_NEW_EMITTER); | 26 preferSemicolonToNewlineInMinifiedOutput: USE_NEW_EMITTER); |
| 27 CodeBuffer outBuffer = new CodeBuffer(); |
| 28 SourceInformationProcessor sourceInformationProcessor = |
| 29 sourceInformationFactory.createProcessor( |
| 30 new SourceLocationsMapper(outBuffer)); |
24 Dart2JSJavaScriptPrintingContext context = | 31 Dart2JSJavaScriptPrintingContext context = |
25 new Dart2JSJavaScriptPrintingContext(compiler, monitor); | 32 new Dart2JSJavaScriptPrintingContext( |
| 33 compiler, monitor, outBuffer, sourceInformationProcessor); |
26 Printer printer = new Printer(options, context); | 34 Printer printer = new Printer(options, context); |
27 printer.visit(node); | 35 printer.visit(node); |
28 return context.outBuffer; | 36 sourceInformationProcessor.process(node); |
| 37 return outBuffer; |
29 } | 38 } |
30 | 39 |
31 class Dart2JSJavaScriptPrintingContext implements JavaScriptPrintingContext { | 40 class Dart2JSJavaScriptPrintingContext implements JavaScriptPrintingContext { |
32 final leg.Compiler compiler; | 41 final leg.Compiler compiler; |
33 final DumpInfoTask monitor; | 42 final DumpInfoTask monitor; |
34 final CodeBuffer outBuffer = new CodeBuffer(); | 43 final CodeBuffer outBuffer; |
35 Node rootNode; | 44 final CodePositionListener codePositionListener; |
36 | 45 |
37 Dart2JSJavaScriptPrintingContext(leg.Compiler this.compiler, | 46 Dart2JSJavaScriptPrintingContext( |
38 DumpInfoTask this.monitor); | 47 this.compiler, |
| 48 this.monitor, |
| 49 this.outBuffer, |
| 50 this.codePositionListener); |
39 | 51 |
40 @override | 52 @override |
41 void error(String message) { | 53 void error(String message) { |
42 compiler.internalError(NO_LOCATION_SPANNABLE, message); | 54 compiler.internalError(NO_LOCATION_SPANNABLE, message); |
43 } | 55 } |
44 | 56 |
45 @override | 57 @override |
46 void emit(String string) { | 58 void emit(String string) { |
47 outBuffer.add(string); | 59 outBuffer.add(string); |
48 } | 60 } |
49 | 61 |
50 @override | 62 @override |
51 void enterNode(Node node, int startPosition) { | 63 void enterNode(Node, int startPosition) {} |
52 SourceInformation sourceInformation = node.sourceInformation; | |
53 if (sourceInformation != null) { | |
54 if (rootNode == null) { | |
55 rootNode = node; | |
56 } | |
57 if (sourceInformation.startPosition != null) { | |
58 outBuffer.addSourceLocation( | |
59 startPosition, sourceInformation.startPosition); | |
60 } | |
61 } | |
62 } | |
63 | 64 |
| 65 @override |
64 void exitNode(Node node, | 66 void exitNode(Node node, |
65 int startPosition, | 67 int startPosition, |
66 int endPosition, | 68 int endPosition, |
67 int closingPosition) { | 69 int closingPosition) { |
68 SourceInformation sourceInformation = node.sourceInformation; | |
69 if (sourceInformation != null) { | |
70 if (closingPosition != null && | |
71 sourceInformation.closingPosition != null) { | |
72 outBuffer.addSourceLocation( | |
73 closingPosition, sourceInformation.closingPosition); | |
74 } | |
75 if (sourceInformation.endPosition != null) { | |
76 outBuffer.addSourceLocation(endPosition, sourceInformation.endPosition); | |
77 } | |
78 if (rootNode == node) { | |
79 outBuffer.addSourceLocation(endPosition, null); | |
80 rootNode = null; | |
81 } | |
82 } | |
83 if (monitor != null) { | 70 if (monitor != null) { |
84 monitor.recordAstSize(node, endPosition - startPosition); | 71 monitor.recordAstSize(node, endPosition - startPosition); |
85 } | 72 } |
| 73 codePositionListener.onPositions( |
| 74 node, startPosition, endPosition, closingPosition); |
86 } | 75 } |
87 } | 76 } |
OLD | NEW |