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 '../common.dart'; | 10 import '../common.dart'; |
11 import '../compiler.dart' show | 11 import '../compiler.dart' show |
12 Compiler; | 12 Compiler; |
13 import '../dump_info.dart' show | 13 import '../dump_info.dart' show |
14 DumpInfoTask; | 14 DumpInfoTask; |
15 import '../io/code_output.dart' show | 15 import '../io/code_output.dart' show |
16 CodeBuffer, | 16 CodeBuffer, |
17 CodeOutput; | 17 CodeOutput; |
18 import '../js_emitter/js_emitter.dart' show | 18 import '../js_emitter/js_emitter.dart' show |
19 USE_LAZY_EMITTER; | 19 USE_LAZY_EMITTER; |
20 | 20 |
21 import 'js_source_mapping.dart'; | 21 import 'js_source_mapping.dart'; |
22 | 22 |
23 CodeBuffer prettyPrint(Node node, | 23 String prettyPrint( |
24 Compiler compiler, | 24 Node node, |
25 {DumpInfoTask monitor, | 25 Compiler compiler, |
26 bool allowVariableMinification: true, | 26 {bool allowVariableMinification: true, |
27 Renamer renamerForNames: | 27 Renamer renamerForNames: JavaScriptPrintingOptions.identityRenamer}) { |
28 JavaScriptPrintingOptions.identityRenamer}) { | 28 // TODO(johnniwinther): Do we need all the options here? |
| 29 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions( |
| 30 shouldCompressOutput: compiler.enableMinification, |
| 31 minifyLocalVariables: allowVariableMinification, |
| 32 preferSemicolonToNewlineInMinifiedOutput: USE_LAZY_EMITTER, |
| 33 renamerForNames: renamerForNames); |
| 34 SimpleJavaScriptPrintingContext context = |
| 35 new SimpleJavaScriptPrintingContext(); |
| 36 Printer printer = new Printer(options, context); |
| 37 printer.visit(node); |
| 38 return context.getText(); |
| 39 } |
| 40 |
| 41 CodeBuffer createCodeBuffer( |
| 42 Node node, |
| 43 Compiler compiler, |
| 44 {DumpInfoTask monitor, |
| 45 bool allowVariableMinification: true, |
| 46 Renamer renamerForNames: JavaScriptPrintingOptions.identityRenamer}) { |
29 JavaScriptSourceInformationStrategy sourceInformationFactory = | 47 JavaScriptSourceInformationStrategy sourceInformationFactory = |
30 compiler.backend.sourceInformationStrategy; | 48 compiler.backend.sourceInformationStrategy; |
31 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions( | 49 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions( |
32 shouldCompressOutput: compiler.enableMinification, | 50 shouldCompressOutput: compiler.enableMinification, |
33 minifyLocalVariables: allowVariableMinification, | 51 minifyLocalVariables: allowVariableMinification, |
34 preferSemicolonToNewlineInMinifiedOutput: USE_LAZY_EMITTER, | 52 preferSemicolonToNewlineInMinifiedOutput: USE_LAZY_EMITTER, |
35 renamerForNames: renamerForNames); | 53 renamerForNames: renamerForNames); |
36 CodeBuffer outBuffer = new CodeBuffer(); | 54 CodeBuffer outBuffer = new CodeBuffer(); |
37 SourceInformationProcessor sourceInformationProcessor = | 55 SourceInformationProcessor sourceInformationProcessor = |
38 sourceInformationFactory.createProcessor( | 56 sourceInformationFactory.createProcessor( |
39 new SourceLocationsMapper(outBuffer)); | 57 new SourceLocationsMapper(outBuffer)); |
40 Dart2JSJavaScriptPrintingContext context = | 58 Dart2JSJavaScriptPrintingContext context = |
41 new Dart2JSJavaScriptPrintingContext( | 59 new Dart2JSJavaScriptPrintingContext( |
42 compiler.reporter, monitor, outBuffer, sourceInformationProcessor); | 60 compiler.reporter, monitor, outBuffer, sourceInformationProcessor); |
43 Printer printer = new Printer(options, context); | 61 Printer printer = new Printer(options, context); |
44 printer.visit(node); | 62 printer.visit(node); |
45 sourceInformationProcessor.process(node); | 63 sourceInformationProcessor.process(node, outBuffer); |
46 return outBuffer; | 64 return outBuffer; |
47 } | 65 } |
48 | 66 |
49 class Dart2JSJavaScriptPrintingContext implements JavaScriptPrintingContext { | 67 class Dart2JSJavaScriptPrintingContext implements JavaScriptPrintingContext { |
50 final DiagnosticReporter reporter; | 68 final DiagnosticReporter reporter; |
51 final DumpInfoTask monitor; | 69 final DumpInfoTask monitor; |
52 final CodeBuffer outBuffer; | 70 final CodeBuffer outBuffer; |
53 final CodePositionListener codePositionListener; | 71 final CodePositionListener codePositionListener; |
54 | 72 |
55 Dart2JSJavaScriptPrintingContext( | 73 Dart2JSJavaScriptPrintingContext( |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 154 |
137 /// A [js.Literal] that represents the string result of unparsing [ast]. | 155 /// A [js.Literal] that represents the string result of unparsing [ast]. |
138 /// | 156 /// |
139 /// When its string [value] is requested, the node pretty-prints the given | 157 /// When its string [value] is requested, the node pretty-prints the given |
140 /// [ast] and, if [protectForEval] is true, wraps the resulting string in | 158 /// [ast] and, if [protectForEval] is true, wraps the resulting string in |
141 /// parenthesis. The result is also escaped. | 159 /// parenthesis. The result is also escaped. |
142 UnparsedNode(this.tree, this._compiler, this._protectForEval); | 160 UnparsedNode(this.tree, this._compiler, this._protectForEval); |
143 | 161 |
144 LiteralString get _literal { | 162 LiteralString get _literal { |
145 if (_cachedLiteral == null) { | 163 if (_cachedLiteral == null) { |
146 String text = prettyPrint(tree, _compiler).getText(); | 164 String text = prettyPrint(tree, _compiler); |
147 if (_protectForEval) { | 165 if (_protectForEval) { |
148 if (tree is Fun) text = '($text)'; | 166 if (tree is Fun) text = '($text)'; |
149 if (tree is LiteralExpression) { | 167 if (tree is LiteralExpression) { |
150 LiteralExpression literalExpression = tree; | 168 LiteralExpression literalExpression = tree; |
151 String template = literalExpression.template; | 169 String template = literalExpression.template; |
152 if (template.startsWith("function ") || | 170 if (template.startsWith("function ") || |
153 template.startsWith("{")) { | 171 template.startsWith("{")) { |
154 text = '($text)'; | 172 text = '($text)'; |
155 } | 173 } |
156 } | 174 } |
(...skipping 22 matching lines...) Expand all Loading... |
179 } | 197 } |
180 if (node is PropertyAccess) { | 198 if (node is PropertyAccess) { |
181 PropertyAccess access = node; | 199 PropertyAccess access = node; |
182 if (access.receiver is InterpolatedExpression) { | 200 if (access.receiver is InterpolatedExpression) { |
183 InterpolatedExpression hole = access.receiver; | 201 InterpolatedExpression hole = access.receiver; |
184 return hole.isPositional && hole.nameOrPosition == 0; | 202 return hole.isPositional && hole.nameOrPosition == 0; |
185 } | 203 } |
186 } | 204 } |
187 return false; | 205 return false; |
188 } | 206 } |
OLD | NEW |