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 | 8 |
9 import '../common.dart'; | 9 import '../common.dart'; |
10 import '../compiler.dart' show Compiler; | 10 import '../options.dart'; |
11 import '../dump_info.dart' show DumpInfoTask; | 11 import '../dump_info.dart' show DumpInfoTask; |
12 import '../io/code_output.dart' show CodeBuffer; | 12 import '../io/code_output.dart' show CodeBuffer; |
13 import '../js_emitter/js_emitter.dart' show USE_LAZY_EMITTER; | 13 import '../js_emitter/js_emitter.dart' show USE_LAZY_EMITTER; |
14 import 'js_source_mapping.dart'; | 14 import 'js_source_mapping.dart'; |
15 | 15 |
16 export 'package:js_ast/js_ast.dart'; | 16 export 'package:js_ast/js_ast.dart'; |
17 | 17 |
18 String prettyPrint(Node node, Compiler compiler, | 18 String prettyPrint(Node node, CompilerOptions compilerOptions, |
19 {bool allowVariableMinification: true, | 19 {bool allowVariableMinification: true, |
20 Renamer renamerForNames: JavaScriptPrintingOptions.identityRenamer}) { | 20 Renamer renamerForNames: JavaScriptPrintingOptions.identityRenamer}) { |
21 // TODO(johnniwinther): Do we need all the options here? | 21 // TODO(johnniwinther): Do we need all the options here? |
22 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions( | 22 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions( |
23 shouldCompressOutput: compiler.options.enableMinification, | 23 shouldCompressOutput: compilerOptions.enableMinification, |
24 minifyLocalVariables: allowVariableMinification, | 24 minifyLocalVariables: allowVariableMinification, |
25 preferSemicolonToNewlineInMinifiedOutput: USE_LAZY_EMITTER, | 25 preferSemicolonToNewlineInMinifiedOutput: USE_LAZY_EMITTER, |
26 renamerForNames: renamerForNames); | 26 renamerForNames: renamerForNames); |
27 SimpleJavaScriptPrintingContext context = | 27 SimpleJavaScriptPrintingContext context = |
28 new SimpleJavaScriptPrintingContext(); | 28 new SimpleJavaScriptPrintingContext(); |
29 Printer printer = new Printer(options, context); | 29 Printer printer = new Printer(options, context); |
30 printer.visit(node); | 30 printer.visit(node); |
31 return context.getText(); | 31 return context.getText(); |
32 } | 32 } |
33 | 33 |
34 CodeBuffer createCodeBuffer(Node node, Compiler compiler, | 34 CodeBuffer createCodeBuffer(Node node, CompilerOptions compilerOptions, |
| 35 JavaScriptSourceInformationStrategy sourceInformationStrategy, |
35 {DumpInfoTask monitor, | 36 {DumpInfoTask monitor, |
36 bool allowVariableMinification: true, | 37 bool allowVariableMinification: true, |
37 Renamer renamerForNames: JavaScriptPrintingOptions.identityRenamer}) { | 38 Renamer renamerForNames: JavaScriptPrintingOptions.identityRenamer}) { |
38 JavaScriptSourceInformationStrategy sourceInformationFactory = | |
39 compiler.backend.sourceInformationStrategy; | |
40 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions( | 39 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions( |
41 shouldCompressOutput: compiler.options.enableMinification, | 40 shouldCompressOutput: compilerOptions.enableMinification, |
42 minifyLocalVariables: allowVariableMinification, | 41 minifyLocalVariables: allowVariableMinification, |
43 preferSemicolonToNewlineInMinifiedOutput: USE_LAZY_EMITTER, | 42 preferSemicolonToNewlineInMinifiedOutput: USE_LAZY_EMITTER, |
44 renamerForNames: renamerForNames); | 43 renamerForNames: renamerForNames); |
45 CodeBuffer outBuffer = new CodeBuffer(); | 44 CodeBuffer outBuffer = new CodeBuffer(); |
46 SourceInformationProcessor sourceInformationProcessor = | 45 SourceInformationProcessor sourceInformationProcessor = |
47 sourceInformationFactory.createProcessor( | 46 sourceInformationStrategy.createProcessor( |
48 new SourceMapperProviderImpl(outBuffer), | 47 new SourceMapperProviderImpl(outBuffer), |
49 const SourceInformationReader()); | 48 const SourceInformationReader()); |
50 Dart2JSJavaScriptPrintingContext context = | 49 Dart2JSJavaScriptPrintingContext context = |
51 new Dart2JSJavaScriptPrintingContext( | 50 new Dart2JSJavaScriptPrintingContext( |
52 compiler.reporter, monitor, outBuffer, sourceInformationProcessor); | 51 monitor, outBuffer, sourceInformationProcessor); |
53 Printer printer = new Printer(options, context); | 52 Printer printer = new Printer(options, context); |
54 printer.visit(node); | 53 printer.visit(node); |
55 sourceInformationProcessor.process(node, outBuffer); | 54 sourceInformationProcessor.process(node, outBuffer); |
56 return outBuffer; | 55 return outBuffer; |
57 } | 56 } |
58 | 57 |
59 class Dart2JSJavaScriptPrintingContext implements JavaScriptPrintingContext { | 58 class Dart2JSJavaScriptPrintingContext implements JavaScriptPrintingContext { |
60 final DiagnosticReporter reporter; | |
61 final DumpInfoTask monitor; | 59 final DumpInfoTask monitor; |
62 final CodeBuffer outBuffer; | 60 final CodeBuffer outBuffer; |
63 final CodePositionListener codePositionListener; | 61 final CodePositionListener codePositionListener; |
64 | 62 |
65 Dart2JSJavaScriptPrintingContext( | 63 Dart2JSJavaScriptPrintingContext( |
66 this.reporter, this.monitor, this.outBuffer, this.codePositionListener); | 64 this.monitor, this.outBuffer, this.codePositionListener); |
67 | 65 |
68 @override | 66 @override |
69 void error(String message) { | 67 void error(String message) { |
70 reporter.internalError(NO_LOCATION_SPANNABLE, message); | 68 throw new SpannableAssertionFailure(NO_LOCATION_SPANNABLE, message); |
71 } | 69 } |
72 | 70 |
73 @override | 71 @override |
74 void emit(String string) { | 72 void emit(String string) { |
75 outBuffer.add(string); | 73 outBuffer.add(string); |
76 } | 74 } |
77 | 75 |
78 @override | 76 @override |
79 void enterNode(Node node, int startPosition) { | 77 void enterNode(Node node, int startPosition) { |
80 codePositionListener.onStartPosition(node, startPosition); | 78 codePositionListener.onStartPosition(node, startPosition); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 } | 124 } |
127 | 125 |
128 /// Represents the LiteralString resulting from unparsing [expression]. The | 126 /// Represents the LiteralString resulting from unparsing [expression]. The |
129 /// actual unparsing is done on demand when requesting the [value] of this | 127 /// actual unparsing is done on demand when requesting the [value] of this |
130 /// node. | 128 /// node. |
131 /// | 129 /// |
132 /// This is used when generated code needs to be represented as a string, | 130 /// This is used when generated code needs to be represented as a string, |
133 /// for example by the lazy emitter or when generating code generators. | 131 /// for example by the lazy emitter or when generating code generators. |
134 class UnparsedNode extends DeferredString implements AstContainer { | 132 class UnparsedNode extends DeferredString implements AstContainer { |
135 final Node tree; | 133 final Node tree; |
136 final Compiler _compiler; | 134 final CompilerOptions _compilerOptions; |
137 final bool _protectForEval; | 135 final bool _protectForEval; |
138 LiteralString _cachedLiteral; | 136 LiteralString _cachedLiteral; |
139 | 137 |
140 Iterable<Node> get containedNodes => [tree]; | 138 Iterable<Node> get containedNodes => [tree]; |
141 | 139 |
142 /// A [js.Literal] that represents the string result of unparsing [ast]. | 140 /// A [js.Literal] that represents the string result of unparsing [ast]. |
143 /// | 141 /// |
144 /// When its string [value] is requested, the node pretty-prints the given | 142 /// When its string [value] is requested, the node pretty-prints the given |
145 /// [ast] and, if [protectForEval] is true, wraps the resulting string in | 143 /// [ast] and, if [protectForEval] is true, wraps the resulting string in |
146 /// parenthesis. The result is also escaped. | 144 /// parenthesis. The result is also escaped. |
147 UnparsedNode(this.tree, this._compiler, this._protectForEval); | 145 UnparsedNode(this.tree, this._compilerOptions, this._protectForEval); |
148 | 146 |
149 LiteralString get _literal { | 147 LiteralString get _literal { |
150 if (_cachedLiteral == null) { | 148 if (_cachedLiteral == null) { |
151 String text = prettyPrint(tree, _compiler); | 149 String text = prettyPrint(tree, _compilerOptions); |
152 if (_protectForEval) { | 150 if (_protectForEval) { |
153 if (tree is Fun) text = '($text)'; | 151 if (tree is Fun) text = '($text)'; |
154 if (tree is LiteralExpression) { | 152 if (tree is LiteralExpression) { |
155 LiteralExpression literalExpression = tree; | 153 LiteralExpression literalExpression = tree; |
156 String template = literalExpression.template; | 154 String template = literalExpression.template; |
157 if (template.startsWith("function ") || template.startsWith("{")) { | 155 if (template.startsWith("function ") || template.startsWith("{")) { |
158 text = '($text)'; | 156 text = '($text)'; |
159 } | 157 } |
160 } | 158 } |
161 } | 159 } |
(...skipping 27 matching lines...) Expand all Loading... |
189 } | 187 } |
190 if (node is PropertyAccess) { | 188 if (node is PropertyAccess) { |
191 PropertyAccess access = node; | 189 PropertyAccess access = node; |
192 if (access.receiver is InterpolatedExpression) { | 190 if (access.receiver is InterpolatedExpression) { |
193 InterpolatedExpression hole = access.receiver; | 191 InterpolatedExpression hole = access.receiver; |
194 return hole.isPositional && hole.nameOrPosition == 0; | 192 return hole.isPositional && hole.nameOrPosition == 0; |
195 } | 193 } |
196 } | 194 } |
197 return false; | 195 return false; |
198 } | 196 } |
OLD | NEW |